Merge branch 'fix/serial-manager-invalid-json' into beta

# Conflicts:
#	ESP/lib/src/io/Serial/SerialManager.cpp
This commit is contained in:
lorow 2024-04-01 22:41:31 +02:00
commit 97d9c3f5e5
3 changed files with 40 additions and 32 deletions

View File

@ -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<JsonArray>()) {
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<std::string>();
if (command["data"].containsKey("network_name"))
customNetworkName = command["data"]["network_name"].as<std::string>();
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
);

View File

@ -13,7 +13,7 @@ enum CommandType {
};
struct Command {
struct CommandsPayload {
JsonDocument data;
};
@ -28,12 +28,13 @@ const std::unordered_map<std::string, CommandType> 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

View File

@ -1,7 +1,7 @@
#include "SerialManager.hpp"
SerialManager::SerialManager(CommandManager* commandManager, ProjectConfig& configManager)
: commandManager(commandManager), configManager(configManager) {}
SerialManager::SerialManager(CommandManager* commandManager)
: commandManager(commandManager) {}
#ifdef ETVR_EYE_TRACKER_USB_API
void SerialManager::send_frame() {
@ -61,21 +61,18 @@ void SerialManager::init() {
void SerialManager::run() {
if (Serial.available()) {
auto serialData = Serial.readString();
JsonDocument doc;
DeserializationError deserializationError = deserializeJson(doc, serialData);
DeserializationError deserializationError = deserializeJson(doc, Serial);
if (deserializationError) {
log_e("Command deserialization failed: %s",
deserializationError.c_str());
return;
}
Command command = {doc};
configManager.setDeviceConfig("", "", serialData.c_str(), 81, true);
configManager.deviceConfigSave();
this->commandManager->handleCommand(command);
configManager.save(); // in case we receive something totally different than supported command, save anyway and restart
CommandsPayload commands = {doc};
this->commandManager->handleCommands(commands);
}
#ifdef ETVR_EYE_TRACKER_USB_API
else {