Forráskód Böngészése

优化mqtt断网重连无法订阅mqtt消息的问题

james 11 hónapja
szülő
commit
c13a6102a2

+ 4 - 2
agFreshAirProcess/core.cpp

@@ -51,7 +51,9 @@ Core::Core(QObject *parent) : QObject(parent)
     connect(m_client,&QMQTT::Client::connected,this,&Core::onConnected);
     m_client->setUsername(username);
     m_client->setPassword(password.toLatin1());
-    m_client->setCleanSession(true);
+    m_client->setCleanSession(false);
+    m_client->setAutoReconnect(true);
+    m_client->setAutoReconnectInterval(60000);
     m_client->connectToHost();
 
     jzmqttsub = new JZMqttSub(this);
@@ -101,7 +103,7 @@ void Core::start()
 
 void Core::time_out()
 {
-    agBoxShm->processStatus[1].t_time=QDateTime::currentDateTime().toTime_t();
+    agBoxShm->processStatus[11].t_time=QDateTime::currentDateTime().toTime_t();
 
 
 }

+ 28 - 6
agFreshAirProcess/httpthread.cpp

@@ -43,6 +43,7 @@ void  HttpThread::finishedSlot(QNetworkReply *reply)
             int voc= 0;
             int fan_status = 0;
             int ts = (QDateTime::fromString(QDateTime::currentDateTime().toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss")).toTime_t();
+            int count = 0;
             for(int i=0;i<list_array.size();i++){
                 QJsonObject list_object = list_array.at(i).toObject();
                 device_code = list_object.value("roomNo").toString();
@@ -50,22 +51,41 @@ void  HttpThread::finishedSlot(QNetworkReply *reply)
                 //printf("222  list_object.value(eventTime).toString() %s, eventTime %s \n",list_object.value("eventTime").toString().toUtf8().data(),eventTime.toUtf8().data());
                 if(attribute_name.compare("新风系统-室内温度")==0){
                     wd = list_object.value("value").toDouble();
+                    int tmpValue = list_object.value("value").toInt();
+                    if(tmpValue == 0){
+                        count++;
+                    }
                     nodeInfo.append(QString("\"wd\":%1,").arg(wd));
                 }
                 if(attribute_name.compare("新风系统-室内湿度")==0){
                     sd = list_object.value("value").toDouble();
+                    int tmpValue = list_object.value("value").toInt();
+                    if(tmpValue == 0){
+                        count++;
+                    }
                     nodeInfo.append(QString("\"sd\":%1,").arg(sd));
                 }
                 if(attribute_name.compare("新风系统-室内PM2.5")==0){
                     pm2_5 = list_object.value("value").toDouble();
+                    int tmpValue = list_object.value("value").toInt();
+                    if(tmpValue == 0){
+                        count++;
+                    }
                     nodeInfo.append(QString("\"pm2_5\":%1,").arg(pm2_5));
                 }
                 if(attribute_name.compare("新风系统-室内CO2")==0){
                     co2 = list_object.value("value").toDouble();
+                    int tmpValue = list_object.value("value").toInt();
+                    if((tmpValue == 0)||(tmpValue == 244)){
+                        count++;
+                    }
                     nodeInfo.append(QString("\"co2\":%1,").arg(co2));
                 }
                 if(attribute_name.compare("新风系统-室内VOC等级")==0){
                     voc = list_object.value("value").toInt();
+                    if(voc == 0){
+                        count++;
+                    }
                     nodeInfo.append(QString("\"voc\":%1,").arg(voc));
                 }
                 if(attribute_name.contains("排气扇")){
@@ -77,12 +97,14 @@ void  HttpThread::finishedSlot(QNetworkReply *reply)
             }
             nodeInfo = nodeInfo.left(nodeInfo.length()-1);
 
-            for(int i=0;i<1024;i++){
-                if(agBoxShm->device[i].Enabled == 0x01){
-                    if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
-                        emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
-                                                                          .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(ts).arg(nodeInfo).arg(agBoxShm->device[i].device_type));
-                        break;
+            if(count < 5){
+                for(int i=0;i<1024;i++){
+                    if(agBoxShm->device[i].Enabled == 0x01){
+                        if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
+                            emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
+                                                                              .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(ts).arg(nodeInfo).arg(agBoxShm->device[i].device_type));
+                            break;
+                        }
                     }
                 }
             }

+ 3 - 1
agFreshAirProcess/jzmqttsub.cpp

@@ -36,7 +36,9 @@ void JZMqttSub::mqtt_conf(QString ip,QString port,QString username,QString passw
     m_client = new QMQTT::Client(QHostAddress("10.108.34.2"),1883,this);
     m_client->setUsername("usky");
     m_client->setPassword("usky");
-    m_client->setCleanSession(true);
+    m_client->setCleanSession(false);
+    m_client->setAutoReconnect(true);
+    m_client->setAutoReconnectInterval(60000);
     connect(m_client,&QMQTT::Client::connected,this,&JZMqttSub::onConnected);
     connect(m_client,&QMQTT::Client::received,this,&JZMqttSub::onReceived);
     m_client->connectToHost();

+ 10 - 0
agFreshAirProcess/logthread.cpp

@@ -36,6 +36,16 @@ void LogThread::run()
             if(day!=QDate::currentDate().day()){
                 day = QDate::currentDate().day();
                 file = new QFile("/usky/data-agbox/agmqttprocess/log/mqttprocess-"+QDate::currentDate().toString("yyyyMMdd")+".log");
+
+                uint l_time = QDateTime::currentDateTime().toTime_t()-7*86400;
+                QDir dir("/usky/data-agbox/agmqttprocess/log");
+                QFileInfoList file_list = dir.entryInfoList(QDir::Files);
+                for(int i=0;i<file_list.size();i++){
+                    QFileInfo f_info = file_list.at(i);
+                    if(f_info.lastModified().toTime_t()<l_time){
+                        QFile(f_info.canonicalFilePath()).remove();
+                    }
+                }
             }
             if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
                 while(dataList.length()>0){

+ 106 - 33
agFreshAirProcess/ytmqttpub.cpp

@@ -37,50 +37,123 @@ void YTMqttPub::run()
                         QJsonArray list_array = dev_value.toArray();
                         for(int i=0;i<list_array.size();i++){
                             QJsonObject obj_dev = list_array.at(i).toObject();
+                            QString dev = obj_dev.value("dev").toString();
 
                             QJsonValue d_value = obj_dev.value("d");
                             if(d_value.isArray()){
                                 QJsonArray d_array = d_value.toArray();
                                 if(d_array.size() > 0){
+                                    QString deviceCode = "";
+                                    if(dev.compare("AHU_B1_01") == 0){//ÌØÊâÉ豸´¦Àí
+                                        deviceCode = "AHU_B1_01";
+                                        for(int j=0;j<1024;j++){
+                                            if(agBoxShm->device[j].Enabled == 0x01){
+                                                if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
+                                                    deviceId = QString(agBoxShm->device[j].device_id);
+                                                    productCode = QString(agBoxShm->device[j].product_code);
+                                                    deviceType = QString::number(agBoxShm->device[j].device_type);
+                                                    int count = 0;
+                                                    for(int k=0;k<d_array.size();k++){
+                                                        QJsonObject obj_d = d_array.at(k).toObject();
+                                                        QString name = obj_d.value("m").toString();
+                                                        QList<QString> nameList = name.split("_");
+                                                        if(QString(nameList.at(4)).compare("SNCO2")==0){
+                                                            int value = obj_d.value("v").toInt();
+                                                            if((value == 0)||(value == 550)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"co2\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(4)).compare("SNPM2")==0){
+                                                            int value = obj_d.value("v").toInt();
+                                                            if((value == 0)||(value == 30)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"pm2_5\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(4)).compare("SNSB")==0){
+                                                            double value = obj_d.value("v").toDouble();
+                                                            int tmpValue = obj_d.value("v").toInt();
+                                                            if((tmpValue == 0)||(tmpValue == 58)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"sd\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(4)).compare("SNWD")==0){
+                                                            double value = obj_d.value("v").toDouble();
+                                                            int tmpValue = obj_d.value("v").toInt();
+                                                            if((tmpValue == 0)||(tmpValue == 18)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"wd\":%1,").arg(value));
+                                                        }
 
-                                    QString deviceCode = d_array.at(0).toObject().value("m").toString().mid(0,6);
-                                    printf("yts deviceCode: %s\n",deviceCode.toUtf8().data());
-                                    for(int j=0;j<1024;j++){
-                                        if(agBoxShm->device[j].Enabled == 0x01){
-                                            if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
-                                                deviceId = QString(agBoxShm->device[j].device_id);
-                                                productCode = QString(agBoxShm->device[j].product_code);
-                                                deviceType = QString::number(agBoxShm->device[j].device_type);
-                                                for(int k=0;k<d_array.size();k++){
-                                                    QJsonObject obj_d = d_array.at(k).toObject();
-                                                    QString name = obj_d.value("m").toString();
-                                                    QList<QString> nameList = name.split("_");
-                                                    int value = obj_d.value("v").toInt();
-                                                    if(QString(nameList.at(2)).compare("CO2")==0){
-                                                        nodeInfo.append(QString("\"co2\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("FLDW")==0){
-                                                        nodeInfo.append(QString("\"fldw\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("PM2")==0){
-                                                        nodeInfo.append(QString("\"pm2_5\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("SNSD")==0){
-                                                        nodeInfo.append(QString("\"sd\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("SNWD")==0){
-                                                        nodeInfo.append(QString("\"wd\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("VOC")==0){
-                                                        nodeInfo.append(QString("\"voc\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("YXMS")==0){
-                                                        nodeInfo.append(QString("\"yxms\":%1,").arg(value));
-                                                    }else if(QString(nameList.at(2)).compare("YXZT")==0){
-                                                        nodeInfo.append(QString("\"yxzt\":%1,").arg(value));
                                                     }
 
+                                                    nodeInfo = nodeInfo.left(nodeInfo.length()-1);
+                                                    if(count < 4){
+                                                        emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
+                                                                      .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
+                                                    }
+
+                                                    break;
                                                 }
+                                            }
+                                        }
+                                    }else{
+                                        deviceCode = d_array.at(0).toObject().value("m").toString().mid(0,6);
+                                        for(int j=0;j<1024;j++){
+                                            if(agBoxShm->device[j].Enabled == 0x01){
+                                                if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
+                                                    deviceId = QString(agBoxShm->device[j].device_id);
+                                                    productCode = QString(agBoxShm->device[j].product_code);
+                                                    deviceType = QString::number(agBoxShm->device[j].device_type);
+                                                    int count = 0;
+                                                    for(int k=0;k<d_array.size();k++){
+                                                        QJsonObject obj_d = d_array.at(k).toObject();
+                                                        QString name = obj_d.value("m").toString();
+                                                        QList<QString> nameList = name.split("_");
+                                                        int value = obj_d.value("v").toInt();
+                                                        if(QString(nameList.at(2)).compare("CO2")==0){
+                                                            if((value == 0)||(value == 550)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"co2\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("FLDW")==0){
+                                                            nodeInfo.append(QString("\"fldw\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("PM2")==0){
+                                                            if((value == 0)||(value == 30)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"pm2_5\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("SNSD")==0){
+                                                            if((value == 0)||(value == 58)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"sd\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("SNWD")==0){
+                                                            if((value == 0)||(value == 18)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"wd\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("VOC")==0){
+                                                            if((value == 0)||(value == 1)){
+                                                                count++;
+                                                            }
+                                                            nodeInfo.append(QString("\"voc\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("YXMS")==0){
+                                                            nodeInfo.append(QString("\"yxms\":%1,").arg(value));
+                                                        }else if(QString(nameList.at(2)).compare("YXZT")==0){
+                                                            nodeInfo.append(QString("\"yxzt\":%1,").arg(value));
+                                                        }
 
-                                                nodeInfo = nodeInfo.left(nodeInfo.length()-1);
-                                                emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
-                                                              .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
+                                                    }
+
+                                                    nodeInfo = nodeInfo.left(nodeInfo.length()-1);
+                                                    if(count < 5){
+                                                        emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
+                                                                      .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
+                                                    }
 
-                                                break;
+                                                    break;
+                                                }
                                             }
                                         }
                                     }

+ 1 - 0
agFreshAirProcess/ytmqttsub.cpp

@@ -49,6 +49,7 @@ void YTMqttSub::onConnected()
     m_client->subscribe("/edge/baInfo/E4C82427D44D40E5B398002CA5A1EF42/rtg",0);
     m_client->subscribe("/edge/baInfo/0344B9CD3B6A468E87551B25BC679A96/rtg",0);
     m_client->subscribe("/edge/baInfo/2F0C601E76E9444997F5E72EAB86856F/rtg",0);
+    m_client->subscribe("/edge/baInfo/3B6CEDD977434B89AE9029938D673BB3/rtg",0);
 
 }