Extract subsystem into separate function

This commit is contained in:
sqozz 2018-07-13 00:24:16 +02:00
parent 0356892526
commit 0e0a10ae7b

View file

@ -41,11 +41,6 @@ const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
//const PROGMEM char* MQTT_PASSWORD = "[Redacted]"; //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_SUBSYSTEM = "rfsockets/";
const char* MQTT_RFSOCKET_STATE_TOPIC = "/status"; const char* MQTT_RFSOCKET_STATE_TOPIC = "/status";
const char* MQTT_RFSOCKET_COMMAND_TOPIC = "/switch"; const char* MQTT_RFSOCKET_COMMAND_TOPIC = "/switch";
@ -74,22 +69,55 @@ void publishLightState(int socketIdx) {
// function called to turn on/off the light // function called to turn on/off the light
void setLightState(byte socketIdx) { void setLightState(byte socketIdx) {
if (sockets[socketIdx].powered) { if (sockets[socketIdx].powered) {
setSocket(socketIdx, true); setSocket(socketIdx, true);
Serial.println("INFO: Turn light on..."); Serial.println("INFO: Turn light on...");
} else { } else {
setSocket(socketIdx, false); 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 // function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) { void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
// concat the payload into a string // concat the payload into a string
String payload; String payload;
for (uint8_t i = 0; i < p_length; i++) { for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]); payload.concat((char)p_payload[i]);
} }
// handle message topic // handle message topic
Serial.print("Got a message on 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; char *subsystem = topic_ptr;
Serial.print("Found subsystem: "); Serial.print("Found subsystem: ");
Serial.println(subsystem); Serial.println(subsystem);
topic_ptr = strtok(NULL, delimiter); if(strcmp(subsystem, "rfsockets") == 0) {
if (topic_ptr != NULL) { subsystem_rfsockets(topic_ptr, payload);
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 { } else {
Serial.print("Got topic without delimiter ("); Serial.print("Unknown subsystem \"");
Serial.print(delimiter); Serial.print(subsystem);
Serial.print("), topic was:"); Serial.println("\" in topic found. Ignoring…");
Serial.println(p_topic);
} }
} else { } else {
Serial.print("Got topic without delimiter ("); Serial.print("Got topic without delimiter (");