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