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 typeMap["LoadContent"] = &CommandLineFactory::CreateObject<LoadContentCommand>;
50 } else {
51 typeMap["Power"] = &CommandLineFactory::CreateObject<PowerCommand>;
52 typeMap["Volume"] = &CommandLineFactory::CreateObject<VolumeCommand>;
53 typeMap["Barometer"] = &CommandLineFactory::CreateObject<BarometerCommand>;
54 typeMap["Location"] = &CommandLineFactory::CreateObject<LocationCommand>;
55 typeMap["KeepScreenOnState"] = &CommandLineFactory::CreateObject<KeepScreenOnStateCommand>;
56 typeMap["WearingState"] = &CommandLineFactory::CreateObject<WearingStateCommand>;
57 typeMap["BrightnessMode"] = &CommandLineFactory::CreateObject<BrightnessModeCommand>;
58 typeMap["ChargeMode"] = &CommandLineFactory::CreateObject<ChargeModeCommand>;
59 typeMap["Brightness"] = &CommandLineFactory::CreateObject<BrightnessCommand>;
60 typeMap["HeartRate"] = &CommandLineFactory::CreateObject<HeartRateCommand>;
61 typeMap["StepCount"] = &CommandLineFactory::CreateObject<StepCountCommand>;
62 typeMap["DistributedCommunications"] = &CommandLineFactory::CreateObject<DistributedCommunicationsCommand>;
63 typeMap["CrownRotate"] = &CommandLineFactory::CreateObject<MouseWheelCommand>;
64 }
65 typeMap["MousePress"] = &CommandLineFactory::CreateObject<TouchPressCommand>;
66 typeMap["MouseRelease"] = &CommandLineFactory::CreateObject<TouchReleaseCommand>;
67 typeMap["MouseMove"] = &CommandLineFactory::CreateObject<TouchMoveCommand>;
68 typeMap["Language"] = &CommandLineFactory::CreateObject<LanguageCommand>;
69 typeMap["SupportedLanguages"] = &CommandLineFactory::CreateObject<SupportedLanguagesCommand>;
70 typeMap["exit"] = &CommandLineFactory::CreateObject<ExitCommand>;
71 typeMap["Resolution"] = &CommandLineFactory::CreateObject<ResolutionCommand>;
72 typeMap["DeviceType"] = &CommandLineFactory::CreateObject<DeviceTypeCommand>;
73 typeMap["PointEvent"] = &CommandLineFactory::CreateObject<PointEventCommand>;
74 }
75
CreateCommandLine(string command,CommandLine::CommandType type,Json::Value val,const LocalSocket & socket)76 unique_ptr<CommandLine> CommandLineFactory::CreateCommandLine(string command,
77 CommandLine::CommandType type,
78 Json::Value val,
79 const LocalSocket& socket)
80 {
81 if (typeMap.find(command) == typeMap.end()) {
82 Json::Value commandResult;
83 commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
84 commandResult["command"] = command;
85 commandResult["result"] = "Unsupported command";
86 socket << commandResult.toStyledString();
87 ELOG("Unsupported command");
88 TraceTool::GetInstance().HandleTrace("Mismatched SDK version");
89 return nullptr;
90 }
91 if (typeMap[command] == nullptr) {
92 ELOG("CommandLineFactory::CreateCommandLine:typeMap is null");
93 }
94 ILOG("Create Command: %s", command.c_str());
95 unique_ptr<CommandLine> cmdLine = typeMap[command](type, val, socket);
96 if (cmdLine == nullptr) {
97 ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null");
98 }
99 cmdLine->SetCommandName(command);
100 return cmdLine;
101 }
102
103 template <typename T>
CreateObject(CommandLine::CommandType type,const Json::Value & args,const LocalSocket & socket)104 unique_ptr<CommandLine> CommandLineFactory::CreateObject(CommandLine::CommandType type,
105 const Json::Value& args, const LocalSocket& socket)
106 {
107 return make_unique<T>(type, args, socket);
108 }
109