// 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: */
|