Skip to content

Commit

Permalink
LastWill by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
danimtb committed Nov 2, 2017
1 parent f050fdc commit 66bca3e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
14 changes: 6 additions & 8 deletions MqttManager/MqttDiscoveryComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ MqttDiscoveryComponent::MqttDiscoveryComponent(String component, String name)
this->entity_id.replace(" ", "_");

this->setConfigurtionVariable("name", name);
this->setConfigurtionVariable("state_topic", discovery_prefix + "/" + component + "/" + entity_id + "/" + state_suffix);

if (component == "light" || component == "switch")
{
Expand All @@ -27,24 +28,21 @@ void MqttDiscoveryComponent::setConfigurtionVariable(String configKey, String co
}
}

String MqttDiscoveryComponent::getConfigTopic()
String& MqttDiscoveryComponent::getConfigTopic()
{
return discovery_prefix + "/" + component + "/" + entity_id + "/" + discovery_suffix;
m_configTopic = discovery_prefix + "/" + component + "/" + entity_id + "/" + discovery_suffix;
return m_configTopic;
}

String MqttDiscoveryComponent::getStateTopic()
String& MqttDiscoveryComponent::getStateTopic()
{
if (m_fields.find("state_topic") != m_fields.end())
{
return m_fields["state_topic"];
}
else
{
return (discovery_prefix + "/" + component + "/" + entity_id + "/" + state_suffix);
}
}

String MqttDiscoveryComponent::getCommandTopic()
String& MqttDiscoveryComponent::getCommandTopic()
{
return m_fields["command_topic"];
}
Expand Down
7 changes: 4 additions & 3 deletions MqttManager/MqttDiscoveryComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class MqttDiscoveryComponent
String discovery_suffix{"config"};
String state_suffix{"state"};
String command_suffix{"command"};
String m_configTopic;


String getConfigTopic();
String getStateTopic();
String getCommandTopic();
String& getConfigTopic();
String& getStateTopic();
String& getCommandTopic();
String getConfigPayload();
};

Expand Down
10 changes: 6 additions & 4 deletions MqttManager/MqttManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
MqttManager::MqttManager()
{
m_connected = false;
lastWillPayload = "Offline";


m_checkConnectivityTimeOnline = 20000;
Expand Down Expand Up @@ -56,6 +57,7 @@ void MqttManager::setDeviceData(String deviceName, String hardware, String devic
m_mqttClient.setClientId(m_deviceName.c_str());

m_deviceDataTopic = "/" + m_deviceName;
m_lastWillTopic = m_deviceDataTopic + "/status";


if (m_mqttDiscoveryEnabled)
Expand All @@ -78,11 +80,11 @@ 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");
this->setLastWillMQTT(m_deviceStatusSensor->getStateTopic(), m_lastWillPayload);
}
else
{
this->setLastWillMQTT(m_deviceDataTopic + "/status", "Offline");
this->setLastWillMQTT(m_lastWillTopic, m_lastWillPayload);
}
}

Expand Down Expand Up @@ -239,9 +241,9 @@ void MqttManager::setCallback(void (*callback)(String , String))
messageReceivedCallback = callback;
}

void MqttManager::setLastWillMQTT(String topic, const char* payload)
void MqttManager::setLastWillMQTT(const String& topic, const String& payload)
{
m_mqttClient.setWill(topic.c_str(), 1, true, payload);
m_mqttClient.setWill(topic.c_str(), 1, true, payload.c_str());
}

void MqttManager::setDeviceStatusInfoTime(unsigned long deviceStatusInfoTime)
Expand Down
5 changes: 4 additions & 1 deletion MqttManager/MqttManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class MqttManager

String m_deviceDataTopic;

String m_lastWillPayload;
String m_lastWillTopic;

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

public:
MqttManager();
Expand Down

0 comments on commit 66bca3e

Please sign in to comment.