xref: /ide/tools/previewer/cli/CommandLine.h (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#ifndef COMMANDLINE_H
17#define COMMANDLINE_H
18
19#include <set>
20#include <vector>
21#include "JsonReader.h"
22#include "LocalSocket.h"
23
24class CommandLine {
25public:
26    enum class CommandType { SET = 0, GET, ACTION, INVALID };
27
28    CommandLine(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
29    virtual ~CommandLine();
30    void CheckAndRun();
31    void SetCommandResult(const std::string& type, const Json2::Value& resultContent);
32    void SetResultToManager(const std::string& type, const Json2::Value& resultContent, const std::string& messageType);
33    void RunAndSendResultToManager();
34    void SendResultToManager();
35    void SendResult();
36    virtual void RunSet() {}
37    bool IsArgValid() const;
38    uint8_t ToUint8(std::string str) const;
39    void SetCommandName(std::string command);
40
41protected:
42    const Json2::Value& args;
43    const LocalSocket& cliSocket;
44    Json2::Value commandResult = JsonReader::CreateObject();
45    Json2::Value commandResultToManager = JsonReader::CreateObject();
46    CommandType type;
47    std::string commandName;
48    const std::vector<std::string> liteSupportedLanguages = {"zh-CN", "en-US"};
49    const std::vector<std::string> richSupportedLanguages = {
50        "zh_CN", "zh_HK", "zh_TW", "en_US", "en_GB", "ar_AE", "bg_BG", "bo_CN", "cs_CZ", "da_DK",
51        "de_DE", "el_GR", "en_PH", "es_ES", "es_LA", "fi_FI", "fr_FR", "he_IL", "hi_IN", "hu_HU",
52        "id_ID", "it_IT", "ja_JP", "kk_KZ", "ms_MY", "nl_NL", "no_NO", "pl_PL", "pt_BR", "pt_PT",
53        "ro_RO", "ru_RU", "sr_RS", "sv_SE", "th_TH", "tr_TR", "ug_CN", "uk_UA", "vi_VN"
54    };
55    const std::vector<std::string> LoadDocDevs = {"phone", "tablet", "wearable", "car", "tv", "2in1", "default"};
56    const int maxWidth = 3000;
57    const int minWidth = 50;
58    const int maxDpi = 640;
59    const int minDpi = 120;
60    const int maxKeyVal = 2119;
61    const int minKeyVal = 2000;
62    const int maxActionVal = 2;
63    const int minActionVal = 0;
64    const int maxLoadDocWidth = 3000;
65    const int minLoadDocWidth = 20;
66
67    virtual bool IsSetArgValid() const
68    {
69        return true;
70    }
71    virtual bool IsGetArgValid() const
72    {
73        return true;
74    }
75    virtual bool IsActionArgValid() const
76    {
77        return true;
78    }
79    virtual void RunGet() {}
80    virtual void RunAction() {}
81
82    bool IsBoolType(std::string arg) const;
83    bool IsIntType(std::string arg) const;
84    bool IsOneDigitFloatType(std::string arg, bool allowNegativeNumber) const;
85
86private:
87    void Run();
88};
89
90class TouchAndMouseCommand {
91protected:
92    struct EventParams {
93        double x;
94        double y;
95        int type;
96        int button;
97        int action;
98        int sourceType;
99        int sourceTool;
100        std::set<int> pressedBtnsVec;
101        std::vector<double> axisVec; // 13 is array size
102        std::string name;
103    };
104    void SetEventParams(EventParams& params);
105};
106
107class TouchPressCommand : public CommandLine, public TouchAndMouseCommand {
108public:
109    TouchPressCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
110    ~TouchPressCommand() override {}
111
112protected:
113    void RunAction() override;
114    bool IsActionArgValid() const override;
115};
116
117class TouchMoveCommand : public CommandLine, public TouchAndMouseCommand {
118public:
119    TouchMoveCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
120    ~TouchMoveCommand() override {}
121
122protected:
123    void RunAction() override;
124    bool IsActionArgValid() const override;
125};
126
127class TouchReleaseCommand : public CommandLine, public TouchAndMouseCommand {
128public:
129    TouchReleaseCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
130    ~TouchReleaseCommand() override {}
131
132protected:
133    void RunAction() override;
134    bool IsActionArgValid() const override;
135};
136
137class MouseWheelCommand : public CommandLine {
138public:
139    MouseWheelCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
140    ~MouseWheelCommand() override {}
141
142protected:
143    void RunAction() override;
144    bool IsActionArgValid() const override;
145};
146
147class BackClickedCommand : public CommandLine {
148public:
149    BackClickedCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
150    ~BackClickedCommand() override {}
151
152protected:
153    void RunAction() override;
154};
155
156class RestartCommand : public CommandLine {
157public:
158    RestartCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
159    ~RestartCommand() override {}
160
161protected:
162    void RunAction() override;
163};
164
165class PowerCommand : public CommandLine {
166public:
167    PowerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
168    ~PowerCommand() override {}
169    void RunSet() override;
170
171protected:
172    void RunGet() override;
173    bool IsSetArgValid() const override;
174};
175
176class VolumeCommand : public CommandLine {
177public:
178    VolumeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
179    ~VolumeCommand() override {}
180    void RunSet() override;
181
182protected:
183    void RunGet() override;
184    bool IsSetArgValid() const override;
185};
186
187class BarometerCommand : public CommandLine {
188public:
189    BarometerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
190    ~BarometerCommand() override {}
191    void RunSet() override;
192
193protected:
194    void RunGet() override;
195    bool IsSetArgValid() const override;
196};
197
198class ResolutionSwitchCommand : public CommandLine {
199public:
200    ResolutionSwitchCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
201    ~ResolutionSwitchCommand() override {}
202    void RunSet() override;
203
204protected:
205    bool IsSetArgValid() const override;
206    bool IsIntValValid() const;
207};
208
209class OrientationCommand : public CommandLine {
210public:
211    OrientationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
212    ~OrientationCommand() override {}
213    void RunSet() override;
214
215protected:
216    bool IsSetArgValid() const override;
217};
218
219class ColorModeCommand : public CommandLine {
220public:
221    ColorModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
222    ~ColorModeCommand() override {}
223    void RunSet() override;
224
225protected:
226    bool IsSetArgValid() const override;
227};
228
229class LanguageCommand : public CommandLine {
230public:
231    LanguageCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
232    ~LanguageCommand() override {}
233    void RunSet() override;
234
235protected:
236    void RunGet() override;
237    bool IsSetArgValid() const override;
238};
239
240class FontSelectCommand : public CommandLine {
241public:
242    FontSelectCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
243    ~FontSelectCommand() override {}
244    void RunSet() override;
245
246protected:
247    bool IsSetArgValid() const override;
248};
249
250class MemoryRefreshCommand : public CommandLine {
251public:
252    MemoryRefreshCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
253    ~MemoryRefreshCommand() override {}
254    void RunSet() override;
255
256protected:
257    bool IsSetArgValid() const override;
258};
259
260class LoadDocumentCommand : public CommandLine {
261public:
262    LoadDocumentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
263    ~LoadDocumentCommand() override {}
264    void RunSet() override;
265
266protected:
267    bool IsSetArgValid() const override;
268    bool IsIntValValid(const Json2::Value& previewParam) const;
269    bool IsStrValVailid(const Json2::Value& previewParam) const;
270};
271
272class ReloadRuntimePageCommand : public CommandLine {
273public:
274    ReloadRuntimePageCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
275    ~ReloadRuntimePageCommand() override {}
276    void RunSet() override;
277
278protected:
279    bool IsSetArgValid() const override;
280};
281
282class CurrentRouterCommand : public CommandLine {
283public:
284    CurrentRouterCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
285    ~CurrentRouterCommand() override {}
286
287protected:
288    void RunGet() override;
289};
290
291class LoadContentCommand : public CommandLine {
292public:
293    LoadContentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
294    ~LoadContentCommand() override {}
295
296protected:
297    void RunGet() override;
298};
299
300class SupportedLanguagesCommand : public CommandLine {
301public:
302    SupportedLanguagesCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
303    ~SupportedLanguagesCommand() override {}
304
305protected:
306    void RunGet() override;
307};
308
309class LocationCommand : public CommandLine {
310public:
311    LocationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
312    ~LocationCommand() override {}
313    void RunSet() override;
314
315protected:
316    void RunGet() override;
317    bool IsSetArgValid() const override;
318};
319
320class DistributedCommunicationsCommand : public CommandLine {
321public:
322    DistributedCommunicationsCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
323    ~DistributedCommunicationsCommand() override {}
324
325protected:
326    void RunAction() override;
327    bool IsActionArgValid() const override;
328    std::vector<char> StringToCharVector(std::string str) const;
329};
330
331class KeepScreenOnStateCommand : public CommandLine {
332public:
333    KeepScreenOnStateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
334    ~KeepScreenOnStateCommand() override {}
335    void RunSet() override;
336
337protected:
338    void RunGet() override;
339    bool IsSetArgValid() const override;
340};
341
342class WearingStateCommand : public CommandLine {
343public:
344    WearingStateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
345    ~WearingStateCommand() override {}
346    void RunSet() override;
347
348protected:
349    void RunGet() override;
350    bool IsSetArgValid() const override;
351};
352
353class BrightnessModeCommand : public CommandLine {
354public:
355    BrightnessModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
356    ~BrightnessModeCommand() override {}
357    void RunSet() override;
358
359protected:
360    void RunGet() override;
361    bool IsSetArgValid() const override;
362};
363
364class ChargeModeCommand : public CommandLine {
365public:
366    ChargeModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
367    ~ChargeModeCommand() override {}
368    void RunSet() override;
369
370protected:
371    void RunGet() override;
372    bool IsSetArgValid() const override;
373};
374
375class BrightnessCommand : public CommandLine {
376public:
377    BrightnessCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
378    ~BrightnessCommand() override {}
379    void RunSet() override;
380
381protected:
382    void RunGet() override;
383    bool IsSetArgValid() const override;
384};
385
386class HeartRateCommand : public CommandLine {
387public:
388    HeartRateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
389    ~HeartRateCommand() override {}
390    void RunSet() override;
391
392protected:
393    void RunGet() override;
394    bool IsSetArgValid() const override;
395};
396
397class StepCountCommand : public CommandLine {
398public:
399    StepCountCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
400    ~StepCountCommand() override {}
401    void RunSet() override;
402
403protected:
404    void RunGet() override;
405    bool IsSetArgValid() const override;
406};
407
408class ExitCommand : public CommandLine {
409public:
410    ExitCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
411    ~ExitCommand() override {}
412
413protected:
414    void RunAction() override;
415};
416
417class InspectorJSONTree : public CommandLine {
418public:
419    InspectorJSONTree(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
420    ~InspectorJSONTree() override {}
421
422protected:
423    void RunAction() override;
424};
425
426class InspectorDefault : public CommandLine {
427public:
428    InspectorDefault(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
429    ~InspectorDefault() override {}
430
431protected:
432    void RunAction() override;
433};
434
435class DeviceTypeCommand : public CommandLine {
436public:
437    DeviceTypeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
438    ~DeviceTypeCommand() override {}
439
440protected:
441    void RunSet() override;
442};
443
444class ResolutionCommand : public CommandLine {
445public:
446    ResolutionCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
447    ~ResolutionCommand() override {}
448
449protected:
450    void RunSet() override;
451};
452
453class FastPreviewMsgCommand : public CommandLine {
454public:
455    FastPreviewMsgCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
456    ~FastPreviewMsgCommand() override {}
457
458protected:
459    void RunGet() override;
460};
461
462class DropFrameCommand : public CommandLine {
463public:
464    DropFrameCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
465    ~DropFrameCommand() override {}
466    void RunSet() override;
467
468protected:
469    bool IsSetArgValid() const override;
470};
471
472class KeyPressCommand : public CommandLine {
473public:
474    KeyPressCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
475    ~KeyPressCommand() override {}
476
477protected:
478    void RunAction() override;
479    bool IsActionArgValid() const override;
480    bool IsImeArgsValid() const;
481    bool IsKeyArgsValid() const;
482};
483
484class PointEventCommand : public CommandLine, public TouchAndMouseCommand {
485public:
486    PointEventCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
487    ~PointEventCommand() override {}
488
489protected:
490    void RunAction() override;
491    bool IsActionArgValid() const override;
492    bool IsArgsExist() const;
493    bool IsArgsValid() const;
494};
495
496class FoldStatusCommand : public CommandLine {
497public:
498    FoldStatusCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
499    ~FoldStatusCommand() override {}
500
501protected:
502    void RunSet() override;
503    bool IsSetArgValid() const override;
504};
505
506class AvoidAreaCommand : public CommandLine {
507public:
508    AvoidAreaCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
509    ~AvoidAreaCommand() override {}
510
511protected:
512    void RunSet() override;
513    bool IsSetArgValid() const override;
514    bool IsObjectValid(const Json2::Value& val) const;
515};
516
517class AvoidAreaChangedCommand : public CommandLine {
518public:
519    AvoidAreaChangedCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
520    ~AvoidAreaChangedCommand() override {}
521
522protected:
523    void RunGet() override;
524};
525#endif // COMMANDLINE_H
526