Browse Source

Thermostat: BackLoop: fix objects in a different thread

master
Richard Genoud 5 years ago
parent
commit
4047f6268a
5 changed files with 37 additions and 17 deletions
  1. +9
    -0
      soft/thermostat/inc/backgroundloop.h
  2. +0
    -2
      soft/thermostat/inc/mainwindow.h
  3. +13
    -2
      soft/thermostat/src/backgroundloop.cpp
  4. +8
    -12
      soft/thermostat/src/mainwindow.cpp
  5. +7
    -1
      soft/thermostat/src/mqttclient.cpp

+ 9
- 0
soft/thermostat/inc/backgroundloop.h View File

@ -11,6 +11,8 @@
#include <QThread> #include <QThread>
#include "mqttclient.h"
enum power_states { enum power_states {
OFF, OFF,
ON, ON,
@ -34,7 +36,14 @@ private:
void run() override; void run() override;
void allHeatersOn(bool on); void allHeatersOn(bool on);
MQTTClient *m_mqtt;
enum power_states m_pwr_state; 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 #endif // BACKGROUNDLOOP_H


+ 0
- 2
soft/thermostat/inc/mainwindow.h View File

@ -26,8 +26,6 @@ public:
explicit MainWindow(QWidget *parent = NULL); explicit MainWindow(QWidget *parent = NULL);
~MainWindow(); ~MainWindow();
MQTTClient *mqttclient;
private: private:
QPushButton m_state_btn; QPushButton m_state_btn;
BackgroundLoop m_backLoop; BackgroundLoop m_backLoop;


+ 13
- 2
soft/thermostat/src/backgroundloop.cpp View File

@ -8,6 +8,7 @@
#include <QEventLoop> #include <QEventLoop>
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QHostAddress>
#define LOOP_SLEEP_TIME_S 5 #define LOOP_SLEEP_TIME_S 5
@ -28,7 +29,6 @@ void BackgroundLoop::onNewPowerState(enum power_states state)
} }
void BackgroundLoop::allHeatersOn(bool on) { void BackgroundLoop::allHeatersOn(bool on) {
MQTTClient *mc = MQTTClient::getInstance();
Settings *s = Settings::getInstance(); Settings *s = Settings::getInstance();
QString payload = on ? QString("on") : QString("off"); 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++) { for (int j = 0; j < r->heaters.count(); j++) {
const struct Heater *h = &(r->heaters.at(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() 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()) { while (!isInterruptionRequested()) {
switch (m_pwr_state) { switch (m_pwr_state) {


+ 8
- 12
soft/thermostat/src/mainwindow.cpp View File

@ -12,7 +12,6 @@
#include <QLocale> #include <QLocale>
#include <QVector> #include <QVector>
#include "mqttclient.h"
#include "zoneitem.h" #include "zoneitem.h"
#include "settings.h" #include "settings.h"
#include "mainwindow.h" #include "mainwindow.h"
@ -24,20 +23,11 @@ MainWindow::MainWindow(QWidget *parent) :
Settings *s = Settings::getInstance(); Settings *s = Settings::getInstance();
mqttclient = new MQTTClient(QHostAddress(s->m_broker.address), s->m_broker.port);
for (int i = 0; i < s->nbZones(); i++) { for (int i = 0; i < s->nbZones(); i++) {
zone = new ZoneItem(s->m_rooms.at(i).name, this); zone = new ZoneItem(s->m_rooms.at(i).name, this);
m_zones << zone; 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 * Sensors-related layout
*/ */
@ -61,6 +51,7 @@ MainWindow::MainWindow(QWidget *parent) :
QWidget *mainWidget = new QWidget; QWidget *mainWidget = new QWidget;
mainWidget->setLayout(mainLayout); mainWidget->setLayout(mainLayout);
/* /*
* Top widget * Top widget
*/ */
@ -69,11 +60,16 @@ MainWindow::MainWindow(QWidget *parent) :
setWindowTitle(tr("Sorico's thermostat")); setWindowTitle(tr("Sorico's thermostat"));
m_backLoop.start(); m_backLoop.start();
mqttclient->connectToHost();
connect(this, SIGNAL(new_pwr_state(enum power_states)), connect(this, SIGNAL(new_pwr_state(enum power_states)),
&m_backLoop, SLOT(onNewPowerState(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())); connect(&m_state_btn, SIGNAL(clicked()), this, SLOT(change_state()));
} }


+ 7
- 1
soft/thermostat/src/mqttclient.cpp View File

@ -24,7 +24,13 @@ MQTTClient::MQTTClient(const QHostAddress& host, const quint16 port,
connect(this, &MQTTClient::received, this, &MQTTClient::onReceived); connect(this, &MQTTClient::received, this, &MQTTClient::onReceived);
connect(this, &MQTTClient::published, this, &MQTTClient::onPublished); connect(this, &MQTTClient::published, this, &MQTTClient::onPublished);
connect(this, &MQTTClient::error, this, &MQTTClient::onError); 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() { MQTTClient::~MQTTClient() {


Loading…
Cancel
Save