Skip to content

Commit

Permalink
Add status topic for online and offline monitoring with last will fea…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
danimtb committed Oct 30, 2017
1 parent d31b3b2 commit f050fdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
32 changes: 15 additions & 17 deletions MqttManager/MqttManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void MqttManager::setDeviceData(String deviceName, String hardware, String devic

if (m_mqttDiscoveryEnabled)
{
m_deviceNameSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " name");
m_discoveryComponents.push_back(m_deviceNameSensor);
m_deviceStatusSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Status");
m_discoveryComponents.push_back(m_deviceStatusSensor);

m_deviceIpSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " IP");
m_discoveryComponents.push_back(m_deviceIpSensor);
Expand All @@ -77,6 +77,12 @@ void MqttManager::setDeviceData(String deviceName, String hardware, String devic

m_deviceFirmwareVersionSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Firmware Version");
m_discoveryComponents.push_back(m_deviceFirmwareVersionSensor);

this->setLastWillMQTT(m_deviceStatusSensor->getStateTopic(), "Offline");
}
else
{
this->setLastWillMQTT(m_deviceDataTopic + "/status", "Offline");
}
}

Expand All @@ -95,24 +101,16 @@ void MqttManager::publishDeviceStatusInfo()
{
if (!m_mqttDiscoveryEnabled)
{
StaticJsonBuffer<200> deviceDataBuffer;
JsonObject& deviceDataObject = deviceDataBuffer.createObject();
String deviceDataString;

deviceDataObject["name"] = m_deviceName;
deviceDataObject["ip"] = m_deviceIP;
deviceDataObject["mac"] = m_deviceMac;
deviceDataObject["hardware"] = m_hardware;
deviceDataObject["firmware"] = m_firmware;
deviceDataObject["firmware_version"] = m_firmwareVersion;

deviceDataObject.printTo(deviceDataString);

this->publishMQTT(m_deviceDataTopic, deviceDataString);
this->publishMQTT(m_deviceDataTopic + "/status", "Online");
this->publishMQTT(m_deviceDataTopic + "/ip", m_deviceIP);
this->publishMQTT(m_deviceDataTopic + "/mac", m_deviceMac);
this->publishMQTT(m_deviceDataTopic + "/hardware", m_hardware);
this->publishMQTT(m_deviceDataTopic + "/firmware", m_firmware);
this->publishMQTT(m_deviceDataTopic + "/firmware_version", m_firmwareVersion);
}
else
{
this->publishMQTT(m_deviceNameSensor->getStateTopic(), m_deviceName);
this->publishMQTT(m_deviceStatusSensor->getStateTopic(), "Online");
this->publishMQTT(m_deviceIpSensor->getStateTopic(), m_deviceIP);
this->publishMQTT(m_deviceMacSensor->getStateTopic(), m_deviceMac);
this->publishMQTT(m_deviceHardwareSensor->getStateTopic(), m_hardware);
Expand Down
4 changes: 2 additions & 2 deletions MqttManager/MqttManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MqttManager

String m_deviceDataTopic;

MqttDiscoveryComponent* m_deviceNameSensor;
MqttDiscoveryComponent* m_deviceStatusSensor;
MqttDiscoveryComponent* m_deviceIpSensor;
MqttDiscoveryComponent* m_deviceMacSensor;
MqttDiscoveryComponent* m_deviceHardwareSensor;
Expand All @@ -60,6 +60,7 @@ class MqttManager
void publishDiscoveryInfo();
void refreshStatusTopics();
void checkConnectivity();
void setLastWillMQTT(String topic, const char* payload);

public:
MqttManager();
Expand All @@ -68,7 +69,6 @@ class MqttManager

void setDeviceData(String deviceName, String hardware, String deviceIP, String firmware, String firmwareVersion);
void setCallback(void (*callback)(String , String));
void setLastWillMQTT(String topic, const char* payload);

void setDeviceStatusInfoTime(unsigned long deviceStatusInfoTime);

Expand Down

0 comments on commit f050fdc

Please sign in to comment.