ytmqttpub.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "ytmqttpub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. YTMqttPub::YTMqttPub(QObject *parent) : QThread(parent)
  4. {
  5. keep = false;
  6. mqttDataList.clear();
  7. }
  8. void YTMqttPub::run()
  9. {
  10. keep = true;
  11. while (keep) {
  12. if(mqttDataList.length()>0){
  13. while (mqttDataList.length()>0) {
  14. MqttData md = mqttDataList.first();
  15. QString topic = md.topic;
  16. QByteArray data = md.data;
  17. QJsonParseError parseErr;
  18. QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
  19. printf("yts device parseErr.error %d\n",parseErr.error);
  20. if(parseErr.error==QJsonParseError::NoError){
  21. QString nodeInfo = "";
  22. QString deviceId = "";
  23. QString productCode = "";
  24. QString deviceType = "";
  25. QJsonObject obj_doc = doc.object();
  26. int ts = obj_doc.value("ts").toInt();
  27. QDateTime datatime = QDateTime::fromTime_t(ts);
  28. int minute = datatime.time().minute();
  29. QJsonValue dev_value = obj_doc.value("devs");
  30. if(dev_value.isArray() && (minute == 00)){
  31. int ts = QDateTime::fromString(datatime.toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss").toTime_t();
  32. QJsonArray list_array = dev_value.toArray();
  33. for(int i=0;i<list_array.size();i++){
  34. QJsonObject obj_dev = list_array.at(i).toObject();
  35. QString dev = obj_dev.value("dev").toString();
  36. QJsonValue d_value = obj_dev.value("d");
  37. if(d_value.isArray()){
  38. QJsonArray d_array = d_value.toArray();
  39. if(d_array.size() > 0){
  40. QString deviceCode = "";
  41. if(dev.compare("AHU_B1_01") == 0){//ÌØÊâÉ豸´¦Àí
  42. deviceCode = "AHU_B1_01";
  43. for(int j=0;j<1024;j++){
  44. if(agBoxShm->device[j].Enabled == 0x01){
  45. if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
  46. deviceId = QString(agBoxShm->device[j].device_id);
  47. productCode = QString(agBoxShm->device[j].product_code);
  48. deviceType = QString::number(agBoxShm->device[j].device_type);
  49. int count = 0;
  50. for(int k=0;k<d_array.size();k++){
  51. QJsonObject obj_d = d_array.at(k).toObject();
  52. QString name = obj_d.value("m").toString();
  53. QList<QString> nameList = name.split("_");
  54. if(QString(nameList.at(4)).compare("SNCO2")==0){
  55. int value = obj_d.value("v").toInt();
  56. if((value == 0)||(value == 550)){
  57. count++;
  58. }
  59. nodeInfo.append(QString("\"co2\":%1,").arg(value));
  60. }else if(QString(nameList.at(4)).compare("SNPM2")==0){
  61. int value = obj_d.value("v").toInt();
  62. if((value == 0)||(value == 30)){
  63. count++;
  64. }
  65. nodeInfo.append(QString("\"pm2_5\":%1,").arg(value));
  66. }else if(QString(nameList.at(4)).compare("SNSB")==0){
  67. double value = obj_d.value("v").toDouble();
  68. int tmpValue = obj_d.value("v").toInt();
  69. if((tmpValue == 0)||(tmpValue == 58)){
  70. count++;
  71. }
  72. nodeInfo.append(QString("\"sd\":%1,").arg(value));
  73. }else if(QString(nameList.at(4)).compare("SNWD")==0){
  74. double value = obj_d.value("v").toDouble();
  75. int tmpValue = obj_d.value("v").toInt();
  76. if((tmpValue == 0)||(tmpValue == 18)){
  77. count++;
  78. }
  79. nodeInfo.append(QString("\"wd\":%1,").arg(value));
  80. }
  81. }
  82. nodeInfo = nodeInfo.left(nodeInfo.length()-1);
  83. if(count < 4){
  84. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
  85. .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
  86. }
  87. break;
  88. }
  89. }
  90. }
  91. }else{
  92. deviceCode = d_array.at(0).toObject().value("m").toString().mid(0,6);
  93. for(int j=0;j<1024;j++){
  94. if(agBoxShm->device[j].Enabled == 0x01){
  95. if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
  96. deviceId = QString(agBoxShm->device[j].device_id);
  97. productCode = QString(agBoxShm->device[j].product_code);
  98. deviceType = QString::number(agBoxShm->device[j].device_type);
  99. int count = 0;
  100. for(int k=0;k<d_array.size();k++){
  101. QJsonObject obj_d = d_array.at(k).toObject();
  102. QString name = obj_d.value("m").toString();
  103. QList<QString> nameList = name.split("_");
  104. int value = obj_d.value("v").toInt();
  105. if(QString(nameList.at(2)).compare("CO2")==0){
  106. if((value == 0)||(value == 550)){
  107. count++;
  108. }
  109. nodeInfo.append(QString("\"co2\":%1,").arg(value));
  110. }else if(QString(nameList.at(2)).compare("FLDW")==0){
  111. nodeInfo.append(QString("\"fldw\":%1,").arg(value));
  112. }else if(QString(nameList.at(2)).compare("PM2")==0){
  113. if((value == 0)||(value == 30)){
  114. count++;
  115. }
  116. nodeInfo.append(QString("\"pm2_5\":%1,").arg(value));
  117. }else if(QString(nameList.at(2)).compare("SNSD")==0){
  118. if((value == 0)||(value == 58)){
  119. count++;
  120. }
  121. nodeInfo.append(QString("\"sd\":%1,").arg(value));
  122. }else if(QString(nameList.at(2)).compare("SNWD")==0){
  123. if((value == 0)||(value == 18)){
  124. count++;
  125. }
  126. nodeInfo.append(QString("\"wd\":%1,").arg(value));
  127. }else if(QString(nameList.at(2)).compare("VOC")==0){
  128. if((value == 0)||(value == 1)){
  129. count++;
  130. }
  131. nodeInfo.append(QString("\"voc\":%1,").arg(value));
  132. }else if(QString(nameList.at(2)).compare("YXMS")==0){
  133. nodeInfo.append(QString("\"yxms\":%1,").arg(value));
  134. }else if(QString(nameList.at(2)).compare("YXZT")==0){
  135. nodeInfo.append(QString("\"yxzt\":%1,").arg(value));
  136. }
  137. }
  138. nodeInfo = nodeInfo.left(nodeInfo.length()-1);
  139. if(count < 5){
  140. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
  141. .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
  142. }
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. mqttDataList.removeFirst();
  154. usleep(1000);
  155. }
  156. }
  157. usleep(50000);
  158. }
  159. }
  160. void YTMqttPub::stop()
  161. {
  162. keep = false;
  163. }
  164. void YTMqttPub::devMessage(MqttData dev)
  165. {
  166. mqttDataList.append(dev);
  167. }