|
@@ -1,110 +0,0 @@
|
|
|
-#include "fromMQTT.h"
|
|
|
-
|
|
|
-#include <QDebug>
|
|
|
-#include <QJsonDocument>
|
|
|
-#include <QJsonObject>
|
|
|
-#include <QJsonParseError>
|
|
|
-
|
|
|
-#include "qmqtt_message.h"
|
|
|
-
|
|
|
-
|
|
|
-FromMQTT::FromMQTT(QObject* parent) : QObject(parent)
|
|
|
-{
|
|
|
-
|
|
|
- m_logger = spdlog::get("MQTT");
|
|
|
- if(m_logger == nullptr)
|
|
|
- {
|
|
|
- qDebug() << "获取MQTT logger 失败!";
|
|
|
- exit(-1);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- connect(&m_client,SIGNAL(connected()),this,SLOT(do_connected()));
|
|
|
- connect(&m_client,SIGNAL(disconnected()),this,SLOT(do_disconnect()));
|
|
|
- connect(&m_client,SIGNAL(error(QMQTT::ClientError)),this,SLOT(do_error(QMQTT::ClientError)));
|
|
|
- connect(&m_client,SIGNAL(received(QMQTT::Message)),this,SLOT(do_received(QMQTT::Message)));
|
|
|
- connect(&m_client,SIGNAL(subscribed(QString,quint8)),this,SLOT(do_subscribed(QString, quint8)));
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::setHostName(const QString& hostName)
|
|
|
-{
|
|
|
- QHostAddress addr = QHostAddress(hostName);
|
|
|
- m_client.setHost(addr);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::setSubcribe(const QString& topic, int qos)
|
|
|
-{
|
|
|
- m_client.subscribe(topic,qos);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::connectToServer()
|
|
|
-{
|
|
|
- m_client.connectToHost();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::analyzeAllMessage()
|
|
|
-{
|
|
|
- while (!m_queueMessage.isEmpty())
|
|
|
- {
|
|
|
- analyzeOneMessage();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::analyzeOneMessage()
|
|
|
-{
|
|
|
-
|
|
|
- if(m_queueMessage.isEmpty())
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- SPDLOG_LOGGER_INFO(m_logger,"message:{}",m_queueMessage.front().toStdString());
|
|
|
-
|
|
|
-
|
|
|
- m_queueMessage.dequeue();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::do_connected()
|
|
|
-{
|
|
|
- SPDLOG_LOGGER_INFO(m_logger,"连接成功!");
|
|
|
- m_client.subscribe("test/one");
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::do_disconnect()
|
|
|
-{
|
|
|
- SPDLOG_LOGGER_INFO(m_logger,"断开连接!");
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::do_error(const QMQTT::ClientError error)
|
|
|
-{
|
|
|
- SPDLOG_LOGGER_ERROR(m_logger,"错误:{}",(int)error);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::do_subscribed(const QString& topic, const quint8 qos)
|
|
|
-{
|
|
|
- SPDLOG_LOGGER_INFO(m_logger,"订阅:{},QoS:{} 成功",topic.toStdString(),qos);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void FromMQTT::do_received(const QMQTT::Message& message)
|
|
|
-{
|
|
|
-
|
|
|
-
|
|
|
- QByteArray ba = message.payload();
|
|
|
-
|
|
|
- m_queueMessage.enqueue(ba);
|
|
|
-
|
|
|
- analyzeOneMessage();
|
|
|
-}
|
|
|
-
|