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
21 #include "CommandLineInterface.h"
22 #include "CommandParser.h"
23 #include "Interrupter.h"
24 #include "JsApp.h"
25 #include "JsAppImpl.h"
26 #include "JsonReader.h"
27 #include "LanguageManagerImpl.h"
28 #include "ModelConfig.h"
29 #include "ModelManager.h"
30 #include "MouseInputImpl.h"
31 #include "MouseWheelImpl.h"
32 #include "KeyInputImpl.h"
33 #include "PreviewerEngineLog.h"
34 #include "SharedData.h"
35 #include "VirtualMessageImpl.h"
36 #include "VirtualScreenImpl.h"
37
38 using namespace std;
39
CommandLine(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)40 CommandLine::CommandLine(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
41 : args(arg), cliSocket(socket), type(commandType), commandName("")
42 {
43 SetCommandResult("result", true);
44 }
45
~CommandLine()46 CommandLine::~CommandLine()
47 {
48 args.clear();
49 }
50
CheckAndRun()51 void CommandLine::CheckAndRun()
52 {
53 if (!IsArgValid()) {
54 SetCommandResult("result", false);
55 SendResult();
56 return;
57 }
58
59 Run();
60 SendResult();
61 }
62
SendResult()63 void CommandLine::SendResult()
64 {
65 if (commandResult.empty()) {
66 return;
67 }
68 cliSocket << commandResult.toStyledString();
69 commandResult.clear();
70 }
71
RunAndSendResultToManager()72 void CommandLine::RunAndSendResultToManager()
73 {
74 Run();
75 SendResultToManager();
76 }
77
SendResultToManager()78 void CommandLine::SendResultToManager()
79 {
80 if (commandResultToManager.empty()) {
81 return;
82 }
83 cliSocket << commandResultToManager.toStyledString();
84 commandResultToManager.clear();
85 }
86
IsArgValid() const87 bool CommandLine::IsArgValid() const
88 {
89 if (type == CommandType::GET) {
90 return IsGetArgValid();
91 }
92 if (type == CommandType::SET) {
93 return IsSetArgValid();
94 }
95 if (type == CommandType::ACTION) {
96 return IsActionArgValid();
97 }
98 return true;
99 }
100
ToUint8(std::string str) const101 uint8_t CommandLine::ToUint8(std::string str) const
102 {
103 int value = atoi(str.data());
104 if (value > UINT8_MAX) {
105 ELOG("CommandLine::ToUint8 value is overflow, value: %d", value);
106 }
107 return static_cast<uint8_t>(value);
108 }
109
SetCommandName(std::string command)110 void CommandLine::SetCommandName(std::string command)
111 {
112 this->commandName = command;
113 }
114
SetCommandResult(const std::string & resultType,const Json::Value & resultContent)115 void CommandLine::SetCommandResult(const std::string& resultType, const Json::Value& resultContent)
116 {
117 this->commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
118 this->commandResult["command"] = this->commandName;
119 this->commandResult[resultType] = resultContent;
120 }
121
SetResultToManager(const std::string & resultType,const Json::Value & resultContent,const std::string & messageType)122 void CommandLine::SetResultToManager(const std::string& resultType,
123 const Json::Value& resultContent,
124 const std::string& messageType)
125 {
126 this->commandResultToManager["MessageType"] = messageType;
127 this->commandResultToManager[resultType] = resultContent;
128 }
129
Run()130 void CommandLine::Run()
131 {
132 if (type == CommandType::GET) {
133 RunGet();
134 } else if (type == CommandType::SET) {
135 RunSet();
136 } else if (type == CommandType::ACTION) {
137 RunAction();
138 }
139 }
140
IsBoolType(std::string arg) const141 bool CommandLine::IsBoolType(std::string arg) const
142 {
143 regex tofrx("^(true)|(false)$");
144 if (regex_match(arg, tofrx)) {
145 return true;
146 }
147 return false;
148 }
149
IsIntType(string arg) const150 bool CommandLine::IsIntType(string arg) const
151 {
152 regex isInt("^\\d+$");
153 return regex_match(arg, isInt);
154 }
155
IsOneDigitFloatType(string arg) const156 bool CommandLine::IsOneDigitFloatType(string arg) const
157 {
158 regex isFloat("^\\d(\\.\\d+)?$");
159 return regex_match(arg, isFloat);
160 }
161
IsActionArgValid() const162 bool TouchPressCommand::IsActionArgValid() const
163 {
164 if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
165 !args["x"].isInt() || !args["y"].isInt()) {
166 return false;
167 }
168 int32_t pointX = args["x"].asInt();
169 int32_t pointY = args["y"].asInt();
170 if (pointX < 0 || pointX > VirtualScreenImpl::GetInstance().GetOrignalWidth()) {
171 ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth());
172 return false;
173 }
174 if (pointY < 0 || pointY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) {
175 ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight());
176 return false;
177 }
178 return true;
179 }
180
TouchPressCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)181 TouchPressCommand::TouchPressCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
182 : CommandLine(commandType, arg, socket)
183 {
184 }
185
RunAction()186 void TouchPressCommand::RunAction()
187 {
188 if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
189 return;
190 }
191 MouseInputImpl::GetInstance().SetMousePosition(atof(args["x"].asString().data()),
192 atof(args["y"].asString().data()));
193 MouseInputImpl::GetInstance().SetMouseStatus(MouseInputImpl::INDEV_STATE_PRESS);
194 MouseInputImpl::GetInstance().DispatchOsTouchEvent();
195 SetCommandResult("result", true);
196 ILOG("MousePress(%s,%s)", args["x"].asString().c_str(), args["y"].asString().c_str());
197 }
198
IsActionArgValid() const199 bool MouseWheelCommand::IsActionArgValid() const
200 {
201 if (args.isNull() || !args.isMember("rotate") || !IsOneDigitFloatType(args["rotate"].asString())) {
202 return false;
203 }
204 return true;
205 }
206
MouseWheelCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)207 MouseWheelCommand::MouseWheelCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
208 : CommandLine(commandType, arg, socket)
209 {
210 }
211
RunAction()212 void MouseWheelCommand::RunAction()
213 {
214 if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
215 return;
216 }
217 MouseWheelImpl::GetInstance().SetRotate(atof(args["rotate"].asString().data()));
218 SetCommandResult("result", true);
219 ILOG("CrownRotate (%s)", args["rotate"].asString().c_str());
220 }
221
TouchReleaseCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)222 TouchReleaseCommand::TouchReleaseCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
223 : CommandLine(commandType, arg, socket)
224 {
225 }
226
RunAction()227 void TouchReleaseCommand::RunAction()
228 {
229 if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
230 return;
231 }
232 MouseInputImpl::GetInstance().SetMouseStatus(MouseInputImpl::INDEV_STATE_RELEASE);
233 MouseInputImpl::GetInstance().DispatchOsTouchEvent();
234 SetCommandResult("result", true);
235 ILOG("MouseRelease run finished");
236 }
237
IsActionArgValid() const238 bool TouchMoveCommand::IsActionArgValid() const
239 {
240 if (args.isNull() || !args.isMember("x") || !args.isMember("y") ||
241 !args["x"].isInt() || !args["y"].isInt()) {
242 return false;
243 }
244 int32_t pX = args["x"].asInt();
245 int32_t pY = args["y"].asInt();
246 if (pX < 0 || pX > VirtualScreenImpl::GetInstance().GetOrignalWidth()) {
247 ELOG("X coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalWidth());
248 return false;
249 }
250 if (pY < 0 || pY > VirtualScreenImpl::GetInstance().GetOrignalHeight()) {
251 ELOG("Y coordinate range %d ~ %d", 0, VirtualScreenImpl::GetInstance().GetOrignalHeight());
252 return false;
253 }
254 return true;
255 }
256
TouchMoveCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)257 TouchMoveCommand::TouchMoveCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
258 : CommandLine(commandType, arg, socket)
259 {
260 }
261
RunAction()262 void TouchMoveCommand::RunAction()
263 {
264 if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
265 return;
266 }
267 MouseInputImpl::GetInstance().SetMouseStatus(MouseInput::INDEV_STATE_MOVE);
268 MouseInputImpl::GetInstance().SetMousePosition(atof(args["x"].asString().data()),
269 atof(args["y"].asString().data()));
270 MouseInputImpl::GetInstance().DispatchOsTouchEvent();
271 Json::Value res = true;
272 SetCommandResult("result", res);
273 ILOG("MouseMove run finished");
274 }
275
PowerCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)276 PowerCommand::PowerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
277 : CommandLine(commandType, arg, socket)
278 {
279 }
280
IsSetArgValid() const281 bool PowerCommand::IsSetArgValid() const
282 {
283 if (args.isNull() || !args.isMember("Power") || !IsOneDigitFloatType(args["Power"].asString())) {
284 ELOG("Invalid number of arguments!");
285 return false;
286 }
287 float val = args["Power"].asFloat();
288 if (!SharedData<double>::IsValid(SharedDataType::BATTERY_LEVEL, val)) {
289 ELOG("PowerCommand invalid value: %f", val);
290 return false;
291 }
292 return true;
293 }
294
RunGet()295 void PowerCommand::RunGet()
296 {
297 double power = SharedData<double>::GetData(SharedDataType::BATTERY_LEVEL);
298 Json::Value resultContent;
299 resultContent["Power"] = power;
300 SetCommandResult("result", resultContent);
301 ILOG("Get power run finished");
302 }
303
RunSet()304 void PowerCommand::RunSet()
305 {
306 string power(args["Power"].asString());
307 SharedData<double>::SetData(SharedDataType::BATTERY_LEVEL, atof(power.data()));
308 Json::Value resultContent = true;
309 SetCommandResult("result", resultContent);
310 ILOG("Set power run finished, the value is: %s", args["Power"].asString().c_str());
311 }
312
VolumeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)313 VolumeCommand::VolumeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
314 : CommandLine(commandType, arg, socket)
315 {
316 }
317
IsSetArgValid() const318 bool VolumeCommand::IsSetArgValid() const
319 {
320 return true;
321 }
322
RunGet()323 void VolumeCommand::RunGet()
324 {
325 Json::Value resultContent = "Command offline";
326 SetCommandResult("result", resultContent);
327 ILOG("Command offline");
328 }
329
RunSet()330 void VolumeCommand::RunSet()
331 {
332 Json::Value resultContent = "Command offline";
333 SetCommandResult("result", resultContent);
334 ILOG("Command offline");
335 }
336
BarometerCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)337 BarometerCommand::BarometerCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
338 : CommandLine(commandType, arg, socket)
339 {
340 }
341
IsSetArgValid() const342 bool BarometerCommand::IsSetArgValid() const
343 {
344 if (args.isNull() || !args.isMember("Barometer") || !args["Barometer"].isString()) {
345 ELOG("Invalid number of arguments!");
346 return false;
347 }
348 string barometer(args["Barometer"].asString());
349 if (!IsIntType(barometer)) {
350 ELOG("Invalid arguments!");
351 return false;
352 }
353
354 if (!SharedData<uint32_t>::IsValid(SharedDataType::PRESSURE_VALUE, static_cast<uint32_t>(atoi(barometer.data())))) {
355 ELOG("Barometer invalid value: %d", atoi(barometer.data()));
356 return false;
357 }
358 return true;
359 }
360
RunGet()361 void BarometerCommand::RunGet()
362 {
363 int barometer = static_cast<int>(SharedData<uint32_t>::GetData(SharedDataType::PRESSURE_VALUE));
364 Json::Value resultContent;
365 resultContent["Barometer"] = barometer;
366 SetCommandResult("result", resultContent);
367 ILOG("Get barometer run finished");
368 }
369
RunSet()370 void BarometerCommand::RunSet()
371 {
372 string barometer(args["Barometer"].asString());
373 SharedData<uint32_t>::SetData(SharedDataType::PRESSURE_VALUE, static_cast<uint32_t>(atoi(barometer.data())));
374 SetCommandResult("result", true);
375 ILOG("Set barometer run finished, the value is: %s", args["Barometer"].asString().c_str());
376 }
377
ResolutionSwitchCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)378 ResolutionSwitchCommand::ResolutionSwitchCommand(CommandType commandType,
379 const Json::Value& arg,
380 const LocalSocket& socket)
381 : CommandLine(commandType, arg, socket)
382 {
383 }
384
IsSetArgValid() const385 bool ResolutionSwitchCommand::IsSetArgValid() const
386 {
387 if (args.isNull() || !args.isMember("originWidth") || !args.isMember("originHeight") || !args.isMember("width") ||
388 !args.isMember("height") || !args.isMember("screenDensity")) {
389 ELOG("Invalid param of arguments!");
390 return false;
391 }
392 if (!args["originWidth"].isInt() || !args["originHeight"].isInt() ||
393 !args["screenDensity"].isInt() || !args["width"].isInt() || !args["height"].isInt()) {
394 ELOG("Invalid number of arguments!");
395 return false;
396 }
397 if (!IsIntValValid(args)) {
398 return false;
399 }
400 return true;
401 }
402
IsIntValValid(const Json::Value & args) const403 bool ResolutionSwitchCommand::IsIntValValid(const Json::Value& args) const
404 {
405 if (args["originWidth"].asInt() < minWidth || args["originWidth"].asInt() > maxWidth ||
406 args["originHeight"].asInt() < minWidth || args["originHeight"].asInt() > maxWidth ||
407 args["width"].asInt() < minWidth || args["width"].asInt() > maxWidth ||
408 args["height"].asInt() < minWidth || args["height"].asInt() > maxWidth) {
409 ELOG("width or height is out of range %d-%d", minWidth, maxWidth);
410 return false;
411 }
412 if (args["screenDensity"].asInt() < minDpi || args["screenDensity"].asInt() > maxDpi) {
413 ELOG("screenDensity is out of range %d-%d", minDpi, maxDpi);
414 return false;
415 }
416 return true;
417 }
418
RunSet()419 void ResolutionSwitchCommand::RunSet()
420 {
421 int32_t originWidth = args["originWidth"].asInt();
422 int32_t originHeight = args["originHeight"].asInt();
423 int32_t width = args["width"].asInt();
424 int32_t height = args["height"].asInt();
425 int32_t screenDensity = args["screenDensity"].asInt();
426 JsAppImpl::GetInstance().ResolutionChanged(originWidth, originHeight, width, height, screenDensity);
427 SetCommandResult("result", true);
428 ILOG("ResolutionSwitch run finished.");
429 }
430
OrientationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)431 OrientationCommand::OrientationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
432 : CommandLine(commandType, arg, socket)
433 {
434 }
435
IsSetArgValid() const436 bool OrientationCommand::IsSetArgValid() const
437 {
438 if (args.isNull() || !args.isMember("Orientation") || !args["Orientation"].isString()) {
439 ELOG("Invalid number of arguments!");
440 return false;
441 }
442 if (args["Orientation"].asString() != "portrait" && args["Orientation"].asString() != "landscape") {
443 ELOG("Orientation just support [portrait,landscape].");
444 return false;
445 }
446 return true;
447 }
448
RunSet()449 void OrientationCommand::RunSet()
450 {
451 std::string commandOrientation = args["Orientation"].asString();
452 std::string curruntOrientation = JsAppImpl::GetInstance().GetOrientation();
453 if (commandOrientation != curruntOrientation) {
454 JsAppImpl::GetInstance().OrientationChanged(commandOrientation);
455 }
456 SetCommandResult("result", true);
457 ILOG("Set Orientation run finished, Orientation is: %s", args["Orientation"].asString().c_str());
458 }
459
ColorModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)460 ColorModeCommand::ColorModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
461 : CommandLine(commandType, arg, socket)
462 {
463 }
464
IsSetArgValid() const465 bool ColorModeCommand::IsSetArgValid() const
466 {
467 if (args.isNull() || !args.isMember("ColorMode") || !args["ColorMode"].isString()) {
468 ELOG("Invalid number of arguments!");
469 return false;
470 }
471
472 if (args["ColorMode"].asString() != "light" && args["ColorMode"].asString() != "dark") {
473 ELOG("ColorMode just support [light,dark]");
474 return false;
475 }
476 return true;
477 }
478
RunSet()479 void ColorModeCommand::RunSet()
480 {
481 std::string commandColorMode = args["ColorMode"].asString();
482 std::string currentColorMode = JsAppImpl::GetInstance().GetColorMode();
483 if (commandColorMode != currentColorMode) {
484 JsAppImpl::GetInstance().SetArgsColorMode(args["ColorMode"].asString());
485 JsAppImpl::GetInstance().ColorModeChanged(commandColorMode);
486 }
487 SetCommandResult("result", true);
488 ILOG("Set ColorMode run finished, ColorMode is: %s", args["ColorMode"].asString().c_str());
489 }
490
FontSelectCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)491 FontSelectCommand::FontSelectCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
492 : CommandLine(commandType, arg, socket)
493 {
494 }
495
IsSetArgValid() const496 bool FontSelectCommand::IsSetArgValid() const
497 {
498 if (args.isNull() || !args.isMember("FontSelect") || !args["FontSelect"].isBool()) {
499 ELOG("Invalid number of arguments!");
500 return false;
501 }
502 return true;
503 }
504
RunSet()505 void FontSelectCommand::RunSet()
506 {
507 SetCommandResult("result", true);
508 ILOG("FontSelect finished, currentSelect is: %d", args["FontSelect"].asBool());
509 }
510
MemoryRefreshCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)511 MemoryRefreshCommand::MemoryRefreshCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
512 : CommandLine(commandType, arg, socket)
513 {
514 }
515
IsSetArgValid() const516 bool MemoryRefreshCommand::IsSetArgValid() const
517 {
518 if (args.isNull()) {
519 ELOG("Invalid MemoryRefresh of arguments!");
520 return false;
521 }
522 return true;
523 }
524
RunSet()525 void MemoryRefreshCommand::RunSet()
526 {
527 ILOG("MemoryRefreshCommand begin.");
528 bool ret = JsAppImpl::GetInstance().MemoryRefresh(args.toStyledString());
529 SetCommandResult("result", ret);
530 ILOG("MemoryRefresh finished.");
531 }
532
LoadDocumentCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)533 LoadDocumentCommand::LoadDocumentCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
534 : CommandLine(commandType, arg, socket)
535 {
536 }
537
IsSetArgValid() const538 bool LoadDocumentCommand::IsSetArgValid() const
539 {
540 if (args.isNull() || !args.isMember("url") || !args.isMember("className") || !args.isMember("previewParam") ||
541 !args["url"].isString() || !args["className"].isString() || !args["previewParam"].isObject()) {
542 return false;
543 }
544 Json::Value previewParam = args["previewParam"];
545 if (!previewParam["width"].isInt() || !previewParam["height"].isInt() || !previewParam["dpi"].isInt() ||
546 !previewParam["locale"].isString() || !previewParam["colorMode"].isString() ||
547 !previewParam["orientation"].isString() || !previewParam["deviceType"].isString()) {
548 return false;
549 }
550 if (!IsIntValValid(previewParam) || !IsStrValVailid(previewParam)) {
551 return false;
552 }
553 return true;
554 }
555
IsIntValValid(const Json::Value & previewParam) const556 bool LoadDocumentCommand::IsIntValValid(const Json::Value& previewParam) const
557 {
558 int width = previewParam["width"].asInt();
559 int height = previewParam["height"].asInt();
560 int dpi = previewParam["dpi"].asInt();
561 if (width < minLoadDocWidth || width > maxLoadDocWidth || height < minLoadDocWidth ||
562 height > maxLoadDocWidth || dpi < minDpi || dpi > maxDpi) {
563 return false;
564 }
565 return true;
566 }
567
IsStrValVailid(const Json::Value & previewParam) const568 bool LoadDocumentCommand::IsStrValVailid(const Json::Value& previewParam) const
569 {
570 string locale = previewParam["locale"].asString();
571 bool isLiteDevice = JsApp::IsLiteDevice(CommandParser::GetInstance().GetDeviceType());
572 if (isLiteDevice) {
573 if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), locale) ==
574 liteSupportedLanguages.end()) {
575 return false;
576 }
577 } else {
578 if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), locale) ==
579 richSupportedLanguages.end()) {
580 return false;
581 }
582 }
583 if (previewParam["colorMode"].asString() != "light" && previewParam["colorMode"].asString() != "dark") {
584 return false;
585 }
586 if (previewParam["orientation"].asString() != "portrait" &&
587 previewParam["orientation"].asString() != "landscape") {
588 return false;
589 }
590 if (std::find(LoadDocDevs.begin(), LoadDocDevs.end(), previewParam["deviceType"].asString()) ==
591 LoadDocDevs.end()) {
592 return false;
593 }
594 return true;
595 }
596
RunSet()597 void LoadDocumentCommand::RunSet()
598 {
599 VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::START);
600 ILOG("LoadDocumentCommand begin.");
601 std::string pageUrl = args["url"].asString();
602 std::string className = args["className"].asString();
603 Json::Value previewParam = Json::nullValue;
604 if (args.isMember("previewParam")) {
605 previewParam = args["previewParam"];
606 }
607 JsAppImpl::GetInstance().LoadDocument(pageUrl, className, previewParam);
608 VirtualScreenImpl::GetInstance().SetLoadDocFlag(VirtualScreen::LoadDocType::FINISHED);
609 SetCommandResult("result", true);
610 ILOG("LoadDocumentCommand finished.");
611 }
612
ReloadRuntimePageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)613 ReloadRuntimePageCommand::ReloadRuntimePageCommand(CommandType commandType,
614 const Json::Value& arg,
615 const LocalSocket& socket)
616 : CommandLine(commandType, arg, socket)
617 {
618 }
619
IsSetArgValid() const620 bool ReloadRuntimePageCommand::IsSetArgValid() const
621 {
622 if (args.isNull() || !args.isMember("ReloadRuntimePage") || !args["ReloadRuntimePage"].isString()) {
623 ELOG("Invalid number of arguments!");
624 return false;
625 }
626 return true;
627 }
628
RunSet()629 void ReloadRuntimePageCommand::RunSet()
630 {
631 std::string currentPage = args["ReloadRuntimePage"].asString();
632 JsAppImpl::GetInstance().ReloadRuntimePage(currentPage);
633 SetCommandResult("result", true);
634 ILOG("ReloadRuntimePage finished, currentPage is: %s", args["ReloadRuntimePage"].asString().c_str());
635 }
636
CurrentRouterCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)637 CurrentRouterCommand::CurrentRouterCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
638 : CommandLine(commandType, arg, socket)
639 {
640 }
641
RunGet()642 void CurrentRouterCommand::RunGet()
643 {
644 Json::Value resultContent;
645 std::string currentRouter = VirtualScreenImpl::GetInstance().GetCurrentRouter();
646 resultContent["CurrentRouter"] = currentRouter;
647 SetResultToManager("args", resultContent, "CurrentJsRouter");
648 ILOG("Get CurrentRouter run finished.");
649 }
650
LanguageCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)651 LanguageCommand::LanguageCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
652 : CommandLine(commandType, arg, socket)
653 {
654 }
655
IsSetArgValid() const656 bool LanguageCommand::IsSetArgValid() const
657 {
658 if (args.isNull() || !args.isMember("Language") || !args["Language"].isString()) {
659 ELOG("Invalid number of arguments!");
660 return false;
661 }
662
663 CommandParser& cmdParser = CommandParser::GetInstance();
664 string deviceType = cmdParser.GetDeviceType();
665 bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
666 if (isLiteDevice) {
667 if (std::find(liteSupportedLanguages.begin(), liteSupportedLanguages.end(), args["Language"].asString()) ==
668 liteSupportedLanguages.end()) {
669 ELOG("Language not support liteDevice : %s", args["Language"].asString().c_str());
670 return false;
671 }
672 } else {
673 if (std::find(richSupportedLanguages.begin(), richSupportedLanguages.end(), args["Language"].asString()) ==
674 richSupportedLanguages.end()) {
675 ELOG("Language not support richDevice : %s", args["Language"].asString().c_str());
676 return false;
677 }
678 }
679 return true;
680 }
681
RunGet()682 void LanguageCommand::RunGet()
683 {
684 std::string language = SharedData<string>::GetData(SharedDataType::LANGUAGE);
685 Json::Value resultContent;
686 resultContent["Language"] = language;
687 SetCommandResult("result", resultContent);
688 ILOG("Get language run finished.");
689 }
690
RunSet()691 void LanguageCommand::RunSet()
692 {
693 string language(args["Language"].asString());
694 SharedData<string>::SetData(SharedDataType::LANGUAGE, language);
695 SetCommandResult("result", true);
696 ILOG("Set language run finished, language is: %s", language.c_str());
697 }
698
SupportedLanguagesCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)699 SupportedLanguagesCommand::SupportedLanguagesCommand(CommandType commandType,
700 const Json::Value& arg,
701 const LocalSocket& socket)
702 : CommandLine(commandType, arg, socket)
703 {
704 }
705
RunGet()706 void SupportedLanguagesCommand::RunGet()
707 {
708 Json::Value resultContent;
709 Json::Value languageList;
710 string deviceType = CommandParser::GetInstance().GetDeviceType();
711 bool isLiteDevice = JsApp::IsLiteDevice(deviceType);
712 if (!deviceType.empty() && !isLiteDevice) {
713 for (auto iter = richSupportedLanguages.begin(); iter != richSupportedLanguages.end(); iter++) {
714 languageList.append(*iter);
715 }
716 } else {
717 for (auto iter = liteSupportedLanguages.begin(); iter != liteSupportedLanguages.end(); iter++) {
718 languageList.append(*iter);
719 }
720 }
721 resultContent["SupportedLanguages"] = languageList;
722 SetCommandResult("result", resultContent);
723 ILOG("Get supportedLanguages run finished.");
724 }
725
LocationCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)726 LocationCommand::LocationCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
727 : CommandLine(commandType, arg, socket)
728 {
729 }
730
IsSetArgValid() const731 bool LocationCommand::IsSetArgValid() const
732 {
733 if (args.isNull() || !args.isMember("latitude") || !args.isMember("longitude")) {
734 ELOG("Invalid number of arguments!");
735 return false;
736 }
737 string latitude(args["latitude"].asString());
738 string longitude(args["longitude"].asString());
739 regex isDob("^([\\-]*[0-9]{1,}[\\.][0-9]*)$");
740 if (!regex_match(latitude, isDob) || !regex_match(longitude, isDob)) {
741 ELOG("Invalid arguments!");
742 return false;
743 }
744
745 if (!SharedData<double>::IsValid(SharedDataType::LATITUDE, atof(latitude.data()))) {
746 ELOG("LocationCommand invalid latitude value: %f", atof(latitude.data()));
747 return false;
748 }
749
750 if (!SharedData<double>::IsValid(SharedDataType::LONGITUDE, atof(longitude.data()))) {
751 ELOG("LocationCommand invalid longitude value: %f", atof(longitude.data()));
752 return false;
753 }
754 return true;
755 }
756
RunGet()757 void LocationCommand::RunGet()
758 {
759 double longitude = SharedData<double>::GetData(SharedDataType::LONGITUDE);
760 double latitude = SharedData<double>::GetData(SharedDataType::LATITUDE);
761 Json::Value resultContent;
762 resultContent["latitude"] = latitude;
763 resultContent["longitude"] = longitude;
764 SetCommandResult("result", resultContent);
765 ILOG("Get location run finished");
766 }
767
RunSet()768 void LocationCommand::RunSet()
769 {
770 string latitude(args["latitude"].asString());
771 string longitude(args["longitude"].asString());
772 SharedData<double>::SetData(SharedDataType::LONGITUDE, atof(longitude.data()));
773 SharedData<double>::SetData(SharedDataType::LATITUDE, atof(latitude.data()));
774 Json::Value resultContent = true;
775 SetCommandResult("result", resultContent);
776 ILOG("Set location run finished, latitude: %s,longitude: %s", latitude.c_str(), longitude.c_str());
777 }
778
DistributedCommunicationsCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)779 DistributedCommunicationsCommand::DistributedCommunicationsCommand(CommandType commandType,
780 const Json::Value& arg,
781 const LocalSocket& socket)
782 : CommandLine(commandType, arg, socket)
783 {
784 }
785
RunAction()786 void DistributedCommunicationsCommand::RunAction()
787 {
788 MessageInfo info;
789 info.deviceID = args["DeviceId"].asString();
790 info.bundleName = args["bundleName"].asString();
791 info.abilityName = args["abilityName"].asString();
792 info.message = args["message"].asString();
793 VirtualMessageImpl::GetInstance().SendVirtualMessage(info);
794 Json::Value resultContent = true;
795 SetCommandResult("result", resultContent);
796 ILOG("Send distributedCommunications run finished");
797 }
798
IsActionArgValid() const799 bool DistributedCommunicationsCommand::IsActionArgValid() const
800 {
801 if (args.isNull() || !args.isMember("DeviceId") || !args.isMember("bundleName") || !args.isMember("abilityName") ||
802 !args.isMember("message")) {
803 ELOG("Invalid number of arguments!");
804 return false;
805 }
806 if (args["DeviceId"].asString().empty() || args["bundleName"].asString().empty() ||
807 args["abilityName"].asString().empty() || args["message"].asString().empty()) {
808 ELOG("Invalid arguments!");
809 return false;
810 }
811 return true;
812 }
813
StringToCharVector(string str) const814 std::vector<char> DistributedCommunicationsCommand::StringToCharVector(string str) const
815 {
816 vector<char> vec(str.begin(), str.end());
817 vec.push_back('\0');
818 return vec;
819 }
820
KeepScreenOnStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)821 KeepScreenOnStateCommand::KeepScreenOnStateCommand(CommandType commandType,
822 const Json::Value& arg,
823 const LocalSocket& socket)
824 : CommandLine(commandType, arg, socket)
825 {
826 }
827
RunGet()828 void KeepScreenOnStateCommand::RunGet()
829 {
830 Json::Value result;
831 result["KeepScreenOnState"] = SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON);
832 SetCommandResult("result", result);
833 ILOG("Get keepScreenOnState run finished");
834 }
835
RunSet()836 void KeepScreenOnStateCommand::RunSet()
837 {
838 SharedData<bool>::SetData(SharedDataType::KEEP_SCREEN_ON, args["KeepScreenOnState"].asString() == "true");
839 Json::Value result = true;
840 SetCommandResult("result", result);
841 ILOG("Set keepScreenOnState run finished, the value is: %s", args["KeepScreenOnState"].asString().c_str());
842 }
843
IsSetArgValid() const844 bool KeepScreenOnStateCommand::IsSetArgValid() const
845 {
846 if (args.isNull() || !args.isMember("KeepScreenOnState")) {
847 ELOG("Invalid number of arguments!");
848 return false;
849 }
850 if (!IsBoolType(args["KeepScreenOnState"].asString())) {
851 ELOG("arg 0: %s", args["KeepScreenOnState"].asString().c_str());
852 return false;
853 }
854 return true;
855 }
856
WearingStateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)857 WearingStateCommand::WearingStateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
858 : CommandLine(commandType, arg, socket)
859 {
860 }
861
RunGet()862 void WearingStateCommand::RunGet()
863 {
864 Json::Value result;
865 result["WearingState"] = SharedData<bool>::GetData(SharedDataType::WEARING_STATE);
866 SetCommandResult("result", result);
867 ILOG("Get wearingState run finished");
868 }
869
RunSet()870 void WearingStateCommand::RunSet()
871 {
872 SharedData<bool>::SetData(SharedDataType::WEARING_STATE, args["WearingState"].asString() == "true");
873 Json::Value result = true;
874 SetCommandResult("result", result);
875 ILOG("Set wearingState run finished, the value is: %s", args["WearingState"].asString().c_str());
876 }
877
IsSetArgValid() const878 bool WearingStateCommand::IsSetArgValid() const
879 {
880 if (args.isNull() || !args.isMember("WearingState")) {
881 ELOG("Invalid number of arguments!");
882 return false;
883 }
884 if (!IsBoolType(args["WearingState"].asString())) {
885 ILOG("arg 0: %s", args["WearingState"].asString().c_str());
886 return false;
887 }
888 return true;
889 }
890
BrightnessModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)891 BrightnessModeCommand::BrightnessModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
892 : CommandLine(commandType, arg, socket)
893 {
894 }
895
RunGet()896 void BrightnessModeCommand::RunGet()
897 {
898 Json::Value result;
899 result["BrightnessMode"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_MODE);
900 SetCommandResult("result", result);
901 ILOG("Get brightnessMode run finished");
902 }
903
RunSet()904 void BrightnessModeCommand::RunSet()
905 {
906 SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_MODE,
907 static_cast<uint8_t>(atoi(args["BrightnessMode"].asString().data())));
908 Json::Value result = true;
909 SetCommandResult("result", result);
910 ILOG("Set brightnessMode run finished, the value is: %s", args["BrightnessMode"].asString().c_str());
911 }
912
IsSetArgValid() const913 bool BrightnessModeCommand::IsSetArgValid() const
914 {
915 if (args.isNull() || !args.isMember("BrightnessMode")) {
916 ELOG("Invalid number of arguments!");
917 return false;
918 }
919 if (!std::regex_match(args["BrightnessMode"].asString(), std::regex("^\\d$"))) {
920 ELOG("regex match error");
921 return false;
922 }
923 uint8_t temp = ToUint8(args["BrightnessMode"].asString());
924 if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_MODE, temp)) {
925 ELOG("BrightnessModeCommand invalid value: %d", temp);
926 return false;
927 }
928 return true;
929 }
930
ChargeModeCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)931 ChargeModeCommand::ChargeModeCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
932 : CommandLine(commandType, arg, socket)
933 {
934 }
935
RunGet()936 void ChargeModeCommand::RunGet()
937 {
938 Json::Value result;
939 result["ChargeMode"] = SharedData<uint8_t>::GetData(SharedDataType::BATTERY_STATUS);
940 SetCommandResult("result", result);
941 ILOG("Get chargeMode run finished");
942 }
943
RunSet()944 void ChargeModeCommand::RunSet()
945 {
946 SharedData<uint8_t>::SetData(SharedDataType::BATTERY_STATUS,
947 static_cast<uint8_t>(atoi(args["ChargeMode"].asString().data())));
948 Json::Value result = true;
949 SetCommandResult("result", result);
950 ILOG("Set chargeMode run finished, the value is: %s", args["ChargeMode"].asString().c_str());
951 }
952
IsSetArgValid() const953 bool ChargeModeCommand::IsSetArgValid() const
954 {
955 if (args.isNull() || !args.isMember("ChargeMode")) {
956 ELOG("Invalid number of arguments!");
957 return false;
958 }
959 if (!std::regex_match(args["ChargeMode"].asString().data(), std::regex("\\d"))) {
960 ELOG("Invalid arguments!");
961 return false;
962 }
963 uint8_t temp = ToUint8(args["ChargeMode"].asString());
964 if (!SharedData<uint8_t>::IsValid(SharedDataType::BATTERY_STATUS, temp)) {
965 ELOG("ChargeModeCommand invalid value: %d", temp);
966 return false;
967 }
968 return true;
969 }
970
BrightnessCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)971 BrightnessCommand::BrightnessCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
972 : CommandLine(commandType, arg, socket)
973 {
974 }
975
RunGet()976 void BrightnessCommand::RunGet()
977 {
978 Json::Value result;
979 result["Brightness"] = SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_VALUE);
980 SetCommandResult("result", result);
981 ILOG("Get brightness run finished");
982 }
983
RunSet()984 void BrightnessCommand::RunSet()
985 {
986 SharedData<uint8_t>::SetData(SharedDataType::BRIGHTNESS_VALUE,
987 static_cast<uint8_t>(atoi(args["Brightness"].asString().data())));
988 Json::Value result = true;
989 SetCommandResult("result", result);
990 ILOG("Set brightness run finished, the value is: %s", args["Brightness"].asString().c_str());
991 }
992
IsSetArgValid() const993 bool BrightnessCommand::IsSetArgValid() const
994 {
995 if (args.isNull() || !args.isMember("Brightness")) {
996 ELOG("Invalid number of arguments!");
997 return false;
998 }
999 if (!std::regex_match(args["Brightness"].asString().data(), std::regex("\\d+"))) {
1000 ELOG("Invalid arguments!");
1001 return false;
1002 }
1003 uint8_t temp = ToUint8(args["Brightness"].asString());
1004 if (!SharedData<uint8_t>::IsValid(SharedDataType::BRIGHTNESS_VALUE, temp)) {
1005 ELOG("BrightnessCommand invalid value: ", temp);
1006 return false;
1007 }
1008 return true;
1009 }
1010
HeartRateCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1011 HeartRateCommand::HeartRateCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1012 : CommandLine(commandType, arg, socket)
1013 {
1014 }
1015
RunGet()1016 void HeartRateCommand::RunGet()
1017 {
1018 Json::Value result;
1019 result["HeartRate"] = SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE);
1020 SetCommandResult("result", result);
1021 ILOG("Get heartRate run finished");
1022 }
1023
RunSet()1024 void HeartRateCommand::RunSet()
1025 {
1026 SharedData<uint8_t>::SetData(SharedDataType::HEARTBEAT_VALUE,
1027 static_cast<uint8_t>(atoi(args["HeartRate"].asString().data())));
1028 Json::Value result = true;
1029 SetCommandResult("result", result);
1030 ILOG("Set heartRate run finished, the value is: %s", args["HeartRate"].asString().c_str());
1031 }
1032
IsSetArgValid() const1033 bool HeartRateCommand::IsSetArgValid() const
1034 {
1035 if (args.isNull() || !args.isMember("HeartRate")) {
1036 ELOG("Invalid number of arguments!");
1037 return false;
1038 }
1039 if (!std::regex_match(args["HeartRate"].asString().data(), std::regex("\\d+"))) {
1040 ELOG("Invalid arguments!");
1041 return false;
1042 }
1043 if (atoi(args["HeartRate"].asString().data()) > UINT8_MAX) {
1044 ELOG("Invalid arguments!");
1045 return false;
1046 }
1047 uint8_t temp = ToUint8(args["HeartRate"].asString());
1048 if (!SharedData<uint8_t>::IsValid(SharedDataType::HEARTBEAT_VALUE, temp)) {
1049 ELOG("HeartRateCommand invalid value: %d", temp);
1050 return false;
1051 }
1052 return true;
1053 }
1054
StepCountCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1055 StepCountCommand::StepCountCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1056 : CommandLine(commandType, arg, socket)
1057 {
1058 }
1059
RunGet()1060 void StepCountCommand::RunGet()
1061 {
1062 Json::Value result;
1063 result["StepCount"] = SharedData<uint32_t>::GetData(SharedDataType::SUMSTEP_VALUE);
1064 SetCommandResult("result", result);
1065 ILOG("Get stepCount run finished");
1066 }
1067
RunSet()1068 void StepCountCommand::RunSet()
1069 {
1070 SharedData<uint32_t>::SetData(SharedDataType::SUMSTEP_VALUE,
1071 static_cast<uint32_t>(atoi(args["StepCount"].asString().data())));
1072 Json::Value result = true;
1073 SetCommandResult("result", result);
1074 ILOG("Set stepCount run finished, the value is: %s", args["StepCount"].asString().c_str());
1075 }
1076
IsSetArgValid() const1077 bool StepCountCommand::IsSetArgValid() const
1078 {
1079 if (args.isNull() || !args.isMember("StepCount")) {
1080 ELOG("Invalid number of arguments!");
1081 return false;
1082 }
1083 if (!std::regex_match(args["StepCount"].asString().data(), std::regex("\\d+"))) {
1084 ELOG("Invalid arguments!");
1085 return false;
1086 }
1087
1088 uint32_t temp = ToUint8(args["StepCount"].asString());
1089 if (!SharedData<uint32_t>::IsValid(SharedDataType::SUMSTEP_VALUE, temp)) {
1090 ELOG("StepCountCommand invalid value: %d", temp);
1091 return false;
1092 }
1093 return true;
1094 }
1095
InspectorJSONTree(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1096 InspectorJSONTree::InspectorJSONTree(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1097 : CommandLine(commandType, arg, socket)
1098 {
1099 }
1100
RunAction()1101 void InspectorJSONTree::RunAction()
1102 {
1103 ILOG("GetJsonTree run!");
1104 std::string str = JsAppImpl::GetInstance().GetJSONTree();
1105 if (str == "null") {
1106 str = "{\"children\":\"empty json tree\"}";
1107 }
1108 SetCommandResult("result", str);
1109 ILOG("SendJsonTree end!");
1110 }
1111
InspectorDefault(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1112 InspectorDefault::InspectorDefault(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1113 : CommandLine(commandType, arg, socket)
1114 {
1115 }
1116
RunAction()1117 void InspectorDefault::RunAction()
1118 {
1119 ILOG("GetDefaultJsonTree run!");
1120 std::string str = JsAppImpl::GetInstance().GetDefaultJSONTree();
1121 SetCommandResult("result", str);
1122 ILOG("SendDefaultJsonTree end!");
1123 }
1124
ExitCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1125 ExitCommand::ExitCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1126 : CommandLine(commandType, arg, socket)
1127 {
1128 }
1129
RunAction()1130 void ExitCommand::RunAction()
1131 {
1132 ILOG("ExitCommand run.");
1133 Json::Value res = true;
1134 SetCommandResult("result", res);
1135 SendResult();
1136 Interrupter::Interrupt();
1137 ILOG("Ready to exit");
1138 }
1139
DeviceTypeCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1140 DeviceTypeCommand::DeviceTypeCommand(CommandLine::CommandType commandType,
1141 const Json::Value& arg,
1142 const LocalSocket& socket)
1143 : CommandLine(commandType, arg, socket)
1144 {
1145 }
1146
RunSet()1147 void DeviceTypeCommand::RunSet() {}
1148
ResolutionCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1149 ResolutionCommand::ResolutionCommand(CommandLine::CommandType commandType,
1150 const Json::Value& arg,
1151 const LocalSocket& socket)
1152 : CommandLine(commandType, arg, socket)
1153 {
1154 }
1155
RunSet()1156 void ResolutionCommand::RunSet() {}
1157
BackClickedCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1158 BackClickedCommand::BackClickedCommand(CommandLine::CommandType commandType,
1159 const Json::Value& arg,
1160 const LocalSocket& socket)
1161 : CommandLine(commandType, arg, socket)
1162 {
1163 }
1164
RunAction()1165 void BackClickedCommand::RunAction()
1166 {
1167 MouseInputImpl::GetInstance().DispatchOsBackEvent();
1168 ILOG("BackClickCommand run");
1169 Json::Value res = true;
1170 SetCommandResult("result", res);
1171 ILOG("BackClickCommand end");
1172 }
1173
RestartCommand(CommandLine::CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1174 RestartCommand::RestartCommand(CommandLine::CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1175 : CommandLine(commandType, arg, socket)
1176 {
1177 }
1178
RunAction()1179 void RestartCommand::RunAction()
1180 {
1181 ILOG("RestartCommand start");
1182 JsAppImpl::GetInstance().Restart();
1183 Json::Value res = true;
1184 SetCommandResult("result", res);
1185 ILOG("RestartCommand end");
1186 }
1187
FastPreviewMsgCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1188 FastPreviewMsgCommand::FastPreviewMsgCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1189 : CommandLine(commandType, arg, socket)
1190 {
1191 }
1192
RunGet()1193 void FastPreviewMsgCommand::RunGet()
1194 {
1195 Json::Value resultContent;
1196 std::string fastPreviewMsg = VirtualScreenImpl::GetInstance().GetFastPreviewMsg();
1197 resultContent["FastPreviewMsg"] = fastPreviewMsg;
1198 SetResultToManager("args", resultContent, "MemoryRefresh");
1199 ILOG("Get FastPreviewMsgCommand run finished.");
1200 }
1201
DropFrameCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1202 DropFrameCommand::DropFrameCommand(CommandType commandType, const Json::Value& arg, const LocalSocket& socket)
1203 : CommandLine(commandType, arg, socket)
1204 {
1205 }
1206
IsSetArgValid() const1207 bool DropFrameCommand::IsSetArgValid() const
1208 {
1209 if (args.isNull() || !args.isMember("frequency") || !args["frequency"].isInt()) {
1210 ELOG("Invalid DropFrame of arguments!");
1211 return false;
1212 }
1213 if (args["frequency"].asInt() < 0) {
1214 ELOG("DropFrame param frequency must greater than or equal to 0");
1215 return false;
1216 }
1217 return true;
1218 }
1219
RunSet()1220 void DropFrameCommand::RunSet()
1221 {
1222 ILOG("Set DropFrame frequency start.");
1223 int frequency = args["frequency"].asInt();
1224 VirtualScreenImpl::GetInstance().SetDropFrameFrequency(frequency);
1225 SetCommandResult("result", true);
1226 ILOG("Set DropFrame frequency: %sms.", frequency);
1227 }
1228
IsActionArgValid() const1229 bool KeyPressCommand::IsActionArgValid() const
1230 {
1231 if (args.isNull() || !args.isMember("isInputMethod") || !args["isInputMethod"].isBool()) {
1232 ELOG("Param isInputMethod's value is invalid.");
1233 return false;
1234 }
1235 bool isInputMethod = args["isInputMethod"].asBool();
1236 if (isInputMethod) {
1237 return IsImeArgsValid();
1238 } else {
1239 return IsKeyArgsValid();
1240 }
1241 }
1242
IsImeArgsValid() const1243 bool KeyPressCommand::IsImeArgsValid() const
1244 {
1245 if (!args.isMember("codePoint") || !args["codePoint"].isInt()) {
1246 ELOG("Param codePoint's value is invalid.");
1247 return false;
1248 }
1249 return true;
1250 }
1251
IsKeyArgsValid() const1252 bool KeyPressCommand::IsKeyArgsValid() const
1253 {
1254 if (!args.isMember("keyCode") || !args["keyCode"].isInt() || !args["keyAction"].isInt() ||
1255 !args.isMember("keyAction") || !args["keyAction"].isInt() ||
1256 !args.isMember("pressedCodes") || !args["pressedCodes"].isArray() ||
1257 args["pressedCodes"].size() < 1 || (args.isMember("keyString") && !args["keyString"].isString())) {
1258 ELOG("Param keyEvent's value is invalid.");
1259 return false;
1260 }
1261 if (!args.isMember("keyString")) {
1262 ILOG("Param keyString is lost, it will be empty string.");
1263 }
1264 if (args["keyAction"].asInt() < minActionVal || args["keyAction"].asInt() > maxActionVal) {
1265 ELOG("Param keyAction's value is invalid,value range %d-%d.", minActionVal, maxActionVal);
1266 return false;
1267 }
1268 int keyCode = args["keyCode"].asInt();
1269 if (keyCode > maxKeyVal || keyCode < minKeyVal) {
1270 ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1271 return false;
1272 }
1273 Json::Value arrayNum = args["pressedCodes"];
1274 for (unsigned int i = 0; i < arrayNum.size(); i++) {
1275 if (!arrayNum[i].isInt()) {
1276 ELOG("Param pressedCodes's value is invalid.");
1277 return false;
1278 }
1279 int pressedCode = arrayNum[i].asInt();
1280 if (pressedCode > maxKeyVal || pressedCode < minKeyVal) {
1281 ELOG("Param pressedCode value is invalid,value range %d-%d.", minKeyVal, maxKeyVal);
1282 return false;
1283 }
1284 }
1285 return true;
1286 }
1287
KeyPressCommand(CommandType commandType,const Json::Value & arg,const LocalSocket & socket)1288 KeyPressCommand::KeyPressCommand(CommandType commandType, const Json::Value& arg,
1289 const LocalSocket& socket)
1290 : CommandLine(commandType, arg, socket)
1291 {
1292 }
1293
RunAction()1294 void KeyPressCommand::RunAction()
1295 {
1296 if (CommandParser::GetInstance().GetScreenMode() == CommandParser::ScreenMode::STATIC) {
1297 return;
1298 }
1299 bool isInputMethod = args["isInputMethod"].asBool();
1300 if (isInputMethod) {
1301 VirtualScreen::inputMethodCountPerMinute++;
1302 unsigned int codePoint = args["codePoint"].asInt();
1303 KeyInputImpl::GetInstance().SetCodePoint(codePoint);
1304 KeyInputImpl::GetInstance().DispatchOsInputMethodEvent();
1305 } else {
1306 VirtualScreen::inputKeyCountPerMinute++;
1307 int32_t keyCode = args["keyCode"].asInt();
1308 int32_t keyAction = args["keyAction"].asInt();
1309 Json::Value pressedCodes = args["pressedCodes"];
1310 vector<int32_t> pressedCodesVec;
1311 for (unsigned int i = 0; i < pressedCodes.size(); i++) {
1312 pressedCodesVec.push_back(pressedCodes[i].asInt());
1313 }
1314 string keyString = "";
1315 if (args.isMember("keyString") && args["keyString"].isString()) {
1316 keyString = args["keyString"].asString();
1317 }
1318 KeyInputImpl::GetInstance().SetKeyEvent(keyCode, keyAction, pressedCodesVec, keyString);
1319 KeyInputImpl::GetInstance().DispatchOsKeyEvent();
1320 }
1321 SetCommandResult("result", true);
1322 ILOG("KeyPressCommand run finished.");
1323 }