Skip to content

Commit bd7fdc2

Browse files
committed
Zigbee2mqtt: more accurate synchronization
1 parent 27643cb commit bd7fdc2

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

include/led-drivers/net/DriverNetZigbee2mqtt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public slots:
5454
bool powerOnOff(bool isOn);
5555

5656
Zigbee2mqttInstance _zigInstance;
57-
std::atomic<bool> _discoveryFinished, _colorsFinished;
57+
std::atomic<bool> _discoveryFinished;
58+
std::atomic<int> _colorsFinished;
5859
int _timeLogger;
5960
QString _discoveryMessage;
6061

sources/led-drivers/net/DriverNetZigbee2mqtt.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <led-drivers/net/DriverNetZigbee2mqtt.h>
22
#include <utils/GlobalSignals.h>
3+
#include <utils/InternalClock.h>
34

45
namespace
56
{
@@ -10,7 +11,7 @@ namespace
1011
DriverNetZigbee2mqtt::DriverNetZigbee2mqtt(const QJsonObject& deviceConfig)
1112
: LedDevice(deviceConfig),
1213
_discoveryFinished(false),
13-
_colorsFinished(false),
14+
_colorsFinished(0),
1415
_timeLogger(0)
1516
{
1617
}
@@ -104,7 +105,7 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
104105
{
105106
QJsonDocument doc;
106107

107-
_colorsFinished = false;
108+
_colorsFinished = std::min(ledValues.size(), _zigInstance.lamps.size());
108109

109110
auto rgb = ledValues.begin();
110111
for (const auto& lamp : _zigInstance.lamps)
@@ -149,19 +150,20 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
149150
emit GlobalSignals::getInstance()->SignalMqttPublish(topic, doc.toJson(QJsonDocument::Compact));
150151
}
151152

152-
int timeout = 0;
153-
for (timeout = 0; timeout < 20 && !_colorsFinished; timeout++)
153+
auto start = InternalClock::nowPrecise();
154+
155+
for (int timeout = 0; timeout < 25 && _colorsFinished > 0; timeout++)
154156
{
155-
QThread::msleep(10);
157+
QThread::msleep(8);
156158
}
157159

158-
if (!_colorsFinished)
160+
if (_colorsFinished > 0)
159161
{
160-
Warning(_log, "The communication timed out after 200ms (%i)", (++_timeLogger));
162+
Warning(_log, "The communication timed out after %ims (%i)", (int)(InternalClock::nowPrecise() - start), (++_timeLogger));
161163
}
162164
else if (_timeLogger >= 0 && _timeLogger < DEFAULT_TIME_MEASURE_MESSAGE)
163165
{
164-
Info(_log, "The communication took: %ims (%i/%i)", timeout * 10, ++_timeLogger, DEFAULT_TIME_MEASURE_MESSAGE);
166+
Info(_log, "The communication took: %ims (%i/%i)", (int)(InternalClock::nowPrecise() - start), ++_timeLogger, DEFAULT_TIME_MEASURE_MESSAGE);
165167
}
166168

167169
return 0;
@@ -174,9 +176,9 @@ void DriverNetZigbee2mqtt::handlerSignalMqttReceived(QString topic, QString payl
174176
_discoveryMessage = payload;
175177
_discoveryFinished = true;
176178
}
177-
else
179+
else if (_colorsFinished > 0)
178180
{
179-
_colorsFinished = true;
181+
_colorsFinished--;
180182
}
181183
}
182184

sources/mqtt/mqtt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void mqtt::handleSignalMqttSubscribe(bool subscribe, QString topic)
150150

151151
if (subscribe)
152152
{
153-
_clientInstance->subscribe(topic, 2);
153+
_clientInstance->subscribe(topic, 0);
154154
}
155155
else
156156
{

0 commit comments

Comments
 (0)