mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
Merge pull request #2248 from proddy/dev
fix Last Will (LWT) not set on MQTT Connect #2247
This commit is contained in:
@@ -27,6 +27,7 @@ For more details go to [docs.emsesp.org](https://docs.emsesp.org/).
|
||||
- analog dac output and inputs on dac pins [#2201](https://github.com/emsesp/EMS-ESP32/discussions/2201)
|
||||
- api memory leak [#2216](https://github.com/emsesp/EMS-ESP32/issues/2216)
|
||||
- modbus multiple mixers [#2229](https://github.com/emsesp/EMS-ESP32/issues/2229)
|
||||
- Last Will (LWT) not set on MQTT Connect [#2247](https://github.com/emsesp/EMS-ESP32/issues/2247)
|
||||
|
||||
## Changed
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { Link, useLocation, useNavigate } from 'react-router';
|
||||
|
||||
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
||||
import MenuIcon from '@mui/icons-material/Menu';
|
||||
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
|
||||
import { AppBar, IconButton, Toolbar, Typography } from '@mui/material';
|
||||
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
@@ -59,10 +58,7 @@ const LayoutAppBar = ({ title, onToggleDrawer }: LayoutAppBarProps) => {
|
||||
>
|
||||
<Typography variant="h6">
|
||||
{pathnames[0] === 'status' ? LL.STATUS_OF('') : LL.SETTINGS(0)}
|
||||
<NavigateNextIcon
|
||||
sx={{ fontSize: 20, verticalAlign: 'middle' }}
|
||||
color="primary"
|
||||
/>
|
||||
<span style={{ color: '#90caf9' }}> | </span>
|
||||
</Typography>
|
||||
</Link>
|
||||
</>
|
||||
|
||||
@@ -57,10 +57,6 @@ class ESP8266React {
|
||||
// special functions needed outside scope
|
||||
//
|
||||
|
||||
void setWill(const char * will_topic) {
|
||||
_mqttSettingsService.setWill(will_topic);
|
||||
}
|
||||
|
||||
// true if AP is active
|
||||
bool apStatus() {
|
||||
return _apSettingsService.getAPNetworkStatus() == APNetworkStatus::ACTIVE;
|
||||
|
||||
@@ -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,
|
||||
@@ -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<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
|
||||
@@ -207,6 +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);
|
||||
static_cast<espMqttClient *>(_mqttClient)->setWill(will_topic, 1, true, "offline"); // QOS 1, retain
|
||||
return _mqttClient->connect();
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
10
src/mqtt.cpp
10
src/mqtt.cpp
@@ -386,16 +386,6 @@ void Mqtt::start() {
|
||||
// add the 'publish' command ('call system publish' in console or via API)
|
||||
Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd));
|
||||
|
||||
// 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
|
||||
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
Mqtt::on_connect(); // simulate an MQTT connection
|
||||
#endif
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define EMSESP_APP_VERSION "3.7.1-dev.11"
|
||||
#define EMSESP_APP_VERSION "3.7.1-dev.12"
|
||||
Reference in New Issue
Block a user