From 5716d08de9edbb839ba7651beef2c1a47ccbf29c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 18 Jan 2020 15:34:01 +0100 Subject: [PATCH] Refactor MQTT Logging and publish --- tasmota/support.ino | 8 +++- tasmota/support_tasmota.ino | 6 +-- tasmota/xdrv_02_mqtt.ino | 78 +++++++++++++++---------------------- 3 files changed, 41 insertions(+), 51 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index a9a631fd1..9b8f5dc9d 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1631,8 +1631,12 @@ void AddLog(uint32_t loglevel) if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string } #endif // USE_WEBSERVER - if (!global_state.mqtt_down && (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); } - if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); } + if (Settings.flag.mqtt_enabled && // SetOption3 - Enable MQTT + !global_state.mqtt_down && + (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); } + + if (!global_state.wifi_down && + (loglevel <= syslog_level)) { Syslog(); } } void AddLog_P(uint32_t loglevel, const char *formatP) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 1cb3aae8b..551f5b2c6 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -438,9 +438,9 @@ bool SendKey(uint32_t key, uint32_t device, uint32_t state) #ifdef USE_DOMOTICZ if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) { #endif // USE_DOMOTICZ - MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN - : Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN - (state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages + MqttPublish(stopic, ((key) ? Settings.flag.mqtt_switch_retain // CMND_SWITCHRETAIN + : Settings.flag.mqtt_button_retain) && // CMND_BUTTONRETAIN + (state != POWER_HOLD || !Settings.flag3.no_hold_retain)); // SetOption62 - Don't use retain flag on HOLD messages #ifdef USE_DOMOTICZ } #endif // USE_DOMOTICZ diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 799829fe6..d489606d4 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -221,6 +221,17 @@ void MqttUnsubscribeLib(const char *topic) bool MqttPublishLib(const char* topic, bool retained) { + // If Prefix1 equals Prefix2 disable next MQTT subscription to prevent loop + if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) { + char *str = strstr(topic, SettingsText(SET_MQTTPREFIX1)); + if (str == topic) { + if (0 == mqtt_cmnd_publish) { + mqtt_cmnd_publish += 3; + } + mqtt_cmnd_publish += 3; + } + } + bool result = MqttClient.publish(topic, mqtt_data, retained); yield(); // #3313 return result; @@ -291,43 +302,37 @@ void MqttUnsubscribe(const char *topic) void MqttPublishLogging(const char *mxtime) { - if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT - if (MqttIsConnected()) { + if (MqttIsConnected()) { + char saved_mqtt_data[MESSZ]; + memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data)); - char saved_mqtt_data[MESSZ]; - memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data)); -// ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON - Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!! +// ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON + Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!! - char romram[33]; - char stopic[TOPSZ]; - snprintf_P(romram, sizeof(romram), PSTR("LOGGING")); - GetTopic_P(stopic, STAT, mqtt_topic, romram); + char stopic[TOPSZ]; + GetTopic_P(stopic, STAT, mqtt_topic, PSTR("LOGGING")); + MqttPublishLib(stopic, false); - char *me; - if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) { - me = strstr(stopic, SettingsText(SET_MQTTPREFIX1)); - if (me == stopic) { - mqtt_cmnd_publish += 3; - } - } - MqttPublishLib(stopic, false); - - memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data)); - } + memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data)); } } -void MqttPublishDirect(const char* topic, bool retained) +void MqttPublish(const char* topic, bool retained) { - char sretained[CMDSZ]; - char slog_type[20]; - #ifdef USE_DEBUG_DRIVER - ShowFreeMem(PSTR("MqttPublishDirect")); + ShowFreeMem(PSTR("MqttPublish")); #endif +#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) + if (retained) { +// AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR("Retained are not supported by AWS IoT, using retained = false.")); + retained = false; // AWS IoT does not support retained, it will disconnect if received + } +#endif + + char sretained[CMDSZ]; sretained[0] = '\0'; + char slog_type[20]; snprintf_P(slog_type, sizeof(slog_type), PSTR(D_LOG_RESULT)); if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT @@ -354,25 +359,6 @@ void MqttPublishDirect(const char* topic, bool retained) } } -void MqttPublish(const char* topic, bool retained) -{ - char *me; -#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) - if (retained) { - AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR("Retained are not supported by AWS IoT, using retained = false.")); - } - retained = false; // AWS IoT does not support retained, it will disconnect if received -#endif - - if (!strcmp(SettingsText(SET_MQTTPREFIX1), SettingsText(SET_MQTTPREFIX2))) { - me = strstr(topic, SettingsText(SET_MQTTPREFIX1)); - if (me == topic) { - mqtt_cmnd_publish += 3; - } - } - MqttPublishDirect(topic, retained); -} - void MqttPublish(const char* topic) { MqttPublish(topic, false); @@ -860,7 +846,7 @@ void CmndPublish(void) } else { mqtt_data[0] = '\0'; } - MqttPublishDirect(stemp1, (XdrvMailbox.index == 2)); + MqttPublish(stemp1, (XdrvMailbox.index == 2)); // ResponseCmndDone(); mqtt_data[0] = '\0'; }