From 12de99110ef862ab5e8e136b965669bc8214c5b2 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Thu, 19 Dec 2019 10:49:42 +0100 Subject: [PATCH] Revert "Thermostat: MQTTClient: switch to a singleton" This reverts commit 23c726f740e0d44f06dd3e3bdef2de0a711690d5. It's better to make mqttclient part of the background loop and use signals/slots --- soft/thermostat/inc/mainwindow.h | 2 ++ soft/thermostat/inc/mqttclient.h | 8 +++----- soft/thermostat/src/mainwindow.cpp | 7 ++++++- soft/thermostat/src/mqttclient.cpp | 16 +--------------- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/soft/thermostat/inc/mainwindow.h b/soft/thermostat/inc/mainwindow.h index 29648d0..3119075 100644 --- a/soft/thermostat/inc/mainwindow.h +++ b/soft/thermostat/inc/mainwindow.h @@ -26,6 +26,8 @@ public: explicit MainWindow(QWidget *parent = NULL); ~MainWindow(); + MQTTClient *mqttclient; + private: QPushButton m_state_btn; BackgroundLoop m_backLoop; diff --git a/soft/thermostat/inc/mqttclient.h b/soft/thermostat/inc/mqttclient.h index 96cb969..7390c93 100644 --- a/soft/thermostat/inc/mqttclient.h +++ b/soft/thermostat/inc/mqttclient.h @@ -18,15 +18,13 @@ class MQTTClient: public QMQTT::Client { Q_OBJECT public: - void publish_msg(const QString& topic, const QString& payload); - static MQTTClient *getInstance(void); - -private: explicit MQTTClient(const QHostAddress& host = QHostAddress::LocalHost, const quint16 port = 1883, QObject* parent = NULL); virtual ~MQTTClient(); - static MQTTClient *_singleton; + void publish_msg(const QString& topic, const QString& payload); + +private: QTimer m_timer; public slots: diff --git a/soft/thermostat/src/mainwindow.cpp b/soft/thermostat/src/mainwindow.cpp index d1693f2..b5713f1 100644 --- a/soft/thermostat/src/mainwindow.cpp +++ b/soft/thermostat/src/mainwindow.cpp @@ -21,8 +21,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ZoneItem *zone; + Settings *s = Settings::getInstance(); - MQTTClient *mqttclient = MQTTClient::getInstance(); + + mqttclient = new MQTTClient(QHostAddress(s->m_broker.address), s->m_broker.port); for (int i = 0; i < s->nbZones(); i++) { zone = new ZoneItem(s->m_rooms.at(i).name, this); @@ -67,8 +69,11 @@ MainWindow::MainWindow(QWidget *parent) : setWindowTitle(tr("Sorico's thermostat")); m_backLoop.start(); + mqttclient->connectToHost(); + connect(this, SIGNAL(new_pwr_state(enum power_states)), &m_backLoop, SLOT(onNewPowerState(enum power_states))); + connect(&m_state_btn, SIGNAL(clicked()), this, SLOT(change_state())); } diff --git a/soft/thermostat/src/mqttclient.cpp b/soft/thermostat/src/mqttclient.cpp index 5ce6f2f..c3c787f 100644 --- a/soft/thermostat/src/mqttclient.cpp +++ b/soft/thermostat/src/mqttclient.cpp @@ -16,19 +16,6 @@ #include "settings.h" #include "qmqtt.h" -MQTTClient *MQTTClient::_singleton = NULL; - -MQTTClient *MQTTClient::getInstance(void) -{ - if (!_singleton) { - Settings *s = Settings::getInstance(); - - _singleton = new MQTTClient(QHostAddress(s->m_broker.address), s->m_broker.port); - } - - return _singleton; -} - MQTTClient::MQTTClient(const QHostAddress& host, const quint16 port, QObject* parent) : QMQTT::Client(host, port, parent) { @@ -37,8 +24,7 @@ MQTTClient::MQTTClient(const QHostAddress& host, const quint16 port, connect(this, &MQTTClient::received, this, &MQTTClient::onReceived); connect(this, &MQTTClient::published, this, &MQTTClient::onPublished); connect(this, &MQTTClient::error, this, &MQTTClient::onError); - - connectToHost(); + qDebug() << "created" << endl; } MQTTClient::~MQTTClient() {