MQTTBase.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. bool MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos)
  67. {
  68. if(m_isConnected == false)
  69. {
  70. SPDLOG_ERROR("MQTT未连接到服务器,发送消息失败");
  71. return false;
  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. return false;
  79. }
  80. return true;
  81. }
  82. /* 发送消息,设置消息保留 */
  83. bool MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos, bool retain)
  84. {
  85. if(m_isConnected == false)
  86. {
  87. SPDLOG_ERROR("MQTT未连接到服务器,发送消息失败");
  88. return false;
  89. }
  90. QMQTT::Message msg;
  91. msg.setTopic(topic);
  92. msg.setPayload(message);
  93. msg.setQos(qos);
  94. msg.setRetain(retain); // 设置消息保留
  95. auto ret = m_client.publish(msg);
  96. if(ret != 0)
  97. {
  98. SPDLOG_ERROR("发送消息失败:{}, 错误代码:{}", topic.toStdString(), ret);
  99. return false;
  100. }
  101. return true;
  102. }
  103. /* 连接成功 */
  104. void MQTTBase::do_connected()
  105. {
  106. SPDLOG_INFO("MQTT IP:{} ,Port:{} 连接成功!", m_mqttIP.toStdString(), m_mqttPort);
  107. m_isConnected = true;
  108. /* 订阅所有的主题 */
  109. for(auto& it : m_mapTopic.keys())
  110. {
  111. auto qos = m_mapTopic.value(it);
  112. m_client.subscribe(it, qos);
  113. }
  114. }
  115. /* 断开连接 */
  116. void MQTTBase::do_disconnect()
  117. {
  118. SPDLOG_INFO("断开连接!");
  119. }
  120. /* 错误 */
  121. void MQTTBase::do_error(const QMQTT::ClientError error)
  122. {
  123. SPDLOG_ERROR("错误:{}", (int)error);
  124. }
  125. /* 订阅成功 */
  126. void MQTTBase::do_subscribed(const QString& topic, const quint8 qos)
  127. {
  128. SPDLOG_INFO("订阅:{}, QoS:{} 成功", topic.toStdString(), qos);
  129. }
  130. /* 接收到消息 */
  131. void MQTTBase::do_received(const QMQTT::Message& message)
  132. {
  133. // SPDLOG_INFO("MQTTBase接收到一条消息:{}", message.topic().toStdString());
  134. recvMessage(message);
  135. }