mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-19 14:33:45 +02:00
Make command manager output proper json
This commit is contained in:
@@ -57,14 +57,17 @@ std::function<CommandResult()> CommandManager::createCommand(CommandType type, s
|
|||||||
|
|
||||||
CommandResult CommandManager::executeFromJson(std::string_view json)
|
CommandResult CommandManager::executeFromJson(std::string_view json)
|
||||||
{
|
{
|
||||||
std::string results = "";
|
|
||||||
|
|
||||||
cJSON *parsedJson = cJSON_Parse(json.data());
|
cJSON *parsedJson = cJSON_Parse(json.data());
|
||||||
if (parsedJson == nullptr)
|
if (parsedJson == nullptr)
|
||||||
return CommandResult::getErrorResult("Initial JSON Parse: Invalid JSON");
|
return CommandResult::getErrorResult("Initial JSON Parse: Invalid JSON");
|
||||||
|
|
||||||
const cJSON *commandData = nullptr;
|
const cJSON *commandData = nullptr;
|
||||||
const cJSON *commands = cJSON_GetObjectItem(parsedJson, "commands");
|
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)
|
if (cJSON_GetArraySize(commands) == 0)
|
||||||
{
|
{
|
||||||
@@ -93,13 +96,15 @@ CommandResult CommandManager::executeFromJson(std::string_view json)
|
|||||||
cJSON_Delete(parsedJson);
|
cJSON_Delete(parsedJson);
|
||||||
return CommandResult::getErrorResult("Unknown command");
|
return CommandResult::getErrorResult("Unknown command");
|
||||||
}
|
}
|
||||||
|
response = cJSON_CreateString(command().getResult().c_str());
|
||||||
// todo, make it a proper json later
|
cJSON_AddItemToArray(responses, response);
|
||||||
results += command().getResult() + ", \n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto jsonString = cJSON_Print(responseDocument);
|
||||||
|
cJSON_Delete(responseDocument);
|
||||||
cJSON_Delete(parsedJson);
|
cJSON_Delete(parsedJson);
|
||||||
return CommandResult::getSuccessResult(results);
|
|
||||||
|
return CommandResult::getSuccessResult(jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandResult CommandManager::executeFromType(CommandType type, std::string_view json)
|
CommandResult CommandManager::executeFromType(CommandType type, std::string_view json)
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ struct CameraConfig_t : BaseConfigModel
|
|||||||
// save them under an indexed name and load them as such.
|
// save them under an indexed name and load them as such.
|
||||||
struct WiFiConfig_t : BaseConfigModel
|
struct WiFiConfig_t : BaseConfigModel
|
||||||
{
|
{
|
||||||
// default construcotr used for loading
|
// default constructor used for loading
|
||||||
WiFiConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
|
WiFiConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
|
||||||
|
|
||||||
WiFiConfig_t(
|
WiFiConfig_t(
|
||||||
@@ -284,7 +284,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Helpers::format_string(
|
return Helpers::format_string(
|
||||||
"%s, %s, %s, networks: [%s], %s, %s",
|
"{%s, %s, %s, \"networks\": [%s], %s, %s}",
|
||||||
this->device.toRepresentation().c_str(),
|
this->device.toRepresentation().c_str(),
|
||||||
this->mdns.toRepresentation().c_str(),
|
this->mdns.toRepresentation().c_str(),
|
||||||
this->camera.toRepresentation().c_str(),
|
this->camera.toRepresentation().c_str(),
|
||||||
|
|||||||
Reference in New Issue
Block a user