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";
@ -83,25 +78,8 @@ void setLightState(byte socketIdx) {
} }
} }
// function called when a MQTT message arrived void subsystem_rfsockets(char *topic_ptr, String payload) {
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]);
}
// handle message topic
Serial.print("Got a message on topic: ");
Serial.println(p_topic);
char *topic_ptr;
const char* delimiter = "/"; const char* delimiter = "/";
topic_ptr = strtok(p_topic, delimiter);
if (topic_ptr != NULL) {
char *subsystem = topic_ptr;
Serial.print("Found subsystem: ");
Serial.println(subsystem);
topic_ptr = strtok(NULL, delimiter); topic_ptr = strtok(NULL, delimiter);
if (topic_ptr != NULL) { if (topic_ptr != NULL) {
char *name = topic_ptr; char *name = topic_ptr;
@ -129,10 +107,35 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
Serial.println("Ignoring it…\n"); Serial.println("Ignoring it…\n");
} }
} else { } else {
Serial.print("Got topic without delimiter ("); Serial.println("No name found in mqtt topic (2nd / missing)");
Serial.print(delimiter); }
Serial.print("), topic was:"); }
// 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]);
}
// handle message topic
Serial.print("Got a message on topic: ");
Serial.println(p_topic); Serial.println(p_topic);
char *topic_ptr;
const char* delimiter = "/";
topic_ptr = strtok(p_topic, delimiter);
if (topic_ptr != NULL) {
char *subsystem = topic_ptr;
Serial.print("Found subsystem: ");
Serial.println(subsystem);
if(strcmp(subsystem, "rfsockets") == 0) {
subsystem_rfsockets(topic_ptr, payload);
} else {
Serial.print("Unknown subsystem \"");
Serial.print(subsystem);
Serial.println("\" in topic found. Ignoring…");
} }
} else { } else {
Serial.print("Got topic without delimiter ("); Serial.print("Got topic without delimiter (");