From a7d4e9a4210cd3aac2d9b4bc9089aa83e6fd874a Mon Sep 17 00:00:00 2001 From: lorow Date: Mon, 1 Apr 2024 22:38:27 +0200 Subject: [PATCH] Refactor commands and serial manager to support updated commands payload --- .../data/CommandManager/CommandManager.cpp | 46 +++++++++++-------- .../data/CommandManager/CommandManager.hpp | 9 ++-- ESP/lib/src/io/Serial/SerialManager.cpp | 6 ++- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/ESP/lib/src/data/CommandManager/CommandManager.cpp b/ESP/lib/src/data/CommandManager/CommandManager.cpp index a7edccc..c7fe2e0 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.cpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.cpp @@ -3,61 +3,71 @@ CommandManager::CommandManager(ProjectConfig *deviceConfig) : deviceConfig(deviceConfig) {} -const CommandType CommandManager::getCommandType(Command &command){ - if (!command.data.containsKey("command")) +const CommandType CommandManager::getCommandType(JsonVariant &command){ + if (!command.containsKey("command")) return CommandType::None; - if (auto search = commandMap.find(command.data["command"]); search != commandMap.end()) + if (auto search = commandMap.find(command["command"]); search != commandMap.end()) return search->second; return CommandType::None; } -bool CommandManager::hasHasDataField(Command &command) { - return command.data.containsKey("data"); +bool CommandManager::hasDataField(JsonVariant &command) { + return command.containsKey("data"); } -void CommandManager::handleCommand(Command command) { +void CommandManager::handleCommands(CommandsPayload commandsPayload){ + if (!commandsPayload.data.containsKey("commands")){ + log_e("Json data sent not supported, lacks commands field"); + return; + } + + for(JsonVariant commandData : commandsPayload.data["commands"].as()) { + this->handleCommand(commandData); + } + + this->deviceConfig->save(); +} + +void CommandManager::handleCommand(JsonVariant command) { auto command_type = this->getCommandType(command); switch(command_type) { case CommandType::SET_WIFI: { - if (!this->hasHasDataField(command)) + if (!this->hasDataField(command)) // malformed command, lacked data field break; - - if(!command.data["data"].containsKey("ssid") || !command.data["data"].containsKey("password")) + if(!command["data"].containsKey("ssid") || !command["data"].containsKey("password")) break; std::string customNetworkName = "main"; - if (command.data["data"].containsKey("network_name")) - customNetworkName = command.data["data"]["network_name"].as(); + if (command["data"].containsKey("network_name")) + customNetworkName = command["data"]["network_name"].as(); this->deviceConfig->setWifiConfig( customNetworkName, - command.data["data"]["ssid"], - command.data["data"]["password"], + command["data"]["ssid"], + command["data"]["password"], 0, // channel, should this be zero? 0, // power, should this be zero? false, false ); - // we purposefully save here - this->deviceConfig->save(); break; } case CommandType::SET_MDNS: { - if (!this->hasHasDataField(command)) + if (!this->hasDataField(command)) break; - if(!command.data["data"].containsKey("hostname") || !strlen(command.data["data"]["hostname"])) + if(!command["data"].containsKey("hostname") || !strlen(command["data"]["hostname"])) break; this->deviceConfig->setMDNSConfig( - command.data["data"]["hostname"], + command["data"]["hostname"], "openiristracker", false ); diff --git a/ESP/lib/src/data/CommandManager/CommandManager.hpp b/ESP/lib/src/data/CommandManager/CommandManager.hpp index 1fbad74..1631e3c 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.hpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.hpp @@ -13,7 +13,7 @@ enum CommandType { }; -struct Command { +struct CommandsPayload { JsonDocument data; }; @@ -28,12 +28,13 @@ const std::unordered_map commandMap = { ProjectConfig* deviceConfig; -bool hasHasDataField(Command &command); +bool hasDataField(JsonVariant &command); +void handleCommand(JsonVariant command); +const CommandType getCommandType(JsonVariant &command); public: CommandManager(ProjectConfig *deviceConfig); - void handleCommand(Command command); - const CommandType getCommandType(Command &command); + void handleCommands(CommandsPayload commandsPayload); }; #endif \ No newline at end of file diff --git a/ESP/lib/src/io/Serial/SerialManager.cpp b/ESP/lib/src/io/Serial/SerialManager.cpp index 6466d3d..70049a6 100644 --- a/ESP/lib/src/io/Serial/SerialManager.cpp +++ b/ESP/lib/src/io/Serial/SerialManager.cpp @@ -67,10 +67,12 @@ void SerialManager::run() { if (deserializationError) { log_e("Command deserialization failed: %s", deserializationError.c_str()); + + return; } - Command command = {doc}; - this->commandManager->handleCommand(command); + CommandsPayload commands = {doc}; + this->commandManager->handleCommands(commands); } #ifdef ETVR_EYE_TRACKER_USB_API else {