Browse Source

Thermostat: we don't need a Qthread after all

master
Richard Genoud 4 years ago
parent
commit
cd68f8bc7d
7 changed files with 54 additions and 141 deletions
  1. +0
    -51
      soft/thermostat/inc/backgroundloop.h
  2. +10
    -3
      soft/thermostat/inc/mainwindow.h
  3. +1
    -0
      soft/thermostat/inc/mqttclient.h
  4. +0
    -78
      soft/thermostat/src/backgroundloop.cpp
  5. +27
    -7
      soft/thermostat/src/mainwindow.cpp
  6. +16
    -0
      soft/thermostat/src/mqttclient.cpp
  7. +0
    -2
      soft/thermostat/thermostat.pro

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

@ -1,51 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Qt mutizone MQTT thermostat
*
* Copyright (C) 2019 Richard Genoud
*
*/
#ifndef BACKGROUNDLOOP_H
#define BACKGROUNDLOOP_H
#include <QThread>
#include "mqttclient.h"
enum power_states {
OFF,
ON,
AUTO,
MAX_POWER_STATES
};
class BackgroundLoop : public QThread
{
Q_OBJECT
public:
BackgroundLoop();
~BackgroundLoop();
public slots:
void onNewPowerState(enum power_states state);
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
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */

+ 10
- 3
soft/thermostat/inc/mainwindow.h View File

@ -14,10 +14,17 @@
#include <QtWidgets>
#include <QVector>
#include "mqttclient.h"
#include "zoneitem.h"
#include "backgroundloop.h"
#include "settings.h"
enum power_states {
OFF,
ON,
AUTO,
MAX_POWER_STATES
};
class MainWindow : public QMainWindow
{
Q_OBJECT
@ -27,8 +34,8 @@ public:
~MainWindow();
private:
MQTTClient *m_mqtt;
QPushButton m_state_btn;
BackgroundLoop m_backLoop;
QVector<ZoneItem *> m_zones;
enum power_states m_pwr_state;
void update_state_btn(enum power_states st);
@ -40,7 +47,7 @@ private slots:
void change_state(void);
signals:
void new_pwr_state(enum power_states state);
void setAllHeatersOn(bool on);
};


+ 1
- 0
soft/thermostat/inc/mqttclient.h View File

@ -34,6 +34,7 @@ public slots:
void onError(const QMQTT::ClientError error);
void onPublished(const QMQTT::Message& message, quint16 msgid);
void onDisconnected(void);
void allHeatersOn(bool on);
signals:
void new_temperature(int idx, double val);


+ 0
- 78
soft/thermostat/src/backgroundloop.cpp View File

@ -1,78 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Qt mutizone MQTT thermostat
*
* Copyright (C) 2019 Richard Genoud
*
*/
#include <QEventLoop>
#include <QLoggingCategory>
#include <QHostAddress>
#define LOOP_SLEEP_TIME_S 5
#include "mqttclient.h"
#include "settings.h"
#include "backgroundloop.h"
BackgroundLoop::BackgroundLoop() {
}
BackgroundLoop::~BackgroundLoop() {
}
void BackgroundLoop::onNewPowerState(enum power_states state)
{
m_pwr_state = state;
}
void BackgroundLoop::allHeatersOn(bool on) {
Settings *s = Settings::getInstance();
QString payload = on ? QString("on") : QString("off");
for (int i = 0; i < s->nbZones(); i++) {
const struct Room *r = &(s->m_rooms.at(i));
for (int j = 0; j < r->heaters.count(); j++) {
const struct Heater *h = &(r->heaters.at(j));
/* 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) {
case ON:
allHeatersOn(true);
break;
case AUTO:
/* TODO */
break;
case OFF:
/* fall */
default:
allHeatersOn(false);
break;
}
this->sleep(LOOP_SLEEP_TIME_S);
}
}
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */

+ 27
- 7
soft/thermostat/src/mainwindow.cpp View File

@ -12,6 +12,7 @@
#include <QLocale>
#include <QVector>
#include "mqttclient.h"
#include "zoneitem.h"
#include "settings.h"
#include "mainwindow.h"
@ -58,18 +59,23 @@ MainWindow::MainWindow(QWidget *parent) :
setCentralWidget(mainWidget);
setWindowTitle(tr("Sorico's thermostat"));
m_backLoop.start();
connect(this, SIGNAL(new_pwr_state(enum power_states)),
&m_backLoop, SLOT(onNewPowerState(enum power_states)));
/*
* MQTT client
*/
m_mqtt = new MQTTClient(QHostAddress(s->m_broker.address), s->m_broker.port, NULL);
connect(&m_backLoop, SIGNAL(new_temperature(int, double)),
m_mqtt->connectToHost();
connect(m_mqtt, SIGNAL(new_temperature(int, double)),
this, SLOT(temperature_slot(int, double)));
connect(&m_backLoop, SIGNAL(new_hygro(int, double)),
connect(m_mqtt, SIGNAL(new_hygro(int, double)),
this, SLOT(hygro_slot(int, double)));
connect(&m_backLoop, SIGNAL(new_availability(int, bool)),
connect(m_mqtt, SIGNAL(new_availability(int, bool)),
this, SLOT(availability_slot(int, bool)));
connect(this, SIGNAL(setAllHeatersOn(bool)), m_mqtt, SLOT(allHeatersOn(bool)));
connect(&m_state_btn, SIGNAL(clicked()), this, SLOT(change_state()));
}
@ -110,8 +116,22 @@ void MainWindow::change_state(void)
m_pwr_state = OFF;
break;
}
update_state_btn(m_pwr_state);
emit new_pwr_state(m_pwr_state);
switch (m_pwr_state) {
case ON:
emit setAllHeatersOn(true);
break;
case AUTO:
/* TODO */
break;
case OFF:
/* fall */
default:
emit setAllHeatersOn(false);
break;
}
}
void MainWindow::temperature_slot(int idx, double val)


+ 16
- 0
soft/thermostat/src/mqttclient.cpp View File

@ -118,4 +118,20 @@ void MQTTClient::publish_msg(const QString& topic, const QString& payload)
publish(message);
}
void MQTTClient::allHeatersOn(bool on) {
Settings *s = Settings::getInstance();
QString payload = on ? QString("on") : QString("off");
for (int i = 0; i < s->nbZones(); i++) {
const struct Room *r = &(s->m_rooms.at(i));
for (int j = 0; j < r->heaters.count(); j++) {
const struct Heater *h = &(r->heaters.at(j));
/* TODO: check if connected */
publish_msg(h->ctrl_topic, payload);
}
}
}
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */

+ 0
- 2
soft/thermostat/thermostat.pro View File

@ -23,14 +23,12 @@ DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs depr
SOURCES += src/main.cpp \
src/mainwindow.cpp \
src/zoneitem.cpp \
src/backgroundloop.cpp \
src/mqttclient.cpp \
src/settings.cpp \
HEADERS += inc/mainwindow.h \
inc/zoneitem.h \
inc/backgroundloop.h \
inc/mqttclient.h \
inc/settings.h \


Loading…
Cancel
Save