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]";
//// 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";
@ -83,25 +78,8 @@ void setLightState(byte socketIdx) {
}
}
// 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);
char *topic_ptr;
void subsystem_rfsockets(char *topic_ptr, String payload) {
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);
if (topic_ptr != NULL) {
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");
}
} else {
Serial.print("Got topic without delimiter (");
Serial.print(delimiter);
Serial.print("), topic was:");
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]);
}
// handle message topic
Serial.print("Got a message on 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 {
Serial.print("Got topic without delimiter (");