Pilight: Handle when 'value' is not a string (#2129)

This commit is contained in:
melyux authored and GitHub committed 2024-12-13 16:42:39 -06:00
1 parent 1b50c8ec02
commit 7fa00dd593
1 file changed
+8 -6
+8 -6
View File
@@ -49,6 +49,7 @@ void pilightCallback(const String& protocol, const String& message, int status,
JsonObject RFPiLightdata = RFPiLightdataBuffer.to<JsonObject>(); JsonObject RFPiLightdata = RFPiLightdataBuffer.to<JsonObject>();
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer2; StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer2;
JsonObject msg = jsonBuffer2.to<JsonObject>(); JsonObject msg = jsonBuffer2.to<JsonObject>();
if (message.length() > 0) { if (message.length() > 0) {
auto error = deserializeJson(jsonBuffer2, message); auto error = deserializeJson(jsonBuffer2, message);
if (error) { if (error) {
@@ -57,26 +58,27 @@ void pilightCallback(const String& protocol, const String& message, int status,
} }
RFPiLightdata["message"] = msg; RFPiLightdata["message"] = msg;
} }
if (protocol.length() > 0) { if (protocol.length() > 0) {
RFPiLightdata["protocol"] = protocol; RFPiLightdata["protocol"] = protocol;
} }
if (deviceID.length() > 0) { if (deviceID.length() > 0) {
// If deviceID is non-empty, use it directly for value
RFPiLightdata["value"] = deviceID; RFPiLightdata["value"] = deviceID;
const char* device_id = deviceID.c_str(); } else if (!msg.isNull()) {
if (!strlen(device_id) && !msg.isNull()) {
// deviceID returned from Pilight is only extracted from id field // deviceID returned from Pilight is only extracted from id field
// but some device may use another name as unique identifier // but some device may use another name as unique identifier
char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"}; char* choices[] = {"key", "unit", "device_id", "systemcode", "unitcode", "programcode"};
for (uint8_t i = 0; i < 6; i++) { for (uint8_t i = 0; i < 6; i++) {
if (msg[choices[i]]) { if (msg.containsKey(choices[i])) {
device_id = (const char*)msg[choices[i]]; // Set value directly from msg; supports both strings and integers
RFPiLightdata["value"] = msg[choices[i]];
break; break;
} }
} }
} }
RFPiLightdata["value"] = device_id;
}
RFPiLightdata["repeats"] = (int)repeats; RFPiLightdata["repeats"] = (int)repeats;
RFPiLightdata["status"] = (int)status; RFPiLightdata["status"] = (int)status;