MQTTBase.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void MQTTBase::sendMessage(const QString& topic, const QByteArray& message, int qos)
  62. {
  63. if(m_isConnected == false)
  64. {
  65. SPDLOG_ERROR("MQTT未连接到服务器,发送消息失败");
  66. return;
  67. }
  68. QMQTT::Message msg(0, topic, message, qos);
  69. auto ret = m_client.publish(msg);
  70. if(ret != 0)
  71. {
  72. SPDLOG_ERROR("发送消息失败:{}, 错误代码:{}", topic.toStdString(), ret);
  73. }
  74. }
  75. /* 连接成功 */
  76. void MQTTBase::do_connected()
  77. {
  78. SPDLOG_INFO("MQTT IP:{} ,Port:{} 连接成功!", m_mqttIP.toStdString(), m_mqttPort);
  79. m_isConnected = true;
  80. /* 订阅所有的主题 */
  81. for(auto& it : m_mapTopic.keys())
  82. {
  83. auto qos = m_mapTopic.value(it);
  84. m_client.subscribe(it, qos);
  85. }
  86. }
  87. /* 断开连接 */
  88. void MQTTBase::do_disconnect()
  89. {
  90. SPDLOG_INFO("断开连接!");
  91. }
  92. /* 错误 */
  93. void MQTTBase::do_error(const QMQTT::ClientError error)
  94. {
  95. SPDLOG_ERROR("错误:{}", (int)error);
  96. }
  97. /* 订阅成功 */
  98. void MQTTBase::do_subscribed(const QString& topic, const quint8 qos)
  99. {
  100. SPDLOG_INFO("订阅:{}, QoS:{} 成功", topic.toStdString(), qos);
  101. }
  102. /* 接收到消息 */
  103. void MQTTBase::do_received(const QMQTT::Message& message)
  104. {
  105. // SPDLOG_INFO("MQTTBase接收到一条消息:{}", message.topic().toStdString());
  106. recvMessage(message);
  107. }