From f57134fbef2742e4cde38fc577e111c8addec0c8 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Tue, 17 Dec 2019 22:54:56 +0100 Subject: [PATCH] add heaters in settings --- soft/thermostat/Thermostat.conf | 9 ++++++ soft/thermostat/inc/mainwindow.h | 9 +++--- soft/thermostat/inc/mqttclient.h | 8 ++--- soft/thermostat/inc/settings.h | 20 +++++++++--- soft/thermostat/src/mainwindow.cpp | 64 +++++++++++++++++++++++--------------- soft/thermostat/src/mqttclient.cpp | 7 +++-- soft/thermostat/src/settings.cpp | 33 ++++++++++++++++---- 7 files changed, 103 insertions(+), 47 deletions(-) diff --git a/soft/thermostat/Thermostat.conf b/soft/thermostat/Thermostat.conf index 3368abe..5c15ae5 100644 --- a/soft/thermostat/Thermostat.conf +++ b/soft/thermostat/Thermostat.conf @@ -2,15 +2,24 @@ # $HOME/.config/Sorico/Thermostat.conf [rooms] +1\heaters\1\control_topic=chauffage/chambre_so +1\heaters\size=1 1\availability_topic=sensors/chambre_so/availability 1\name=Bedroom 1\sensor_topic=sensors/chambre_so/xiaomi +2\heaters\1\control_topic=chauffage/chambre_rico +2\heaters\size=1 2\availability_topic=sensors/chambre_rico/availability 2\name=Workspace 2\sensor_topic=sensors/chambre_rico/xiaomi +3\heaters\1\control_topic=chauffage/cuisine +3\heaters\2\control_topic=chauffage/salon; +3\heaters\size=2 3\availability_topic=sensors/salon/availability 3\name=Living room 3\sensor_topic=sensors/salon/xiaomi +4\heaters\1\control_topic=chauffage/sdb +4\heaters\size=1 4\availability_topic=sensors/salle_de_bains/availability 4\name=Bathroom 4\sensor_topic=sensors/salle_de_bains/xiaomi diff --git a/soft/thermostat/inc/mainwindow.h b/soft/thermostat/inc/mainwindow.h index 371054d..e524007 100644 --- a/soft/thermostat/inc/mainwindow.h +++ b/soft/thermostat/inc/mainwindow.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "mqttclient.h" #include "zoneitem.h" @@ -30,12 +31,12 @@ public: private: BackgroundLoop m_backLoop; - ZoneItem *m_zones[NB_ZONES]; + QVector m_zones; private slots: - void temperature_slot(unsigned idx, double val); - void hygro_slot(unsigned idx, double val); - void availability_slot(unsigned idx, bool ok); + void temperature_slot(int idx, double val); + void hygro_slot(int idx, double val); + void availability_slot(int idx, bool ok); }; #endif // MAINWINDOW_H diff --git a/soft/thermostat/inc/mqttclient.h b/soft/thermostat/inc/mqttclient.h index cce76f3..d95215e 100644 --- a/soft/thermostat/inc/mqttclient.h +++ b/soft/thermostat/inc/mqttclient.h @@ -28,10 +28,10 @@ public slots: void onReceived(const QMQTT::Message& message); signals: - void new_temperature(unsigned idx, double val); - void new_hygro(unsigned idx, double val); - void new_battery(unsigned idx, double val); - void new_availability(unsigned idx, bool ok); + 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 diff --git a/soft/thermostat/inc/settings.h b/soft/thermostat/inc/settings.h index f1ce3c3..e92de8e 100644 --- a/soft/thermostat/inc/settings.h +++ b/soft/thermostat/inc/settings.h @@ -9,20 +9,30 @@ #ifndef SETTINGS_H #define SETTINGS_H -#include #include +#include -#define NB_ZONES 4 +#define MAX_NB_ZONES 4 + +struct Heater { + QString ctrl_topic; +}; + +struct Room { + QString name; + QString sensor_topic; + QString availability_topic; + QVector heaters; +}; class Settings : public QSettings { Q_OBJECT public: static Settings *getInstance(void); + int nbZones(); - QStringList m_rooms_names; - QStringList m_sensor_topics; - QStringList m_availability_topics; + QVector m_rooms; private: Settings(); diff --git a/soft/thermostat/src/mainwindow.cpp b/soft/thermostat/src/mainwindow.cpp index 33318ad..9de20f0 100644 --- a/soft/thermostat/src/mainwindow.cpp +++ b/soft/thermostat/src/mainwindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "mqttclient.h" #include "zoneitem.h" @@ -19,33 +20,40 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { + ZoneItem *zone; + Settings *s = Settings::getInstance(); mqttclient = new MQTTSubcriber(QHostAddress("192.168.1.2"), 1883); - for (int i = 0; i < NB_ZONES; i++) { - m_zones[i] = new ZoneItem(s->m_rooms_names[i], this); + 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(unsigned, double)), - this, SLOT(temperature_slot(unsigned, double))); - connect(mqttclient, SIGNAL(new_hygro(unsigned, double)), - this, SLOT(hygro_slot(unsigned, double))); + 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(unsigned, bool)), - this, SLOT(availability_slot(unsigned, bool))); + connect(mqttclient, SIGNAL(new_availability(int, bool)), + this, SLOT(availability_slot(int, bool))); /* * Sensors-related layout */ QGridLayout *mainLayout = new QGridLayout; - mainLayout->addWidget(m_zones[0], 0, 0); - mainLayout->addWidget(m_zones[1], 0, 1); - mainLayout->addWidget(new QPushButton(tr("Menu")), 0, 2); - mainLayout->addWidget(m_zones[2], 1, 0); - mainLayout->addWidget(m_zones[3], 1, 1); - mainLayout->addWidget(new QPushButton(tr("Auto")), 1, 2); - + if (s->nbZones() < 4) { + // TODO + qDebug() << "bad configuration" ; + } else { + mainLayout->addWidget(m_zones.at(0), 0, 0); + mainLayout->addWidget(m_zones.at(1), 0, 1); + mainLayout->addWidget(new QPushButton(tr("Menu")), 0, 2); + mainLayout->addWidget(m_zones.at(2), 1, 0); + mainLayout->addWidget(m_zones.at(3), 1, 1); + mainLayout->addWidget(new QPushButton(tr("Auto")), 1, 2); + } QWidget *mainWidget = new QWidget; mainWidget->setLayout(mainLayout); @@ -64,26 +72,32 @@ MainWindow::~MainWindow() { } -void MainWindow::temperature_slot(unsigned idx, double val) +void MainWindow::temperature_slot(int idx, double val) { + Settings *s = Settings::getInstance(); + qDebug() << "temperature idx:" << idx << " val: " << val << endl; - if (idx < NB_ZONES) { - m_zones[idx]->set_temperature_value(val); + if (idx < s->nbZones()) { + m_zones.at(idx)->set_temperature_value(val); } } -void MainWindow::hygro_slot(unsigned idx, double val) +void MainWindow::hygro_slot(int idx, double val) { + Settings *s = Settings::getInstance(); + qDebug() << "hygro idx:" << idx << " val: " << val << endl; - if (idx < NB_ZONES) { - m_zones[idx]->set_hygro_value(val); + if (idx < s->nbZones()) { + m_zones.at(idx)->set_hygro_value(val); } } -void MainWindow::availability_slot(unsigned idx, bool ok) +void MainWindow::availability_slot(int idx, bool ok) { - if ((idx < NB_ZONES) && !ok) { - m_zones[idx]->set_hygro_value(0); - m_zones[idx]->set_temperature_value(0); + Settings *s = Settings::getInstance(); + + if ((idx < s->nbZones()) && !ok) { + m_zones.at(idx)->set_hygro_value(0); + m_zones.at(idx)->set_temperature_value(0); } } diff --git a/soft/thermostat/src/mqttclient.cpp b/soft/thermostat/src/mqttclient.cpp index dc5186a..85c6a43 100644 --- a/soft/thermostat/src/mqttclient.cpp +++ b/soft/thermostat/src/mqttclient.cpp @@ -42,14 +42,15 @@ void MQTTSubcriber::onSubscribed(const QString& topic) void MQTTSubcriber::onReceived(const QMQTT::Message& message) { + Settings *s = Settings::getInstance(); QJsonDocument sensorData; QJsonValue val; qDebug() << "publish received: \"" << QString::fromUtf8(message.payload()) << "\"" << " from: " << message.topic() << endl; - for (int i = 0; i < Settings::getInstance()->m_sensor_topics.size(); ++i) { - if (Settings::getInstance()->m_sensor_topics.at(i) == message.topic()) { + for (int i = 0; i < s->nbZones(); ++i) { + if (s->m_rooms.at(i).sensor_topic == message.topic()) { qDebug() << "this is for us !" << endl; sensorData = QJsonDocument::fromJson(message.payload()); if (!sensorData.isObject()) { @@ -78,7 +79,7 @@ void MQTTSubcriber::onReceived(const QMQTT::Message& message) break; } - if (Settings::getInstance()->m_availability_topics.at(i) == message.topic()) { + if (s->m_rooms.at(i).availability_topic == message.topic()) { emit new_availability(i, message.payload() == QString("online").toUtf8()); } diff --git a/soft/thermostat/src/settings.cpp b/soft/thermostat/src/settings.cpp index ddf2507..45d2e66 100644 --- a/soft/thermostat/src/settings.cpp +++ b/soft/thermostat/src/settings.cpp @@ -7,7 +7,6 @@ */ #include -#include #include #include "settings.h" @@ -17,16 +16,38 @@ Settings *Settings::_singleton = NULL; Settings::Settings() { QSettings s; - (void)s.beginReadArray("rooms"); - for (int i = 0; i < NB_ZONES; ++i) { + struct Room room; + struct Heater heater; + int nb_rooms; + + nb_rooms = s.beginReadArray("rooms"); + if (nb_rooms > MAX_NB_ZONES) { + // TODO; + nb_rooms = MAX_NB_ZONES; + } + + for (int i = 0; i < nb_rooms; ++i) { s.setArrayIndex(i); - m_rooms_names << s.value("name", "").toString(); - m_sensor_topics << s.value("sensor_topic", "").toString(); - m_availability_topics << s.value("availability_topic", "").toString(); + room.name = s.value("name", "").toString(); + room.sensor_topic = s.value("sensor_topic", "").toString(); + room.availability_topic = s.value("availability_topic", "").toString(); + int size = s.beginReadArray("heaters"); + for (int j = 0; j < size; ++j) { + s.setArrayIndex(j); + heater.ctrl_topic = s.value("control_topic", "").toString(); + room.heaters << heater; + } + s.endArray(); + m_rooms << room; } s.endArray(); } +int Settings::nbZones(void) +{ + return m_rooms.size(); +} + Settings::~Settings() { }