// SPDX-License-Identifier: GPL-3.0-or-later /* * Qt mutizone MQTT thermostat * * Copyright (C) 2019 Richard Genoud * */ #include #include #include #include #include #include #include "qmqtt.h" #include "backgroundloop.h" #define MQTT_SERVER "nas" #define MQTT_PORT 1883 #define MQTT_USERNAME "" #define MQTT_PASSWORD "" #define MQTT_CLIENT_ID "" #define LOOP_SLEEP_TIME_S 5 BackgroundLoop::BackgroundLoop() { m_mqtt_client = NULL; qsrand((uint)QDateTime::currentMSecsSinceEpoch()); } BackgroundLoop::~BackgroundLoop() { } bool BackgroundLoop::connectMQTT() { QHostInfo info; QHostAddress address; bool result = false, ok; if (m_mqtt_client) { delete m_mqtt_client; m_mqtt_client = NULL; } ok = address.setAddress(MQTT_SERVER); if (!ok) { /* This must be a domain name, not an IP */ info = QHostInfo::fromName(MQTT_SERVER); if (info.error() != QHostInfo::NoError) { qWarning() << QString("Error looking for address %1").arg(MQTT_SERVER) << info.errorString(); goto out; } if (info.addresses().isEmpty()) { qWarning() << QString("No address returned for %1").arg(MQTT_SERVER); goto out; } address = info.addresses().first(); } m_mqtt_client = new QMQTT::Client(address, MQTT_PORT); if (!m_mqtt_client) { qWarning() << QString("Error allocating memory for MQTT client"); goto out; } if (QString(MQTT_CLIENT_ID).isEmpty()) { m_mqtt_client->setClientId(QString("thermostat%1").arg(qrand())); } else { m_mqtt_client->setClientId(MQTT_CLIENT_ID); } connect(m_mqtt_client, SIGNAL(connected()), this, SLOT(mqtt_connected())); connect(m_mqtt_client, SIGNAL(disconnected()), this, SLOT(mqtt_disconnected())); connect(m_mqtt_client, SIGNAL(error(const QMQTT::ClientError)), this, SLOT(mqtt_error(const QMQTT::ClientError))); connect(m_mqtt_client, SIGNAL(subscribed(const QString&, const quint8)), this, SLOT(mqtt_subscribed(const QString&, const quint8))); connect(m_mqtt_client, SIGNAL(received(const QMQTT::Message&)), this, SLOT(mqtt_received(const QMQTT::Message&))); m_mqtt_client->setUsername(MQTT_USERNAME); m_mqtt_client->setPassword(MQTT_PASSWORD); m_mqtt_client->connectToHost(); result = true; out: return result; } void BackgroundLoop::mqtt_connected(void) { qDebug() << "MQTT connected !"; } void BackgroundLoop::mqtt_disconnected(void) { qDebug() << "MQTT disconnected !"; } void BackgroundLoop::mqtt_error(const QMQTT::ClientError error) { qDebug() << "MQTT error ! code: " << error; } void BackgroundLoop::mqtt_subscribed(const QString& topic, const quint8 qos) { qDebug() << "MQTT subscribed! topic: " << topic << " QoS: " << qos; } void BackgroundLoop::mqtt_received(const QMQTT::Message& message) { qDebug() << "MQTT recieved: " << QString::fromUtf8(message.payload()); } void BackgroundLoop::run() { connectMQTT(); while(!isInterruptionRequested()) { this->sleep(LOOP_SLEEP_TIME_S); } } /* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */