add lwt topic
This commit is contained in:
@@ -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"
|
||||
13
Network.cpp
13
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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user