diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 373fb1c4..2d7409db 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -45,7 +45,7 @@ void wifiRegister(wifi_callback_f callback); // ----------------------------------------------------------------------------- typedef std::function mqtt_callback_f; void mqttRegister(mqtt_callback_f callback); -String mqttTopicKey(char * topic); +String mqttMagnitude(char * topic); // ----------------------------------------------------------------------------- // Broker diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 8b8813dd..5f4a3b43 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -77,7 +77,7 @@ void _ledMQTTCallback(unsigned int type, const char * topic, const char * payloa if (type == MQTT_MESSAGE_EVENT) { // Match topic - String t = mqttTopicKey((char *) topic); + String t = mqttMagnitude((char *) topic); if (!t.startsWith(MQTT_TOPIC_LED)) return; // Get led ID diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 8b120e70..a64e4652 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -496,7 +496,7 @@ void _lightMQTTCallback(unsigned int type, const char * topic, const char * payl } // Match topic - String t = mqttTopicKey((char *) topic); + String t = mqttMagnitude((char *) topic); // Color temperature in mireds if (t.equals(MQTT_TOPIC_MIRED)) { diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 5123020e..697712f2 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -357,7 +357,7 @@ void _mqttCallback(unsigned int type, const char * topic, const char * payload) if (type == MQTT_MESSAGE_EVENT) { // Match topic - String t = mqttTopicKey((char *) topic); + String t = mqttMagnitude((char *) topic); // Actions if (t.equals(MQTT_TOPIC_ACTION)) { @@ -426,7 +426,13 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) { // Public API // ----------------------------------------------------------------------------- -String mqttTopicKey(char * topic) { +/** + Returns the magnitude part of a topic + + @param topic the full MQTT topic + @return String object with the magnitude part. +*/ +String mqttMagnitude(char * topic) { String pattern = _mqtt_topic + _mqtt_setter; int position = pattern.indexOf("#"); @@ -434,28 +440,45 @@ String mqttTopicKey(char * topic) { String start = pattern.substring(0, position); String end = pattern.substring(position + 1); - String response = String(topic); - if (response.startsWith(start) && response.endsWith(end)) { - response.replace(start, ""); - response.replace(end, ""); + String magnitude = String(topic); + if (magnitude.startsWith(start) && magnitude.endsWith(end)) { + magnitude.replace(start, ""); + magnitude.replace(end, ""); } else { - response = String(); + magnitude = String(); } - return response; + return magnitude; } -String mqttTopic(const char * topic, bool is_set) { +/** + Returns a full MQTT topic from the magnitude + + @param magnitude the magnitude part of the topic. + @param is_set whether to build a command topic (true) + or a state topic (false). + @return String full MQTT topic. +*/ +String mqttTopic(const char * magnitude, bool is_set) { String output = _mqtt_topic; - output.replace("#", topic); + output.replace("#", magnitude); output += is_set ? _mqtt_setter : _mqtt_getter; return output; } -String mqttTopic(const char * topic, unsigned int index, bool is_set) { - char buffer[strlen(topic)+5]; - snprintf_P(buffer, sizeof(buffer), PSTR("%s/%d"), topic, index); +/** + Returns a full MQTT topic from the magnitude + + @param magnitude the magnitude part of the topic. + @param index index of the magnitude when more than one such magnitudes. + @param is_set whether to build a command topic (true) + or a state topic (false). + @return String full MQTT topic. +*/ +String mqttTopic(const char * magnitude, unsigned int index, bool is_set) { + char buffer[strlen(magnitude)+5]; + snprintf_P(buffer, sizeof(buffer), PSTR("%s/%d"), magnitude, index); return mqttTopic(buffer, is_set); } diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 5fa9af7e..fa9ce88b 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -611,7 +611,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo if (type == MQTT_MESSAGE_EVENT) { // Check relay topic - String t = mqttTopicKey((char *) topic); + String t = mqttMagnitude((char *) topic); if (t.startsWith(MQTT_TOPIC_RELAY)) { // Get value diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index c102630a..08431ee2 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -354,7 +354,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa if (type == MQTT_MESSAGE_EVENT) { // Match topic - String t = mqttTopicKey((char *) topic); + String t = mqttMagnitude((char *) topic); // Check if should go into learn mode if (t.startsWith(MQTT_TOPIC_RFLEARN)) {