Browse Source

Changed naming convention: topic key is now magnitude

softuart
Xose Pérez 6 years ago
parent
commit
5c662432ee
6 changed files with 41 additions and 18 deletions
  1. +1
    -1
      code/espurna/config/prototypes.h
  2. +1
    -1
      code/espurna/led.ino
  3. +1
    -1
      code/espurna/light.ino
  4. +36
    -13
      code/espurna/mqtt.ino
  5. +1
    -1
      code/espurna/relay.ino
  6. +1
    -1
      code/espurna/rfbridge.ino

+ 1
- 1
code/espurna/config/prototypes.h View File

@ -45,7 +45,7 @@ void wifiRegister(wifi_callback_f callback);
// -----------------------------------------------------------------------------
typedef std::function<void(unsigned int, const char *, const char *)> mqtt_callback_f;
void mqttRegister(mqtt_callback_f callback);
String mqttTopicKey(char * topic);
String mqttMagnitude(char * topic);
// -----------------------------------------------------------------------------
// Broker


+ 1
- 1
code/espurna/led.ino View File

@ -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


+ 1
- 1
code/espurna/light.ino View File

@ -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)) {


+ 36
- 13
code/espurna/mqtt.ino View File

@ -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);
}


+ 1
- 1
code/espurna/relay.ino View File

@ -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


+ 1
- 1
code/espurna/rfbridge.ino View File

@ -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)) {


Loading…
Cancel
Save