From 7fa00dd5932d63d60d117e1fce1ecb894ae1c2f7 Mon Sep 17 00:00:00 2001 From: melyux <10296053+melyux@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:42:39 -0800 Subject: [PATCH] Pilight: Handle when 'value' is not a string (#2129) --- main/ZgatewayPilight.ino | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/main/ZgatewayPilight.ino b/main/ZgatewayPilight.ino index 1550b8853f..b2329d92a3 100644 --- a/main/ZgatewayPilight.ino +++ b/main/ZgatewayPilight.ino @@ -49,6 +49,7 @@ void pilightCallback(const String& protocol, const String& message, int status, JsonObject RFPiLightdata = RFPiLightdataBuffer.to(); StaticJsonDocument jsonBuffer2; JsonObject msg = jsonBuffer2.to(); + if (message.length() > 0) { auto error = deserializeJson(jsonBuffer2, message); if (error) { @@ -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;