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
24 CommandLineFactory::CommandTypeMap CommandLineFactory::typeMap = CommandLineFactory::CommandTypeMap();
CommandLineFactory()25 CommandLineFactory::CommandLineFactory() {}
26
27 using namespace std;
28
InitCommandMap()29 void CommandLineFactory::InitCommandMap()
30 {
31 CommandParser& cmdParser = CommandParser::GetInstance();
32 string deviceType = cmdParser.GetDeviceType();
33 bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
34 if (!isLiteDevice) {
35 typeMap["BackClicked"] = &CommandLineFactory::CreateObject<BackClickedCommand>;
36 typeMap["inspector"] = &CommandLineFactory::CreateObject<InspectorJSONTree>;
37 typeMap["inspectorDefault"] = &CommandLineFactory::CreateObject<InspectorDefault>;
38 typeMap["ColorMode"] = &CommandLineFactory::CreateObject<ColorModeCommand>;
39 typeMap["Orientation"] = &CommandLineFactory::CreateObject<OrientationCommand>;
40 typeMap["ResolutionSwitch"] = &CommandLineFactory::CreateObject<ResolutionSwitchCommand>;
41 typeMap["CurrentRouter"] = &CommandLineFactory::CreateObject<CurrentRouterCommand>;
42 typeMap["ReloadRuntimePage"] = &CommandLineFactory::CreateObject<ReloadRuntimePageCommand>;
43 typeMap["FontSelect"] = &CommandLineFactory::CreateObject<FontSelectCommand>;
44 typeMap["MemoryRefresh"] = &CommandLineFactory::CreateObject<MemoryRefreshCommand>;
45 typeMap["LoadDocument"] = &CommandLineFactory::CreateObject<LoadDocumentCommand>;
46 typeMap["FastPreviewMsg"] = &CommandLineFactory::CreateObject<FastPreviewMsgCommand>;
47 typeMap["DropFrame"] = &CommandLineFactory::CreateObject<DropFrameCommand>;
48 typeMap["KeyPress"] = &CommandLineFactory::CreateObject<KeyPressCommand>;
49 } else {
50 typeMap["Power"] = &CommandLineFactory::CreateObject<PowerCommand>;
51 typeMap["Volume"] = &CommandLineFactory::CreateObject<VolumeCommand>;
52 typeMap["Barometer"] = &CommandLineFactory::CreateObject<BarometerCommand>;
53 typeMap["Location"] = &CommandLineFactory::CreateObject<LocationCommand>;
54 typeMap["KeepScreenOnState"] = &CommandLineFactory::CreateObject<KeepScreenOnStateCommand>;
55 typeMap["WearingState"] = &CommandLineFactory::CreateObject<WearingStateCommand>;
56 typeMap["BrightnessMode"] = &CommandLineFactory::CreateObject<BrightnessModeCommand>;
57 typeMap["ChargeMode"] = &CommandLineFactory::CreateObject<ChargeModeCommand>;
58 typeMap["Brightness"] = &CommandLineFactory::CreateObject<BrightnessCommand>;
59 typeMap["HeartRate"] = &CommandLineFactory::CreateObject<HeartRateCommand>;
60 typeMap["StepCount"] = &CommandLineFactory::CreateObject<StepCountCommand>;
61 typeMap["DistributedCommunications"] = &CommandLineFactory::CreateObject<DistributedCommunicationsCommand>;
62 typeMap["CrownRotate"] = &CommandLineFactory::CreateObject<MouseWheelCommand>;
63 }
64 typeMap["MousePress"] = &CommandLineFactory::CreateObject<TouchPressCommand>;
65 typeMap["MouseRelease"] = &CommandLineFactory::CreateObject<TouchReleaseCommand>;
66 typeMap["MouseMove"] = &CommandLineFactory::CreateObject<TouchMoveCommand>;
67 typeMap["Language"] = &CommandLineFactory::CreateObject<LanguageCommand>;
68 typeMap["SupportedLanguages"] = &CommandLineFactory::CreateObject<SupportedLanguagesCommand>;
69 typeMap["exit"] = &CommandLineFactory::CreateObject<ExitCommand>;
70 typeMap["Resolution"] = &CommandLineFactory::CreateObject<ResolutionCommand>;
71 typeMap["DeviceType"] = &CommandLineFactory::CreateObject<DeviceTypeCommand>;
72 }
73
CreateCommandLine(string command,CommandLine::CommandType type,Json::Value val,const LocalSocket & socket)74 unique_ptr<CommandLine> CommandLineFactory::CreateCommandLine(string command,
75 CommandLine::CommandType type,
76 Json::Value val,
77 const LocalSocket& socket)
78 {
79 if (typeMap.find(command) == typeMap.end()) {
80 Json::Value commandResult;
81 commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
82 commandResult["command"] = command;
83 commandResult["result"] = "Unsupported command";
84 socket << commandResult.toStyledString();
85 ELOG("Unsupported command");
86 TraceTool::GetInstance().HandleTrace("Mismatched SDK version");
87 return nullptr;
88 }
89 if (typeMap[command] == nullptr) {
90 ELOG("CommandLineFactory::CreateCommandLine:typeMap is null");
91 }
92 ILOG("Create Command: %s", command.c_str());
93 unique_ptr<CommandLine> cmdLine = typeMap[command](type, val, socket);
94 if (cmdLine == nullptr) {
95 ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null");
96 }
97 cmdLine->SetCommandName(command);
98 return cmdLine;
99 }
100
101 template <typename T>
CreateObject(CommandLine::CommandType type,const Json::Value & args,const LocalSocket & socket)102 unique_ptr<CommandLine> CommandLineFactory::CreateObject(CommandLine::CommandType type,
103 const Json::Value& args, const LocalSocket& socket)
104 {
105 return make_unique<T>(type, args, socket);
106 }
107