From c8335d221d98b26d8ee43db720a485a1adba7219 Mon Sep 17 00:00:00 2001 From: Lorow Date: Sun, 2 Jun 2024 18:59:44 +0200 Subject: [PATCH] simplify query implementation --- .../data/CommandManager/CommandManager.cpp | 127 +++++++++--------- .../data/CommandManager/CommandManager.hpp | 42 +++--- .../src/data/QueryManager/QueryManager.cpp | 8 -- .../src/data/QueryManager/QueryManager.hpp | 46 ------- ESP/lib/src/io/Serial/SerialManager.cpp | 16 ++- ESP/lib/src/io/Serial/SerialManager.hpp | 23 +++- 6 files changed, 114 insertions(+), 148 deletions(-) delete mode 100644 ESP/lib/src/data/QueryManager/QueryManager.cpp delete mode 100644 ESP/lib/src/data/QueryManager/QueryManager.hpp diff --git a/ESP/lib/src/data/CommandManager/CommandManager.cpp b/ESP/lib/src/data/CommandManager/CommandManager.cpp index c7fe2e0..02aaeba 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.cpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.cpp @@ -1,84 +1,81 @@ #include "CommandManager.hpp" -CommandManager::CommandManager(ProjectConfig *deviceConfig) : deviceConfig(deviceConfig) {} +CommandManager::CommandManager(ProjectConfig* deviceConfig) + : deviceConfig(deviceConfig) {} - -const CommandType CommandManager::getCommandType(JsonVariant &command){ - if (!command.containsKey("command")) - return CommandType::None; - - if (auto search = commandMap.find(command["command"]); search != commandMap.end()) - return search->second; - +const CommandType CommandManager::getCommandType(JsonVariant& command) { + if (!command.containsKey("command")) return CommandType::None; + + if (auto search = commandMap.find(command["command"]); + search != commandMap.end()) + return search->second; + + return CommandType::None; } -bool CommandManager::hasDataField(JsonVariant &command) { - return command.containsKey("data"); +bool CommandManager::hasDataField(JsonVariant& command) { + return command.containsKey("data"); } -void CommandManager::handleCommands(CommandsPayload commandsPayload){ - if (!commandsPayload.data.containsKey("commands")){ - log_e("Json data sent not supported, lacks commands field"); - return; - } +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); - } + for (JsonVariant commandData : + commandsPayload.data["commands"].as()) { + this->handleCommand(commandData); + } - this->deviceConfig->save(); -} + this->deviceConfig->save(); +} void CommandManager::handleCommand(JsonVariant command) { - auto command_type = this->getCommandType(command); + auto command_type = this->getCommandType(command); - switch(command_type) - { - case CommandType::SET_WIFI: { - if (!this->hasDataField(command)) - // malformed command, lacked data field - break; + switch (command_type) { + case CommandType::SET_WIFI: { + if (!this->hasDataField(command)) + // malformed command, lacked data field + break; - if(!command["data"].containsKey("ssid") || !command["data"].containsKey("password")) - break; + if (!command["data"].containsKey("ssid") || + !command["data"].containsKey("password")) + break; - std::string customNetworkName = "main"; - if (command["data"].containsKey("network_name")) - customNetworkName = command["data"]["network_name"].as(); + std::string customNetworkName = "main"; + if (command["data"].containsKey("network_name")) + customNetworkName = command["data"]["network_name"].as(); - this->deviceConfig->setWifiConfig( - customNetworkName, - command["data"]["ssid"], - command["data"]["password"], - 0, // channel, should this be zero? - 0, // power, should this be zero? - false, - false - ); - - break; - } - case CommandType::SET_MDNS: { - if (!this->hasDataField(command)) - break; + this->deviceConfig->setWifiConfig(customNetworkName, + command["data"]["ssid"], + command["data"]["password"], + 0, // channel, should this be zero? + 0, // power, should this be zero? + false, false); - if(!command["data"].containsKey("hostname") || !strlen(command["data"]["hostname"])) - break; - - this->deviceConfig->setMDNSConfig( - command["data"]["hostname"], - "openiristracker", - false - ); - - break; - } - case CommandType::PING: { - Serial.println("PONG \n\r"); - break; - } - default: - break; + break; } + case CommandType::SET_MDNS: { + if (!this->hasDataField(command)) + break; + + if (!command["data"].containsKey("hostname") || + !strlen(command["data"]["hostname"])) + break; + + this->deviceConfig->setMDNSConfig(command["data"]["hostname"], + "openiristracker", false); + + break; + } + case CommandType::PING: { + Serial.println("PONG \n\r"); + break; + } + default: + break; + } } \ 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 1631e3c..55440f4 100644 --- a/ESP/lib/src/data/CommandManager/CommandManager.hpp +++ b/ESP/lib/src/data/CommandManager/CommandManager.hpp @@ -6,35 +6,33 @@ #include "data/config/project_config.hpp" enum CommandType { - None, - PING, - SET_WIFI, - SET_MDNS, + None, + PING, + SET_WIFI, + SET_MDNS, }; - -struct CommandsPayload { - JsonDocument data; +struct CommandsPayload { + JsonDocument data; }; -class CommandManager{ - -private: -const std::unordered_map commandMap = { - {"ping", CommandType::PING}, - {"set_wifi", CommandType::SET_WIFI}, - {"set_mdns", CommandType::SET_MDNS}, -}; +class CommandManager { + private: + const std::unordered_map commandMap = { + {"ping", CommandType::PING}, + {"set_wifi", CommandType::SET_WIFI}, + {"set_mdns", CommandType::SET_MDNS}, + }; -ProjectConfig* deviceConfig; + ProjectConfig* deviceConfig; -bool hasDataField(JsonVariant &command); -void handleCommand(JsonVariant command); -const CommandType getCommandType(JsonVariant &command); + bool hasDataField(JsonVariant& command); + void handleCommand(JsonVariant command); + const CommandType getCommandType(JsonVariant& command); -public: - CommandManager(ProjectConfig *deviceConfig); - void handleCommands(CommandsPayload commandsPayload); + public: + CommandManager(ProjectConfig* deviceConfig); + void handleCommands(CommandsPayload commandsPayload); }; #endif \ No newline at end of file diff --git a/ESP/lib/src/data/QueryManager/QueryManager.cpp b/ESP/lib/src/data/QueryManager/QueryManager.cpp deleted file mode 100644 index 4f5c77c..0000000 --- a/ESP/lib/src/data/QueryManager/QueryManager.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "QueryManager.hpp" - -std::string QueryManager::formatMessage(QueryPayload* messagePayload) { - return Helpers::format_string( - "{\"action\": \"%s\", \"status\": %d, \"additional_info\": \"%s\"}", - queryActionMap.at(messagePayload->action).c_str(), messagePayload->status, - messagePayload->additionalInfo.c_str()); -} \ No newline at end of file diff --git a/ESP/lib/src/data/QueryManager/QueryManager.hpp b/ESP/lib/src/data/QueryManager/QueryManager.hpp deleted file mode 100644 index 9678ca6..0000000 --- a/ESP/lib/src/data/QueryManager/QueryManager.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once -#ifndef QUERY_MANAGER_HPP -#define QUERY_MANAGER_HPP -#include -#include -#include "data/utilities/helpers.hpp" - -enum QueryAction { - READY_TO_RECEIVE, - PARSE_COMMANDS, - CONNECT_TO_WIFI, -}; - -enum QueryStatus { - NONE, - SUCCESS, - ERROR, -}; - -struct QueryPayload { - QueryAction action; - QueryStatus status; - std::string additionalInfo; - - QueryPayload(QueryAction action, - QueryStatus status, - std::string additionalInfo) { - this->action = action; - this->status = status; - this->additionalInfo = additionalInfo; - } -}; - -class QueryManager { - private: - const std::unordered_map queryActionMap = { - {QueryAction::READY_TO_RECEIVE, "ready_to_receive"}, - {QueryAction::PARSE_COMMANDS, "parse_commands"}, - {QueryAction::CONNECT_TO_WIFI, "connect_to_wifi"}, - }; - - public: - std::string formatMessage(QueryPayload* messagePayload); -}; - -#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 603c478..c9f7c52 100644 --- a/ESP/lib/src/io/Serial/SerialManager.cpp +++ b/ESP/lib/src/io/Serial/SerialManager.cpp @@ -59,13 +59,19 @@ void SerialManager::init() { Serial.flush(); } - QueryPayload* setupFinishedMessage = - new QueryPayload(QueryAction::READY_TO_RECEIVE, QueryStatus::NONE, ""); - this->snedQuery(setupFinishedMessage); + this->sendQuery(QueryAction::READY_TO_RECEIVE, QueryStatus::NONE, ""); } -void SerialManager::snedQuery(QueryPayload* messagePayload) { - Serial.println(this->queryManager->formatMessage(messagePayload).c_str()); +void SerialManager::(QueryAction action, + QueryStatus status, + std::string additional_info) { + JsonDocument doc; + doc["action"] = queryActionMap.at(action); + doc["status"] = status; + doc["additional_info"] = additional_info; + + doc.shrinkToFit(); // optional + serializeJson(doc, Serial); } void SerialManager::run() { diff --git a/ESP/lib/src/io/Serial/SerialManager.hpp b/ESP/lib/src/io/Serial/SerialManager.hpp index aa851a4..34b1584 100644 --- a/ESP/lib/src/io/Serial/SerialManager.hpp +++ b/ESP/lib/src/io/Serial/SerialManager.hpp @@ -13,11 +13,28 @@ const char* const ETVR_HEADER = "\xff\xa0"; const char* const ETVR_HEADER_FRAME = "\xff\xa1"; +enum QueryAction { + READY_TO_RECEIVE, + PARSE_COMMANDS, + CONNECT_TO_WIFI, +}; + +enum QueryStatus { + NONE, + SUCCESS, + ERROR, +}; + +const std::unordered_map queryActionMap = { + {QueryAction::READY_TO_RECEIVE, "ready_to_receive"}, + {QueryAction::PARSE_COMMANDS, "parse_commands"}, + {QueryAction::CONNECT_TO_WIFI, "connect_to_wifi"}, +}; + class SerialManager { private: esp_err_t err = ESP_OK; CommandManager* commandManager; - QueryManager* queryManager; #ifdef ETVR_EYE_TRACKER_USB_API int64_t last_frame = 0; @@ -28,7 +45,9 @@ class SerialManager { public: SerialManager(CommandManager* commandManager); - void snedQuery(QueryPayload* messagePayload); + void sendQuery(QueryAction action, + QueryStatus status, + std::string additional_info); void init(); void run(); };