@@ -30,140 +30,117 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
3030 {" get_who_am_i" , CommandType::GET_WHO_AM_I },
3131};
3232
33- std::function<CommandResult()> CommandManager::createCommand (const CommandType type, const nlohmann::json & json) const
33+ std::function<CommandResult()> CommandManager::createCommand (const CommandType type, const nlohmann::json& json) const
3434{
35- switch (type)
36- {
37- case CommandType::PING :
38- return {PingCommand};
39- case CommandType::PAUSE :
40- return [json]
41- { return PauseCommand (json); };
42- case CommandType::UPDATE_OTA_CREDENTIALS :
43- return [this , json]
44- { return updateOTACredentialsCommand (this ->registry , json); };
45- case CommandType::SET_WIFI :
46- return [this , json]
47- { return setWiFiCommand (this ->registry , json); };
48- case CommandType::UPDATE_WIFI :
49- return [this , json]
50- { return updateWiFiCommand (this ->registry , json); };
51- case CommandType::UPDATE_AP_WIFI :
52- return [this , json]
53- { return updateAPWiFiCommand (this ->registry , json); };
54- case CommandType::DELETE_NETWORK :
55- return [this , json]
56- { return deleteWiFiCommand (this ->registry , json); };
57- case CommandType::SET_MDNS :
58- return [this , json]
59- { return setMDNSCommand (this ->registry , json); };
60- case CommandType::GET_MDNS_NAME :
61- return [this ]
62- { return getMDNSNameCommand (this ->registry ); };
63- case CommandType::UPDATE_CAMERA :
64- return [this , json]
65- { return updateCameraCommand (this ->registry , json); };
66- case CommandType::GET_CONFIG :
67- return [this ]
68- { return getConfigCommand (this ->registry ); };
69- case CommandType::SAVE_CONFIG :
70- return [this ]
71- { return saveConfigCommand (this ->registry ); };
72- case CommandType::RESET_CONFIG :
73- return [this , json]
74- { return resetConfigCommand (this ->registry , json); };
75- case CommandType::RESTART_DEVICE :
76- return restartDeviceCommand;
77- case CommandType::SCAN_NETWORKS :
78- return [this , json]
79- { return scanNetworksCommand (this ->registry , json); };
80- case CommandType::START_STREAMING :
81- return startStreamingCommand;
82- case CommandType::GET_WIFI_STATUS :
83- return [this ]
84- { return getWiFiStatusCommand (this ->registry ); };
85- case CommandType::CONNECT_WIFI :
86- return [this ]
87- { return connectWiFiCommand (this ->registry ); };
88- case CommandType::SWITCH_MODE :
89- return [this , json]
90- { return switchModeCommand (this ->registry , json); };
91- case CommandType::GET_DEVICE_MODE :
92- return [this ]
93- { return getDeviceModeCommand (this ->registry ); };
94- case CommandType::SET_LED_DUTY_CYCLE :
95- return [this , json]
96- { return updateLEDDutyCycleCommand (this ->registry , json); };
97- case CommandType::GET_LED_DUTY_CYCLE :
98- return [this ]
99- { return getLEDDutyCycleCommand (this ->registry ); };
100- case CommandType::GET_SERIAL :
101- return [this ]
102- { return getSerialNumberCommand (this ->registry ); };
103- case CommandType::GET_LED_CURRENT :
104- return [this ]
105- { return getLEDCurrentCommand (this ->registry ); };
106- case CommandType::GET_BATTERY_STATUS :
107- return [this ]
108- { return getBatteryStatusCommand (this ->registry ); };
109- case CommandType::GET_WHO_AM_I :
110- return [this ]
111- { return getInfoCommand (this ->registry ); };
112- default :
113- return nullptr ;
114- }
35+ switch (type)
36+ {
37+ case CommandType::PING :
38+ return {PingCommand};
39+ case CommandType::PAUSE :
40+ return [json] { return PauseCommand (json); };
41+ case CommandType::UPDATE_OTA_CREDENTIALS :
42+ return [this , json] { return updateOTACredentialsCommand (this ->registry , json); };
43+ case CommandType::SET_WIFI :
44+ return [this , json] { return setWiFiCommand (this ->registry , json); };
45+ case CommandType::UPDATE_WIFI :
46+ return [this , json] { return updateWiFiCommand (this ->registry , json); };
47+ case CommandType::UPDATE_AP_WIFI :
48+ return [this , json] { return updateAPWiFiCommand (this ->registry , json); };
49+ case CommandType::DELETE_NETWORK :
50+ return [this , json] { return deleteWiFiCommand (this ->registry , json); };
51+ case CommandType::SET_MDNS :
52+ return [this , json] { return setMDNSCommand (this ->registry , json); };
53+ case CommandType::GET_MDNS_NAME :
54+ return [this ] { return getMDNSNameCommand (this ->registry ); };
55+ case CommandType::UPDATE_CAMERA :
56+ return [this , json] { return updateCameraCommand (this ->registry , json); };
57+ case CommandType::GET_CONFIG :
58+ return [this ] { return getConfigCommand (this ->registry ); };
59+ case CommandType::SAVE_CONFIG :
60+ return [this ] { return saveConfigCommand (this ->registry ); };
61+ case CommandType::RESET_CONFIG :
62+ return [this , json] { return resetConfigCommand (this ->registry , json); };
63+ case CommandType::RESTART_DEVICE :
64+ return restartDeviceCommand;
65+ case CommandType::SCAN_NETWORKS :
66+ return [this , json] { return scanNetworksCommand (this ->registry , json); };
67+ case CommandType::START_STREAMING :
68+ return startStreamingCommand;
69+ case CommandType::GET_WIFI_STATUS :
70+ return [this ] { return getWiFiStatusCommand (this ->registry ); };
71+ case CommandType::CONNECT_WIFI :
72+ return [this ] { return connectWiFiCommand (this ->registry ); };
73+ case CommandType::SWITCH_MODE :
74+ return [this , json] { return switchModeCommand (this ->registry , json); };
75+ case CommandType::GET_DEVICE_MODE :
76+ return [this ] { return getDeviceModeCommand (this ->registry ); };
77+ case CommandType::SET_LED_DUTY_CYCLE :
78+ return [this , json] { return updateLEDDutyCycleCommand (this ->registry , json); };
79+ case CommandType::GET_LED_DUTY_CYCLE :
80+ return [this ] { return getLEDDutyCycleCommand (this ->registry ); };
81+ case CommandType::GET_SERIAL :
82+ return [this ] { return getSerialNumberCommand (this ->registry ); };
83+ case CommandType::GET_LED_CURRENT :
84+ return [this ] { return getLEDCurrentCommand (this ->registry ); };
85+ case CommandType::GET_BATTERY_STATUS :
86+ return [this ] { return getBatteryStatusCommand (this ->registry ); };
87+ case CommandType::GET_WHO_AM_I :
88+ return [this ] { return getInfoCommand (this ->registry ); };
89+ default :
90+ return nullptr ;
91+ }
11592}
11693
11794CommandManagerResponse CommandManager::executeFromJson (const std::string_view json) const
11895{
119- if (!nlohmann::json::accept (json))
120- {
121- return CommandManagerResponse (nlohmann::json{{" error" , " Initial JSON Parse - Invalid JSON" }});
122- }
123-
124- nlohmann::json parsedJson = nlohmann::json::parse (json);
125- if (!parsedJson.contains (" commands" ) || !parsedJson[" commands" ].is_array () || parsedJson[" commands" ].empty ())
126- {
127- return CommandManagerResponse (CommandResult::getErrorResult (" Commands missing" ));
128- }
129-
130- nlohmann::json results = nlohmann::json::array ();
131-
132- for (auto &commandObject : parsedJson[" commands" ].items ())
133- {
134- auto commandData = commandObject.value ();
135- if (!commandData.contains (" command" ))
96+ if (!nlohmann::json::accept (json))
13697 {
137- return CommandManagerResponse ({{" command " , " Unknown command " }, { " error " , " Missing command type " }});
98+ return CommandManagerResponse (nlohmann::json {{" error " , " Initial JSON Parse - Invalid JSON " }});
13899 }
139100
140- const auto commandName = commandData[ " command " ]. get <std::string>( );
141- if (!commandTypeMap .contains (commandName ))
101+ nlohmann::json parsedJson = nlohmann::json::parse (json );
102+ if (!parsedJson .contains (" commands " ) || !parsedJson[ " commands " ]. is_array () || parsedJson[ " commands " ]. empty ( ))
142103 {
143- return CommandManagerResponse ({{ " command " , commandName}, { " error " , " Unknown command " }} );
104+ return CommandManagerResponse (CommandResult::getErrorResult ( " Commands missing " ) );
144105 }
145106
146- const auto commandType = commandTypeMap.at (commandName);
147- const auto commandPayload = commandData.contains (" data" ) ? commandData[" data" ] : nlohmann::json::object ();
107+ nlohmann::json results = nlohmann::json::array ();
108+
109+ for (auto & commandObject : parsedJson[" commands" ].items ())
110+ {
111+ auto commandData = commandObject.value ();
112+ if (!commandData.contains (" command" ))
113+ {
114+ return CommandManagerResponse ({{" command" , " Unknown command" }, {" error" , " Missing command type" }});
115+ }
116+
117+ const auto commandName = commandData[" command" ].get <std::string>();
118+ if (!commandTypeMap.contains (commandName))
119+ {
120+ return CommandManagerResponse ({{" command" , commandName}, {" error" , " Unknown command" }});
121+ }
122+
123+ const auto commandType = commandTypeMap.at (commandName);
124+ const auto commandPayload = commandData.contains (" data" ) ? commandData[" data" ] : nlohmann::json::object ();
148125
149- auto command = createCommand (commandType, commandPayload);
150- results.push_back ({
151- {" command" , commandName},
152- {" result" , command ()},
153- });
154- }
155- auto response = nlohmann::json{{" results" , results}};
156- return CommandManagerResponse (response);
126+ auto command = createCommand (commandType, commandPayload);
127+ results.push_back ({
128+ {" command" , commandName},
129+ {" result" , command ()},
130+ });
131+ }
132+ auto response = nlohmann::json{{" results" , results}};
133+ return CommandManagerResponse (response);
157134}
158135
159136CommandManagerResponse CommandManager::executeFromType (const CommandType type, const std::string_view json) const
160137{
161- const auto command = createCommand (type, json);
138+ const auto command = createCommand (type, json);
162139
163- if (command == nullptr )
164- {
165- return CommandManagerResponse ({{" command" , type}, {" error" , " Unknown command" }});
166- }
140+ if (command == nullptr )
141+ {
142+ return CommandManagerResponse ({{" command" , type}, {" error" , " Unknown command" }});
143+ }
167144
168- return CommandManagerResponse (nlohmann::json{{" result" , command ()}});
145+ return CommandManagerResponse (nlohmann::json{{" result" , command ()}});
169146}
0 commit comments