/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "CommandLineFactory.h" #include "CommandLineInterface.h" #include "CommandParser.h" #include "JsApp.h" #include "PreviewerEngineLog.h" #include "TraceTool.h" CommandLineFactory::CommandTypeMap CommandLineFactory::typeMap = CommandLineFactory::CommandTypeMap(); CommandLineFactory::CommandLineFactory() {} using namespace std; void CommandLineFactory::InitCommandMap() { CommandParser& cmdParser = CommandParser::GetInstance(); string deviceType = cmdParser.GetDeviceType(); bool isLiteDevice = JsApp::IsLiteDevice(deviceType); if (!isLiteDevice) { typeMap["BackClicked"] = &CommandLineFactory::CreateObject; typeMap["inspector"] = &CommandLineFactory::CreateObject; typeMap["inspectorDefault"] = &CommandLineFactory::CreateObject; typeMap["ColorMode"] = &CommandLineFactory::CreateObject; typeMap["Orientation"] = &CommandLineFactory::CreateObject; typeMap["ResolutionSwitch"] = &CommandLineFactory::CreateObject; typeMap["CurrentRouter"] = &CommandLineFactory::CreateObject; typeMap["ReloadRuntimePage"] = &CommandLineFactory::CreateObject; typeMap["FontSelect"] = &CommandLineFactory::CreateObject; typeMap["MemoryRefresh"] = &CommandLineFactory::CreateObject; typeMap["LoadDocument"] = &CommandLineFactory::CreateObject; typeMap["FastPreviewMsg"] = &CommandLineFactory::CreateObject; typeMap["DropFrame"] = &CommandLineFactory::CreateObject; typeMap["KeyPress"] = &CommandLineFactory::CreateObject; } else { typeMap["Power"] = &CommandLineFactory::CreateObject; typeMap["Volume"] = &CommandLineFactory::CreateObject; typeMap["Barometer"] = &CommandLineFactory::CreateObject; typeMap["Location"] = &CommandLineFactory::CreateObject; typeMap["KeepScreenOnState"] = &CommandLineFactory::CreateObject; typeMap["WearingState"] = &CommandLineFactory::CreateObject; typeMap["BrightnessMode"] = &CommandLineFactory::CreateObject; typeMap["ChargeMode"] = &CommandLineFactory::CreateObject; typeMap["Brightness"] = &CommandLineFactory::CreateObject; typeMap["HeartRate"] = &CommandLineFactory::CreateObject; typeMap["StepCount"] = &CommandLineFactory::CreateObject; typeMap["DistributedCommunications"] = &CommandLineFactory::CreateObject; typeMap["CrownRotate"] = &CommandLineFactory::CreateObject; } typeMap["MousePress"] = &CommandLineFactory::CreateObject; typeMap["MouseRelease"] = &CommandLineFactory::CreateObject; typeMap["MouseMove"] = &CommandLineFactory::CreateObject; typeMap["Language"] = &CommandLineFactory::CreateObject; typeMap["SupportedLanguages"] = &CommandLineFactory::CreateObject; typeMap["exit"] = &CommandLineFactory::CreateObject; typeMap["Resolution"] = &CommandLineFactory::CreateObject; typeMap["DeviceType"] = &CommandLineFactory::CreateObject; } unique_ptr CommandLineFactory::CreateCommandLine(string command, CommandLine::CommandType type, Json::Value val, const LocalSocket& socket) { if (typeMap.find(command) == typeMap.end()) { Json::Value commandResult; commandResult["version"] = CommandLineInterface::COMMAND_VERSION; commandResult["command"] = command; commandResult["result"] = "Unsupported command"; socket << commandResult.toStyledString(); ELOG("Unsupported command"); TraceTool::GetInstance().HandleTrace("Mismatched SDK version"); return nullptr; } if (typeMap[command] == nullptr) { ELOG("CommandLineFactory::CreateCommandLine:typeMap is null"); } ILOG("Create Command: %s", command.c_str()); unique_ptr cmdLine = typeMap[command](type, val, socket); if (cmdLine == nullptr) { ELOG("CommandLineFactory::CreateCommandLine:cmdLine is null"); } cmdLine->SetCommandName(command); return cmdLine; } template unique_ptr CommandLineFactory::CreateObject(CommandLine::CommandType type, const Json::Value& args, const LocalSocket& socket) { return make_unique(type, args, socket); }