Refactor command system - remove BaseCommand class implementation in favour of std::function

This commit is contained in:
Lorow
2025-04-09 21:43:49 +02:00
parent 8a2695977c
commit eaa60cb877
17 changed files with 268 additions and 271 deletions

View File

@@ -1,18 +1,6 @@
#include "config_commands.hpp"
CommandResult saveConfigCommand::execute(std::string_view jsonPayload)
{
projectConfig->save();
return CommandResult::getSuccessResult("Config saved");
}
CommandResult getConfigCommand::execute(std::string_view jsonPayload)
{
auto configRepresentation = projectConfig->getTrackerConfig().toRepresentation();
return CommandResult::getSuccessResult(configRepresentation);
}
std::optional<ResetConfigPayload> resetConfigCommand::parsePayload(std::string_view jsonPayload)
std::optional<ResetConfigPayload> parseResetConfigCommandPayload(std::string_view jsonPayload)
{
ResetConfigPayload payload;
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
@@ -34,9 +22,28 @@ std::optional<ResetConfigPayload> resetConfigCommand::parsePayload(std::string_v
return payload;
}
CommandResult resetConfigCommand::execute(std::string_view jsonPayload)
CommandResult saveConfigCommand(std::shared_ptr<DependencyRegistry> registry)
{
auto payload = parsePayload(jsonPayload);
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->save();
return CommandResult::getSuccessResult("Config saved");
}
CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry)
{
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
auto configRepresentation = projectConfig->getTrackerConfig().toRepresentation();
return CommandResult::getSuccessResult(configRepresentation);
}
CommandResult resetConfigCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
{
std::array<std::string, 4> supported_sections = {
"all",
};
auto payload = parseResetConfigCommandPayload(jsonPayload);
if (!payload.has_value())
{
@@ -45,7 +52,7 @@ CommandResult resetConfigCommand::execute(std::string_view jsonPayload)
auto sectionPayload = payload.value();
if (std::find(this->supported_sections.begin(), this->supported_sections.end(), sectionPayload.section) == this->supported_sections.end())
if (std::find(supported_sections.begin(), supported_sections.end(), sectionPayload.section) == supported_sections.end())
{
return CommandResult::getErrorResult("Selected section is unsupported");
}
@@ -53,7 +60,10 @@ CommandResult resetConfigCommand::execute(std::string_view jsonPayload)
// we cannot match on string, and making a map would be overkill right now, sooo
// todo, add more granual control for other sections, like only reset camera, or only reset wifi
if (sectionPayload.section == "all")
this->projectConfig->reset();
{
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->reset();
}
return CommandResult::getSuccessResult("Config reset");
}