1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "CommandLineFactory.h" 17 18#include "CommandLineInterface.h" 19#include "CommandParser.h" 20#include "JsApp.h" 21#include "PreviewerEngineLog.h" 22#include "TraceTool.h" 23 24CommandLineFactory::CommandTypeMap CommandLineFactory::typeMap = CommandLineFactory::CommandTypeMap(); 25CommandLineFactory::CommandLineFactory() {} 26 27void CommandLineFactory::InitCommandMap() 28{ 29 CommandParser& cmdParser = CommandParser::GetInstance(); 30 std::string deviceType = cmdParser.GetDeviceType(); 31 bool isLiteDevice = JsApp::IsLiteDevice(deviceType); 32 if (!isLiteDevice) { 33 typeMap["BackClicked"] = &CommandLineFactory::CreateObject<BackClickedCommand>; 34 typeMap["inspector"] = &CommandLineFactory::CreateObject<InspectorJSONTree>; 35 typeMap["inspectorDefault"] = &CommandLineFactory::CreateObject<InspectorDefault>; 36 typeMap["ColorMode"] = &CommandLineFactory::CreateObject<ColorModeCommand>; 37 typeMap["Orientation"] = &CommandLineFactory::CreateObject<OrientationCommand>; 38 typeMap["ResolutionSwitch"] = &CommandLineFactory::CreateObject<ResolutionSwitchCommand>; 39 typeMap["CurrentRouter"] = &CommandLineFactory::CreateObject<CurrentRouterCommand>; 40 typeMap["ReloadRuntimePage"] = &CommandLineFactory::CreateObject<ReloadRuntimePageCommand>; 41 typeMap["FontSelect"] = &CommandLineFactory::CreateObject<FontSelectCommand>; 42 typeMap["MemoryRefresh"] = &CommandLineFactory::CreateObject<MemoryRefreshCommand>; 43 typeMap["LoadDocument"] = &CommandLineFactory::CreateObject<LoadDocumentCommand>; 44 typeMap["FastPreviewMsg"] = &CommandLineFactory::CreateObject<FastPreviewMsgCommand>; 45 typeMap["DropFrame"] = &CommandLineFactory::CreateObject<DropFrameCommand>; 46 typeMap["KeyPress"] = &CommandLineFactory::CreateObject<KeyPressCommand>; 47 typeMap["LoadContent"] = &CommandLineFactory::CreateObject<LoadContentCommand>; 48 typeMap["FoldStatus"] = &CommandLineFactory::CreateObject<FoldStatusCommand>; 49 typeMap["AvoidArea"] = &CommandLineFactory::CreateObject<AvoidAreaCommand>; 50 typeMap["AvoidAreaChanged"] = &CommandLineFactory::CreateObject<AvoidAreaChangedCommand>; 51 } else { 52 typeMap["Power"] = &CommandLineFactory::CreateObject<PowerCommand>; 53 typeMap["Volume"] = &CommandLineFactory::CreateObject<VolumeCommand>; 54 typeMap["Barometer"] = &CommandLineFactory::CreateObject<BarometerCommand>; 55 typeMap["Location"] = &CommandLineFactory::CreateObject<LocationCommand>; 56 typeMap["KeepScreenOnState"] = &CommandLineFactory::CreateObject<KeepScreenOnStateCommand>; 57 typeMap["WearingState"] = &CommandLineFactory::CreateObject<WearingStateCommand>; 58 typeMap["BrightnessMode"] = &CommandLineFactory::CreateObject<BrightnessModeCommand>; 59 typeMap["ChargeMode"] = &CommandLineFactory::CreateObject<ChargeModeCommand>; 60 typeMap["Brightness"] = &CommandLineFactory::CreateObject<BrightnessCommand>; 61 typeMap["HeartRate"] = &CommandLineFactory::CreateObject<HeartRateCommand>; 62 typeMap["StepCount"] = &CommandLineFactory::CreateObject<StepCountCommand>; 63 typeMap["DistributedCommunications"] = &CommandLineFactory::CreateObject<DistributedCommunicationsCommand>; 64 typeMap["CrownRotate"] = &CommandLineFactory::CreateObject<MouseWheelCommand>; 65 } 66 typeMap["MousePress"] = &CommandLineFactory::CreateObject<TouchPressCommand>; 67 typeMap["MouseRelease"] = &CommandLineFactory::CreateObject<TouchReleaseCommand>; 68 typeMap["MouseMove"] = &CommandLineFactory::CreateObject<TouchMoveCommand>; 69 typeMap["Language"] = &CommandLineFactory::CreateObject<LanguageCommand>; 70 typeMap["SupportedLanguages"] = &CommandLineFactory::CreateObject<SupportedLanguagesCommand>; 71 typeMap["exit"] = &CommandLineFactory::CreateObject<ExitCommand>; 72 typeMap["Resolution"] = &CommandLineFactory::CreateObject<ResolutionCommand>; 73 typeMap["DeviceType"] = &CommandLineFactory::CreateObject<DeviceTypeCommand>; 74 typeMap["PointEvent"] = &CommandLineFactory::CreateObject<PointEventCommand>; 75} 76 77std::unique_ptr<CommandLine> CommandLineFactory::CreateCommandLine(std::string command, 78 CommandLine::CommandType type, const Json2::Value& val, const LocalSocket& socket) 79{ 80 if (typeMap.find(command) == typeMap.end()) { 81 Json2::Value commandResult = JsonReader::CreateObject(); 82 commandResult.Add("version", CommandLineInterface::COMMAND_VERSION.c_str()); 83 commandResult.Add("command", command.c_str()); 84 commandResult.Add("result", "Unsupported command"); 85 socket << commandResult.ToStyledString(); 86 ELOG("Unsupported command"); 87 TraceTool::GetInstance().HandleTrace("Mismatched SDK version"); 88 return nullptr; 89 } 90 if (typeMap[command] == nullptr) { 91 ELOG("CommandLineFactory::CreateCommandLine:typeMap is null"); 92 } 93 ILOG("Create Command: %s", command.c_str()); 94 std::unique_ptr<CommandLine> cmdLine = typeMap[command](type, val, socket); 95 if (cmdLine == nullptr) { 96 ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null"); 97 } 98 cmdLine->SetCommandName(command); 99 return cmdLine; 100} 101 102template <typename T> 103std::unique_ptr<CommandLine> CommandLineFactory::CreateObject(CommandLine::CommandType type, 104 const Json2::Value& args, const LocalSocket& socket) 105{ 106 return std::make_unique<T>(type, args, socket); 107} 108