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 typeMap["FoldStatus"] = &CommandLineFactory::CreateObject<FoldStatusCommand>;
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
CreateCommandLine(string command,CommandLine::CommandType type,Json::Value val,const LocalSocket & socket)77 unique_ptr<CommandLine> CommandLineFactory::CreateCommandLine(string command,
78 CommandLine::CommandType type,
79 Json::Value val,
80 const LocalSocket& socket)
81 {
82 if (typeMap.find(command) == typeMap.end()) {
83 Json::Value commandResult;
84 commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
85 commandResult["command"] = command;
86 commandResult["result"] = "Unsupported command";
87 socket << commandResult.toStyledString();
88 ELOG("Unsupported command");
89 TraceTool::GetInstance().HandleTrace("Mismatched SDK version");
90 return nullptr;
91 }
92 if (typeMap[command] == nullptr) {
93 ELOG("CommandLineFactory::CreateCommandLine:typeMap is null");
94 }
95 ILOG("Create Command: %s", command.c_str());
96 unique_ptr<CommandLine> cmdLine = typeMap[command](type, val, socket);
97 if (cmdLine == nullptr) {
98 ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null");
99 }
100 cmdLine->SetCommandName(command);
101 return cmdLine;
102 }
103
104 template <typename T>
CreateObject(CommandLine::CommandType type,const Json::Value & args,const LocalSocket & socket)105 unique_ptr<CommandLine> CommandLineFactory::CreateObject(CommandLine::CommandType type,
106 const Json::Value& args, const LocalSocket& socket)
107 {
108 return make_unique<T>(type, args, socket);
109 }
110