Make command manager output proper json

This commit is contained in:
Lorow
2025-05-13 22:53:51 +02:00
parent ab0b1db432
commit 0635bbd5c2
2 changed files with 13 additions and 8 deletions

View File

@@ -57,14 +57,17 @@ std::function<CommandResult()> CommandManager::createCommand(CommandType type, s
CommandResult CommandManager::executeFromJson(std::string_view json)
{
std::string results = "";
cJSON *parsedJson = cJSON_Parse(json.data());
if (parsedJson == nullptr)
return CommandResult::getErrorResult("Initial JSON Parse: Invalid JSON");
const cJSON *commandData = nullptr;
const cJSON *commands = cJSON_GetObjectItem(parsedJson, "commands");
cJSON *responseDocument = cJSON_CreateObject();
cJSON *responses = cJSON_CreateArray();
cJSON *response = nullptr;
cJSON_AddItemToObject(responseDocument, "results", responses);
if (cJSON_GetArraySize(commands) == 0)
{
@@ -93,13 +96,15 @@ CommandResult CommandManager::executeFromJson(std::string_view json)
cJSON_Delete(parsedJson);
return CommandResult::getErrorResult("Unknown command");
}
// todo, make it a proper json later
results += command().getResult() + ", \n";
response = cJSON_CreateString(command().getResult().c_str());
cJSON_AddItemToArray(responses, response);
}
auto jsonString = cJSON_Print(responseDocument);
cJSON_Delete(responseDocument);
cJSON_Delete(parsedJson);
return CommandResult::getSuccessResult(results);
return CommandResult::getSuccessResult(jsonString);
}
CommandResult CommandManager::executeFromType(CommandType type, std::string_view json)

View File

@@ -127,7 +127,7 @@ struct CameraConfig_t : BaseConfigModel
// save them under an indexed name and load them as such.
struct WiFiConfig_t : BaseConfigModel
{
// default construcotr used for loading
// default constructor used for loading
WiFiConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
WiFiConfig_t(
@@ -284,7 +284,7 @@ public:
}
return Helpers::format_string(
"%s, %s, %s, networks: [%s], %s, %s",
"{%s, %s, %s, \"networks\": [%s], %s, %s}",
this->device.toRepresentation().c_str(),
this->mdns.toRepresentation().c_str(),
this->camera.toRepresentation().c_str(),