17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "CommandLineFactory.h"
177c804472Sopenharmony_ci
187c804472Sopenharmony_ci#include "CommandLineInterface.h"
197c804472Sopenharmony_ci#include "CommandParser.h"
207c804472Sopenharmony_ci#include "JsApp.h"
217c804472Sopenharmony_ci#include "PreviewerEngineLog.h"
227c804472Sopenharmony_ci#include "TraceTool.h"
237c804472Sopenharmony_ci
247c804472Sopenharmony_ciCommandLineFactory::CommandTypeMap CommandLineFactory::typeMap = CommandLineFactory::CommandTypeMap();
257c804472Sopenharmony_ciCommandLineFactory::CommandLineFactory() {}
267c804472Sopenharmony_ci
277c804472Sopenharmony_civoid CommandLineFactory::InitCommandMap()
287c804472Sopenharmony_ci{
297c804472Sopenharmony_ci    CommandParser& cmdParser = CommandParser::GetInstance();
307c804472Sopenharmony_ci    std::string deviceType = cmdParser.GetDeviceType();
317c804472Sopenharmony_ci    bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
327c804472Sopenharmony_ci    if (!isLiteDevice) {
337c804472Sopenharmony_ci        typeMap["BackClicked"] = &CommandLineFactory::CreateObject<BackClickedCommand>;
347c804472Sopenharmony_ci        typeMap["inspector"] = &CommandLineFactory::CreateObject<InspectorJSONTree>;
357c804472Sopenharmony_ci        typeMap["inspectorDefault"] = &CommandLineFactory::CreateObject<InspectorDefault>;
367c804472Sopenharmony_ci        typeMap["ColorMode"] = &CommandLineFactory::CreateObject<ColorModeCommand>;
377c804472Sopenharmony_ci        typeMap["Orientation"] = &CommandLineFactory::CreateObject<OrientationCommand>;
387c804472Sopenharmony_ci        typeMap["ResolutionSwitch"] = &CommandLineFactory::CreateObject<ResolutionSwitchCommand>;
397c804472Sopenharmony_ci        typeMap["CurrentRouter"] = &CommandLineFactory::CreateObject<CurrentRouterCommand>;
407c804472Sopenharmony_ci        typeMap["ReloadRuntimePage"] = &CommandLineFactory::CreateObject<ReloadRuntimePageCommand>;
417c804472Sopenharmony_ci        typeMap["FontSelect"] = &CommandLineFactory::CreateObject<FontSelectCommand>;
427c804472Sopenharmony_ci        typeMap["MemoryRefresh"] = &CommandLineFactory::CreateObject<MemoryRefreshCommand>;
437c804472Sopenharmony_ci        typeMap["LoadDocument"] = &CommandLineFactory::CreateObject<LoadDocumentCommand>;
447c804472Sopenharmony_ci        typeMap["FastPreviewMsg"] = &CommandLineFactory::CreateObject<FastPreviewMsgCommand>;
457c804472Sopenharmony_ci        typeMap["DropFrame"] = &CommandLineFactory::CreateObject<DropFrameCommand>;
467c804472Sopenharmony_ci        typeMap["KeyPress"] = &CommandLineFactory::CreateObject<KeyPressCommand>;
477c804472Sopenharmony_ci        typeMap["LoadContent"] = &CommandLineFactory::CreateObject<LoadContentCommand>;
487c804472Sopenharmony_ci        typeMap["FoldStatus"] = &CommandLineFactory::CreateObject<FoldStatusCommand>;
497c804472Sopenharmony_ci        typeMap["AvoidArea"] = &CommandLineFactory::CreateObject<AvoidAreaCommand>;
507c804472Sopenharmony_ci        typeMap["AvoidAreaChanged"] = &CommandLineFactory::CreateObject<AvoidAreaChangedCommand>;
517c804472Sopenharmony_ci    } else {
527c804472Sopenharmony_ci        typeMap["Power"] = &CommandLineFactory::CreateObject<PowerCommand>;
537c804472Sopenharmony_ci        typeMap["Volume"] = &CommandLineFactory::CreateObject<VolumeCommand>;
547c804472Sopenharmony_ci        typeMap["Barometer"] = &CommandLineFactory::CreateObject<BarometerCommand>;
557c804472Sopenharmony_ci        typeMap["Location"] = &CommandLineFactory::CreateObject<LocationCommand>;
567c804472Sopenharmony_ci        typeMap["KeepScreenOnState"] = &CommandLineFactory::CreateObject<KeepScreenOnStateCommand>;
577c804472Sopenharmony_ci        typeMap["WearingState"] = &CommandLineFactory::CreateObject<WearingStateCommand>;
587c804472Sopenharmony_ci        typeMap["BrightnessMode"] = &CommandLineFactory::CreateObject<BrightnessModeCommand>;
597c804472Sopenharmony_ci        typeMap["ChargeMode"] = &CommandLineFactory::CreateObject<ChargeModeCommand>;
607c804472Sopenharmony_ci        typeMap["Brightness"] = &CommandLineFactory::CreateObject<BrightnessCommand>;
617c804472Sopenharmony_ci        typeMap["HeartRate"] = &CommandLineFactory::CreateObject<HeartRateCommand>;
627c804472Sopenharmony_ci        typeMap["StepCount"] = &CommandLineFactory::CreateObject<StepCountCommand>;
637c804472Sopenharmony_ci        typeMap["DistributedCommunications"] = &CommandLineFactory::CreateObject<DistributedCommunicationsCommand>;
647c804472Sopenharmony_ci        typeMap["CrownRotate"] = &CommandLineFactory::CreateObject<MouseWheelCommand>;
657c804472Sopenharmony_ci    }
667c804472Sopenharmony_ci    typeMap["MousePress"] = &CommandLineFactory::CreateObject<TouchPressCommand>;
677c804472Sopenharmony_ci    typeMap["MouseRelease"] = &CommandLineFactory::CreateObject<TouchReleaseCommand>;
687c804472Sopenharmony_ci    typeMap["MouseMove"] = &CommandLineFactory::CreateObject<TouchMoveCommand>;
697c804472Sopenharmony_ci    typeMap["Language"] = &CommandLineFactory::CreateObject<LanguageCommand>;
707c804472Sopenharmony_ci    typeMap["SupportedLanguages"] = &CommandLineFactory::CreateObject<SupportedLanguagesCommand>;
717c804472Sopenharmony_ci    typeMap["exit"] = &CommandLineFactory::CreateObject<ExitCommand>;
727c804472Sopenharmony_ci    typeMap["Resolution"] = &CommandLineFactory::CreateObject<ResolutionCommand>;
737c804472Sopenharmony_ci    typeMap["DeviceType"] = &CommandLineFactory::CreateObject<DeviceTypeCommand>;
747c804472Sopenharmony_ci    typeMap["PointEvent"] = &CommandLineFactory::CreateObject<PointEventCommand>;
757c804472Sopenharmony_ci}
767c804472Sopenharmony_ci
777c804472Sopenharmony_cistd::unique_ptr<CommandLine> CommandLineFactory::CreateCommandLine(std::string command,
787c804472Sopenharmony_ci    CommandLine::CommandType type, const Json2::Value& val, const LocalSocket& socket)
797c804472Sopenharmony_ci{
807c804472Sopenharmony_ci    if (typeMap.find(command) == typeMap.end()) {
817c804472Sopenharmony_ci        Json2::Value commandResult = JsonReader::CreateObject();
827c804472Sopenharmony_ci        commandResult.Add("version", CommandLineInterface::COMMAND_VERSION.c_str());
837c804472Sopenharmony_ci        commandResult.Add("command", command.c_str());
847c804472Sopenharmony_ci        commandResult.Add("result", "Unsupported command");
857c804472Sopenharmony_ci        socket << commandResult.ToStyledString();
867c804472Sopenharmony_ci        ELOG("Unsupported command");
877c804472Sopenharmony_ci        TraceTool::GetInstance().HandleTrace("Mismatched SDK version");
887c804472Sopenharmony_ci        return nullptr;
897c804472Sopenharmony_ci    }
907c804472Sopenharmony_ci    if (typeMap[command] == nullptr) {
917c804472Sopenharmony_ci        ELOG("CommandLineFactory::CreateCommandLine:typeMap is null");
927c804472Sopenharmony_ci    }
937c804472Sopenharmony_ci    ILOG("Create Command: %s", command.c_str());
947c804472Sopenharmony_ci    std::unique_ptr<CommandLine> cmdLine = typeMap[command](type, val, socket);
957c804472Sopenharmony_ci    if (cmdLine == nullptr) {
967c804472Sopenharmony_ci        ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null");
977c804472Sopenharmony_ci    }
987c804472Sopenharmony_ci    cmdLine->SetCommandName(command);
997c804472Sopenharmony_ci    return cmdLine;
1007c804472Sopenharmony_ci}
1017c804472Sopenharmony_ci
1027c804472Sopenharmony_citemplate <typename T>
1037c804472Sopenharmony_cistd::unique_ptr<CommandLine> CommandLineFactory::CreateObject(CommandLine::CommandType type,
1047c804472Sopenharmony_ci    const Json2::Value& args, const LocalSocket& socket)
1057c804472Sopenharmony_ci{
1067c804472Sopenharmony_ci    return std::make_unique<T>(type, args, socket);
1077c804472Sopenharmony_ci}
108