Skip to content

Commit

Permalink
Zigbee2mqtt: more accurate synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Dec 28, 2024
1 parent 27643cb commit bd7fdc2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/led-drivers/net/DriverNetZigbee2mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public slots:
bool powerOnOff(bool isOn);

Zigbee2mqttInstance _zigInstance;
std::atomic<bool> _discoveryFinished, _colorsFinished;
std::atomic<bool> _discoveryFinished;
std::atomic<int> _colorsFinished;
int _timeLogger;
QString _discoveryMessage;

Expand Down
22 changes: 12 additions & 10 deletions sources/led-drivers/net/DriverNetZigbee2mqtt.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <led-drivers/net/DriverNetZigbee2mqtt.h>
#include <utils/GlobalSignals.h>
#include <utils/InternalClock.h>

namespace
{
Expand All @@ -10,7 +11,7 @@ namespace
DriverNetZigbee2mqtt::DriverNetZigbee2mqtt(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig),
_discoveryFinished(false),
_colorsFinished(false),
_colorsFinished(0),
_timeLogger(0)
{
}
Expand Down Expand Up @@ -104,7 +105,7 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
{
QJsonDocument doc;

_colorsFinished = false;
_colorsFinished = std::min(ledValues.size(), _zigInstance.lamps.size());

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

int timeout = 0;
for (timeout = 0; timeout < 20 && !_colorsFinished; timeout++)
auto start = InternalClock::nowPrecise();

for (int timeout = 0; timeout < 25 && _colorsFinished > 0; timeout++)
{
QThread::msleep(10);
QThread::msleep(8);
}

if (!_colorsFinished)
if (_colorsFinished > 0)
{
Warning(_log, "The communication timed out after 200ms (%i)", (++_timeLogger));
Warning(_log, "The communication timed out after %ims (%i)", (int)(InternalClock::nowPrecise() - start), (++_timeLogger));
}
else if (_timeLogger >= 0 && _timeLogger < DEFAULT_TIME_MEASURE_MESSAGE)
{
Info(_log, "The communication took: %ims (%i/%i)", timeout * 10, ++_timeLogger, DEFAULT_TIME_MEASURE_MESSAGE);
Info(_log, "The communication took: %ims (%i/%i)", (int)(InternalClock::nowPrecise() - start), ++_timeLogger, DEFAULT_TIME_MEASURE_MESSAGE);
}

return 0;
Expand All @@ -174,9 +176,9 @@ void DriverNetZigbee2mqtt::handlerSignalMqttReceived(QString topic, QString payl
_discoveryMessage = payload;
_discoveryFinished = true;
}
else
else if (_colorsFinished > 0)
{
_colorsFinished = true;
_colorsFinished--;
}
}

Expand Down
2 changes: 1 addition & 1 deletion sources/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void mqtt::handleSignalMqttSubscribe(bool subscribe, QString topic)

if (subscribe)
{
_clientInstance->subscribe(topic, 2);
_clientInstance->subscribe(topic, 0);
}
else
{
Expand Down

0 comments on commit bd7fdc2

Please sign in to comment.