Skip to content

Commit

Permalink
remove setWill() to inline
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Nov 25, 2024
1 parent acebc03 commit 2751aca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
31 changes: 10 additions & 21 deletions lib/framework/MqttSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<espMqttClientSecure *>(_mqttClient)->setWill(topic, 1, true, "offline");
return;
}
#endif
static_cast<espMqttClient *>(_mqttClient)->setWill(topic, 1, true, "offline");
}

void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties & properties,
const char * topic,
const uint8_t * payload,
Expand Down Expand Up @@ -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) {
Expand All @@ -197,6 +195,7 @@ bool MqttSettingsService::configureMqtt() {
static_cast<espMqttClientSecure *>(_mqttClient)->setClientId(_state.clientId.c_str());
static_cast<espMqttClientSecure *>(_mqttClient)->setKeepAlive(_state.keepAlive);
static_cast<espMqttClientSecure *>(_mqttClient)->setCleanSession(_state.cleanSession);
static_cast<espMqttClientSecure *>(_mqttClient)->setWill(will_topic, 1, true, "offline"); // QOS 1, retain
return _mqttClient->connect();
}
#endif
Expand All @@ -207,17 +206,7 @@ bool MqttSettingsService::configureMqtt() {
static_cast<espMqttClient *>(_mqttClient)->setClientId(_state.clientId.c_str());
static_cast<espMqttClient *>(_mqttClient)->setKeepAlive(_state.keepAlive);
static_cast<espMqttClient *>(_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<espMqttClient *>(_mqttClient)->setWill(will_topic, 1, true, "offline"); // QOS 1, retain
return _mqttClient->connect();
}

Expand Down
1 change: 0 additions & 1 deletion lib/framework/MqttSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
const char * getClientId();
espMqttClientTypes::DisconnectReason getDisconnectReason();
MqttClient * getMqttClient();
void setWill(const char * topic);

protected:
void onConfigUpdated();
Expand Down
10 changes: 0 additions & 10 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2751aca

Please sign in to comment.