From ddf70f5d8a36e9ca525aaae1e0d89667f666e111 Mon Sep 17 00:00:00 2001 From: technyon Date: Mon, 27 Mar 2023 18:46:07 +0200 Subject: [PATCH] add lwt topic --- MqttTopics.h | 1 + Network.cpp | 13 ++++++++----- Network.h | 5 +++++ networkDevices/EthLan8720Device.cpp | 13 +++++++++++++ networkDevices/EthLan8720Device.h | 2 ++ networkDevices/NetworkDevice.h | 1 + networkDevices/W5500Device.cpp | 5 +++++ networkDevices/W5500Device.h | 2 ++ networkDevices/WifiDevice.cpp | 12 ++++++++++++ networkDevices/WifiDevice.h | 2 ++ 10 files changed, 51 insertions(+), 5 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index dce7bf2..b80feee 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -59,4 +59,5 @@ #define mqtt_topic_freeheap "/maintenance/freeHeap" #define mqtt_topic_restart_reason_fw "/maintenance/restartReasonNukiHub" #define mqtt_topic_restart_reason_esp "/maintenance/restartReasonNukiEsp" +#define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState" #define mqtt_topic_network_device "/maintenance/networkDevice" \ No newline at end of file diff --git a/Network.cpp b/Network.cpp index 8ab8cdc..70a3a8d 100644 --- a/Network.cpp +++ b/Network.cpp @@ -1,6 +1,5 @@ #include "Network.h" #include "PreferencesKeys.h" -#include "MqttTopics.h" #include "networkDevices/W5500Device.h" #include "networkDevices/WifiDevice.h" #include "Logger.h" @@ -369,20 +368,21 @@ bool Network::reconnect() Log->println(F("Attempting MQTT connection")); _connectReplyReceived = false; + if(strlen(_mqttUser) == 0) { Log->println(F("MQTT: Connecting without credentials")); - _device->mqttSetServer(_mqttBrokerAddr, port); - _device->mqttConnect(); } else { Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser); _device->mqttSetCredentials(_mqttUser, _mqttPass); - _device->mqttSetServer(_mqttBrokerAddr, port); - _device->mqttConnect(); } + _device->setWill(_mqttConnectionStateTopic, 1, true, _lastWillPayload); + _device->mqttSetServer(_mqttBrokerAddr, port); + _device->mqttConnect(); + unsigned long timeout = millis() + 60000; while(!_connectReplyReceived && millis() < timeout) @@ -416,6 +416,9 @@ bool Network::reconnect() _device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str()); } } + + publishString(_maintenancePathPrefix, _mqttConnectionStateTopic, "online"); + _mqttConnectionState = 2; for(const auto& callback : _reconnectedCallbacks) { diff --git a/Network.h b/Network.h index e14f553..121637e 100644 --- a/Network.h +++ b/Network.h @@ -6,6 +6,7 @@ #include "networkDevices/NetworkDevice.h" #include "MqttReceiver.h" #include "networkDevices/IPConfiguration.h" +#include "MqttTopics.h" enum class NetworkDeviceType { @@ -99,6 +100,10 @@ private: void buildMqttPath(const char* prefix, const char* path, char* outPath); static Network* _inst; + + const char* _mqttConnectionStateTopic = mqtt_topic_mqtt_connection_state; + const char* _lastWillPayload = "offline"; + Preferences* _preferences; IPConfiguration* _ipConfiguration = nullptr; String _hostname; diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index 9053514..2c56464 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -255,6 +255,18 @@ bool EthLan8720Device::mqttDisonnect(bool force) } } +void EthLan8720Device::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + if(_useEncryption) + { + _mqttClientSecure->setWill(topic, qos, retain, payload); + } + else + { + _mqttClient->setWill(topic, qos, retain, payload); + } +} + void EthLan8720Device::mqttSetCredentials(const char *username, const char *password) { if(_useEncryption) @@ -329,3 +341,4 @@ void EthLan8720Device::disableMqtt() _mqttEnabled = false; } + diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index cc8d92e..4f4af9d 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -53,6 +53,8 @@ public: bool mqttDisonnect(bool force) override; + void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; + void mqttSetCredentials(const char *username, const char *password) override; void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override; diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index b73b058..6d55e3c 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -40,6 +40,7 @@ public: virtual void mqttSetServer(const char* host, uint16_t port) = 0; virtual bool mqttConnect() = 0; virtual bool mqttDisonnect(bool force) = 0; + virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) = 0; virtual void mqttSetCredentials(const char* username, const char* password) = 0; virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) = 0; virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) = 0; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index a6d5801..8c6ab43 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -265,6 +265,11 @@ bool W5500Device::mqttDisonnect(bool force) return _mqttClient.disconnect(force); } +void W5500Device::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + _mqttClient.setWill(topic, qos, retain, payload); +} + void W5500Device::mqttSetCredentials(const char *username, const char *password) { _mqttClient.setCredentials(username, password); diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index 8fa330a..3d4ed3c 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -49,6 +49,8 @@ public: bool mqttDisonnect(bool force) override; + void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; + void mqttSetCredentials(const char *username, const char *password) override; void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override; diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index d614778..bb66d42 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -273,6 +273,18 @@ bool WifiDevice::mqttDisonnect(bool force) } } +void WifiDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + if(_useEncryption) + { + _mqttClientSecure->setWill(topic, qos, retain, payload); + } + else + { + _mqttClient->setWill(topic, qos, retain, payload); + } +} + void WifiDevice::mqttSetCredentials(const char *username, const char *password) { if(_useEncryption) diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 3c04220..f05af77 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -43,6 +43,8 @@ public: bool mqttDisonnect(bool force) override; + void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; + void mqttSetCredentials(const char *username, const char *password) override; void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;