From 0e0a10ae7b9f34ec5bda50b0db7598f7496ae7eb Mon Sep 17 00:00:00 2001 From: sqozz Date: Fri, 13 Jul 2018 00:24:16 +0200 Subject: [PATCH] Extract subsystem into separate function --- multi-esp.ino | 93 ++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/multi-esp.ino b/multi-esp.ino index a8d2d0c..99813a6 100644 --- a/multi-esp.ino +++ b/multi-esp.ino @@ -41,11 +41,6 @@ const PROGMEM uint16_t MQTT_SERVER_PORT = 1883; //const PROGMEM char* MQTT_PASSWORD = "[Redacted]"; - -//// MQTT: topics -//const char* MQTT_LIGHT_STATE_TOPIC = "balcony/hanging_lamp/status"; -//const char* MQTT_LIGHT_COMMAND_TOPIC = "balcony/hanging_lamp/switch"; - const char* MQTT_RFSOCKET_SUBSYSTEM = "rfsockets/"; const char* MQTT_RFSOCKET_STATE_TOPIC = "/status"; const char* MQTT_RFSOCKET_COMMAND_TOPIC = "/switch"; @@ -74,22 +69,55 @@ void publishLightState(int socketIdx) { // function called to turn on/off the light void setLightState(byte socketIdx) { - if (sockets[socketIdx].powered) { + if (sockets[socketIdx].powered) { setSocket(socketIdx, true); - Serial.println("INFO: Turn light on..."); - } else { + Serial.println("INFO: Turn light on..."); + } else { setSocket(socketIdx, false); - Serial.println("INFO: Turn light off..."); - } + Serial.println("INFO: Turn light off..."); + } +} + +void subsystem_rfsockets(char *topic_ptr, String payload) { + const char* delimiter = "/"; + topic_ptr = strtok(NULL, delimiter); + if (topic_ptr != NULL) { + char *name = topic_ptr; + Serial.print("Found socket name: "); + Serial.println(name); + int socketIdx = findSocketIndex(name); + Serial.print("Found socket index "); + Serial.println(socketIdx); + // We only listen on the command channel so no further splitting needed + if (payload.equals(String(LIGHT_ON))) { + if (sockets[socketIdx].powered != true) { + sockets[socketIdx].powered = true; + setLightState(socketIdx); + publishLightState(socketIdx); + } + } else if (payload.equals(String(LIGHT_OFF))) { + if (sockets[socketIdx].powered != false) { + sockets[socketIdx].powered = false; + setLightState(socketIdx); + publishLightState(socketIdx); + } + } else { + Serial.print("Unknown payload received: "); + Serial.println(payload); + Serial.println("Ignoring it…\n"); + } + } else { + Serial.println("No name found in mqtt topic (2nd / missing)"); + } } // function called when a MQTT message arrived void callback(char* p_topic, byte* p_payload, unsigned int p_length) { - // concat the payload into a string - String payload; - for (uint8_t i = 0; i < p_length; i++) { - payload.concat((char)p_payload[i]); - } + // concat the payload into a string + String payload; + for (uint8_t i = 0; i < p_length; i++) { + payload.concat((char)p_payload[i]); + } // handle message topic Serial.print("Got a message on topic: "); @@ -102,37 +130,12 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) { char *subsystem = topic_ptr; Serial.print("Found subsystem: "); Serial.println(subsystem); - topic_ptr = strtok(NULL, delimiter); - if (topic_ptr != NULL) { - char *name = topic_ptr; - Serial.print("Found socket name: "); - Serial.println(name); - int socketIdx = findSocketIndex(name); - Serial.print("Found socket index "); - Serial.println(socketIdx); - // We only listen on the command channel so no further splitting needed - if (payload.equals(String(LIGHT_ON))) { - if (sockets[socketIdx].powered != true) { - sockets[socketIdx].powered = true; - setLightState(socketIdx); - publishLightState(socketIdx); - } - } else if (payload.equals(String(LIGHT_OFF))) { - if (sockets[socketIdx].powered != false) { - sockets[socketIdx].powered = false; - setLightState(socketIdx); - publishLightState(socketIdx); - } - } else { - Serial.print("Unknown payload received: "); - Serial.println(payload); - Serial.println("Ignoring it…\n"); - } + if(strcmp(subsystem, "rfsockets") == 0) { + subsystem_rfsockets(topic_ptr, payload); } else { - Serial.print("Got topic without delimiter ("); - Serial.print(delimiter); - Serial.print("), topic was:"); - Serial.println(p_topic); + Serial.print("Unknown subsystem \""); + Serial.print(subsystem); + Serial.println("\" in topic found. Ignoring…"); } } else { Serial.print("Got topic without delimiter (");