#include "mqtt.h" #include "pws2mqtt.h" #include #include #include #include #include #include #include #include #include using namespace std; extern MqttClient *mqttClient; MqttClient::MqttClient() { // init mqtt connexion to server debug(DEBUGMACRO, "declaration of qmqttClient", DEBUG); this->qmqttClient = new QMqttClient(this); debug(DEBUGMACRO, "setting hostname of mqtt server", DEBUG); this->qmqttClient->setHostname(this->server); debug(DEBUGMACRO, "setting port of mqtt server", DEBUG); this->qmqttClient->setPort(this->serverPort); //this->serverPort); debug(DEBUGMACRO, "Connecting 'messageReceived'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::messageReceived, this, &MqttClient::on_message); debug (DEBUGMACRO, "Connecting 'disconnect'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::disconnected, this, &MqttClient::on_disconnect); debug (DEBUGMACRO, "Connecting 'connected'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::connected, this, &MqttClient::on_connect); debug (DEBUGMACRO, "Connecting 'errorChanged", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::errorChanged, this, &MqttClient::on_error); debug (DEBUGMACRO, "Connecting 'messageSent'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::messageSent, this, &MqttClient::on_messageSent); debug (DEBUGMACRO, "Connecting 'messageStatusChanged'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::messageStatusChanged, this, &MqttClient::on_messageSentStatusChanged); debug (DEBUGMACRO, "Connecting 'stateChanged'", DEBUG); QObject::connect(this->qmqttClient, &QMqttClient::stateChanged, this, &MqttClient::on_stateChanged); // qmqttClient->setClientId("pws2mqtt"); // qmqttClient->setProtocolVersion(QMqttClient::MQTT_3_1_1); debug(DEBUGMACRO, "connecting to mqtt server", DEBUG); this->qmqttClient->connectToHost(); //debug(DEBUGMACRO, "error status => " + this->qmqttErrors[this->qmqttClient->error()], DEBUG); } MqttClient::~MqttClient() { } qint32 MqttClient::send_message(QString message) { qint32 mid; debug(DEBUGMACRO, "send_message() for topic: " + this->topic + " => " + message , DEBUG); mid = this->qmqttClient->publish(this->topic, message.toUtf8(), this->qos, true); if (mid == -1) { debug(DEBUGMACRO, "Error, can't publish message : " + this->qmqttErrors[this->qmqttClient->error()], ERROR); } messagesList.insert(mid, message); return (mid); } void MqttClient::on_connect() { //this->connected = true; debug(DEBUGMACRO, "Connected to broker", DEBUG); this->subscription = this->qmqttClient->subscribe(this->topic, this->qos); debug (DEBUGMACRO, "Connecting sign 'subscription state Changed'", DEBUG); QObject::connect(this->subscription, &QMqttSubscription::stateChanged, mqttClient, &MqttClient::updateStatus); } void MqttClient::on_messageSentStatusChanged(qint32 id, QMqtt::MessageStatus s, const QMqttMessageStatusProperties &properties) { (void) properties; if ( id == 0) { debug(DEBUGMACRO, "message id = " + QString::number(id) + this->messageStatus[static_cast(s)], DEBUG); } } void MqttClient::on_error(QMqttClient::ClientError error) { debug(DEBUGMACRO, "Error " + qmqttErrors[error], ERROR); } void MqttClient::on_message(const QByteArray &message, const QMqttTopicName &topic) { (void) message; (void) topic; debug(DEBUGMACRO, "Message : " + message, DEBUG); } void MqttClient::on_disconnect() { debug(DEBUGMACRO, "Unexpected disconnect", DEBUG); this->qmqttClient->connectToHost(); } void MqttClient::on_messageSent(int mid) { debug(DEBUGMACRO, "published(" + QString::number(mid) + ")", DEBUG); messagesList.remove(mid); } void MqttClient::updateStatus(QMqttSubscription::SubscriptionState state) { switch (state) { case QMqttSubscription::Unsubscribed: debug(DEBUGMACRO, "Unsubscribed", INFO); break; case QMqttSubscription::SubscriptionPending: debug(DEBUGMACRO, "Pending subscription", DEBUG); break; case QMqttSubscription::Subscribed: debug(DEBUGMACRO, "Subscribed", INFO); break; case QMqttSubscription::Error: debug(DEBUGMACRO, "Subscription error" + this->subscription->reason(), ERROR); break; case QMqttSubscription::UnsubscriptionPending: debug(DEBUGMACRO, "Pending Unsubscription", DEBUG); break; default: debug(DEBUGMACRO, "--Unknown--", DEBUG); break; } } void MqttClient::on_stateChanged(QMqttClient::ClientState state) { debug(DEBUGMACRO, QLatin1String("State Change : ") + this->clientStateString[state], DEBUG ); }