From 6241cf9addf3cf061506f6b6d8000c1e61640577 Mon Sep 17 00:00:00 2001 From: Lorow Date: Sat, 17 Aug 2024 22:57:29 +0200 Subject: [PATCH] simplify and document handleBatchCommands --- .../data/CommandManager/CommandManager.cpp | 31 +++++++++++++------ .../data/CommandManager/CommandManager.hpp | 3 +- ESP/lib/src/io/Serial/SerialManager.cpp | 22 ++++++------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/ESP/lib/src/data/CommandManager/CommandManager.cpp b/ESP/lib/src/data/CommandManager/CommandManager.cpp index ac0f1a5..e4ffc87 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.cpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.cpp @@ -29,11 +29,11 @@ bool CommandManager::hasDataField(JsonVariant& command) { return command.containsKey("data"); } -std::variant, CommandResult> -CommandManager::handleBatchCommands(CommandsPayload commandsPayload) { - std::vector results = {}; - std::vector errors = {}; +CommandResult CommandManager::handleBatchCommands( + CommandsPayload commandsPayload) { std::vector> commands; + std::vector results = {}; + std::vector errors = {}; if (!commandsPayload.data.containsKey("commands")) { std::string error = "Json data sent not supported, lacks commands field"; @@ -41,10 +41,15 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) { return CommandResult::getErrorResult(error); } + // we first try to create a command based on the payload + // if it's not supported, we register that as an error + // then, we try to validate the command, if it's succeful + // we add it to the list of commands to execute + // otherwise - you guessed it, error + // we only execute them if no errors were registered for (JsonVariant commandData : commandsPayload.data["commands"].as()) { auto command_or_result = this->createCommandFromJsonVariant(commandData); - if (auto command_ptr = std::get_if>(&command_or_result)) { auto validation_result = (*command_ptr)->validate(); @@ -60,16 +65,24 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) { } // if we have any errors, consolidate them into a single message and return - if (errors.size() > 0) { + if (errors.size() > 0) return CommandResult::getErrorResult( Helpers::format_string("\"[%s]\"", this->join_strings(errors, ", "))); - } for (auto& valid_command : commands) { - results.push_back(valid_command->execute()); + auto result = valid_command->execute(); + if (result.isSuccess()) { + results.push_back(result.getSuccessMessage()); + } else { + // since we're executing them already, and we've encountered an error + // we should add it to regular results + results.push_back(result.getErrorMessage()); + } } - return results; + return CommandResult::getErrorResult( + Helpers::format_string("\"[%s]\"", this->join_strings(results, ", "))); + ; } CommandResult CommandManager::handleSingleCommand( diff --git a/ESP/lib/src/data/CommandManager/CommandManager.hpp b/ESP/lib/src/data/CommandManager/CommandManager.hpp index 560b5d2..c94ab25 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.hpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.hpp @@ -64,7 +64,6 @@ class CommandManager { : projectConfig(projectConfig) {}; CommandResult handleSingleCommand(CommandsPayload commandsPayload); - std::variant, CommandResult> handleBatchCommands( - CommandsPayload commandsPayload); + CommandResult handleBatchCommands(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 be363c6..172bb44 100644 --- a/ESP/lib/src/io/Serial/SerialManager.cpp +++ b/ESP/lib/src/io/Serial/SerialManager.cpp @@ -71,25 +71,25 @@ void SerialManager::run() { } CommandsPayload commands = {doc}; - auto results = this->commandManager->handleBatchCommands(commands); + CommandResult result = CommandResult::getSuccessResult(""); - if (std::holds_alternative(results)) { - auto error = std::get(results); - Serial.printf("%s \n\r", error.getErrorMessage().c_str()); + if (doc.containsKey("command")) { + result = this->commandManager->handleSingleCommand(commands); } else { - for (auto& result : std::get>(results)) { - Serial.printf("%s \n\r", result.getSuccessMessage().c_str()); - } - // we should only call save on the config when the commands where - // successful, no point otherwise + result = this->commandManager->handleBatchCommands(commands); + } - // also, I'm not really vibing with havin to create - // an entire JsonDocument for this, though it's light + if (result.isSuccess()) { + Serial.printf("%s \n\r", result.getSuccessMessage().c_str()); + + // we also save the config if the commands were successful JsonDocument saveCommanddDoc; saveCommanddDoc["command"] = "save_config"; saveCommanddDoc["data"].to(); CommandsPayload saveCommandPayload = {saveCommanddDoc}; this->commandManager->handleSingleCommand(saveCommandPayload); + } else { + Serial.printf("%s \n\r", result.getErrorMessage().c_str()); } } #ifdef ETVR_EYE_TRACKER_USB_API