From 4ef0b6802786c75bd3a694114c3980a2d51fecea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 21 Jul 2022 15:41:14 +0200 Subject: [PATCH] Add connected property to MqttChannel --- .../network/mqtt/mqttchannelimplementation.cpp | 15 +++++++++++++++ .../network/mqtt/mqttchannelimplementation.h | 4 ++++ .../network/mqtt/mqttproviderimplementation.cpp | 8 ++++---- .../network/mqtt/mqttproviderimplementation.h | 2 +- libnymea/network/mqtt/mqttchannel.h | 2 ++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.cpp b/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.cpp index 0621276c..765b06e3 100644 --- a/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.cpp +++ b/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.cpp @@ -72,4 +72,19 @@ void MqttChannelImplementation::publish(const QString &topic, const QByteArray & emit pluginPublished(topic, payload); } +bool MqttChannelImplementation::isConnected() const +{ + return m_connected; +} + +void MqttChannelImplementation::setConnected(bool connected) +{ + m_connected = connected; + if (m_connected) { + emit clientConnected(this); + } else { + emit clientDisconnected(this); + } +} + } diff --git a/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.h b/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.h index cf35a908..016fff12 100644 --- a/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.h +++ b/libnymea-core/hardware/network/mqtt/mqttchannelimplementation.h @@ -50,6 +50,9 @@ public: void publish(const QString &topic, const QByteArray &payload) override; + bool isConnected() const override; + void setConnected(bool connected); + signals: void pluginPublished(const QString &topic, const QByteArray &payload); @@ -60,6 +63,7 @@ private: QHostAddress m_serverAddress; quint16 m_serverPort; QStringList m_topicPrefixList; + bool m_connected = false; friend class MqttProviderImplementation; }; diff --git a/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.cpp b/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.cpp index 11c66f0d..e9266cbd 100644 --- a/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.cpp +++ b/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.cpp @@ -205,16 +205,16 @@ void MqttProviderImplementation::setEnabled(bool enabled) void MqttProviderImplementation::onClientConnected(const QString &clientId) { if (m_createdChannels.contains(clientId)) { - MqttChannel* channel = m_createdChannels.value(clientId); - emit channel->clientConnected(channel); + MqttChannelImplementation *channel = qobject_cast(m_createdChannels.value(clientId)); + channel->setConnected(true); } } void MqttProviderImplementation::onClientDisconnected(const QString &clientId) { if (m_createdChannels.contains(clientId)) { - MqttChannel *channel = m_createdChannels.value(clientId); - emit channel->clientDisconnected(channel); + MqttChannelImplementation *channel = qobject_cast(m_createdChannels.value(clientId)); + channel->setConnected(false); } } diff --git a/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.h b/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.h index aad46127..a171a4c1 100644 --- a/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.h +++ b/libnymea-core/hardware/network/mqtt/mqttproviderimplementation.h @@ -64,7 +64,7 @@ private slots: private: MqttBroker* m_broker = nullptr; - QHash m_createdChannels; + QHash m_createdChannels; }; } diff --git a/libnymea/network/mqtt/mqttchannel.h b/libnymea/network/mqtt/mqttchannel.h index 32cd622d..116aa113 100644 --- a/libnymea/network/mqtt/mqttchannel.h +++ b/libnymea/network/mqtt/mqttchannel.h @@ -52,6 +52,8 @@ public: virtual void publish(const QString &topic, const QByteArray &payload) = 0; + virtual bool isConnected() const = 0; + signals: void clientConnected(MqttChannel* channel); void clientDisconnected(MqttChannel* channel);