mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-25 17:13:46 +02:00
Rewrite commands to nlohmann-json
This commit is contained in:
@@ -1,27 +1,5 @@
|
||||
#include "config_commands.hpp"
|
||||
|
||||
std::optional<ResetConfigPayload> parseResetConfigCommandPayload(std::string_view jsonPayload)
|
||||
{
|
||||
ResetConfigPayload payload;
|
||||
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
|
||||
|
||||
if (parsedJson == nullptr)
|
||||
return std::nullopt;
|
||||
|
||||
cJSON *section = cJSON_GetObjectItem(parsedJson, "section");
|
||||
|
||||
if (section == nullptr)
|
||||
{
|
||||
cJSON_Delete(parsedJson);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
payload.section = std::string(section->valuestring);
|
||||
|
||||
cJSON_Delete(parsedJson);
|
||||
return payload;
|
||||
}
|
||||
|
||||
CommandResult saveConfigCommand(std::shared_ptr<DependencyRegistry> registry)
|
||||
{
|
||||
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
|
||||
@@ -38,29 +16,27 @@ CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry)
|
||||
return CommandResult::getSuccessResult(configRepresentation);
|
||||
}
|
||||
|
||||
CommandResult resetConfigCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
|
||||
CommandResult resetConfigCommand(std::shared_ptr<DependencyRegistry> registry, const nlohmann::json &json)
|
||||
{
|
||||
std::array<std::string, 4> supported_sections = {
|
||||
"all",
|
||||
};
|
||||
|
||||
auto payload = parseResetConfigCommandPayload(jsonPayload);
|
||||
|
||||
if (!payload.has_value())
|
||||
if (!json.contains("section"))
|
||||
{
|
||||
return CommandResult::getErrorResult("Invalid payload or missing section");
|
||||
return CommandResult::getErrorResult("Invalid payload - missing section");
|
||||
}
|
||||
|
||||
auto sectionPayload = payload.value();
|
||||
auto section = json["section"].get<std::string>();
|
||||
|
||||
if (std::find(supported_sections.begin(), supported_sections.end(), sectionPayload.section) == supported_sections.end())
|
||||
if (std::find(supported_sections.begin(), supported_sections.end(), section) == supported_sections.end())
|
||||
{
|
||||
return CommandResult::getErrorResult("Selected section is unsupported");
|
||||
}
|
||||
|
||||
// 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")
|
||||
// todo, add more granular control for other sections, like only reset camera, or only reset wifi
|
||||
if (section == "all")
|
||||
{
|
||||
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
|
||||
projectConfig->reset();
|
||||
|
||||
Reference in New Issue
Block a user