From 7bf33d8c45895b52898fb6c85276a843dd70b6a4 Mon Sep 17 00:00:00 2001 From: Lorow Date: Sat, 17 Aug 2024 00:05:59 +0200 Subject: [PATCH] simplify command result creation, add saving config in serial manager, fix bug in handleSingleCommand --- ESP/lib/src/data/CommandManager/Command.cpp | 13 +++++----- ESP/lib/src/data/CommandManager/Command.hpp | 17 +++++++++--- .../data/CommandManager/CommandManager.cpp | 26 ++++++++++++------- .../data/CommandManager/CommandManager.hpp | 2 +- ESP/lib/src/io/Serial/SerialManager.cpp | 22 ++++++++++++++-- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/ESP/lib/src/data/CommandManager/Command.cpp b/ESP/lib/src/data/CommandManager/Command.cpp index e216656..e5c41a8 100644 --- a/ESP/lib/src/data/CommandManager/Command.cpp +++ b/ESP/lib/src/data/CommandManager/Command.cpp @@ -6,9 +6,9 @@ CommandResult PingCommand::execute() { CommandResult SetWiFiCommand::validate() { if (!data.containsKey("ssid")) - return CommandResult::getErrorResult("{\"error\": \"Missing ssid\"}"); + return CommandResult::getErrorResult("Missing ssid"); if (!data.containsKey("password")) - return CommandResult::getErrorResult("{\"error\": \"Missing password\"}"); + return CommandResult::getErrorResult("Missing password"); return CommandResult::getSuccessResult(""); } @@ -19,20 +19,21 @@ CommandResult SetWiFiCommand::execute() { projectConfig.setWifiConfig(network_name, data["ssid"], data["password"], 0, 0, false, false); - - return CommandResult::getSuccessResult("WIFI SET"); + return CommandResult::getSuccessResult("WIFI Set to: " + + data["ssid"].as()); } CommandResult SetMDNSCommand::validate() { if (!data.containsKey("hostname") || !strlen(data["hostname"])) - return CommandResult::getErrorResult("{\"error\": \"Missing hostname\"}"); + return CommandResult::getErrorResult("Missing hostname"); return CommandResult::getSuccessResult(""); } CommandResult SetMDNSCommand::execute() { projectConfig.setMDNSConfig(data["hostname"], "openiristracker", false); - return CommandResult::getSuccessResult("MDNS SET"); + return CommandResult::getSuccessResult("MDNS set to:" + + data["hostname"].as()); } CommandResult SaveConfigCommand::execute() { diff --git a/ESP/lib/src/data/CommandManager/Command.hpp b/ESP/lib/src/data/CommandManager/Command.hpp index 067cdae..895a2a1 100644 --- a/ESP/lib/src/data/CommandManager/Command.hpp +++ b/ESP/lib/src/data/CommandManager/Command.hpp @@ -8,14 +8,23 @@ class CommandResult { private: - // or maybe std::optional? std::optional successMessage; std::optional errorMessage; public: - CommandResult(std::optional successMessage, - std::optional errorMessage) - : successMessage(successMessage), errorMessage(errorMessage) {} + CommandResult(std::optional success_message, + std::optional error_message) { + if (success_message.has_value()) { + this->successMessage = + "{\"message\":\"" + success_message.value() + "\"}"; + } else + this->successMessage = std::nullopt; + + if (error_message.has_value()) + this->errorMessage = "{\"error\":\"" + error_message.value() + "\"}"; + else + this->errorMessage = std::nullopt; + } bool isSuccess() const { return successMessage.has_value(); } diff --git a/ESP/lib/src/data/CommandManager/CommandManager.cpp b/ESP/lib/src/data/CommandManager/CommandManager.cpp index 1a5677d..ac0f1a5 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.cpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.cpp @@ -38,8 +38,7 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) { if (!commandsPayload.data.containsKey("commands")) { std::string error = "Json data sent not supported, lacks commands field"; log_e("%s", error.c_str()); - return CommandResult::getErrorResult( - Helpers::format_string("\"error\":\"%s\"", error)); + return CommandResult::getErrorResult(error); } for (JsonVariant commandData : @@ -62,8 +61,8 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) { // if we have any errors, consolidate them into a single message and return if (errors.size() > 0) { - return CommandResult::getErrorResult(Helpers::format_string( - "\"error\":\"[%s]\"", this->join_strings(errors, ","))); + return CommandResult::getErrorResult( + Helpers::format_string("\"[%s]\"", this->join_strings(errors, ", "))); } for (auto& valid_command : commands) { @@ -79,11 +78,10 @@ CommandResult CommandManager::handleSingleCommand( std::string error = "Json data sent not supported, lacks commands field"; log_e("%s", error.c_str()); - CommandResult::getErrorResult( - Helpers::format_string("\"error\":\"%s\"", error)); + CommandResult::getErrorResult(error); } - JsonVariant commandData = commandsPayload.data["command"]; + JsonVariant commandData = commandsPayload.data; auto command_or_result = this->createCommandFromJsonVariant(commandData); if (std::holds_alternative(command_or_result)) { @@ -108,8 +106,16 @@ CommandManager::createCommandFromJsonVariant(JsonVariant& command) { std::string error = Helpers::format_string("Command not supported: %s", command["command"]); log_e("%s", error.c_str()); - throw CommandResult::getErrorResult( - Helpers::format_string("\"error\":\"%s\"", error)); + return CommandResult::getErrorResult(error); } - return this->createCommand(command_type, command); + + if (!this->hasDataField(command)) { + std::string error = Helpers::format_string( + "Command is missing data field: %s", command["command"]); + log_e("%s", error.c_str()); + return CommandResult::getErrorResult(error); + } + + auto command_data = command["data"].as(); + return this->createCommand(command_type, command_data); } \ No newline at end of file diff --git a/ESP/lib/src/data/CommandManager/CommandManager.hpp b/ESP/lib/src/data/CommandManager/CommandManager.hpp index c122042..560b5d2 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.hpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.hpp @@ -32,7 +32,7 @@ const std::unordered_map commandMap = { {"ping", CommandType::PING}, {"set_wifi", CommandType::SET_WIFI}, {"set_mdns", CommandType::SET_MDNS}, -}; + {"save_config", CommandType::SAVE_CONFIG}}; class CommandManager { private: diff --git a/ESP/lib/src/io/Serial/SerialManager.cpp b/ESP/lib/src/io/Serial/SerialManager.cpp index f5a1cc5..be363c6 100644 --- a/ESP/lib/src/io/Serial/SerialManager.cpp +++ b/ESP/lib/src/io/Serial/SerialManager.cpp @@ -67,12 +67,30 @@ void SerialManager::run() { if (deserializationError) { log_e("Command deserialization failed: %s", deserializationError.c_str()); - return; } CommandsPayload commands = {doc}; - this->commandManager->handleBatchCommands(commands); + auto results = this->commandManager->handleBatchCommands(commands); + + if (std::holds_alternative(results)) { + auto error = std::get(results); + Serial.printf("%s \n\r", error.getErrorMessage().c_str()); + } 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 + + // also, I'm not really vibing with havin to create + // an entire JsonDocument for this, though it's light + JsonDocument saveCommanddDoc; + saveCommanddDoc["command"] = "save_config"; + saveCommanddDoc["data"].to(); + CommandsPayload saveCommandPayload = {saveCommanddDoc}; + this->commandManager->handleSingleCommand(saveCommandPayload); + } } #ifdef ETVR_EYE_TRACKER_USB_API else {