Skip to content

Commit

Permalink
Add global MQTT communication signals
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Dec 26, 2024
1 parent 90017ee commit 32e85af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public slots:

void handleSettingsUpdate(settings::type type, const QJsonDocument& config);

void handleSignalMqttSubscribe(bool subscribe, QString topic);

private slots:
void connected();
void error(const QMQTT::ClientError error);
Expand Down
4 changes: 4 additions & 0 deletions include/utils/GlobalSignals.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ class GlobalSignals : public QObject
void SignalSetLut(MemoryBuffer<uint8_t>* lut);

void SignalLutRequest();

void SignalMqttSubscribe(bool subscribe, QString topic);

void SignalMqttReceived(QString topic, QString payload);
};
23 changes: 22 additions & 1 deletion sources/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QHostInfo>

#include <api/HyperAPI.h>
#include <utils/GlobalSignals.h>

// default param %1 is 'HyperHDR', do not edit templates here
const static QString TEMPLATE_HYPERHDRAPI = QStringLiteral("%1/JsonAPI");
Expand Down Expand Up @@ -114,6 +115,8 @@ void mqtt::connected()
{
Debug(_log, "Connected");

connect(GlobalSignals::getInstance(), &GlobalSignals::SignalMqttSubscribe, this, &mqtt::handleSignalMqttSubscribe);

if (_retryTimer != nullptr)
{
Debug(_log, "Removing retry timer");
Expand All @@ -122,12 +125,26 @@ void mqtt::connected()
_retryTimer = nullptr;
}

if (_clientInstance != nullptr)
if (_clientInstance != nullptr && !_disableApiAccess)
{
_clientInstance->subscribe(HYPERHDRAPI, 2);
}
}

void mqtt::handleSignalMqttSubscribe(bool subscribe, QString topic)
{
if (_clientInstance == nullptr)
return;

if (subscribe)
{
_clientInstance->subscribe(topic, 2);
}
else
{
_clientInstance->unsubscribe(topic);
}
}

void mqtt::error(const QMQTT::ClientError error)
{
Expand Down Expand Up @@ -307,4 +324,8 @@ void mqtt::received(const QMQTT::Message& message)
}
_clientInstance->publish(result);
}
else
{
emit GlobalSignals::getInstance()->SignalMqttReceived(topic, payload);
}
}

0 comments on commit 32e85af

Please sign in to comment.