diff --git a/soft/thermostat/inc/mqttclient.h b/soft/thermostat/inc/mqttclient.h index d95215e..98ce8f0 100644 --- a/soft/thermostat/inc/mqttclient.h +++ b/soft/thermostat/inc/mqttclient.h @@ -10,6 +10,7 @@ #define MQTTCLIENT_H #include +#include #include "qmqtt.h" @@ -21,11 +22,18 @@ public: const quint16 port = 1883, QObject* parent = NULL); virtual ~MQTTSubcriber(); + void publish_msg(const QString& topic, const QString& payload); + +private: + QTimer m_timer; public slots: void onConnected(); void onSubscribed(const QString& topic); void onReceived(const QMQTT::Message& message); + void onError(const QMQTT::ClientError error); + void onPublished(const QMQTT::Message& message, quint16 msgid); + void onDisconnected(void); signals: void new_temperature(int idx, double val); diff --git a/soft/thermostat/src/mqttclient.cpp b/soft/thermostat/src/mqttclient.cpp index 85c6a43..e513af2 100644 --- a/soft/thermostat/src/mqttclient.cpp +++ b/soft/thermostat/src/mqttclient.cpp @@ -22,13 +22,19 @@ MQTTSubcriber::MQTTSubcriber(const QHostAddress& host, const quint16 port, connect(this, &MQTTSubcriber::connected, this, &MQTTSubcriber::onConnected); connect(this, &MQTTSubcriber::subscribed, this, &MQTTSubcriber::onSubscribed); connect(this, &MQTTSubcriber::received, this, &MQTTSubcriber::onReceived); - + connect(this, &MQTTSubcriber::published, this, &MQTTSubcriber::onPublished); + connect(this, &MQTTSubcriber::error, this, &MQTTSubcriber::onError); qDebug() << "created" << endl; } MQTTSubcriber::~MQTTSubcriber() { } +void MQTTSubcriber::onError(const QMQTT::ClientError error) +{ + qDebug() << "error" << error << endl; +} + void MQTTSubcriber::onConnected() { qDebug() << "connected" << endl; @@ -88,4 +94,22 @@ out: return; } +void MQTTSubcriber::onPublished(const QMQTT::Message& message, quint16 msgid) +{ + qDebug() << "published" << msgid << " payload: " << message.topic() << endl; +} + +void MQTTSubcriber::onDisconnected(void) +{ + qDebug() << "disconnected" << endl; +} + +void MQTTSubcriber::publish_msg(const QString& topic, const QString& payload) +{ + // id is automatically incremented if ==0 and QoS>0 + // DUP: if a message is resent, it should have the DUP flag + QMQTT::Message message(0, topic, payload.toUtf8(), 1, true, false); + publish(message); +} + /* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */