MQTTBase.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "MQTTBase.h"
  2. #include <QDebug>
  3. #include "qmqtt_message.h"
  4. #include "spdlog/spdlog.h"
  5. MQTTBase::MQTTBase(QObject* parent) : QObject(parent)
  6. {
  7. /* 获取日志 */
  8. // m_logger = spdlog::get("MQTT");
  9. // if(m_logger == nullptr)
  10. // {
  11. // SPDLOG_ERROR("获取MQTT logger 失败!");
  12. // exit(-1);
  13. // }
  14. /* 连接信号和槽 */
  15. connect(&m_client,SIGNAL(connected()),this,SLOT(do_connected()));
  16. connect(&m_client,SIGNAL(disconnected()),this,SLOT(do_disconnect()));
  17. connect(&m_client,SIGNAL(error(QMQTT::ClientError)),this,SLOT(do_error(QMQTT::ClientError)));
  18. connect(&m_client,SIGNAL(subscribed(QString,quint8)),this,SLOT(do_subscribed(QString, quint8)));
  19. connect(&m_client,SIGNAL(received(QMQTT::Message)),this,SLOT(do_received(QMQTT::Message)));
  20. connect(&m_client, &QMQTT::Client::received, this, &MQTTBase::signal_recvMessage);
  21. }
  22. /* 设置地址 */
  23. void MQTTBase::setIPAndPort(const QString& IP, int port)
  24. {
  25. m_mqttIP = IP;
  26. m_mqttPort = port;
  27. QHostAddress addr = QHostAddress(IP);
  28. m_client.setHost(addr);
  29. m_client.setPort(port);
  30. }
  31. /* 设置订阅 */
  32. void MQTTBase::addSubcribe(const QString& topic, int qos)
  33. {
  34. if(qos > 2)
  35. {
  36. SPDLOG_ERROR("QoS值不合法:{}", qos);
  37. return;
  38. }
  39. if(m_isConnected)
  40. {
  41. /* 已连接到MQTT,之前添加的主题已经订阅,现在添加的也直接添加订阅 */
  42. m_client.subscribe(topic, qos);
  43. m_mapTopic.insert(topic, qos);
  44. }else
  45. {
  46. /* 还未连接到MQTT,直接加入到订阅列表 */
  47. m_mapTopic.insert(topic, qos);
  48. }
  49. }
  50. /* 设置自动重连 */
  51. void MQTTBase::setAutoReconnect(bool isAuto)
  52. {
  53. m_client.setAutoReconnect(isAuto);
  54. }
  55. /* 连接到服务器 */
  56. void MQTTBase::connectToServer()
  57. {
  58. m_client.connectToHost();
  59. }
  60. /* 获取连接状态 */
  61. QMQTT::ConnectionState MQTTBase::connectState()
  62. {
  63. return m_client.connectionState();
  64. }
  65. /* 发送消息 */
  66. void MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos)
  67. {
  68. if(m_isConnected == false)
  69. {
  70. SPDLOG_ERROR("MQTT未连接到服务器,发送消息失败");
  71. return;
  72. }
  73. QMQTT::Message msg(0, topic, message, qos);
  74. auto ret = m_client.publish(msg);
  75. if(ret != 0)
  76. {
  77. SPDLOG_ERROR("发送消息失败:{}, 错误代码:{}", topic.toStdString(), ret);
  78. }
  79. }
  80. /* 发送消息,不检测是否连接了服务器 */
  81. bool MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos, int& errorCode)
  82. {
  83. if(qos > 2)
  84. {
  85. SPDLOG_ERROR("QoS值不合法:{}", qos);
  86. errorCode = -1;
  87. return false;
  88. }
  89. QMQTT::Message msg(0, topic, message, qos);
  90. auto ret = m_client.publish(msg);
  91. if(ret != 0)
  92. {
  93. // SPDLOG_ERROR("发送消息失败:{}, 错误代码:{}", topic.toStdString(), ret);
  94. errorCode = ret;
  95. return false;
  96. }
  97. errorCode = 0;
  98. return true;
  99. }
  100. /* 连接成功 */
  101. void MQTTBase::do_connected()
  102. {
  103. SPDLOG_INFO("MQTT IP:{} ,Port:{} 连接成功!", m_mqttIP.toStdString(), m_mqttPort);
  104. m_isConnected = true;
  105. /* 订阅所有的主题 */
  106. for(auto& it : m_mapTopic.keys())
  107. {
  108. auto qos = m_mapTopic.value(it);
  109. m_client.subscribe(it, qos);
  110. }
  111. }
  112. /* 断开连接 */
  113. void MQTTBase::do_disconnect()
  114. {
  115. SPDLOG_INFO("断开连接!");
  116. }
  117. /* 错误 */
  118. void MQTTBase::do_error(const QMQTT::ClientError error)
  119. {
  120. SPDLOG_ERROR("错误:{}", (int)error);
  121. }
  122. /* 订阅成功 */
  123. void MQTTBase::do_subscribed(const QString& topic, const quint8 qos)
  124. {
  125. SPDLOG_INFO("订阅:{}, QoS:{} 成功", topic.toStdString(), qos);
  126. }
  127. /* 接收到消息 */
  128. void MQTTBase::do_received(const QMQTT::Message& message)
  129. {
  130. // SPDLOG_INFO("MQTTBase接收到一条消息:{}", message.topic().toStdString());
  131. recvMessage(message);
  132. }