#include "mqttpub.h" #include "../AGBoxDog/boxshm.h" MqttPub::MqttPub(QObject *parent) : QThread(parent) { keep = false; mqttDataList.clear(); } void MqttPub::stop() { keep = false; } void MqttPub::run() { keep = true; while (keep) { if(mqttDataList.length()>0){ while (mqttDataList.length()>0) { MqttData md = mqttDataList.first(); QString topic = md.topic; QByteArray data = md.data; QJsonParseError parseErr; QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr); printf("environment parseErr.error %d\n",parseErr.error); if(parseErr.error==QJsonParseError::NoError){ QString nodeInfo = ""; QString deviceId = ""; QString companyCode = ""; QString deviceType = ""; QJsonObject obj_doc = doc.object(); QString sn = obj_doc.value("sn").toString(); int ts = obj_doc.value("ts").toInt(); QDateTime datetime = QDateTime::fromTime_t(ts); int minute = datetime.time().minute(); printf("topic:%s, datetime: %s, minute: %d\n",topic.toUtf8().data(),datetime.toString("yyyy-MM-dd HH:mm:ss").toUtf8().data(),minute); QJsonValue dev_value = obj_doc.value("devs"); if(dev_value.isArray() && (minute == 00)){ QJsonArray list_array = dev_value.toArray(); for(int i=0;idevice[i].Enabled == 0x01){ if(deviceCode.compare(QString(agBoxShm->device[i].device_code))==0){ agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t(); deviceId = QString(agBoxShm->device[i].device_id); companyCode = QString(agBoxShm->device[i].product_code); deviceType = QString::number(agBoxShm->device[i].device_type); agBoxShm->device->lastTime = QDateTime::currentDateTime().toTime_t(); break; } } } QJsonValue d_value = obj_dev.value("d"); if(d_value.isArray()){ QJsonArray d_array = d_value.toArray(); if(d_array.size()==8){ for(int i=0;iprocuctattrbute[i].attribute_name))==0){ nodeInfo.append(QString("\"%1\":%2,").arg(QString(agBoxShm->procuctattrbute[i].attribute_code)).arg(value)); break; } } } nodeInfo.append(QString("\"device_status\":1")); emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-ev\"}") .arg(deviceId).arg(deviceCode).arg(companyCode).arg(ts).arg(nodeInfo).arg(deviceType)); } } } } } mqttDataList.removeFirst(); usleep(1000); } } usleep(50000); } } void MqttPub::devMessage(MqttData dev) { mqttDataList.append(dev); }