Refactor commands and serial manager to support updated commands payload

This commit is contained in:
lorow 2024-04-01 22:38:27 +02:00
parent 3fa84dc100
commit a7d4e9a421
3 changed files with 37 additions and 24 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

@ -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 {