return error as json and reduce json documents

This commit is contained in:
Nicu Hodos 2022-10-29 13:09:23 +02:00
parent e3e13c2536
commit c8710a1cc9

View File

@ -49,7 +49,7 @@ void readRcSwitch() {
unsigned long value = mySwitch.getReceivedValue();
mySwitch.resetAvailable();
StaticJsonDocument<200> jsonDoc;
StaticJsonDocument<128> jsonDoc;
Protocol* p = findProtocol(mySwitch.getReceivedProtocol());
p->toJson(value, jsonDoc);
delete p;
@ -61,7 +61,6 @@ void readRcSwitch() {
}
void runJsonCommands(const char* cmd) {
String origCmd = String(cmd);
StaticJsonDocument<512> jsonArray;
DeserializationError err = deserializeJson(jsonArray, cmd);
if (err == DeserializationError::Ok) {
@ -77,9 +76,13 @@ void runJsonCommands(const char* cmd) {
}
}
} else {
Serial.print(err.c_str());
Serial.print(": ");
Serial.println(origCmd);
StaticJsonDocument<128> jsonError;
JsonObject error = jsonError.createNestedObject("error");
error["message"] = err.c_str();
error["original_cmd"] = cmd;
serializeJson(jsonError, Serial);
Serial.println();
}
}
@ -95,7 +98,7 @@ void readCommand() {
if (cmd.endsWith(",")) {
cmd = cmd.substring(0, cmd.lastIndexOf(','));
}
char buffer[cmd.length()+2];
char buffer[cmd.length()+3];
sprintf(buffer, "[%s]", cmd.c_str());
runJsonCommands(buffer);
}