xref: /ide/tools/previewer/cli/CommandLine.cpp (revision 7c804472)
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 "CommandLine.h"
17
18#include <algorithm>
19#include <regex>
20#include <sstream>
21
22#include "CommandLineInterface.h"
23#include "CommandParser.h"
24#include "Interrupter.h"
25#include "JsApp.h"
26#include "JsAppImpl.h"
27#include "JsonReader.h"
28#include "LanguageManagerImpl.h"
29#include "ModelConfig.h"
30#include "ModelManager.h"
31#include "MouseInputImpl.h"
32#include "MouseWheelImpl.h"
33#include "KeyInputImpl.h"
34#include "PreviewerEngineLog.h"
35#include "SharedData.h"
36#include "VirtualMessageImpl.h"
37#include "VirtualScreenImpl.h"
38
39CommandLine::CommandLine(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
40    : args(arg), cliSocket(socket), type(commandType), commandName("")
41{
42}
43
44CommandLine::~CommandLine()
45{
46}
47
48void CommandLine::CheckAndRun()
49{
50    if (!IsArgValid()) {
51        ELOG("CheckAndRun: invalid command params");
52        SetCommandResult("result", JsonReader::CreateBool(false));
53        SendResult();
54        return;
55    }
56    Run();
57    SendResult();
58}
59
60void CommandLine::SendResult()
61{
62    if (commandResult.IsNull() || !commandResult.IsValid()) {
63        return;
64    }
65    cliSocket << commandResult.ToStyledString();
66    ELOG("SendResult commandResult: %s", commandResult.ToStyledString().c_str());
67    commandResult.Clear();
68}
69
70void CommandLine::RunAndSendResultToManager()
71{
72    Run();
73    SendResultToManager();
74}
75
76void CommandLine::SendResultToManager()
77{
78    if (commandResultToManager.IsNull() || !commandResultToManager.IsValid()) {
79        return;
80    }
81    cliSocket << commandResultToManager.ToStyledString();
82    commandResultToManager.Clear();
83}
84
85bool CommandLine::IsArgValid() const
86{
87    if (type == CommandType::GET) {
88        return IsGetArgValid();
89    }
90    if (type == CommandType::SET) {
91        return IsSetArgValid();
92    }
93    if (type == CommandType::ACTION) {
94        return IsActionArgValid();
95    }
96    return true;
97}
98
99uint8_t CommandLine::ToUint8(std::string str) const
100{
101    int value = atoi(str.data());
102    if (value > UINT8_MAX) {
103        ELOG("CommandLine::ToUint8 value is overflow, value: %d", value);
104    }
105    return static_cast<uint8_t>(value);
106}
107
108void CommandLine::SetCommandName(std::string command)
109{
110    this->commandName = command;
111}
112
113void CommandLine::SetCommandResult(const std::string& resultType, const Json2::Value& resultContent)
114{
115    this->commandResult.Add("version", CommandLineInterface::COMMAND_VERSION.c_str());
116    this->commandResult.Add("command", this->commandName.c_str());
117    this->commandResult.Add(resultType.c_str(), resultContent);
118}
119
120void CommandLine::SetResultToManager(const std::string& resultType,
121                                     const Json2::Value& resultContent,
122                                     const std::string& messageType)
123{
124    this->commandResultToManager.Add("MessageType", messageType.c_str());
125    this->commandResultToManager.Add(resultType.c_str(), resultContent);
126}
127
128void CommandLine::Run()
129{
130    if (type == CommandType::GET) {
131        RunGet();
132    } else if (type == CommandType::SET) {
133        RunSet();
134    } else if (type == CommandType::ACTION) {
135        RunAction();
136    }
137}
138
139bool CommandLine::IsBoolType(std::string arg) const
140{
141    std::regex tofrx("^(true)|(false)$");
142    if (regex_match(arg, tofrx)) {
143        return true;
144    }
145    return false;
146}
147
148bool CommandLine::IsIntType(std::string arg) const
149{
150    std::regex isInt("^\\d+$");
151    return regex_match(arg, isInt);
152}
153
154bool CommandLine::IsOneDigitFloatType(std::string arg, bool allowNegativeNumber) const
155{
156    if (allowNegativeNumber) {
157        std::regex isFloat("^-?\\d+(\\.\\d+)?$");
158        return regex_match(arg, isFloat);
159    } else {
160        std::regex isFloat("^\\d(\\.\\d+)?$");
161        return regex_match(arg, isFloat);
162    }
163}
164
165void TouchAndMouseCommand::SetEventParams(EventParams& params)
166{
167    if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
168        return;
169    }
170    MouseInputImpl::GetInstance().SetMousePosition(params.x, params.y);
171    MouseInputImpl::GetInstance().SetMouseStatus(params.type);
172    MouseInputImpl::GetInstance().SetMouseButton(params.button);
173    MouseInputImpl::GetInstance().SetMouseAction(params.action);
174    MouseInputImpl::GetInstance().SetSourceType(params.sourceType);
175    MouseInputImpl::GetInstance().SetSourceTool(params.sourceTool);
176    MouseInputImpl::GetInstance().SetPressedBtns(params.pressedBtnsVec);
177    MouseInputImpl::GetInstance().SetAxisValues(params.axisVec);
178    MouseInputImpl::GetInstance().DispatchOsTouchEvent();
179    std::stringstream ss;
180    ss << "[";
181    for (double val : params.axisVec) {
182        ss << " " << val << " ";
183    }
184    ss << "]" << std::endl;
185    ILOG("%s(%f,%f,%d,%d,%d,%d,%d,%d,%d,%s)", params.name.c_str(), params.x, params.y, params.type, params.button,
186        params.action, params.sourceType, params.sourceTool, params.pressedBtnsVec.size(), params.axisVec.size(),
187        ss.str().c_str());
188}
189
190bool TouchPressCommand::IsActionArgValid() const
191{
192    if (args.IsNull() || !args.IsMember("x") || !args.IsMember("y") ||
193        !args["x"].IsInt() || !args["y"].IsInt()) {
194        return false;
195    }
196    int32_t pointX = args["x"].AsInt();
197    int32_t pointY = args["y"].AsInt();
198    if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
199        ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
200        return false;
201    }
202    if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
203        ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
204        return false;
205    }
206    return true;
207}
208
209TouchPressCommand::TouchPressCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
210    : CommandLine(commandType, arg, socket)
211{
212}
213
214void TouchPressCommand::RunAction()
215{
216    int type = 0;
217    EventParams param;
218    param.x = args["x"].AsDouble();
219    param.y = args["y"].AsDouble();
220    param.type = type;
221    param.name = "TouchPress";
222    param.button = MouseInputImpl::GetInstance().defaultButton;
223    param.action = MouseInputImpl::GetInstance().defaultAction;
224    param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
225    param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
226    SetEventParams(param);
227    SetCommandResult("result", JsonReader::CreateBool(true));
228}
229
230bool MouseWheelCommand::IsActionArgValid() const
231{
232    if (args.IsNull() || !args.IsMember("rotate") || !args["rotate"].IsDouble()) {
233        return false;
234    }
235    return true;
236}
237
238MouseWheelCommand::MouseWheelCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
239    : CommandLine(commandType, arg, socket)
240{
241}
242
243void MouseWheelCommand::RunAction()
244{
245    if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
246        return;
247    }
248    MouseWheelImpl::GetInstance().SetRotate(args["rotate"].AsDouble());
249    SetCommandResult("result", JsonReader::CreateBool(true));
250    ILOG("CrownRotate (%f)", args["rotate"].AsDouble());
251}
252
253bool TouchReleaseCommand::IsActionArgValid() const
254{
255    if (args.IsNull() || !args.IsMember("x") || !args.IsMember("y") ||
256        !args["x"].IsInt() || !args["y"].IsInt()) {
257        return false;
258    }
259    int32_t pX = args["x"].AsInt();
260    int32_t pY = args["y"].AsInt();
261    if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
262        ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
263        return false;
264    }
265    if (pX < 0 || pX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
266        ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
267        return false;
268    }
269    return true;
270}
271
272TouchReleaseCommand::TouchReleaseCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
273    : CommandLine(commandType, arg, socket)
274{
275}
276
277void TouchReleaseCommand::RunAction()
278{
279    int type = 1;
280    EventParams param;
281    param.x = args["x"].AsDouble();
282    param.y = args["y"].AsDouble();
283    param.type = type;
284    param.name = "TouchRelease";
285    param.button = MouseInputImpl::GetInstance().defaultButton;
286    param.action = MouseInputImpl::GetInstance().defaultAction;
287    param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
288    param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
289    SetEventParams(param);
290    SetCommandResult("result", JsonReader::CreateBool(true));
291}
292
293bool TouchMoveCommand::IsActionArgValid() const
294{
295    if (args.IsNull() || !args.IsMember("x") || !args.IsMember("y") ||
296        !args["x"].IsInt() || !args["y"].IsInt()) {
297        return false;
298    }
299    int32_t pX = args["x"].AsInt();
300    int32_t pY = args["y"].AsInt();
301    if (pX < 0 || pX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
302        ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
303        return false;
304    }
305    if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
306        ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
307        return false;
308    }
309    return true;
310}
311
312TouchMoveCommand::TouchMoveCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
313    : CommandLine(commandType, arg, socket)
314{
315}
316
317void TouchMoveCommand::RunAction()
318{
319    int type = 2;
320    EventParams param;
321    param.x = args["x"].AsDouble();
322    param.y = args["y"].AsDouble();
323    param.type = type;
324    param.name = "TouchMove";
325    param.button = MouseInputImpl::GetInstance().defaultButton;
326    param.action = MouseInputImpl::GetInstance().defaultAction;
327    param.sourceType = MouseInputImpl::GetInstance().defaultSourceType;
328    param.sourceTool = MouseInputImpl::GetInstance().defaultSourceTool;
329    SetEventParams(param);
330    SetCommandResult("result", JsonReader::CreateBool(true));
331}
332
333PowerCommand::PowerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
334    : CommandLine(commandType, arg, socket)
335{
336}
337
338bool PowerCommand::IsSetArgValid() const
339{
340    if (args.IsNull() || !args.IsMember("Power") || !args["Power"].IsDouble()) {
341        ELOG("Invalid number of arguments!");
342        return false;
343    }
344    double val = args["Power"].AsDouble();
345    if (!SharedData<double>::IsValid(SharedDataType::BATTERY_LEVEL, val)) {
346        ELOG("PowerCommand invalid value: %f", val);
347        return false;
348    }
349    return true;
350}
351
352void PowerCommand::RunGet()
353{
354    double power = SharedData<double>::GetData(SharedDataType::BATTERY_LEVEL);
355    Json2::Value resultContent = JsonReader::CreateObject();
356    resultContent.Add("Power", power);
357    SetCommandResult("result", resultContent);
358    ILOG("Get power run finished");
359}
360
361void PowerCommand::RunSet()
362{
363    double val = args["Power"].AsDouble();
364    SharedData<double>::SetData(SharedDataType::BATTERY_LEVEL, val);
365    SetCommandResult("result", JsonReader::CreateBool(true));
366    ILOG("Set power run finished, the value is: %f", val);
367}
368
369VolumeCommand::VolumeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
370    : CommandLine(commandType, arg, socket)
371{
372}
373
374bool VolumeCommand::IsSetArgValid() const
375{
376    return true;
377}
378
379void VolumeCommand::RunGet()
380{
381    SetCommandResult("result", JsonReader::CreateString("Command offline"));
382    ILOG("Command offline");
383}
384
385void VolumeCommand::RunSet()
386{
387    SetCommandResult("result", JsonReader::CreateString("Command offline"));
388    ILOG("Command offline");
389}
390
391BarometerCommand::BarometerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
392    : CommandLine(commandType, arg, socket)
393{
394}
395
396bool BarometerCommand::IsSetArgValid() const
397{
398    if (args.IsNull() || !args.IsMember("Barometer") || !args["Barometer"].IsUInt()) {
399        ELOG("Invalid number of arguments!");
400        return false;
401    }
402    uint32_t val = args["Barometer"].AsUInt();
403    if (!SharedData<uint32_t>::IsValid(SharedDataType::PRESSURE_VALUE, val)) {
404        ELOG("Barometer invalid value: %d", val);
405        return false;
406    }
407    return true;
408}
409
410void BarometerCommand::RunGet()
411{
412    int barometer = static_cast<int>(SharedData<uint32_t>::GetData(SharedDataType::PRESSURE_VALUE));
413    Json2::Value resultContent = JsonReader::CreateObject();
414    resultContent.Add("Barometer", barometer);
415    SetCommandResult("result", resultContent);
416    ILOG("Get barometer run finished");
417}
418
419void BarometerCommand::RunSet()
420{
421    uint32_t val = args["Barometer"].AsUInt();
422    SharedData<uint32_t>::SetData(SharedDataType::PRESSURE_VALUE, val);
423    SetCommandResult("result", JsonReader::CreateBool(true));
424    ILOG("Set barometer run finished, the value is: %d", val);
425}
426
427ResolutionSwitchCommand::ResolutionSwitchCommand(CommandType commandType,
428                                                 const Json2::Value& arg,
429                                                 const LocalSocket& socket)
430    : CommandLine(commandType, arg, socket)
431{
432}
433
434bool ResolutionSwitchCommand::IsSetArgValid() const
435{
436    if (args.IsNull() || !args.IsMember("originWidth") || !args.IsMember("originHeight") || !args.IsMember("width") ||
437        !args.IsMember("height") || !args.IsMember("screenDensity")) {
438        ELOG("Invalid param of arguments!");
439        return false;
440    }
441    if (!args["originWidth"].IsInt() || !args["originHeight"].IsInt() ||
442        !args["screenDensity"].IsInt() || !args["width"].IsInt() || !args["height"].IsInt()) {
443        ELOG("Invalid number of arguments!");
444        return false;
445    }
446    if (!IsIntValValid()) {
447        return false;
448    }
449    if (args.IsMember("reason")) {
450        if (!args["reason"].IsString()) {
451            return false;
452        }
453        std::string reason = args["reason"].AsString();
454        if (reason != "rotation" && reason != "resize" && reason != "undefined") {
455            ELOG("Invalid value of reason!");
456            return false;
457        }
458    }
459    return true;
460}
461
462bool ResolutionSwitchCommand::IsIntValValid() const
463{
464    if (args["originWidth"].AsInt() < minWidth || args["originWidth"].AsInt() > maxWidth ||
465        args["originHeight"].AsInt() < minWidth || args["originHeight"].AsInt() > maxWidth ||
466        args["width"].AsInt() < minWidth || args["width"].AsInt() > maxWidth ||
467        args["height"].AsInt() < minWidth || args["height"].AsInt() > maxWidth) {
468        ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
469        return false;
470    }
471    if (args["screenDensity"].AsInt() < minDpi || args["screenDensity"].AsInt() > maxDpi) {
472        ELOG("screenDensity is out of range %d-%d", minDpi, maxDpi);
473        return false;
474    }
475    return true;
476}
477
478void ResolutionSwitchCommand::RunSet()
479{
480    int32_t originWidth = args["originWidth"].AsInt();
481    int32_t originHeight = args["originHeight"].AsInt();
482    int32_t width = args["width"].AsInt();
483    int32_t height = args["height"].AsInt();
484    int32_t screenDensity = args["screenDensity"].AsInt();
485    std::string reason = "undefined";
486    if (args.IsMember("reason")) {
487        reason = args["reason"].AsString();
488    }
489    ResolutionParam param(originWidth, originHeight, width, height);
490    JsAppImpl::GetInstance().ResolutionChanged(param, screenDensity, reason);
491    SetCommandResult("result", JsonReader::CreateBool(true));
492    ILOG("ResolutionSwitch run finished.");
493}
494
495OrientationCommand::OrientationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
496    : CommandLine(commandType, arg, socket)
497{
498}
499
500bool OrientationCommand::IsSetArgValid() const
501{
502    if (args.IsNull() || !args.IsMember("Orientation") || !args["Orientation"].IsString()) {
503        ELOG("Invalid number of arguments!");
504        return false;
505    }
506    if (args["Orientation"].AsString() != "portrait" && args["Orientation"].AsString() != "landscape") {
507        ELOG("Orientation just support [portrait,landscape].");
508        return false;
509    }
510    return true;
511}
512
513void OrientationCommand::RunSet()
514{
515    std::string commandOrientation = args["Orientation"].AsString();
516    std::string currentOrientation = JsAppImpl::GetInstance().GetOrientation();
517    if (commandOrientation != currentOrientation) {
518        JsAppImpl::GetInstance().OrientationChanged(commandOrientation);
519    }
520    SetCommandResult("result", JsonReader::CreateBool(true));
521    ILOG("Set Orientation run finished, Orientation is: %s", args["Orientation"].AsString().c_str());
522}
523
524ColorModeCommand::ColorModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
525    : CommandLine(commandType, arg, socket)
526{
527}
528
529bool ColorModeCommand::IsSetArgValid() const
530{
531    if (args.IsNull() || !args.IsMember("ColorMode") || !args["ColorMode"].IsString()) {
532        ELOG("Invalid number of arguments!");
533        return false;
534    }
535
536    if (args["ColorMode"].AsString() != "light" && args["ColorMode"].AsString() != "dark") {
537        ELOG("ColorMode just support [light,dark]");
538        return false;
539    }
540    return true;
541}
542
543void ColorModeCommand::RunSet()
544{
545    std::string commandColorMode = args["ColorMode"].AsString();
546    std::string currentColorMode = JsAppImpl::GetInstance().GetColorMode();
547    if (commandColorMode != currentColorMode) {
548        JsAppImpl::GetInstance().SetArgsColorMode(args["ColorMode"].AsString());
549        JsAppImpl::GetInstance().ColorModeChanged(commandColorMode);
550    }
551    SetCommandResult("result", JsonReader::CreateBool(true));
552    ILOG("Set ColorMode run finished, ColorMode is: %s", args["ColorMode"].AsString().c_str());
553}
554
555FontSelectCommand::FontSelectCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
556    : CommandLine(commandType, arg, socket)
557{
558}
559
560bool FontSelectCommand::IsSetArgValid() const
561{
562    if (args.IsNull() || !args.IsMember("FontSelect") || !args["FontSelect"].IsBool()) {
563        ELOG("Invalid number of arguments!");
564        return false;
565    }
566    return true;
567}
568
569void FontSelectCommand::RunSet()
570{
571    SetCommandResult("result", JsonReader::CreateBool(true));
572    ILOG("FontSelect finished, currentSelect is: %s", args["FontSelect"].AsBool() ? "true" : "false");
573}
574
575MemoryRefreshCommand::MemoryRefreshCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
576    : CommandLine(commandType, arg, socket)
577{
578}
579
580bool MemoryRefreshCommand::IsSetArgValid() const
581{
582    if (args.IsNull()) {
583        ELOG("Invalid MemoryRefresh of arguments!");
584        return false;
585    }
586    return true;
587}
588
589void MemoryRefreshCommand::RunSet()
590{
591    ILOG("MemoryRefreshCommand begin.");
592    bool ret = JsAppImpl::GetInstance().MemoryRefresh(args.ToStyledString());
593    SetCommandResult("result", JsonReader::CreateBool(ret));
594    ILOG("MemoryRefresh finished.");
595}
596
597LoadDocumentCommand::LoadDocumentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
598    : CommandLine(commandType, arg, socket)
599{
600}
601
602bool LoadDocumentCommand::IsSetArgValid() const
603{
604    if (args.IsNull() || !args.IsMember("url") || !args.IsMember("className") || !args.IsMember("previewParam") ||
605        !args["url"].IsString() || !args["className"].IsString() || !args["previewParam"].IsObject()) {
606        return false;
607    }
608    Json2::Value previewParam = args["previewParam"];
609    if (!previewParam["width"].IsInt() || !previewParam["height"].IsInt() || !previewParam["dpi"].IsInt() ||
610        !previewParam["locale"].IsString() || !previewParam["colorMode"].IsString() ||
611        !previewParam["orientation"].IsString() || !previewParam["deviceType"].IsString()) {
612        return false;
613    }
614    if (!IsIntValValid(previewParam) || !IsStrValVailid(previewParam)) {
615        return false;
616    }
617    return true;
618}
619
620bool LoadDocumentCommand::IsIntValValid(const Json2::Value& previewParam) const
621{
622    int width = previewParam["width"].AsInt();
623    int height = previewParam["height"].AsInt();
624    int dpi = previewParam["dpi"].AsInt();
625    if (width < minLoadDocWidth || width > maxLoadDocWidth || height < minLoadDocWidth ||
626        height > maxLoadDocWidth || dpi < minDpi || dpi > maxDpi) {
627        return false;
628    }
629    return true;
630}
631
632bool LoadDocumentCommand::IsStrValVailid(const Json2::Value& previewParam) const
633{
634    std::string locale = previewParam["locale"].AsString();
635    bool isLiteDevice = JsApp::IsLiteDevice(CommandParser::GetInstance().GetDeviceType());
636    if (isLiteDevice) {
637        if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), locale) ==
638            liteSupportedLanguages.end()) {
639            return false;
640        }
641    } else {
642        if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), locale) ==
643            richSupportedLanguages.end()) {
644            return false;
645        }
646    }
647    if (previewParam["colorMode"].AsString() != "light" && previewParam["colorMode"].AsString() != "dark") {
648        return false;
649    }
650    if (previewParam["orientation"].AsString() != "portrait" &&
651        previewParam["orientation"].AsString() != "landscape") {
652        return false;
653    }
654    if (std::find(LoadDocDevs.begin(), LoadDocDevs.end(), previewParam["deviceType"].AsString()) ==
655        LoadDocDevs.end()) {
656        return false;
657    }
658    return true;
659}
660
661void LoadDocumentCommand::RunSet()
662{
663    VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::START);
664    ILOG("LoadDocumentCommand begin.");
665    std::string pageUrl = args["url"].AsString();
666    std::string className = args["className"].AsString();
667    VirtualScreenImpl::GetInstance().InitFlushEmptyTime();
668    JsAppImpl::GetInstance().LoadDocument(pageUrl, className, args["previewParam"]);
669    VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::FINISHED);
670    SetCommandResult("result", JsonReader::CreateBool(true));
671    ILOG("LoadDocumentCommand finished.");
672}
673
674ReloadRuntimePageCommand::ReloadRuntimePageCommand(CommandType commandType,
675                                                   const Json2::Value& arg,
676                                                   const LocalSocket& socket)
677    : CommandLine(commandType, arg, socket)
678{
679}
680
681bool ReloadRuntimePageCommand::IsSetArgValid() const
682{
683    if (args.IsNull() || !args.IsMember("ReloadRuntimePage") || !args["ReloadRuntimePage"].IsString()) {
684        ELOG("Invalid number of arguments!");
685        return false;
686    }
687    return true;
688}
689
690void ReloadRuntimePageCommand::RunSet()
691{
692    std::string currentPage = args["ReloadRuntimePage"].AsString();
693    JsAppImpl::GetInstance().ReloadRuntimePage(currentPage);
694    SetCommandResult("result", JsonReader::CreateBool(true));
695    ILOG("ReloadRuntimePage finished, currentPage is: %s", args["ReloadRuntimePage"].AsString().c_str());
696}
697
698CurrentRouterCommand::CurrentRouterCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
699    : CommandLine(commandType, arg, socket)
700{
701}
702
703void CurrentRouterCommand::RunGet()
704{
705    Json2::Value resultContent = JsonReader::CreateObject();
706    std::string currentRouter = VirtualScreenImpl::GetInstance().GetCurrentRouter();
707    resultContent.Add("CurrentRouter", currentRouter.c_str());
708    SetResultToManager("args", resultContent, "CurrentJsRouter");
709    ILOG("Get CurrentRouter run finished.");
710}
711
712LoadContentCommand::LoadContentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
713    : CommandLine(commandType, arg, socket)
714{
715}
716
717void LoadContentCommand::RunGet()
718{
719    Json2::Value resultContent = JsonReader::CreateObject();
720    std::string currentRouter = VirtualScreenImpl::GetInstance().GetAbilityCurrentRouter();
721    resultContent.Add("AbilityCurrentRouter", currentRouter.c_str());
722    SetResultToManager("args", resultContent, "AbilityCurrentJsRouter");
723    ILOG("Get AbilityCurrentRouter run finished.");
724}
725
726LanguageCommand::LanguageCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
727    : CommandLine(commandType, arg, socket)
728{
729}
730
731bool LanguageCommand::IsSetArgValid() const
732{
733    if (args.IsNull() || !args.IsMember("Language") || !args["Language"].IsString()) {
734        ELOG("Invalid number of arguments!");
735        return false;
736    }
737
738    CommandParser& cmdParser = CommandParser::GetInstance();
739    std::string deviceType = cmdParser.GetDeviceType();
740    bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
741    if (isLiteDevice) {
742        if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), args["Language"].AsString()) ==
743            liteSupportedLanguages.end()) {
744            ELOG("Language not support liteDevice : %s", args["Language"].AsString().c_str());
745            return false;
746        }
747    } else {
748        if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), args["Language"].AsString()) ==
749            richSupportedLanguages.end()) {
750            ELOG("Language not support richDevice : %s", args["Language"].AsString().c_str());
751            return false;
752        }
753    }
754    return true;
755}
756
757void LanguageCommand::RunGet()
758{
759    std::string language = SharedData<std::string>::GetData(SharedDataType::LANGUAGE);
760    Json2::Value resultContent = JsonReader::CreateObject();
761    resultContent.Add("Language", language.c_str());
762    SetCommandResult("result", resultContent);
763    ILOG("Get language run finished.");
764}
765
766void LanguageCommand::RunSet()
767{
768    std::string language(args["Language"].AsString());
769    SharedData<std::string>::SetData(SharedDataType::LANGUAGE, language);
770    SetCommandResult("result", JsonReader::CreateBool(true));
771    ILOG("Set language run finished, language is: %s", language.c_str());
772}
773
774SupportedLanguagesCommand::SupportedLanguagesCommand(CommandType commandType,
775                                                     const Json2::Value& arg,
776                                                     const LocalSocket& socket)
777    : CommandLine(commandType, arg, socket)
778{
779}
780
781void SupportedLanguagesCommand::RunGet()
782{
783    Json2::Value resultContent = JsonReader::CreateObject();
784    Json2::Value languageList = JsonReader::CreateArray();
785    std::string deviceType = CommandParser::GetInstance().GetDeviceType();
786    bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
787    if (!deviceType.empty() && !isLiteDevice) {
788        for (auto iter = richSupportedLanguages.begin(); iter != richSupportedLanguages.end(); iter++) {
789            languageList.Add((*iter).c_str());
790        }
791    } else {
792        for (auto iter = liteSupportedLanguages.begin(); iter != liteSupportedLanguages.end(); iter++) {
793            languageList.Add((*iter).c_str());
794        }
795    }
796    resultContent.Add("SupportedLanguages", languageList);
797    SetCommandResult("result", resultContent);
798    ILOG("Get supportedLanguages run finished.");
799}
800
801LocationCommand::LocationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
802    : CommandLine(commandType, arg, socket)
803{
804}
805
806bool LocationCommand::IsSetArgValid() const
807{
808    if (args.IsNull() || !args.IsMember("latitude") || !args.IsMember("longitude")) {
809        ELOG("Invalid number of arguments!");
810        return false;
811    }
812    std::string latitude(args["latitude"].AsString());
813    std::string longitude(args["longitude"].AsString());
814    std::regex isDob("^([\\-]*[0-9]{1,}[\\.][0-9]*)$");
815    if (!regex_match(latitude, isDob) || !regex_match(longitude, isDob)) {
816        ELOG("Invalid arguments!");
817        return false;
818    }
819
820    if (!SharedData<double>::IsValid(SharedDataType::LATITUDE, atof(latitude.data()))) {
821        ELOG("LocationCommand invalid latitude value: %f", atof(latitude.data()));
822        return false;
823    }
824
825    if (!SharedData<double>::IsValid(SharedDataType::LONGITUDE, atof(longitude.data()))) {
826        ELOG("LocationCommand invalid longitude value: %f", atof(longitude.data()));
827        return false;
828    }
829    return true;
830}
831
832void LocationCommand::RunGet()
833{
834    double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE);
835    double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE);
836    Json2::Value resultContent = JsonReader::CreateObject();
837    resultContent.Add("latitude", latitude);
838    resultContent.Add("longitude", longitude);
839    SetCommandResult("result", resultContent);
840    ILOG("Get location run finished");
841}
842
843void LocationCommand::RunSet()
844{
845    std::string latitude(args["latitude"].AsString());
846    std::string longitude(args["longitude"].AsString());
847    SharedData<double>::SetData(SharedDataType::LONGITUDE, atof(longitude.data()));
848    SharedData<double>::SetData(SharedDataType::LATITUDE, atof(latitude.data()));
849    Json2::Value resultContent = JsonReader::CreateBool(true);
850    SetCommandResult("result", resultContent);
851    ILOG("Set location run finished, latitude: %s,longitude: %s", latitude.c_str(), longitude.c_str());
852}
853
854DistributedCommunicationsCommand::DistributedCommunicationsCommand(CommandType commandType,
855                                                                   const Json2::Value& arg,
856                                                                   const LocalSocket& socket)
857    : CommandLine(commandType, arg, socket)
858{
859}
860
861void DistributedCommunicationsCommand::RunAction()
862{
863    MessageInfo info;
864    info.deviceID = args["DeviceId"].AsString();
865    info.bundleName = args["bundleName"].AsString();
866    info.abilityName = args["abilityName"].AsString();
867    info.message = args["message"].AsString();
868    VirtualMessageImpl::GetInstance().SendVirtualMessage(info);
869    Json2::Value resultContent = JsonReader::CreateBool(true);
870    SetCommandResult("result", resultContent);
871    ILOG("Send distributedCommunications run finished");
872}
873
874bool DistributedCommunicationsCommand::IsActionArgValid() const
875{
876    if (args.IsNull() || !args.IsMember("DeviceId") || !args.IsMember("bundleName") || !args.IsMember("abilityName") ||
877        !args.IsMember("message")) {
878        ELOG("Invalid number of arguments!");
879        return false;
880    }
881    if (args["DeviceId"].AsString().empty() || args["bundleName"].AsString().empty() ||
882        args["abilityName"].AsString().empty() || args["message"].AsString().empty()) {
883        ELOG("Invalid arguments!");
884        return false;
885    }
886    return true;
887}
888
889std::vector<char> DistributedCommunicationsCommand::StringToCharVector(std::string str) const
890{
891    std::vector<char> vec(str.begin(), str.end());
892    vec.push_back('\0');
893    return vec;
894}
895
896KeepScreenOnStateCommand::KeepScreenOnStateCommand(CommandType commandType,
897                                                   const Json2::Value& arg,
898                                                   const LocalSocket& socket)
899    : CommandLine(commandType, arg, socket)
900{
901}
902
903void KeepScreenOnStateCommand::RunGet()
904{
905    Json2::Value result = JsonReader::CreateObject();
906    result.Add("KeepScreenOnState", SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON));
907    SetCommandResult("result", result);
908    ILOG("Get keepScreenOnState run finished");
909}
910
911void KeepScreenOnStateCommand::RunSet()
912{
913    SharedData<bool>::SetData(SharedDataType::KEEP_SCREEN_ON, args["KeepScreenOnState"].AsBool());
914    SetCommandResult("result", JsonReader::CreateBool(true));
915    ILOG("Set keepScreenOnState run finished, the value is: %s",
916        args["KeepScreenOnState"].AsBool() ? "true" : "false");
917}
918
919bool KeepScreenOnStateCommand::IsSetArgValid() const
920{
921    if (args.IsNull() || !args.IsMember("KeepScreenOnState")) {
922        ELOG("Invalid number of arguments!");
923        return false;
924    }
925    if (!args["KeepScreenOnState"].IsBool()) {
926        ELOG("arg KeepScreenOnState id not bool");
927        return false;
928    }
929    return true;
930}
931
932WearingStateCommand::WearingStateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
933    : CommandLine(commandType, arg, socket)
934{
935}
936
937void WearingStateCommand::RunGet()
938{
939    Json2::Value result = JsonReader::CreateObject();
940    result.Add("WearingState", SharedData<bool>::GetData(SharedDataType::WEARING_STATE));
941    SetCommandResult("result", result);
942    ILOG("Get wearingState run finished");
943}
944
945void WearingStateCommand::RunSet()
946{
947    SharedData<bool>::SetData(SharedDataType::WEARING_STATE, args["WearingState"].AsBool());
948    SetCommandResult("result", JsonReader::CreateBool(true));
949    ILOG("Set wearingState run finished, the value is: %s", args["WearingState"].AsBool() ? "true" : "false");
950}
951
952bool WearingStateCommand::IsSetArgValid() const
953{
954    if (args.IsNull() || !args.IsMember("WearingState")) {
955        ELOG("Invalid number of arguments!");
956        return false;
957    }
958    if (!args["WearingState"].IsBool()) {
959        ILOG("arg WearingState is not bool");
960        return false;
961    }
962    return true;
963}
964
965BrightnessModeCommand::BrightnessModeCommand(CommandType commandType, const Json2::Value& arg,
966    const LocalSocket& socket) : CommandLine(commandType, arg, socket)
967{
968}
969
970void BrightnessModeCommand::RunGet()
971{
972    Json2::Value result = JsonReader::CreateObject();
973    result.Add("BrightnessMode", SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_MODE));
974    SetCommandResult("result", result);
975    ILOG("Get brightnessMode run finished");
976}
977
978void BrightnessModeCommand::RunSet()
979{
980    SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_MODE,
981                                 static_cast<uint8_t>(args["BrightnessMode"].AsInt()));
982    SetCommandResult("result", JsonReader::CreateBool(true));
983    ILOG("Set brightnessMode run finished, the value is: %d", args["BrightnessMode"].AsInt());
984}
985
986bool BrightnessModeCommand::IsSetArgValid() const
987{
988    if (args.IsNull() || !args.IsMember("BrightnessMode")) {
989        ELOG("Invalid number of arguments!");
990        return false;
991    }
992    if (!args["BrightnessMode"].IsInt()) {
993        ELOG("BrightnessMode is not int");
994        return false;
995    }
996    uint8_t temp = static_cast<uint8_t>(args["BrightnessMode"].AsInt());
997    if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_MODE, temp)) {
998        ELOG("BrightnessModeCommand invalid value: %d", temp);
999        return false;
1000    }
1001    return true;
1002}
1003
1004ChargeModeCommand::ChargeModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1005    : CommandLine(commandType, arg, socket)
1006{
1007}
1008
1009void ChargeModeCommand::RunGet()
1010{
1011    Json2::Value result = JsonReader::CreateObject();
1012    result.Add("ChargeMode", SharedData<uint8_t>::GetData(SharedDataType::BATTERY_STATUS));
1013    SetCommandResult("result", result);
1014    ILOG("Get chargeMode run finished");
1015}
1016
1017void ChargeModeCommand::RunSet()
1018{
1019    SharedData<uint8_t>::SetData(SharedDataType::BATTERY_STATUS,
1020                                 static_cast<uint8_t>(args["ChargeMode"].AsInt()));
1021    SetCommandResult("result", JsonReader::CreateBool(true));
1022    ILOG("Set chargeMode run finished, the value is: %d", args["ChargeMode"].AsInt());
1023}
1024
1025bool ChargeModeCommand::IsSetArgValid() const
1026{
1027    if (args.IsNull() || !args.IsMember("ChargeMode")) {
1028        ELOG("Invalid number of arguments!");
1029        return false;
1030    }
1031    if (!args["ChargeMode"].IsInt()) {
1032        ELOG("ChargeMode is not int");
1033        return false;
1034    }
1035    uint8_t temp = static_cast<uint8_t>(args["ChargeMode"].AsInt());
1036    if (!SharedData<uint8_t>::IsValid(SharedDataType::BATTERY_STATUS, temp)) {
1037        ELOG("ChargeModeCommand invalid value: %d", temp);
1038        return false;
1039    }
1040    return true;
1041}
1042
1043BrightnessCommand::BrightnessCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1044    : CommandLine(commandType, arg, socket)
1045{
1046}
1047
1048void BrightnessCommand::RunGet()
1049{
1050    Json2::Value result = JsonReader::CreateObject();
1051    result.Add("Brightness", SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_VALUE));
1052    SetCommandResult("result", result);
1053    ILOG("Get brightness run finished");
1054}
1055
1056void BrightnessCommand::RunSet()
1057{
1058    SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_VALUE,
1059                                 static_cast<uint8_t>(args["Brightness"].AsInt()));
1060    Json2::Value result = JsonReader::CreateBool(true);
1061    SetCommandResult("result", result);
1062    ILOG("Set brightness run finished, the value is: %d", args["Brightness"].AsInt());
1063}
1064
1065bool BrightnessCommand::IsSetArgValid() const
1066{
1067    if (args.IsNull() || !args.IsMember("Brightness")) {
1068        ELOG("Invalid number of arguments!");
1069        return false;
1070    }
1071    if (!args["Brightness"].IsInt()) {
1072        ELOG("Brightness is not int");
1073        return false;
1074    }
1075    uint8_t temp = static_cast<uint8_t>(args["Brightness"].AsInt());
1076    if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_VALUE, temp)) {
1077        ELOG("BrightnessCommand invalid value: ", temp);
1078        return false;
1079    }
1080    return true;
1081}
1082
1083HeartRateCommand::HeartRateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1084    : CommandLine(commandType, arg, socket)
1085{
1086}
1087
1088void HeartRateCommand::RunGet()
1089{
1090    Json2::Value result = JsonReader::CreateObject();
1091    result.Add("HeartRate", SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE));
1092    SetCommandResult("result", result);
1093    ILOG("Get heartRate run finished");
1094}
1095
1096void HeartRateCommand::RunSet()
1097{
1098    SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE,
1099                                 static_cast<uint8_t>(args["HeartRate"].AsInt()));
1100    SetCommandResult("result", JsonReader::CreateBool(true));
1101    ILOG("Set heartRate run finished, the value is: %d", args["HeartRate"].AsInt());
1102}
1103
1104bool HeartRateCommand::IsSetArgValid() const
1105{
1106    if (args.IsNull() || !args.IsMember("HeartRate")) {
1107        ELOG("Invalid number of arguments!");
1108        return false;
1109    }
1110    if (!args["HeartRate"].IsInt()) {
1111        ELOG("HeartRate is not int");
1112        return false;
1113    }
1114    if (args["HeartRate"].AsInt() > UINT8_MAX) {
1115        ELOG("Invalid arguments!");
1116        return false;
1117    }
1118    uint8_t temp = static_cast<uint8_t>(args["HeartRate"].AsInt());
1119    if (!SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, temp)) {
1120        ELOG("HeartRateCommand invalid value: %d", temp);
1121        return false;
1122    }
1123    return true;
1124}
1125
1126StepCountCommand::StepCountCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1127    : CommandLine(commandType, arg, socket)
1128{
1129}
1130
1131void StepCountCommand::RunGet()
1132{
1133    Json2::Value result = JsonReader::CreateObject();
1134    result.Add("StepCount", SharedData<uint32_t>::GetData(SharedDataType::SUMSTEP_VALUE));
1135    SetCommandResult("result", result);
1136    ILOG("Get stepCount run finished");
1137}
1138
1139void StepCountCommand::RunSet()
1140{
1141    SharedData<uint32_t>::SetData(SharedDataType::SUMSTEP_VALUE,
1142                                  static_cast<uint32_t>(args["StepCount"].AsInt()));
1143    SetCommandResult("result", JsonReader::CreateBool(true));
1144    ILOG("Set stepCount run finished, the value is: %d", args["StepCount"].AsInt());
1145}
1146
1147bool StepCountCommand::IsSetArgValid() const
1148{
1149    if (args.IsNull() || !args.IsMember("StepCount")) {
1150        ELOG("Invalid number of arguments!");
1151        return false;
1152    }
1153    if (!args["StepCount"].IsInt()) {
1154        ELOG("StepCount is not int");
1155        return false;
1156    }
1157
1158    uint32_t temp = args["StepCount"].AsUInt();
1159    if (!SharedData<uint32_t>::IsValid(SharedDataType::SUMSTEP_VALUE, temp)) {
1160        ELOG("StepCountCommand invalid value: %d", temp);
1161        return false;
1162    }
1163    return true;
1164}
1165
1166InspectorJSONTree::InspectorJSONTree(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1167    : CommandLine(commandType, arg, socket)
1168{
1169}
1170
1171void InspectorJSONTree::RunAction()
1172{
1173    ILOG("GetJsonTree run!");
1174    std::string str = JsAppImpl::GetInstance().GetJSONTree();
1175    if (str == "null") {
1176        str = "{\"children\":\"empty json tree\"}";
1177    }
1178    SetCommandResult("result", JsonReader::CreateString(str));
1179    ILOG("SendJsonTree end!");
1180}
1181
1182InspectorDefault::InspectorDefault(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1183    : CommandLine(commandType, arg, socket)
1184{
1185}
1186
1187void InspectorDefault::RunAction()
1188{
1189    ILOG("GetDefaultJsonTree run!");
1190    std::string str = JsAppImpl::GetInstance().GetDefaultJSONTree();
1191    SetCommandResult("result", JsonReader::CreateString(str));
1192    ILOG("SendDefaultJsonTree end!");
1193}
1194
1195ExitCommand::ExitCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1196    : CommandLine(commandType, arg, socket)
1197{
1198}
1199
1200void ExitCommand::RunAction()
1201{
1202    ILOG("ExitCommand run.");
1203    SetCommandResult("result", JsonReader::CreateBool(true));
1204    SendResult();
1205    Interrupter::Interrupt();
1206    ILOG("Ready to exit");
1207}
1208
1209DeviceTypeCommand::DeviceTypeCommand(CommandLine::CommandType commandType,
1210                                     const Json2::Value& arg,
1211                                     const LocalSocket& socket)
1212    : CommandLine(commandType, arg, socket)
1213{
1214}
1215
1216void DeviceTypeCommand::RunSet() {}
1217
1218ResolutionCommand::ResolutionCommand(CommandLine::CommandType commandType,
1219                                     const Json2::Value& arg,
1220                                     const LocalSocket& socket)
1221    : CommandLine(commandType, arg, socket)
1222{
1223}
1224
1225void ResolutionCommand::RunSet() {}
1226
1227BackClickedCommand::BackClickedCommand(CommandLine::CommandType commandType,
1228                                       const Json2::Value& arg,
1229                                       const LocalSocket& socket)
1230    : CommandLine(commandType, arg, socket)
1231{
1232}
1233
1234void BackClickedCommand::RunAction()
1235{
1236    MouseInputImpl::GetInstance().DispatchOsBackEvent();
1237    ILOG("BackClickCommand run");
1238    SetCommandResult("result", JsonReader::CreateBool(true));
1239    ILOG("BackClickCommand end");
1240}
1241
1242RestartCommand::RestartCommand(CommandLine::CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1243    : CommandLine(commandType, arg, socket)
1244{
1245}
1246
1247void RestartCommand::RunAction()
1248{
1249    ILOG("RestartCommand start");
1250    JsAppImpl::GetInstance().Restart();
1251    SetCommandResult("result", JsonReader::CreateBool(true));
1252    ILOG("RestartCommand end");
1253}
1254
1255FastPreviewMsgCommand::FastPreviewMsgCommand(CommandType commandType, const Json2::Value& arg,
1256    const LocalSocket& socket) : CommandLine(commandType, arg, socket)
1257{
1258}
1259
1260void FastPreviewMsgCommand::RunGet()
1261{
1262    Json2::Value resultContent = JsonReader::CreateObject();
1263    std::string fastPreviewMsg = VirtualScreenImpl::GetInstance().GetFastPreviewMsg();
1264    resultContent.Add("FastPreviewMsg", fastPreviewMsg.c_str());
1265    SetResultToManager("args", resultContent, "MemoryRefresh");
1266    ILOG("Get FastPreviewMsgCommand run finished.");
1267}
1268
1269DropFrameCommand::DropFrameCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1270    : CommandLine(commandType, arg, socket)
1271{
1272}
1273
1274bool DropFrameCommand::IsSetArgValid() const
1275{
1276    if (args.IsNull() || !args.IsMember("frequency") || !args["frequency"].IsInt()) {
1277        ELOG("Invalid DropFrame of arguments!");
1278        return false;
1279    }
1280    if (args["frequency"].AsInt() < 0) {
1281        ELOG("DropFrame param frequency must greater than or equal to 0");
1282        return false;
1283    }
1284    return true;
1285}
1286
1287void DropFrameCommand::RunSet()
1288{
1289    ILOG("Set DropFrame frequency start.");
1290    int frequency = args["frequency"].AsInt();
1291    VirtualScreenImpl::GetInstance().SetDropFrameFrequency(frequency);
1292    SetCommandResult("result", JsonReader::CreateBool(true));
1293    ILOG("Set DropFrame frequency: %dms.", frequency);
1294}
1295
1296bool KeyPressCommand::IsActionArgValid() const
1297{
1298    if (args.IsNull() || !args.IsMember("isInputMethod") || !args["isInputMethod"].IsBool()) {
1299        ELOG("Param isInputMethod's value is invalid.");
1300        return false;
1301    }
1302    bool isInputMethod = args["isInputMethod"].AsBool();
1303    if (isInputMethod) {
1304        return IsImeArgsValid();
1305    } else {
1306        return IsKeyArgsValid();
1307    }
1308}
1309
1310bool KeyPressCommand::IsImeArgsValid() const
1311{
1312    if (!args.IsMember("codePoint") || !args["codePoint"].IsInt()) {
1313        ELOG("Param codePoint's value is invalid.");
1314        return false;
1315    }
1316    return true;
1317}
1318
1319bool KeyPressCommand::IsKeyArgsValid() const
1320{
1321    if (!args.IsMember("keyCode") || !args["keyCode"].IsInt() || !args["keyAction"].IsInt() ||
1322        !args.IsMember("keyAction") || !args["keyAction"].IsInt() ||
1323        !args.IsMember("pressedCodes") || !args["pressedCodes"].IsArray() ||
1324        args["pressedCodes"].GetArraySize() < 1 || (args.IsMember("keyString") && !args["keyString"].IsString())) {
1325        ELOG("Param keyEvent's value is invalid.");
1326        return false;
1327    }
1328    if (!args.IsMember("keyString")) {
1329        ILOG("Param keyString is lost, it will be empty string.");
1330    }
1331    if (args["keyAction"].AsInt() < minActionVal || args["keyAction"].AsInt() > maxActionVal) {
1332        ELOG("Param keyAction's value is invalid,value range %d-%d.", minActionVal, maxActionVal);
1333        return false;
1334    }
1335    int keyCode = args["keyCode"].AsInt();
1336    if (keyCode > maxKeyVal || keyCode < minKeyVal) {
1337        ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1338        return false;
1339    }
1340    Json2::Value arrayNum = args["pressedCodes"];
1341    for (unsigned int i = 0; i < arrayNum.GetArraySize(); i++) {
1342        if (!arrayNum.GetArrayItem(i).IsInt()) {
1343            ELOG("Param pressedCodes's value is invalid.");
1344            return false;
1345        }
1346        int pressedCode = arrayNum.GetArrayItem(i).AsInt();
1347        if (pressedCode > maxKeyVal || pressedCode < minKeyVal) {
1348            ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1349            return false;
1350        }
1351    }
1352    return true;
1353}
1354
1355KeyPressCommand::KeyPressCommand(CommandType commandType, const Json2::Value& arg,
1356                                 const LocalSocket& socket)
1357    : CommandLine(commandType, arg, socket)
1358{
1359}
1360
1361void KeyPressCommand::RunAction()
1362{
1363    if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
1364        return;
1365    }
1366    bool isInputMethod = args["isInputMethod"].AsBool();
1367    if (isInputMethod) {
1368        VirtualScreen::inputMethodCountPerMinute++;
1369        unsigned int codePoint = args["codePoint"].AsInt();
1370        KeyInputImpl::GetInstance().SetCodePoint(codePoint);
1371        KeyInputImpl::GetInstance().DispatchOsInputMethodEvent();
1372    } else {
1373        VirtualScreen::inputKeyCountPerMinute++;
1374        int32_t keyCode = args["keyCode"].AsInt();
1375        int32_t keyAction = args["keyAction"].AsInt();
1376        Json2::Value pressedCodes = args["pressedCodes"];
1377        std::vector<int32_t> pressedCodesVec;
1378        for (unsigned int i = 0; i < pressedCodes.GetArraySize(); i++) {
1379            pressedCodesVec.push_back(pressedCodes.GetArrayItem(i).AsInt());
1380        }
1381        std::string keyString = "";
1382        if (args.IsMember("keyString") && args["keyString"].IsString()) {
1383            keyString = args["keyString"].AsString();
1384        }
1385        KeyInputImpl::GetInstance().SetKeyEvent(keyCode, keyAction, pressedCodesVec, keyString);
1386        KeyInputImpl::GetInstance().DispatchOsKeyEvent();
1387    }
1388    SetCommandResult("result", JsonReader::CreateBool(true));
1389    ILOG("KeyPressCommand run finished.");
1390}
1391
1392bool PointEventCommand::IsActionArgValid() const
1393{
1394    return IsArgsExist() && IsArgsValid();
1395}
1396
1397bool PointEventCommand::IsArgsExist() const
1398{
1399    if (args.IsNull() || !args.IsMember("x") || !args.IsMember("y") ||
1400        !args["x"].IsInt() || !args["y"].IsInt()) {
1401        return false;
1402    }
1403    if (!args.IsMember("button") || !args.IsMember("action") ||
1404        !args["button"].IsInt() || !args["action"].IsInt()) {
1405        return false;
1406    }
1407    if (!args.IsMember("sourceType") || !args.IsMember("sourceTool") ||
1408        !args["sourceType"].IsInt() || !args["sourceTool"].IsInt()) {
1409        return false;
1410    }
1411    if (!args.IsMember("axisValues") || !args["axisValues"].IsArray()) {
1412        return false;
1413    }
1414    return true;
1415}
1416
1417bool PointEventCommand::IsArgsValid() const
1418{
1419    int32_t pointX = args["x"].AsInt();
1420    int32_t pointY = args["y"].AsInt();
1421    int32_t button = args["button"].AsInt();
1422    int32_t action = args["action"].AsInt();
1423    int32_t sourceType = args["sourceType"].AsInt();
1424    int32_t sourceTool = args["sourceTool"].AsInt();
1425    if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetCurrentWidth()) {
1426        ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentWidth());
1427        return false;
1428    }
1429    if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetCurrentHeight()) {
1430        ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetCurrentHeight());
1431        return false;
1432    }
1433    if (button < -1 || action < 0 || sourceType < 0 || sourceTool < 0) {
1434        ELOG("action,sourceType,sourceTool must >= 0, button must >= -1");
1435        return false;
1436    }
1437    Json2::Value axisArrayNum = args["axisValues"];
1438    for (unsigned int i = 0; i < axisArrayNum.GetArraySize(); i++) {
1439        if (!axisArrayNum.GetArrayItem(i).IsDouble()) {
1440            ELOG("Param axisValues's value is invalid.");
1441            return false;
1442        }
1443    }
1444    return true;
1445}
1446
1447PointEventCommand::PointEventCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1448    : CommandLine(commandType, arg, socket)
1449{
1450}
1451
1452void PointEventCommand::RunAction()
1453{
1454    int type = 9;
1455    EventParams param;
1456    if (args.IsMember("pressedButtons") && args["pressedButtons"].IsArray()) {
1457        Json2::Value pressedCodes = args["pressedButtons"];
1458        for (unsigned int i = 0; i < pressedCodes.GetArraySize(); i++) {
1459            Json2::Value val = pressedCodes.GetArrayItem(i);
1460            if (!val.IsInt() || val.AsInt() < -1) {
1461                continue;
1462            }
1463            param.pressedBtnsVec.insert(val.AsInt());
1464        }
1465    }
1466    std::vector<double> axisVec; // 13 is array size
1467    Json2::Value axisCodes = args["axisValues"];
1468    for (unsigned int i = 0; i < axisCodes.GetArraySize(); i++) {
1469        param.axisVec.push_back(axisCodes.GetArrayItem(i).AsDouble());
1470    }
1471    param.x = args["x"].AsDouble();
1472    param.y = args["y"].AsDouble();
1473    param.type = type;
1474    param.button = args["button"].AsInt();
1475    param.action = args["action"].AsInt();
1476    param.sourceType = args["sourceType"].AsInt();
1477    param.sourceTool = args["sourceTool"].AsInt();
1478    param.name = "PointEvent";
1479    SetEventParams(param);
1480    SetCommandResult("result", JsonReader::CreateBool(true));
1481}
1482
1483FoldStatusCommand::FoldStatusCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket)
1484    : CommandLine(commandType, arg, socket)
1485{
1486}
1487
1488bool FoldStatusCommand::IsSetArgValid() const
1489{
1490    if (args.IsNull() || !args.IsMember("FoldStatus") || !args["FoldStatus"].IsString()) {
1491        ELOG("Invalid FoldStatus of arguments!");
1492        return false;
1493    }
1494    if (!args.IsMember("width") || !args["width"].IsInt() ||
1495        !args.IsMember("height") || !args["height"].IsInt()) {
1496        ELOG("Invalid width and height of arguments!");
1497        return false;
1498    }
1499    if (args["width"].AsInt() < minWidth || args["width"].AsInt() > maxWidth ||
1500        args["height"].AsInt() < minWidth || args["height"].AsInt() > maxWidth) {
1501        ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
1502        return false;
1503    }
1504    if (args["FoldStatus"].AsString() == "fold" || args["FoldStatus"].AsString() == "unfold" ||
1505        args["FoldStatus"].AsString() == "unknown" || args["FoldStatus"].AsString() == "half_fold") {
1506        return true;
1507    }
1508    ELOG("FoldStatus param must be \"fold\" or \"unfold\" or \"unknown\" or \"half_fold\"");
1509    return false;
1510}
1511
1512void FoldStatusCommand::RunSet()
1513{
1514    std::string commandStatus = args["FoldStatus"].AsString();
1515    int32_t width = args["width"].AsInt();
1516    int32_t height = args["height"].AsInt();
1517    std::string currentStatus = VirtualScreenImpl::GetInstance().GetFoldStatus();
1518    if (commandStatus != currentStatus) {
1519        JsAppImpl::GetInstance().FoldStatusChanged(commandStatus, width, height);
1520    }
1521    SetCommandResult("result", JsonReader::CreateBool(true));
1522    ILOG("Set FoldStatus run finished, FoldStatus is: %s", args["FoldStatus"].AsString().c_str());
1523}
1524
1525AvoidAreaCommand::AvoidAreaCommand(CommandType commandType, const Json2::Value& arg,
1526    const LocalSocket& socket) : CommandLine(commandType, arg, socket)
1527{
1528}
1529
1530bool AvoidAreaCommand::IsSetArgValid() const
1531{
1532    if (args.IsNull() || !args.IsMember("topRect") || !args.IsMember("bottomRect") ||
1533        !args.IsMember("leftRect") || !args.IsMember("rightRect")) {
1534        ELOG("AvoidAreaCommand missing arguments!");
1535        return false;
1536    }
1537    if (!args["topRect"].IsObject() || !args["bottomRect"].IsObject() || !args["leftRect"].IsObject()
1538        || !args["rightRect"].IsObject()) {
1539        ELOG("Invalid values of arguments!");
1540        return false;
1541    }
1542    Json2::Value topRect = args.GetValue("topRect");
1543    Json2::Value bottomRect = args.GetValue("bottomRect");
1544    Json2::Value leftRect = args.GetValue("leftRect");
1545    Json2::Value rightRect = args.GetValue("rightRect");
1546    if (!IsObjectValid(topRect) || !IsObjectValid(bottomRect) ||
1547        !IsObjectValid(leftRect) || !IsObjectValid(rightRect)) {
1548        ELOG("Invalid values of arguments!");
1549        return false;
1550    }
1551    return true;
1552}
1553
1554bool AvoidAreaCommand::IsObjectValid(const Json2::Value& val) const
1555{
1556    if (val.IsNull() || !val.IsMember("posX") || !val["posY"].IsInt() ||
1557        !val.IsMember("width") || !val.IsMember("height")) {
1558        ELOG("AvoidAreaCommand missing arguments!");
1559        return false;
1560    }
1561    if (!val["posX"].IsInt() || !val["posY"].IsInt() || !val["width"].IsUInt()
1562        || !val["height"].IsUInt()) {
1563        ELOG("Invalid values of arguments!");
1564        return false;
1565    }
1566    if (val["posX"].AsInt() < 0 || val["posY"].AsInt() < 0 ||
1567        val["width"].AsUInt() < 0 || val["height"].AsUInt() < 0) {
1568        ELOG("Invalid values of arguments!");
1569        return false;
1570    }
1571    return true;
1572}
1573
1574void AvoidAreaCommand::RunSet()
1575{
1576    Json2::Value topRectObj = args.GetValue("topRect");
1577    AvoidRect topRect = AvoidRect(topRectObj["posX"].AsInt(), topRectObj["posY"].AsInt(),
1578        topRectObj["width"].AsUInt(), topRectObj["height"].AsUInt());
1579    Json2::Value bottomRectObj = args.GetValue("bottomRect");
1580    AvoidRect bottomRect = AvoidRect(bottomRectObj["posX"].AsInt(), bottomRectObj["posY"].AsInt(),
1581        bottomRectObj["width"].AsUInt(), bottomRectObj["height"].AsUInt());
1582    Json2::Value leftRectObj = args.GetValue("leftRect");
1583    AvoidRect leftRect = AvoidRect(leftRectObj["posX"].AsInt(), leftRectObj["posY"].AsInt(),
1584        leftRectObj["width"].AsUInt(), leftRectObj["height"].AsUInt());
1585    Json2::Value rightRectObj = args.GetValue("rightRect");
1586    AvoidRect rightRect = AvoidRect(rightRectObj["posX"].AsInt(), rightRectObj["posY"].AsInt(),
1587        rightRectObj["width"].AsUInt(), rightRectObj["height"].AsUInt());
1588    JsAppImpl::GetInstance().SetAvoidArea(AvoidAreas(topRect, leftRect, rightRect, bottomRect));
1589    SetCommandResult("result", JsonReader::CreateBool(true));
1590    ILOG("Set AvoidArea run finished");
1591}
1592
1593AvoidAreaChangedCommand::AvoidAreaChangedCommand(CommandType commandType, const Json2::Value& arg,
1594    const LocalSocket& socket) : CommandLine(commandType, arg, socket)
1595{
1596}
1597
1598void AvoidAreaChangedCommand::RunGet()
1599{
1600    SetResultToManager("args", args, "AvoidAreaChanged");
1601    ILOG("Get AvoidAreaChangedCommand run finished.");
1602}