From 4047f6268af941ee36198d22c3bf5a23f57812ba Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Thu, 19 Dec 2019 17:33:31 +0100 Subject: [PATCH] Thermostat: BackLoop: fix objects in a different thread --- soft/thermostat/inc/backgroundloop.h | 9 +++++++++ soft/thermostat/inc/mainwindow.h | 2 -- soft/thermostat/src/backgroundloop.cpp | 15 +++++++++++++-- soft/thermostat/src/mainwindow.cpp | 20 ++++++++------------ soft/thermostat/src/mqttclient.cpp | 8 +++++++- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/soft/thermostat/inc/backgroundloop.h b/soft/thermostat/inc/backgroundloop.h index f1fd185..2bbaf9d 100644 --- a/soft/thermostat/inc/backgroundloop.h +++ b/soft/thermostat/inc/backgroundloop.h @@ -11,6 +11,8 @@ #include +#include "mqttclient.h" + enum power_states { OFF, ON, @@ -34,7 +36,14 @@ private: void run() override; void allHeatersOn(bool on); + MQTTClient *m_mqtt; enum power_states m_pwr_state; + +signals: + void new_temperature(int idx, double val); + void new_hygro(int idx, double val); + void new_battery(int idx, double val); + void new_availability(int idx, bool ok); }; #endif // BACKGROUNDLOOP_H diff --git a/soft/thermostat/inc/mainwindow.h b/soft/thermostat/inc/mainwindow.h index 3119075..29648d0 100644 --- a/soft/thermostat/inc/mainwindow.h +++ b/soft/thermostat/inc/mainwindow.h @@ -26,8 +26,6 @@ public: explicit MainWindow(QWidget *parent = NULL); ~MainWindow(); - MQTTClient *mqttclient; - private: QPushButton m_state_btn; BackgroundLoop m_backLoop; diff --git a/soft/thermostat/src/backgroundloop.cpp b/soft/thermostat/src/backgroundloop.cpp index a9e7c24..d81e804 100644 --- a/soft/thermostat/src/backgroundloop.cpp +++ b/soft/thermostat/src/backgroundloop.cpp @@ -8,6 +8,7 @@ #include #include +#include #define LOOP_SLEEP_TIME_S 5 @@ -28,7 +29,6 @@ void BackgroundLoop::onNewPowerState(enum power_states state) } void BackgroundLoop::allHeatersOn(bool on) { - MQTTClient *mc = MQTTClient::getInstance(); Settings *s = Settings::getInstance(); QString payload = on ? QString("on") : QString("off"); @@ -38,13 +38,24 @@ void BackgroundLoop::allHeatersOn(bool on) { for (int j = 0; j < r->heaters.count(); j++) { const struct Heater *h = &(r->heaters.at(j)); - mc->publish_msg(h->ctrl_topic, payload); + /* TODO: check if connected */ + m_mqtt->publish_msg(h->ctrl_topic, payload); } } } void BackgroundLoop::run() { + Settings *s = Settings::getInstance(); + + m_mqtt = new MQTTClient(QHostAddress(s->m_broker.address), s->m_broker.port, NULL); + + connect(m_mqtt, SIGNAL(new_temperature(int, double)), this, SIGNAL(new_temperature(int, double))); + connect(m_mqtt, SIGNAL(new_hygro(int, double)), this, SIGNAL(new_hygro(int, double))); + connect(m_mqtt, SIGNAL(new_battery(int, double)), this, SIGNAL(new_battery(int, double))); + connect(m_mqtt, SIGNAL(new_availability(int, bool)), this, SIGNAL(new_availability(int, bool))); + + m_mqtt->connectToHost(); while (!isInterruptionRequested()) { switch (m_pwr_state) { diff --git a/soft/thermostat/src/mainwindow.cpp b/soft/thermostat/src/mainwindow.cpp index b5713f1..4e2c9cc 100644 --- a/soft/thermostat/src/mainwindow.cpp +++ b/soft/thermostat/src/mainwindow.cpp @@ -12,7 +12,6 @@ #include #include -#include "mqttclient.h" #include "zoneitem.h" #include "settings.h" #include "mainwindow.h" @@ -24,20 +23,11 @@ MainWindow::MainWindow(QWidget *parent) : Settings *s = Settings::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); m_zones << zone; } - connect(mqttclient, SIGNAL(new_temperature(int, double)), - this, SLOT(temperature_slot(int, double))); - connect(mqttclient, SIGNAL(new_hygro(int, double)), - this, SLOT(hygro_slot(int, double))); - - connect(mqttclient, SIGNAL(new_availability(int, bool)), - this, SLOT(availability_slot(int, bool))); /* * Sensors-related layout */ @@ -61,6 +51,7 @@ MainWindow::MainWindow(QWidget *parent) : QWidget *mainWidget = new QWidget; mainWidget->setLayout(mainLayout); + /* * Top widget */ @@ -69,11 +60,16 @@ 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_backLoop, SIGNAL(new_temperature(int, double)), + this, SLOT(temperature_slot(int, double))); + connect(&m_backLoop, SIGNAL(new_hygro(int, double)), + this, SLOT(hygro_slot(int, double))); + connect(&m_backLoop, SIGNAL(new_availability(int, bool)), + this, SLOT(availability_slot(int, bool))); + 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 c3c787f..dfe8fa9 100644 --- a/soft/thermostat/src/mqttclient.cpp +++ b/soft/thermostat/src/mqttclient.cpp @@ -24,7 +24,13 @@ 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); - qDebug() << "created" << endl; + + /* + * 2 more signals: + * void unsubscribed(const QString& topic); + * void disconnected(void); + * void pingresp(); + */ } MQTTClient::~MQTTClient() {