@@ -24,13 +24,12 @@ MqttManager::MqttManager()
24
24
m_checkConnectivityTimer.setup (RT_ON, m_checkConnectivityTimeOffline);
25
25
}
26
26
27
- void MqttManager::setup (String mqttServer, String mqttPort, String mqttUsername, String mqttPassword, bool mqttDiscoveryEnabled )
27
+ void MqttManager::setup (String mqttServer, String mqttPort, String mqttUsername, String mqttPassword)
28
28
{
29
29
m_mqttServer = mqttServer;
30
30
m_mqttPort = atoi (mqttPort.c_str ());
31
31
m_mqttUsername = mqttUsername;
32
32
m_mqttPassword = mqttPassword;
33
- m_mqttDiscoveryEnabled = mqttDiscoveryEnabled;
34
33
35
34
IPAddress server;
36
35
server.fromString (m_mqttServer.c_str ());
@@ -57,71 +56,49 @@ void MqttManager::setDeviceData(String deviceName, String hardware, String devic
57
56
m_mqttClient.setClientId (m_deviceName.c_str ());
58
57
59
58
m_deviceDataTopic = " /" + m_deviceName;
60
- m_lastWillTopic = m_deviceDataTopic + " /status" ;
61
59
60
+ m_deviceStatusSensor = MqttDiscoveryComponent (" binary_sensor" , m_deviceName + " Status" , discovery_prefix);
61
+ m_deviceStatusSensor.setConfigurtionVariable (" device_class" , " connectivity" );
62
+ this ->addDiscoveryComponent (m_deviceStatusSensor);
62
63
63
- if (m_mqttDiscoveryEnabled)
64
- {
65
- m_deviceStatusSensor = new MqttDiscoveryComponent (" binary_sensor" , m_deviceName + " Status" , discovery_prefix);
66
- m_deviceStatusSensor->setConfigurtionVariable (" device_class" , " connectivity" );
67
- m_discoveryComponents.push_back (m_deviceStatusSensor);
68
-
69
- m_deviceIpSensor = new MqttDiscoveryComponent (" sensor" , m_deviceName + " IP" , discovery_prefix);
70
- m_discoveryComponents.push_back (m_deviceIpSensor);
64
+ m_deviceIpSensor = MqttDiscoveryComponent (" sensor" , m_deviceName + " IP" , discovery_prefix);
65
+ this ->addDiscoveryComponent (m_deviceIpSensor);
71
66
72
- m_deviceMacSensor = new MqttDiscoveryComponent (" sensor" , m_deviceName + " MAC" , discovery_prefix);
73
- m_discoveryComponents. push_back (m_deviceMacSensor);
67
+ m_deviceMacSensor = MqttDiscoveryComponent (" sensor" , m_deviceName + " MAC" , discovery_prefix);
68
+ this -> addDiscoveryComponent (m_deviceMacSensor);
74
69
75
- m_deviceHardwareSensor = new MqttDiscoveryComponent (" sensor" , m_deviceName + " Hardware" , discovery_prefix);
76
- m_discoveryComponents. push_back (m_deviceHardwareSensor);
70
+ m_deviceHardwareSensor = MqttDiscoveryComponent (" sensor" , m_deviceName + " Hardware" , discovery_prefix);
71
+ this -> addDiscoveryComponent (m_deviceHardwareSensor);
77
72
78
- m_deviceFirmwareSensor = new MqttDiscoveryComponent (" sensor" , m_deviceName + " Firmware" , discovery_prefix);
79
- m_discoveryComponents. push_back (m_deviceFirmwareSensor);
73
+ m_deviceFirmwareSensor = MqttDiscoveryComponent (" sensor" , m_deviceName + " Firmware" , discovery_prefix);
74
+ this -> addDiscoveryComponent (m_deviceFirmwareSensor);
80
75
81
- m_deviceFirmwareVersionSensor = new MqttDiscoveryComponent (" sensor" , m_deviceName + " Firmware Version" , discovery_prefix);
82
- m_discoveryComponents. push_back (m_deviceFirmwareVersionSensor);
76
+ m_deviceFirmwareVersionSensor = MqttDiscoveryComponent (" sensor" , m_deviceName + " Firmware Version" , discovery_prefix);
77
+ this -> addDiscoveryComponent (m_deviceFirmwareVersionSensor);
83
78
84
- this ->setLastWillMQTT (m_deviceStatusSensor->getStateTopic (), m_lastWillPayload);
85
- }
86
- else
87
- {
88
- this ->setLastWillMQTT (m_lastWillTopic, m_lastWillPayload);
89
- }
79
+ this ->setLastWillMQTT (m_deviceStatusSensor.getStateTopic (), m_lastWillPayload);
90
80
}
91
81
92
82
void MqttManager::publishDiscoveryInfo ()
93
83
{
94
- if (m_mqttDiscoveryEnabled )
84
+ if (m_discoveryComponents. size () == m_discoveryCounter )
95
85
{
96
- for (auto const &component : m_discoveryComponents)
97
- {
98
- this ->publishMQTT (component->getConfigTopic (), component->getConfigPayload ());
99
- }
86
+ m_discoveryCounter = 0 ;
100
87
}
88
+
89
+ MqttDiscoveryComponent component = m_discoveryComponents[m_discoveryCounter];
90
+ this ->publishMQTT (component.getConfigTopic (), component.getConfigPayload ());
91
+ ++m_discoveryCounter;
101
92
}
102
93
103
94
void MqttManager::publishDeviceStatusInfo ()
104
95
{
105
- if (!m_mqttDiscoveryEnabled)
106
- {
107
- this ->publishMQTT (m_deviceDataTopic + " /status" , " ON" );
108
- this ->publishMQTT (m_deviceDataTopic + " /ip" , m_deviceIP);
109
- this ->publishMQTT (m_deviceDataTopic + " /mac" , m_deviceMac);
110
- this ->publishMQTT (m_deviceDataTopic + " /hardware" , m_hardware);
111
- this ->publishMQTT (m_deviceDataTopic + " /firmware" , m_firmware);
112
- this ->publishMQTT (m_deviceDataTopic + " /firmware_version" , m_firmwareVersion);
113
- }
114
- else
115
- {
116
- this ->publishMQTT (m_deviceStatusSensor->getStateTopic (), " ON" );
117
- this ->publishMQTT (m_deviceIpSensor->getStateTopic (), m_deviceIP);
118
- this ->publishMQTT (m_deviceMacSensor->getStateTopic (), m_deviceMac);
119
- this ->publishMQTT (m_deviceHardwareSensor->getStateTopic (), m_hardware);
120
- this ->publishMQTT (m_deviceFirmwareSensor->getStateTopic (), m_firmware);
121
- this ->publishMQTT (m_deviceFirmwareVersionSensor->getStateTopic (), m_firmwareVersion);
122
- }
123
-
124
- this ->refreshStatusTopics ();
96
+ this ->publishMQTT (m_deviceStatusSensor.getStateTopic (), " ON" );
97
+ this ->publishMQTT (m_deviceIpSensor.getStateTopic (), m_deviceIP);
98
+ this ->publishMQTT (m_deviceMacSensor.getStateTopic (), m_deviceMac);
99
+ this ->publishMQTT (m_deviceHardwareSensor.getStateTopic (), m_hardware);
100
+ this ->publishMQTT (m_deviceFirmwareSensor.getStateTopic (), m_firmware);
101
+ this ->publishMQTT (m_deviceFirmwareVersionSensor.getStateTopic (), m_firmwareVersion);
125
102
}
126
103
127
104
void MqttManager::checkConnectivity ()
@@ -148,6 +125,7 @@ void MqttManager::checkConnectivity()
148
125
else
149
126
{
150
127
m_connected = true ;
128
+ this ->publishDiscoveryInfo ();
151
129
}
152
130
}
153
131
@@ -159,30 +137,10 @@ void MqttManager::setDeviceMac()
159
137
}
160
138
}
161
139
162
- void MqttManager::enableDiscovery (bool enable)
163
- {
164
- m_mqttDiscoveryEnabled = enable;
165
- }
166
-
167
- void MqttManager::addDiscoveryComponent (MqttDiscoveryComponent * component)
140
+ void MqttManager::addDiscoveryComponent (MqttDiscoveryComponent component)
168
141
{
169
142
m_discoveryComponents.push_back (component);
170
-
171
- this ->addSubscribeTopic (component->getCommandTopic ());
172
- this ->addStatusTopic (component->getStateTopic ());
173
- }
174
-
175
- void MqttManager::addStatusTopic (String statusTopic)
176
- {
177
- if (statusTopic.length () != 0 )
178
- {
179
- m_statusTopics[statusTopic] = " " ;
180
- }
181
- }
182
-
183
- void MqttManager::clearStatusTopics ()
184
- {
185
- m_statusTopics.clear ();
143
+ this ->addSubscribeTopic (component.getCommandTopic ());
186
144
}
187
145
188
146
void MqttManager::addSubscribeTopic (String subscribeTopic)
@@ -195,9 +153,9 @@ void MqttManager::addSubscribeTopic(String subscribeTopic)
195
153
196
154
void MqttManager::clearSubscribeTopics ()
197
155
{
198
- for (uint8_t i= 0 ; i < m_subscribeTopics. size (); i++ )
156
+ for (auto &topic : m_subscribeTopics)
199
157
{
200
- m_mqttClient.unsubscribe (m_subscribeTopics[i] .c_str ());
158
+ m_mqttClient.unsubscribe (topic .c_str ());
201
159
}
202
160
m_subscribeTopics.clear ();
203
161
}
@@ -216,18 +174,11 @@ void MqttManager::stopConnection()
216
174
217
175
void MqttManager::publishMQTT (String topic, String payload)
218
176
{
219
- if (m_statusTopics.find (topic) != m_statusTopics.end () && m_statusTopics[topic] != payload)
220
- {
221
- m_statusTopics[topic] = payload;
222
-
223
- if (m_connected)
224
- {
225
- this ->refreshStatusTopics ();
226
- }
227
- }
228
- else if (m_connected)
177
+ if (m_connected)
229
178
{
230
179
m_mqttClient.publish (topic.c_str (), 1 , true , payload.c_str ());
180
+ Serial.println (topic.c_str ());
181
+ Serial.println (payload.c_str ());
231
182
}
232
183
}
233
184
@@ -266,22 +217,13 @@ void MqttManager::loop()
266
217
{
267
218
if (m_deviceStatusInfoTimer.check ())
268
219
{
269
- this ->publishDiscoveryInfo ();
270
220
this ->publishDeviceStatusInfo ();
271
221
272
222
m_deviceStatusInfoTimer.start (); // restart timer
273
223
}
274
224
}
275
225
}
276
226
277
- void MqttManager::refreshStatusTopics ()
278
- {
279
- for ( auto const &topic : m_statusTopics)
280
- {
281
- this ->publishMQTT (topic.first , topic.second );
282
- }
283
- }
284
-
285
227
bool MqttManager::connected ()
286
228
{
287
229
return m_connected;
0 commit comments