widget.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "widget.h"
  2. #include "./ui_widget.h"
  3. #include <QNetworkReply>
  4. #include <QNetworkAccessManager>
  5. #include <QNetworkRequest>
  6. #include <QTimer>
  7. #include <QNetworkProxy>
  8. #include <QJsonDocument>
  9. #include <QJsonParseError>
  10. #include "spdlog/spdlog.h"
  11. Widget::Widget(QWidget *parent)
  12. : QWidget(parent)
  13. , ui(new Ui::Widget)
  14. {
  15. ui->setupUi(this);
  16. SPDLOG_INFO("***** Qt Library *****");
  17. /* 这个好像是设置代理,设置成无代理,不然可能会报错 */
  18. QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
  19. m_client = new QMQTT::Client;
  20. QHostAddress addr("192.1.2.61");
  21. m_client->setHost(addr);
  22. m_client->setPort(1883);
  23. m_client->setClientId("qt_client");
  24. m_client->setKeepAlive(60);
  25. m_client->setWillQos(0);
  26. connect(m_client,SIGNAL(received(const QMQTT::Message)),this,SLOT(do_receiveFromMqtt(const QMQTT::Message)));
  27. connect(m_client,&QMQTT::Client::connected,this,[&](){
  28. SPDLOG_INFO("连接成功!");
  29. // m_client->subscribe("test/one");
  30. m_client->subscribe(ui->lineEdit_subscribe->text());
  31. });
  32. connect(m_client,&QMQTT::Client::error,this,[&](const QMQTT::ClientError error){
  33. SPDLOG_ERROR("MQTT Error: {}", (int)error);
  34. });
  35. connect(m_client,&QMQTT::Client::subscribed,this,[&](const QString& topic, const quint8 qos){
  36. SPDLOG_INFO("Subscribed to: {}", topic.toStdString());
  37. });
  38. auto timer = new QTimer(this);
  39. timer->setTimerType(Qt::PreciseTimer);
  40. connect(timer,&QTimer::timeout,this,[&](){
  41. SPDLOG_INFO("Publishing to MQTT broker");
  42. m_client->publish(QMQTT::Message(0,"test/one","Hello from Qt"));
  43. });
  44. // timer->start(1000);
  45. ui->lineEdit_IP->setText("192.1.2.61");
  46. ui->lineEdit_subscribe->setText("LH_ENERGY_FILE/ME24052101-hzlh-com");
  47. }
  48. Widget::~Widget()
  49. {
  50. delete m_client;
  51. delete ui;
  52. }
  53. /* 接收到数据 */
  54. void Widget::do_receiveFromMqtt(const QMQTT::Message& msg)
  55. {
  56. SPDLOG_INFO("Received: {}", msg.payload().toStdString());
  57. QJsonParseError error;
  58. QJsonDocument doc = QJsonDocument::fromJson(msg.payload(),&error);
  59. // if(error.error != QJsonParseError::NoError){
  60. // SPDLOG_ERROR("Json Parse Error: {}", error.errorString().toStdString());
  61. // return;
  62. // }
  63. SPDLOG_INFO("JSON : {}", doc.toJson().toStdString());
  64. SPDLOG_INFO("原始数据 : {}", msg.payload().toHex().toStdString());
  65. }
  66. void Widget::on_pBtn_connectMQTT_clicked()
  67. {
  68. SPDLOG_INFO("点击了“连接按钮”");
  69. m_client->setHost(QHostAddress(ui->lineEdit_IP->text()));
  70. m_client->connectToHost();
  71. }
  72. void Widget::on_pBtn_publishMSG_clicked()
  73. {
  74. SPDLOG_INFO("点击了“发布按钮”");
  75. m_client->publish(QMQTT::Message(0,"test/one","Hello from Qt"));
  76. }
  77. // void mqtt_handle() {
  78. // sleep(5);
  79. // struct mosquitto* mosq = NULL;
  80. // int ret;
  81. // // 初始化Mosquitto库
  82. // mosquitto_lib_init();
  83. // printf("mqtt 1\n");
  84. // // 创建Mosquitto实例
  85. // mosq = mosquitto_new(NULL, true, NULL);
  86. // if (!mosq) {
  87. // printf("Failed to create Mosquitto instance\n");
  88. // return 1;
  89. // }
  90. // printf("mqtt 2\n");
  91. // // 设置连接成功和发布完成的回调函数
  92. // mosquitto_connect_callback_set(mosq, on_connect);
  93. // mosquitto_publish_callback_set(mosq, on_publish);
  94. // printf("mqtt 3\n");
  95. // // 连接到MQTT代理
  96. // ret = mosquitto_connect(mosq, MQTT_HOST, MQTT_PORT, 60);
  97. // if (ret != MOSQ_ERR_SUCCESS) {
  98. // printf("Failed to connect to MQTT broker: %s\n", mosquitto_strerror(ret));
  99. // return 1;
  100. // }
  101. // printf("mqtt 4\n");
  102. // unsigned char devicename[] = "GX24053004-hzlh-com"; // 设备名
  103. // unsigned char timestr[] = "2024-07-31 15:05:36.010"; // 时间
  104. // unsigned char hexValue[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; // 十六进制值
  105. // // unsigned char* binaryValue;
  106. // // size_t binarySize;
  107. // // hexStringToBinary(hexValue, &binaryValue, &binarySize);
  108. // cJSON* root = cJSON_CreateObject(); // 创建根节点
  109. // cJSON_AddItemToObject(root, "devicename", cJSON_CreateString((unsigned char*)devicename)); // 添加设备名
  110. // cJSON_AddItemToObject(root, "time", cJSON_CreateString((unsigned char*)timestr)); // 添加时间
  111. // cJSON_AddItemToObject(root, "value", cJSON_CreateRaw((unsigned char*)hexValue)); // 添加值
  112. // unsigned char* jsonMessage = cJSON_PrintUnformatted(root); // 将JSON对象转换为字符串
  113. // // int bufferSize = 0;
  114. // // unsigned char* jsonMessage = cJSON_PrintBuffered(root, bufferSize, false);
  115. // mqtt_flg =1;
  116. // printf("ssmqttflg 4\n");
  117. // // 发布消息
  118. // while (1) {
  119. // if (mqtt_flg && sel_ok)
  120. // {
  121. // printf("jsonlen:%d\n",strlen(jsonMessage));
  122. // ret = mosquitto_publish(mosq, NULL, TOPIC, strlen(jsonMessage), jsonMessage, QOS, false);
  123. // if (ret != MOSQ_ERR_SUCCESS) {
  124. // printf("Failed to publish message: %s\n", mosquitto_strerror(ret));
  125. // }
  126. // printf("msg:%s\n",jsonMessage);
  127. // // 控制消息发布频率,以避免太快或太频繁
  128. // usleep(1000000); // 暂停1秒
  129. // ret = mosquitto_loop(mosq, 0, 1); // 处理网络消息
  130. // if (ret != MOSQ_ERR_SUCCESS) {
  131. // printf("Failed to handle network events: %s\n", mosquitto_strerror(ret));
  132. // break;
  133. // }
  134. // }
  135. // }
  136. // // free(binaryValue);
  137. // free(jsonMessage);
  138. // cJSON_Delete(root);
  139. // // 清理Mosquitto实例和Mosquitto库
  140. // mosquitto_destroy(mosq);
  141. // mosquitto_lib_cleanup();
  142. // }