#include "widget.h" #include "./ui_widget.h" #include #include #include #include #include #include #include #include "spdlog/spdlog.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); SPDLOG_INFO("***** Qt Library *****"); /* 这个好像是设置代理,设置成无代理,不然可能会报错 */ QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy); m_client = new QMQTT::Client; QHostAddress addr("192.1.2.61"); m_client->setHost(addr); m_client->setPort(1883); m_client->setClientId("qt_client"); m_client->setKeepAlive(60); m_client->setWillQos(0); connect(m_client,SIGNAL(received(const QMQTT::Message)),this,SLOT(do_receiveFromMqtt(const QMQTT::Message))); connect(m_client,&QMQTT::Client::connected,this,[&](){ SPDLOG_INFO("连接成功!"); // m_client->subscribe("test/one"); m_client->subscribe(ui->lineEdit_subscribe->text()); }); connect(m_client,&QMQTT::Client::error,this,[&](const QMQTT::ClientError error){ SPDLOG_ERROR("MQTT Error: {}", (int)error); }); connect(m_client,&QMQTT::Client::subscribed,this,[&](const QString& topic, const quint8 qos){ SPDLOG_INFO("Subscribed to: {}", topic.toStdString()); }); auto timer = new QTimer(this); timer->setTimerType(Qt::PreciseTimer); connect(timer,&QTimer::timeout,this,[&](){ SPDLOG_INFO("Publishing to MQTT broker"); m_client->publish(QMQTT::Message(0,"test/one","Hello from Qt")); }); // timer->start(1000); ui->lineEdit_IP->setText("192.1.2.61"); ui->lineEdit_subscribe->setText("LH_ENERGY_FILE/ME24052101-hzlh-com"); } Widget::~Widget() { delete m_client; delete ui; } /* 接收到数据 */ void Widget::do_receiveFromMqtt(const QMQTT::Message& msg) { SPDLOG_INFO("Received: {}", msg.payload().toStdString()); QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(msg.payload(),&error); // if(error.error != QJsonParseError::NoError){ // SPDLOG_ERROR("Json Parse Error: {}", error.errorString().toStdString()); // return; // } SPDLOG_INFO("JSON : {}", doc.toJson().toStdString()); SPDLOG_INFO("原始数据 : {}", msg.payload().toHex().toStdString()); } void Widget::on_pBtn_connectMQTT_clicked() { SPDLOG_INFO("点击了“连接按钮”"); m_client->setHost(QHostAddress(ui->lineEdit_IP->text())); m_client->connectToHost(); } void Widget::on_pBtn_publishMSG_clicked() { SPDLOG_INFO("点击了“发布按钮”"); m_client->publish(QMQTT::Message(0,"test/one","Hello from Qt")); } // void mqtt_handle() { // sleep(5); // struct mosquitto* mosq = NULL; // int ret; // // 初始化Mosquitto库 // mosquitto_lib_init(); // printf("mqtt 1\n"); // // 创建Mosquitto实例 // mosq = mosquitto_new(NULL, true, NULL); // if (!mosq) { // printf("Failed to create Mosquitto instance\n"); // return 1; // } // printf("mqtt 2\n"); // // 设置连接成功和发布完成的回调函数 // mosquitto_connect_callback_set(mosq, on_connect); // mosquitto_publish_callback_set(mosq, on_publish); // printf("mqtt 3\n"); // // 连接到MQTT代理 // ret = mosquitto_connect(mosq, MQTT_HOST, MQTT_PORT, 60); // if (ret != MOSQ_ERR_SUCCESS) { // printf("Failed to connect to MQTT broker: %s\n", mosquitto_strerror(ret)); // return 1; // } // printf("mqtt 4\n"); // unsigned char devicename[] = "GX24053004-hzlh-com"; // 设备名 // unsigned char timestr[] = "2024-07-31 15:05:36.010"; // 时间 // unsigned char hexValue[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; // 十六进制值 // // unsigned char* binaryValue; // // size_t binarySize; // // hexStringToBinary(hexValue, &binaryValue, &binarySize); // cJSON* root = cJSON_CreateObject(); // 创建根节点 // cJSON_AddItemToObject(root, "devicename", cJSON_CreateString((unsigned char*)devicename)); // 添加设备名 // cJSON_AddItemToObject(root, "time", cJSON_CreateString((unsigned char*)timestr)); // 添加时间 // cJSON_AddItemToObject(root, "value", cJSON_CreateRaw((unsigned char*)hexValue)); // 添加值 // unsigned char* jsonMessage = cJSON_PrintUnformatted(root); // 将JSON对象转换为字符串 // // int bufferSize = 0; // // unsigned char* jsonMessage = cJSON_PrintBuffered(root, bufferSize, false); // mqtt_flg =1; // printf("ssmqttflg 4\n"); // // 发布消息 // while (1) { // if (mqtt_flg && sel_ok) // { // printf("jsonlen:%d\n",strlen(jsonMessage)); // ret = mosquitto_publish(mosq, NULL, TOPIC, strlen(jsonMessage), jsonMessage, QOS, false); // if (ret != MOSQ_ERR_SUCCESS) { // printf("Failed to publish message: %s\n", mosquitto_strerror(ret)); // } // printf("msg:%s\n",jsonMessage); // // 控制消息发布频率,以避免太快或太频繁 // usleep(1000000); // 暂停1秒 // ret = mosquitto_loop(mosq, 0, 1); // 处理网络消息 // if (ret != MOSQ_ERR_SUCCESS) { // printf("Failed to handle network events: %s\n", mosquitto_strerror(ret)); // break; // } // } // } // // free(binaryValue); // free(jsonMessage); // cJSON_Delete(root); // // 清理Mosquitto实例和Mosquitto库 // mosquitto_destroy(mosq); // mosquitto_lib_cleanup(); // }