diff --git a/include/mqtt/mqtt.h b/include/mqtt/mqtt.h index d8dd5a40..68c5aae7 100644 --- a/include/mqtt/mqtt.h +++ b/include/mqtt/mqtt.h @@ -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); diff --git a/include/utils/GlobalSignals.h b/include/utils/GlobalSignals.h index c15d5f81..17fccdac 100644 --- a/include/utils/GlobalSignals.h +++ b/include/utils/GlobalSignals.h @@ -99,4 +99,8 @@ class GlobalSignals : public QObject void SignalSetLut(MemoryBuffer* lut); void SignalLutRequest(); + + void SignalMqttSubscribe(bool subscribe, QString topic); + + void SignalMqttReceived(QString topic, QString payload); }; diff --git a/sources/mqtt/mqtt.cpp b/sources/mqtt/mqtt.cpp index 946525b7..96e5c35c 100644 --- a/sources/mqtt/mqtt.cpp +++ b/sources/mqtt/mqtt.cpp @@ -9,6 +9,7 @@ #include #include +#include // default param %1 is 'HyperHDR', do not edit templates here const static QString TEMPLATE_HYPERHDRAPI = QStringLiteral("%1/JsonAPI"); @@ -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"); @@ -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) { @@ -307,4 +324,8 @@ void mqtt::received(const QMQTT::Message& message) } _clientInstance->publish(result); } + else + { + emit GlobalSignals::getInstance()->SignalMqttReceived(topic, payload); + } }