// SPDX-License-Identifier: GPL-3.0-or-later
|
|
/*
|
|
* Qt mutizone MQTT thermostat
|
|
*
|
|
* Copyright (C) 2019 Richard Genoud
|
|
*
|
|
*/
|
|
|
|
#include <QLoggingCategory>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
|
|
|
|
#include "mqttclient.h"
|
|
#include "settings.h"
|
|
#include "qmqtt.h"
|
|
|
|
MQTTSubcriber::MQTTSubcriber(const QHostAddress& host, const quint16 port,
|
|
QObject* parent) : QMQTT::Client(host, port, parent)
|
|
{
|
|
connect(this, &MQTTSubcriber::connected, this, &MQTTSubcriber::onConnected);
|
|
connect(this, &MQTTSubcriber::subscribed, this, &MQTTSubcriber::onSubscribed);
|
|
connect(this, &MQTTSubcriber::received, this, &MQTTSubcriber::onReceived);
|
|
|
|
qDebug() << "created" << endl;
|
|
}
|
|
|
|
MQTTSubcriber::~MQTTSubcriber() {
|
|
}
|
|
|
|
void MQTTSubcriber::onConnected()
|
|
{
|
|
qDebug() << "connected" << endl;
|
|
this->subscribe("sensors/#", 1);
|
|
}
|
|
|
|
void MQTTSubcriber::onSubscribed(const QString& topic)
|
|
{
|
|
qDebug() << "subscribed " << topic << endl;
|
|
}
|
|
|
|
void MQTTSubcriber::onReceived(const QMQTT::Message& message)
|
|
{
|
|
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()) {
|
|
qDebug() << "this is for us !" << endl;
|
|
sensorData = QJsonDocument::fromJson(message.payload());
|
|
if (!sensorData.isObject()) {
|
|
qWarning() << "malformed JSON data" << endl;
|
|
goto out;
|
|
}
|
|
QJsonObject obj(sensorData.object());
|
|
val = obj["temperature"];
|
|
if (val.isDouble()) {
|
|
emit new_temperature(i, val.toDouble());
|
|
qDebug() << val.toDouble() << endl;
|
|
}
|
|
|
|
val = obj["humidity"];
|
|
if (val.isDouble()) {
|
|
emit new_hygro(i, val.toDouble());
|
|
qDebug() << val.toDouble() << endl;
|
|
}
|
|
|
|
val = obj["battery"];
|
|
if (val.isDouble()) {
|
|
emit new_battery(i, val.toDouble());
|
|
qDebug() << val.toDouble() << endl;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if (Settings::getInstance()->m_availability_topics.at(i) == message.topic()) {
|
|
|
|
emit new_availability(i, message.payload() == QString("online").toUtf8());
|
|
}
|
|
}
|
|
out:
|
|
return;
|
|
}
|
|
|
|
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */
|