123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include "ytmqttpub.h"
- #include "../AGBoxDog/boxshm.h"
- YTMqttPub::YTMqttPub(QObject *parent) : QThread(parent)
- {
- keep = false;
- mqttDataList.clear();
- }
- void YTMqttPub::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("yts device parseErr.error %d\n",parseErr.error);
- if(parseErr.error==QJsonParseError::NoError){
- QString nodeInfo = "";
- QString deviceId = "";
- QString productCode = "";
- QString deviceType = "";
- QJsonObject obj_doc = doc.object();
- int ts = obj_doc.value("ts").toInt();
- QDateTime datatime = QDateTime::fromTime_t(ts);
- int minute = datatime.time().minute();
- QJsonValue dev_value = obj_doc.value("devs");
- if(dev_value.isArray() && (minute == 00)){
- int ts = QDateTime::fromString(datatime.toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss").toTime_t();
- 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));
- }
- }
- 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);
- 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;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- mqttDataList.removeFirst();
- usleep(1000);
- }
- }
- usleep(50000);
- }
- }
- void YTMqttPub::stop()
- {
- keep = false;
- }
- void YTMqttPub::devMessage(MqttData dev)
- {
- mqttDataList.append(dev);
- }
|