mirror of
https://github.com/EyeTrackVR/OpenIris.git
synced 2025-11-04 15:39:42 +08:00
simplify and document handleBatchCommands
This commit is contained in:
parent
7bf33d8c45
commit
6241cf9add
@ -29,11 +29,11 @@ bool CommandManager::hasDataField(JsonVariant& command) {
|
||||
return command.containsKey("data");
|
||||
}
|
||||
|
||||
std::variant<std::vector<CommandResult>, CommandResult>
|
||||
CommandManager::handleBatchCommands(CommandsPayload commandsPayload) {
|
||||
std::vector<CommandResult> results = {};
|
||||
std::vector<std::string> errors = {};
|
||||
CommandResult CommandManager::handleBatchCommands(
|
||||
CommandsPayload commandsPayload) {
|
||||
std::vector<std::unique_ptr<ICommand>> commands;
|
||||
std::vector<std::string> results = {};
|
||||
std::vector<std::string> errors = {};
|
||||
|
||||
if (!commandsPayload.data.containsKey("commands")) {
|
||||
std::string error = "Json data sent not supported, lacks commands field";
|
||||
@ -41,10 +41,15 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) {
|
||||
return CommandResult::getErrorResult(error);
|
||||
}
|
||||
|
||||
// we first try to create a command based on the payload
|
||||
// if it's not supported, we register that as an error
|
||||
// then, we try to validate the command, if it's succeful
|
||||
// we add it to the list of commands to execute
|
||||
// otherwise - you guessed it, error
|
||||
// we only execute them if no errors were registered
|
||||
for (JsonVariant commandData :
|
||||
commandsPayload.data["commands"].as<JsonArray>()) {
|
||||
auto command_or_result = this->createCommandFromJsonVariant(commandData);
|
||||
|
||||
if (auto command_ptr =
|
||||
std::get_if<std::unique_ptr<ICommand>>(&command_or_result)) {
|
||||
auto validation_result = (*command_ptr)->validate();
|
||||
@ -60,16 +65,24 @@ CommandManager::handleBatchCommands(CommandsPayload commandsPayload) {
|
||||
}
|
||||
|
||||
// if we have any errors, consolidate them into a single message and return
|
||||
if (errors.size() > 0) {
|
||||
if (errors.size() > 0)
|
||||
return CommandResult::getErrorResult(
|
||||
Helpers::format_string("\"[%s]\"", this->join_strings(errors, ", ")));
|
||||
}
|
||||
|
||||
for (auto& valid_command : commands) {
|
||||
results.push_back(valid_command->execute());
|
||||
auto result = valid_command->execute();
|
||||
if (result.isSuccess()) {
|
||||
results.push_back(result.getSuccessMessage());
|
||||
} else {
|
||||
// since we're executing them already, and we've encountered an error
|
||||
// we should add it to regular results
|
||||
results.push_back(result.getErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
return CommandResult::getErrorResult(
|
||||
Helpers::format_string("\"[%s]\"", this->join_strings(results, ", ")));
|
||||
;
|
||||
}
|
||||
|
||||
CommandResult CommandManager::handleSingleCommand(
|
||||
|
||||
@ -64,7 +64,6 @@ class CommandManager {
|
||||
: projectConfig(projectConfig) {};
|
||||
|
||||
CommandResult handleSingleCommand(CommandsPayload commandsPayload);
|
||||
std::variant<std::vector<CommandResult>, CommandResult> handleBatchCommands(
|
||||
CommandsPayload commandsPayload);
|
||||
CommandResult handleBatchCommands(CommandsPayload commandsPayload);
|
||||
};
|
||||
#endif
|
||||
@ -71,25 +71,25 @@ void SerialManager::run() {
|
||||
}
|
||||
|
||||
CommandsPayload commands = {doc};
|
||||
auto results = this->commandManager->handleBatchCommands(commands);
|
||||
CommandResult result = CommandResult::getSuccessResult("");
|
||||
|
||||
if (std::holds_alternative<CommandResult>(results)) {
|
||||
auto error = std::get<CommandResult>(results);
|
||||
Serial.printf("%s \n\r", error.getErrorMessage().c_str());
|
||||
if (doc.containsKey("command")) {
|
||||
result = this->commandManager->handleSingleCommand(commands);
|
||||
} 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
|
||||
result = this->commandManager->handleBatchCommands(commands);
|
||||
}
|
||||
|
||||
// also, I'm not really vibing with havin to create
|
||||
// an entire JsonDocument for this, though it's light
|
||||
if (result.isSuccess()) {
|
||||
Serial.printf("%s \n\r", result.getSuccessMessage().c_str());
|
||||
|
||||
// we also save the config if the commands were successful
|
||||
JsonDocument saveCommanddDoc;
|
||||
saveCommanddDoc["command"] = "save_config";
|
||||
saveCommanddDoc["data"].to<JsonObject>();
|
||||
CommandsPayload saveCommandPayload = {saveCommanddDoc};
|
||||
this->commandManager->handleSingleCommand(saveCommandPayload);
|
||||
} else {
|
||||
Serial.printf("%s \n\r", result.getErrorMessage().c_str());
|
||||
}
|
||||
}
|
||||
#ifdef ETVR_EYE_TRACKER_USB_API
|
||||
|
||||
Loading…
Reference in New Issue
Block a user