Skip to content

Commit

Permalink
Zigbee2mqtt: remove synchronization, FOLLOW OUR WIKI because we switc…
Browse files Browse the repository at this point in the history
…hed to our zigbee2mqtt fork for better performance
  • Loading branch information
awawa-dev committed Dec 31, 2024
1 parent 789ace3 commit 2504bca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
1 change: 0 additions & 1 deletion include/led-drivers/net/DriverNetZigbee2mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public slots:

Zigbee2mqttInstance _zigInstance;
std::atomic<bool> _discoveryFinished;
std::atomic<int> _colorsFinished;
int _timeLogger;
QString _discoveryMessage;
int _mqttId;
Expand Down
6 changes: 5 additions & 1 deletion sources/hyperhdr/HyperHdrDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ void HyperHdrDaemon::instanceStateChangedHandler(InstanceState state, quint8 ins
_flatProtoBuffersThread->start();
}

if (_mqttThread != nullptr && !_mqttThread->isRunning())
{
_mqttThread->start();
}

if (_networkThread != nullptr && !_networkThread->isRunning())
{
_networkThread->start();
Expand Down Expand Up @@ -396,7 +401,6 @@ void HyperHdrDaemon::startNetworkServices()
[_mqtt](QThread* mqttThread) {
THREAD_REMOVER(QString("MQTT"), mqttThread, _mqtt);
});
_mqttThread->start();
#endif
}

Expand Down
40 changes: 4 additions & 36 deletions sources/led-drivers/net/DriverNetZigbee2mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ namespace
{
constexpr auto ZIGBEE_DISCOVERY_MESSAGE = "zigbee2mqtt/bridge/devices";
constexpr int DEFAULT_TIME_MEASURE_MESSAGE = 25;
constexpr int DEFAULT_COMMUNICATION_TIMEOUT_MS = 200;
}

DriverNetZigbee2mqtt::DriverNetZigbee2mqtt(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig),
_discoveryFinished(false),
_colorsFinished(0),
_timeLogger(0),
_mqttId(mqttId++)
{
Expand Down Expand Up @@ -65,8 +63,7 @@ bool DriverNetZigbee2mqtt::powerOnOff(bool isOn)

for (const auto& lamp : _zigInstance.lamps)
{
QString topicDevice = QString("zigbee2mqtt/%1").arg(lamp.name);
QString topic = QString("%1/set").arg(topicDevice);
QString topic = QString("zigbee2mqtt/%1/set").arg(lamp.name);
QJsonObject row;

row["state"] = (isOn) ? "ON" : "OFF";
Expand All @@ -76,18 +73,12 @@ bool DriverNetZigbee2mqtt::powerOnOff(bool isOn)

if (isOn)
{
emit GlobalSignals::getInstance()->SignalMqttSubscribe(true, topicDevice);

row["state"] = "OFF";
doc.setObject(row);

lastWill.push_back(topic);
lastWill.push_back(doc.toJson(QJsonDocument::Compact));
}
else
{
emit GlobalSignals::getInstance()->SignalMqttSubscribe(false, topicDevice);
}
}

if (_zigInstance.lamps.size() > 0)
Expand Down Expand Up @@ -123,8 +114,7 @@ bool DriverNetZigbee2mqtt::powerOff()
int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
{
QJsonDocument doc;

_colorsFinished = std::min(ledValues.size(), _zigInstance.lamps.size());
auto start = InternalClock::nowPrecise();

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

auto start = InternalClock::nowPrecise();

std::unique_lock<std::mutex> lck(_mtx);
_cv.wait_for(lck, std::chrono::milliseconds(DEFAULT_COMMUNICATION_TIMEOUT_MS));

for (int timeout = 0; timeout < 20 && _colorsFinished > 0 && (InternalClock::nowPrecise() < start + DEFAULT_COMMUNICATION_TIMEOUT_MS); timeout++)
{
QThread::msleep(10);
}

if (_colorsFinished.exchange(0) > 0)
{
Warning(_log, "The communication timed out after %ims (%i)", (int)(InternalClock::nowPrecise() - start), (++_timeLogger));
}
else if (_timeLogger >= 0 && _timeLogger < DEFAULT_TIME_MEASURE_MESSAGE)

if (_timeLogger >= 0 && _timeLogger < DEFAULT_TIME_MEASURE_MESSAGE)
{
Info(_log, "The communication took: %ims (%i/%i)", (int)(InternalClock::nowPrecise() - start), ++_timeLogger, DEFAULT_TIME_MEASURE_MESSAGE);
}
Expand All @@ -198,14 +174,6 @@ void DriverNetZigbee2mqtt::handlerSignalMqttReceived(QString topic, QString payl
_discoveryMessage = payload;
_discoveryFinished = true;
}
else if (_colorsFinished > 0)
{
_colorsFinished--;
if (_colorsFinished == 0)
{
_cv.notify_all();
}
}
}

QJsonObject DriverNetZigbee2mqtt::discover(const QJsonObject& params)
Expand Down

0 comments on commit 2504bca

Please sign in to comment.