MQTTBase.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. }
  21. /* 设置地址 */
  22. void MQTTBase::setIPAndPort(const QString& IP, int port)
  23. {
  24. m_mqttIP = IP;
  25. m_mqttPort = port;
  26. QHostAddress addr = QHostAddress(IP);
  27. m_client.setHost(addr);
  28. m_client.setPort(port);
  29. }
  30. /* 设置订阅 */
  31. void MQTTBase::addSubcribe(const QString& topic, int qos)
  32. {
  33. if(qos > 2)
  34. {
  35. SPDLOG_ERROR("QoS值不合法:{}", qos);
  36. return;
  37. }
  38. if(m_isConnected)
  39. {
  40. /* 已连接到MQTT,之前添加的主题已经订阅,现在添加的也直接添加订阅 */
  41. m_client.subscribe(topic, qos);
  42. m_mapTopic.insert(topic, qos);
  43. }else
  44. {
  45. /* 还未连接到MQTT,直接加入到订阅列表 */
  46. m_mapTopic.insert(topic, qos);
  47. }
  48. }
  49. /* 设置自动重连 */
  50. void MQTTBase::setAutoReconnect(bool isAuto)
  51. {
  52. m_client.setAutoReconnect(isAuto);
  53. }
  54. /* 连接到服务器 */
  55. void MQTTBase::connectToServer()
  56. {
  57. m_client.connectToHost();
  58. }
  59. /* 发送消息 */
  60. void MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos)
  61. {
  62. if(m_isConnected == false)
  63. {
  64. SPDLOG_ERROR("MQTT未连接到服务器,发送消息失败");
  65. return;
  66. }
  67. QMQTT::Message msg(0, topic, message, qos);
  68. auto ret = m_client.publish(msg);
  69. if(ret != 0)
  70. {
  71. SPDLOG_ERROR("发送消息失败:{}, 错误代码:{}", topic.toStdString(), ret);
  72. }
  73. }
  74. /* 连接成功 */
  75. void MQTTBase::do_connected()
  76. {
  77. SPDLOG_INFO("MQTT IP:{} ,Port:{} 连接成功!", m_mqttIP.toStdString(), m_mqttPort);
  78. m_isConnected = true;
  79. /* 订阅所有的主题 */
  80. for(auto& it : m_mapTopic.keys())
  81. {
  82. auto qos = m_mapTopic.value(it);
  83. m_client.subscribe(it, qos);
  84. }
  85. }
  86. /* 断开连接 */
  87. void MQTTBase::do_disconnect()
  88. {
  89. SPDLOG_INFO("断开连接!");
  90. }
  91. /* 错误 */
  92. void MQTTBase::do_error(const QMQTT::ClientError error)
  93. {
  94. SPDLOG_ERROR("错误:{}", (int)error);
  95. }
  96. /* 订阅成功 */
  97. void MQTTBase::do_subscribed(const QString& topic, const quint8 qos)
  98. {
  99. SPDLOG_INFO("订阅:{}, QoS:{} 成功", topic.toStdString(), qos);
  100. }
  101. /* 接收到消息 */
  102. void MQTTBase::do_received(const QMQTT::Message& message)
  103. {
  104. // SPDLOG_INFO("MQTTBase接收到一条消息:{}", message.topic().toStdString());
  105. recvMessage(message);
  106. }