Skip to content

Commit

Permalink
Pilight: Handle when 'value' is not a string (#2129)
Browse files Browse the repository at this point in the history
  • Loading branch information
melyux authored Dec 13, 2024
1 parent 1b50c8e commit 7fa00dd
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions main/ZgatewayPilight.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void pilightCallback(const String& protocol, const String& message, int status,
JsonObject RFPiLightdata = RFPiLightdataBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer2;
JsonObject msg = jsonBuffer2.to<JsonObject>();

if (message.length() > 0) {
auto error = deserializeJson(jsonBuffer2, message);
if (error) {
Expand All @@ -57,25 +58,26 @@ void pilightCallback(const String& protocol, const String& message, int status,
}
RFPiLightdata["message"] = msg;
}

if (protocol.length() > 0) {
RFPiLightdata["protocol"] = protocol;
}

if (deviceID.length() > 0) {
// If deviceID is non-empty, use it directly for value
RFPiLightdata["value"] = deviceID;
const char* device_id = deviceID.c_str();
if (!strlen(device_id) && !msg.isNull()) {
// deviceID returned from Pilight is only extracted from id field
// but some device may use another name as unique identifier
char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"};

for (uint8_t i = 0; i < 6; i++) {
if (msg[choices[i]]) {
device_id = (const char*)msg[choices[i]];
break;
}
} else if (!msg.isNull()) {
// deviceID returned from Pilight is only extracted from id field
// but some device may use another name as unique identifier
char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"};

for (uint8_t i = 0; i < 6; i++) {
if (msg.containsKey(choices[i])) {
// Set value directly from msg; supports both strings and integers
RFPiLightdata["value"] = msg[choices[i]];
break;
}
}
RFPiLightdata["value"] = device_id;
}

RFPiLightdata["repeats"] = (int)repeats;
Expand Down

0 comments on commit 7fa00dd

Please sign in to comment.