Skip to content

Commit

Permalink
Undo wrong rename
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Sep 6, 2024
1 parent 7894251 commit e51ad23
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/serialization/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ const char* const TAG = "JsonAPI";

#include "Logging.h"

#define OS_LOGJSONE(err, root) OS_LOGE(TAG, "Invalid JSON response (" err "): %s", cJSON_PrintUnformatted(root))
#define ESP_LOGJSONE(err, root) OS_LOGE(TAG, "Invalid JSON response (" err "): %s", cJSON_PrintUnformatted(root))

using namespace OpenShock::Serialization;

bool JsonAPI::ParseLcgInstanceDetailsJsonResponse(int code, const cJSON* root, JsonAPI::LcgInstanceDetailsResponse& out) {
(void)code;

if (cJSON_IsObject(root) == 0) {
OS_LOGJSONE("not an object", root);
ESP_LOGJSONE("not an object", root);
return false;
}

out = {};

const cJSON* name = cJSON_GetObjectItemCaseSensitive(root, "name");
if (cJSON_IsString(name) == 0) {
OS_LOGJSONE("value at 'data.name' is not a string", root);
ESP_LOGJSONE("value at 'data.name' is not a string", root);
return false;
}

const cJSON* version = cJSON_GetObjectItemCaseSensitive(root, "version");
if (cJSON_IsString(version) == 0) {
OS_LOGJSONE("value at 'data.version' is not a string", root);
ESP_LOGJSONE("value at 'data.version' is not a string", root);
return false;
}

const cJSON* currentTime = cJSON_GetObjectItemCaseSensitive(root, "currentTime");
if (cJSON_IsString(currentTime) == 0) {
OS_LOGJSONE("value at 'data.currentTime' is not a string", root);
ESP_LOGJSONE("value at 'data.currentTime' is not a string", root);
return false;
}

const cJSON* countryCode = cJSON_GetObjectItemCaseSensitive(root, "countryCode");
if (cJSON_IsString(countryCode) == 0) {
OS_LOGJSONE("value at 'data.countryCode' is not a string", root);
ESP_LOGJSONE("value at 'data.countryCode' is not a string", root);
return false;
}

const cJSON* fqdn = cJSON_GetObjectItemCaseSensitive(root, "fqdn");
if (cJSON_IsString(fqdn) == 0) {
OS_LOGJSONE("value at 'data.fqdn' is not a string", root);
ESP_LOGJSONE("value at 'data.fqdn' is not a string", root);
return false;
}

Expand All @@ -60,33 +60,33 @@ bool JsonAPI::ParseBackendVersionJsonResponse(int code, const cJSON* root, JsonA
(void)code;

if (cJSON_IsObject(root) == 0) {
OS_LOGJSONE("not an object", root);
ESP_LOGJSONE("not an object", root);
return false;
}

const cJSON* data = cJSON_GetObjectItemCaseSensitive(root, "data");
if (cJSON_IsObject(data) == 0) {
OS_LOGJSONE("value at 'data' is not an object", root);
ESP_LOGJSONE("value at 'data' is not an object", root);
return false;
}

out = {};

const cJSON* version = cJSON_GetObjectItemCaseSensitive(data, "version");
if (cJSON_IsString(version) == 0) {
OS_LOGJSONE("value at 'data.version' is not a string", root);
ESP_LOGJSONE("value at 'data.version' is not a string", root);
return false;
}

const cJSON* commit = cJSON_GetObjectItemCaseSensitive(data, "commit");
if (cJSON_IsString(commit) == 0) {
OS_LOGJSONE("value at 'data.commit' is not a string", root);
ESP_LOGJSONE("value at 'data.commit' is not a string", root);
return false;
}

const cJSON* currentTime = cJSON_GetObjectItemCaseSensitive(data, "currentTime");
if (cJSON_IsString(currentTime) == 0) {
OS_LOGJSONE("value at 'data.currentTime' is not a string", root);
ESP_LOGJSONE("value at 'data.currentTime' is not a string", root);
return false;
}

Expand All @@ -101,13 +101,13 @@ bool JsonAPI::ParseAccountLinkJsonResponse(int code, const cJSON* root, JsonAPI:
(void)code;

if (cJSON_IsObject(root) == 0) {
OS_LOGJSONE("not an object", root);
ESP_LOGJSONE("not an object", root);
return false;
}

const cJSON* data = cJSON_GetObjectItemCaseSensitive(root, "data");
if (cJSON_IsString(data) == 0) {
OS_LOGJSONE("value at 'data' is not a string", root);
ESP_LOGJSONE("value at 'data' is not a string", root);
return false;
}

Expand All @@ -121,31 +121,31 @@ bool JsonAPI::ParseDeviceInfoJsonResponse(int code, const cJSON* root, JsonAPI::
(void)code;

if (cJSON_IsObject(root) == 0) {
OS_LOGJSONE("not an object", root);
ESP_LOGJSONE("not an object", root);
return false;
}

const cJSON* data = cJSON_GetObjectItemCaseSensitive(root, "data");
if (cJSON_IsObject(data) == 0) {
OS_LOGJSONE("value at 'data' is not an object", root);
ESP_LOGJSONE("value at 'data' is not an object", root);
return false;
}

const cJSON* deviceId = cJSON_GetObjectItemCaseSensitive(data, "id");
if (cJSON_IsString(deviceId) == 0) {
OS_LOGJSONE("value at 'data.id' is not a string", root);
ESP_LOGJSONE("value at 'data.id' is not a string", root);
return false;
}

const cJSON* deviceName = cJSON_GetObjectItemCaseSensitive(data, "name");
if (cJSON_IsString(deviceName) == 0) {
OS_LOGJSONE("value at 'data.name' is not a string", root);
ESP_LOGJSONE("value at 'data.name' is not a string", root);
return false;
}

const cJSON* deviceShockers = cJSON_GetObjectItemCaseSensitive(data, "shockers");
if (cJSON_IsArray(deviceShockers) == 0) {
OS_LOGJSONE("value at 'data.shockers' is not an array", root);
ESP_LOGJSONE("value at 'data.shockers' is not an array", root);
return false;
}

Expand All @@ -155,51 +155,51 @@ bool JsonAPI::ParseDeviceInfoJsonResponse(int code, const cJSON* root, JsonAPI::
out.deviceName = deviceName->valuestring;

if (out.deviceId.empty() || out.deviceName.empty()) {
OS_LOGJSONE("value at 'data.id' or 'data.name' is empty", root);
ESP_LOGJSONE("value at 'data.id' or 'data.name' is empty", root);
return false;
}

cJSON* shocker = nullptr;
cJSON_ArrayForEach(shocker, deviceShockers) {
const cJSON* shockerId = cJSON_GetObjectItemCaseSensitive(shocker, "id");
if (cJSON_IsString(shockerId) == 0) {
OS_LOGJSONE("value at 'shocker.id' is not a string", shocker);
ESP_LOGJSONE("value at 'shocker.id' is not a string", shocker);
return false;
}
const char* shockerIdStr = shockerId->valuestring;

if (shockerIdStr == nullptr || shockerIdStr[0] == '\0') {
OS_LOGJSONE("value at 'shocker.id' is empty", shocker);
ESP_LOGJSONE("value at 'shocker.id' is empty", shocker);
return false;
}

const cJSON* shockerRfId = cJSON_GetObjectItemCaseSensitive(shocker, "rfId");
if (cJSON_IsNumber(shockerRfId) == 0) {
OS_LOGJSONE("value at 'shocker.rfId' is not a number", shocker);
ESP_LOGJSONE("value at 'shocker.rfId' is not a number", shocker);
return false;
}
int shockerRfIdInt = shockerRfId->valueint;
if (shockerRfIdInt < 0 || shockerRfIdInt > UINT16_MAX) {
OS_LOGJSONE("value at 'shocker.rfId' is not a valid uint16_t", shocker);
ESP_LOGJSONE("value at 'shocker.rfId' is not a valid uint16_t", shocker);
return false;
}
uint16_t shockerRfIdU16 = static_cast<uint16_t>(shockerRfIdInt);

const cJSON* shockerModel = cJSON_GetObjectItemCaseSensitive(shocker, "model");
if (cJSON_IsString(shockerModel) == 0) {
OS_LOGJSONE("value at 'shocker.model' is not a string", shocker);
ESP_LOGJSONE("value at 'shocker.model' is not a string", shocker);
return false;
}
const char* shockerModelStr = shockerModel->valuestring;

if (shockerModelStr == nullptr || shockerModelStr[0] == '\0') {
OS_LOGJSONE("value at 'shocker.model' is empty", shocker);
ESP_LOGJSONE("value at 'shocker.model' is empty", shocker);
return false;
}

OpenShock::ShockerModelType shockerModelType;
if (!OpenShock::ShockerModelTypeFromString(shockerModelStr, shockerModelType, true)) { // PetTrainer is a typo in the API, we pass true to allow it
OS_LOGJSONE("value at 'shocker.model' is not a valid shocker model", shocker);
ESP_LOGJSONE("value at 'shocker.model' is not a valid shocker model", shocker);
return false;
}

Expand All @@ -212,21 +212,21 @@ bool JsonAPI::ParseAssignLcgJsonResponse(int code, const cJSON* root, JsonAPI::A
(void)code;

if (cJSON_IsObject(root) == 0) {
OS_LOGJSONE("not an object", root);
ESP_LOGJSONE("not an object", root);
return false;
}

const cJSON* data = cJSON_GetObjectItemCaseSensitive(root, "data");
if (cJSON_IsObject(data) == 0) {
OS_LOGJSONE("value at 'data' is not an object", root);
ESP_LOGJSONE("value at 'data' is not an object", root);
return false;
}

const cJSON* fqdn = cJSON_GetObjectItemCaseSensitive(data, "fqdn");
const cJSON* country = cJSON_GetObjectItemCaseSensitive(data, "country");

if (cJSON_IsString(fqdn) == 0 || cJSON_IsString(country) == 0) {
OS_LOGJSONE("value at 'data.fqdn' or 'data.country' is not a string", root);
ESP_LOGJSONE("value at 'data.fqdn' or 'data.country' is not a string", root);
return false;
}

Expand Down

0 comments on commit e51ad23

Please sign in to comment.