Browse Source

thermostat: monitor the mqtt connection

master
Richard Genoud 4 years ago
parent
commit
a21f65fa00
2 changed files with 38 additions and 7 deletions
  1. +1
    -0
      soft/thermostat/inc/mqttclient.h
  2. +37
    -7
      soft/thermostat/src/mqttclient.cpp

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

@ -33,6 +33,7 @@ public slots:
void onStateChanged(ClientState state);
void onDisconnected(void);
void allHeatersOn(bool on);
void onConnectTimeout(void);
signals:
void new_temperature(int idx, double val);


+ 37
- 7
soft/thermostat/src/mqttclient.cpp View File

@ -16,6 +16,8 @@
#include "mqttclient.h"
#include "settings.h"
#define TIMER_PERIOD_MS 30000
MQTTClient::MQTTClient(const QString& host, int port, QObject* parent)
: QMqttClient(parent)
{
@ -32,19 +34,48 @@ MQTTClient::MQTTClient(const QString& host, int port, QObject* parent)
connect(this, SIGNAL(stateChanged(ClientState)),
this, SLOT(onStateChanged(ClientState)));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(onConnectTimeout()));
m_timer.start(TIMER_PERIOD_MS);
}
MQTTClient::~MQTTClient() {
}
/*
* the QMqttClient doesn't handle auto-reconnection.
* It can stay in the connecting state for ever, even
* if the server is back on.
*/
void MQTTClient::onConnectTimeout(void)
{
switch (state()) {
case Connecting:
qDebug() << "force to disconnect";
disconnectFromHost();
/* fall through */
case Disconnected:
qDebug() << "re-try do connect";
connectToHost();
break;
case Connected:
break;
}
}
void MQTTClient::onStateChanged(ClientState state)
{
qDebug() << "mqtt state " << state;
qDebug() << "mqtt state: ";
switch (state) {
case Disconnected: break;
case Connecting: break;
case Connected: break;
default: break;
case Disconnected:
qDebug() << "disconnected";
break;
case Connecting:
qDebug() << "Connecting";
break;
case Connected:
qDebug() << "Connected";
break;
}
}
@ -68,7 +99,7 @@ void MQTTClient::onError(ClientError error)
void MQTTClient::onConnected()
{
qDebug() << "connected";
qDebug() << "subscribe";
this->subscribe(QString("sensors/#"), 1);
}
@ -151,7 +182,6 @@ void MQTTClient::allHeatersOn(bool on) {
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);
}
}


Loading…
Cancel
Save