From 347783a1c4e52620f0894be86129a88f2e12adbd Mon Sep 17 00:00:00 2001 From: Charles R Date: Wed, 2 Sep 2020 23:46:37 +0200 Subject: [PATCH] Fix pilight value as subject (#752) * feat: pilight gateway now send value * feat: when device id is empty, try fo find it from the message payload --- main/ZgatewayPilight.ino | 16 ++++++++++++++++ main/main.ino | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/main/ZgatewayPilight.ino b/main/ZgatewayPilight.ino index 95999ef..8fd7a57 100644 --- a/main/ZgatewayPilight.ino +++ b/main/ZgatewayPilight.ino @@ -47,6 +47,22 @@ void pilightCallback(const String& protocol, const String& message, int status, RFPiLightdata.set("message", msg); RFPiLightdata.set("protocol", (char*)protocol.c_str()); RFPiLightdata.set("length", (char*)deviceID.c_str()); + + const char* device_id = deviceID.c_str(); + if (!strlen(device_id)) { + // deviceID returned from Pilight is only extracted from id field + // but some device may use another name as unique identifier + char* choices[] = {"key", "unit", "systemcode", "unitcode", "programcode"}; + + for (uint8_t i = 0; i < 5; i++) { + if (msg[choices[i]]) { + device_id = (const char*)msg[choices[i]]; + break; + } + } + } + + RFPiLightdata.set("value", device_id); RFPiLightdata.set("repeats", (int)repeats); RFPiLightdata.set("status", (int)status); pub(subjectPilighttoMQTT, RFPiLightdata); diff --git a/main/main.ino b/main/main.ino index ad4da10..c2194dc 100644 --- a/main/main.ino +++ b/main/main.ino @@ -253,10 +253,17 @@ void pub(char* topicori, JsonObject& data) { if (client.connected()) { String topic = String(mqtt_topic) + String(topicori); #ifdef valueAsASubject +# ifdef ZgatewayPilight + String value = data["value"]; + if (value != 0) { + topic = topic + "/" + value; + } +# else SIGNAL_SIZE_UL_ULL value = data["value"]; if (value != 0) { topic = topic + "/" + String(value); } +# endif #endif #ifdef jsonPublishing