simplify command result creation, add saving config in serial manager, fix bug in handleSingleCommand

This commit is contained in:
Lorow 2024-08-17 00:05:59 +02:00
parent 48e1e8de17
commit 7bf33d8c45
5 changed files with 57 additions and 23 deletions

View File

@ -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<std::string>());
}
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<std::string>());
}
CommandResult SaveConfigCommand::execute() {

View File

@ -8,14 +8,23 @@
class CommandResult {
private:
// or maybe std::optional?
std::optional<std::string> successMessage;
std::optional<std::string> errorMessage;
public:
CommandResult(std::optional<std::string> successMessage,
std::optional<std::string> errorMessage)
: successMessage(successMessage), errorMessage(errorMessage) {}
CommandResult(std::optional<std::string> success_message,
std::optional<std::string> 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(); }

View File

@ -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<CommandResult>(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<JsonVariant>();
return this->createCommand(command_type, command_data);
}

View File

@ -32,7 +32,7 @@ const std::unordered_map<std::string, CommandType> commandMap = {
{"ping", CommandType::PING},
{"set_wifi", CommandType::SET_WIFI},
{"set_mdns", CommandType::SET_MDNS},
};
{"save_config", CommandType::SAVE_CONFIG}};
class CommandManager {
private:

View File

@ -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<CommandResult>(results)) {
auto error = std::get<CommandResult>(results);
Serial.printf("%s \n\r", error.getErrorMessage().c_str());
} else {
for (auto& result : std::get<std::vector<CommandResult>>(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<JsonObject>();
CommandsPayload saveCommandPayload = {saveCommanddDoc};
this->commandManager->handleSingleCommand(saveCommandPayload);
}
}
#ifdef ETVR_EYE_TRACKER_USB_API
else {