|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|