From 2751aca37ac4263a46c950db7876498298cccd62 Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 25 Nov 2024 09:31:21 +0100 Subject: [PATCH] remove setWill() to inline --- lib/framework/MqttSettingsService.cpp | 31 +++++++++------------------ lib/framework/MqttSettingsService.h | 1 - src/mqtt.cpp | 10 --------- 3 files changed, 10 insertions(+), 32 deletions(-) diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index e97c15691..0c24304e6 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -101,16 +101,6 @@ const char * MqttSettingsService::getClientId() { return _mqttClient->getClientId(); } -void MqttSettingsService::setWill(const char * topic) { -#ifndef TASMOTA_SDK - if (_state.enableTLS) { - static_cast(_mqttClient)->setWill(topic, 1, true, "offline"); - return; - } -#endif - static_cast(_mqttClient)->setWill(topic, 1, true, "offline"); -} - void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties & properties, const char * topic, const uint8_t * payload, @@ -183,6 +173,14 @@ bool MqttSettingsService::configureMqtt() { // only connect if WiFi is connected and MQTT is enabled if (_state.enabled && emsesp::EMSESP::system_.network_connected() && !_state.host.isEmpty()) { + // create last will topic with the base prefixed. It has to be static because the client destroys the reference + static char will_topic[FACTORY_MQTT_MAX_TOPIC_LENGTH]; + if (_state.base.isEmpty()) { + snprintf(will_topic, sizeof(will_topic), "status"); + } else { + snprintf(will_topic, sizeof(will_topic), "%s/status", _state.base.c_str()); + } + _reconfigureMqtt = false; #ifndef TASMOTA_SDK if (_state.enableTLS) { @@ -197,6 +195,7 @@ bool MqttSettingsService::configureMqtt() { static_cast(_mqttClient)->setClientId(_state.clientId.c_str()); static_cast(_mqttClient)->setKeepAlive(_state.keepAlive); static_cast(_mqttClient)->setCleanSession(_state.cleanSession); + static_cast(_mqttClient)->setWill(will_topic, 1, true, "offline"); // QOS 1, retain return _mqttClient->connect(); } #endif @@ -207,17 +206,7 @@ bool MqttSettingsService::configureMqtt() { static_cast(_mqttClient)->setClientId(_state.clientId.c_str()); static_cast(_mqttClient)->setKeepAlive(_state.keepAlive); static_cast(_mqttClient)->setCleanSession(_state.cleanSession); - - // create last will topic with the base prefixed. It has to be static because the client destroys the reference - static char will_topic[FACTORY_MQTT_MAX_TOPIC_LENGTH]; - if (_state.base.isEmpty()) { - snprintf(will_topic, sizeof(will_topic), "status"); - } else { - snprintf(will_topic, sizeof(will_topic), "%s/status", _state.base.c_str()); - } - setWill(will_topic); - - + static_cast(_mqttClient)->setWill(will_topic, 1, true, "offline"); // QOS 1, retain return _mqttClient->connect(); } diff --git a/lib/framework/MqttSettingsService.h b/lib/framework/MqttSettingsService.h index 0ea187e0c..ef448af0d 100644 --- a/lib/framework/MqttSettingsService.h +++ b/lib/framework/MqttSettingsService.h @@ -101,7 +101,6 @@ class MqttSettingsService : public StatefulService { const char * getClientId(); espMqttClientTypes::DisconnectReason getDisconnectReason(); MqttClient * getMqttClient(); - void setWill(const char * topic); protected: void onConfigUpdated(); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 3e18c7d9e..88e7f453b 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -514,16 +514,6 @@ void Mqtt::on_connect() { // re-subscribe to all custom registered MQTT topics resubscribe(); - // create last will topic with the base prefixed. It has to be static because the client destroys the reference - static char will_topic[MQTT_TOPIC_MAX_SIZE]; - if (!Mqtt::base().empty()) { - snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", Mqtt::base().c_str()); - } else { - snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status"); - } - // EMSESP::esp8266React.setWill(will_topic); // with qos 1, retain true - - // publish to the last will topic (see Mqtt::start() function) to say we're alive queue_publish_retain("status", "online", true); // retain: https://github.com/emsesp/EMS-ESP32/discussions/2086 }