diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index afe9041d..c2502d0a 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -276,16 +276,21 @@ unsigned char relayParsePayload(const char * payload) { // Payload could be "OFF", "ON", "TOGGLE" // or its number equivalents: 0, 1 or 2 + if (payload[0] == '0') return 0; + if (payload[0] == '1') return 1; + if (payload[0] == '2') return 2; + // trim payload char * p = ltrim((char *)payload); // to lower - for (unsigned char i=0; i6) l=6; + for (unsigned char i=0; i= relayCount()) { + DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id); + } else { + relayStatusWrap(id, value, false); + } + return; + } // Check group topics - bool found = false; for (unsigned int i=0; i < _relays.size(); i++) { + String t = getSetting("mqttGroup", i, ""); - if (t.equals(topic)) { - unsigned char local_value = value; - if (getSetting("mqttGroupInv", i, 0).toInt() == 1) { - if (local_value < 2) local_value = 1 - local_value; - } - found = true; - DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i); - relayStatusWrap(i, local_value, true); - } - } - // If found as group topic quit - if (found) return; + if ((t.length() > 0) && t.equals(topic)) { - // Else, try to match topic - String t = mqttSubtopic((char *) topic); - if (!t.startsWith(MQTT_TOPIC_RELAY)) return; + unsigned char value = relayParsePayload(payload); + if (value == 0xFF) return; - // Get relay ID - unsigned int id = t.substring(strlen(MQTT_TOPIC_RELAY)+1).toInt(); - if (id >= relayCount()) { - DEBUG_MSG_P(PSTR("[RELAY] Wrong relayID (%d)\n"), id); - return; - } + if (value < 2) { + if (getSetting("mqttGroupInv", i, 0).toInt() == 1) { + value = 1 - value; + } + } + + DEBUG_MSG_P(PSTR("[RELAY] Matched group topic for relayID %d\n"), i); + relayStatusWrap(i, value, true); - relayStatusWrap(id, value, false); + } + } }