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