httpthread.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "httpthread.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. HttpThread::HttpThread(QObject *parent) : QObject(parent)
  4. {
  5. minute = 255;
  6. minute1 = 255;
  7. hour = 25;
  8. roomList.clear();
  9. keep = false;
  10. isWaiting = false;
  11. networkManager = new QNetworkAccessManager(this); //3-12层房间设备列表心跳数据
  12. connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
  13. isWaiting1 = false;
  14. networkManager1 = new QNetworkAccessManager(this); //阿里天气接口
  15. connect(networkManager1,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot1(QNetworkReply *)));
  16. timer = new QTimer(this);
  17. connect(timer,&QTimer::timeout,this,&HttpThread::time_out);
  18. timer->start(1000);
  19. }
  20. void HttpThread::stop()
  21. {
  22. keep = false;
  23. }
  24. void HttpThread::finishedSlot(QNetworkReply *reply)
  25. {
  26. if(reply->error()==QNetworkReply::NoError){
  27. QString data = QString::fromUtf8(reply->readAll());
  28. QByteArray ba = data.toLocal8Bit();
  29. ba = QString::fromLocal8Bit(ba).toUtf8();
  30. QJsonParseError parseError;
  31. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  32. printf("event parseError.error %d\n",parseError.error);
  33. if(parseError.error == QJsonParseError::NoError){
  34. QJsonArray list_array = doc.array();
  35. QString device_code = "";
  36. QString nodeInfo = "";
  37. double wd=0.00,sd=0.00,pm2_5=0.00,co2=0.00;
  38. int voc= 0;
  39. int fan_status = 0;
  40. int ts = (QDateTime::fromString(QDateTime::currentDateTime().toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss")).toTime_t();
  41. for(int i=0;i<list_array.size();i++){
  42. QJsonObject list_object = list_array.at(i).toObject();
  43. device_code = list_object.value("roomNo").toString();
  44. QString attribute_name = list_object.value("name").toString();
  45. //printf("222 list_object.value(eventTime).toString() %s, eventTime %s \n",list_object.value("eventTime").toString().toUtf8().data(),eventTime.toUtf8().data());
  46. if(attribute_name.compare("新风系统-室内温度")==0){
  47. wd = list_object.value("value").toDouble();
  48. nodeInfo.append(QString("\"wd\":%1,").arg(wd));
  49. }
  50. if(attribute_name.compare("新风系统-室内湿度")==0){
  51. sd = list_object.value("value").toDouble();
  52. nodeInfo.append(QString("\"sd\":%1,").arg(sd));
  53. }
  54. if(attribute_name.compare("新风系统-室内PM2.5")==0){
  55. pm2_5 = list_object.value("value").toDouble();
  56. nodeInfo.append(QString("\"pm2_5\":%1,").arg(pm2_5));
  57. }
  58. if(attribute_name.compare("新风系统-室内CO2")==0){
  59. co2 = list_object.value("value").toDouble();
  60. nodeInfo.append(QString("\"co2\":%1,").arg(co2));
  61. }
  62. if(attribute_name.compare("新风系统-室内VOC等级")==0){
  63. voc = list_object.value("value").toInt();
  64. nodeInfo.append(QString("\"voc\":%1,").arg(voc));
  65. }
  66. if(attribute_name.contains("排气扇")){
  67. fan_status = list_object.value("value").toInt();
  68. nodeInfo.append(QString("\"fan_status\":%1,").arg(fan_status));
  69. }
  70. }
  71. nodeInfo = nodeInfo.left(nodeInfo.length()-1);
  72. for(int i=0;i<1024;i++){
  73. if(agBoxShm->device[i].Enabled == 0x01){
  74. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  75. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
  76. .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));
  77. break;
  78. }
  79. }
  80. }
  81. }else{
  82. emit dataLog(QString("http room QJson Parse Error %1").arg(parseError.error));
  83. }
  84. emit dataLog(QString("[%1] http room QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  85. }else{
  86. emit dataLog(QString("[%1] http room QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  87. }
  88. isWaiting = false;
  89. roomList.removeFirst();
  90. reply->abort();
  91. reply->deleteLater();
  92. }
  93. void HttpThread::reply_timeout()
  94. {
  95. printf("[%s] room reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  96. isWaiting = false;
  97. roomList.removeFirst();
  98. }
  99. void HttpThread::finishedSlot1(QNetworkReply *reply)
  100. {
  101. if(reply->error()==QNetworkReply::NoError){
  102. QString data = QString::fromUtf8(reply->readAll());
  103. QByteArray ba = data.toLocal8Bit();
  104. ba = QString::fromLocal8Bit(ba).toUtf8();
  105. QJsonParseError parseError;
  106. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  107. printf("status parseError.error %d\n",parseError.error);
  108. if(parseError.error == QJsonParseError::NoError){
  109. QJsonObject obj_doc = doc.object();
  110. QJsonValue data_value = obj_doc.value("showapi_res_body");
  111. QJsonObject data_object = data_value.toObject();
  112. QJsonValue list_value = data_object.value("now");
  113. QJsonObject list_object = list_value.toObject();
  114. double wd = list_object.value("temperature").toString().toDouble();
  115. QString sdStr = list_object.value("sd").toString();
  116. double sd = sdStr.left(sdStr.length()-1).toDouble();
  117. QJsonValue aqi_detail = list_object.value("aqiDetail");
  118. QJsonObject detail_object = aqi_detail.toObject();
  119. double pm2_5 = detail_object.value("pm2_5").toString().toDouble();
  120. QString qual = detail_object.value("quality").toString();
  121. int quality = 0;
  122. if(qual.compare("优质")==0){
  123. quality = 1;
  124. }else if(qual.compare("良好")==0){
  125. quality = 2;
  126. }else if(qual.compare("轻度污染")==0){
  127. quality = 3;
  128. }else if(qual.compare("中度污染")==0){
  129. quality = 4;
  130. }else if(qual.compare("重度污染")==0){
  131. quality = 5;
  132. }else if(qual.compare("严重污染")==0){
  133. quality = 6;
  134. }
  135. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"wd\":%5,\"sd\":%6,\"pm2_5\":%7,\"quality\":%8},\"device_type\":\"%9-xf\"}")
  136. .arg(QString("weather0001")).arg(QString("weather0001")).arg(QString("511_XFFJ")).arg(QDateTime::fromString(QDateTime::currentDateTime().toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss").toTime_t()).arg(wd).arg(sd).arg(pm2_5).arg(quality).arg(511));
  137. }else{
  138. emit dataLog(QString("ali weather QJson Parse Error %1").arg(parseError.error));
  139. }
  140. emit dataLog(QString("[%1] ali weather QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  141. }else{
  142. emit dataLog(QString("[%1] ali weather QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  143. }
  144. isWaiting1 = false;
  145. reply->abort();
  146. reply->deleteLater();
  147. }
  148. void HttpThread::reply_timeout1()
  149. {
  150. printf("[%s] ali weather reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  151. isWaiting1 = false;
  152. }
  153. void HttpThread::time_out()
  154. {
  155. agBoxShm->processStatus[11].t_time=QDateTime::currentDateTime().toTime_t();
  156. if(hour != QDateTime::currentDateTime().time().hour()){
  157. hour = QDateTime::currentDateTime().time().hour();
  158. for(int i=0;i<1024;i++){
  159. if(QString("http").compare(QString(agBoxShm->device[i].device_gateway))==0){
  160. roomList.append(QString(agBoxShm->device[i].device_code));
  161. }
  162. }
  163. //每小时获取一次阿里天气数据
  164. if(!isWaiting1){
  165. isWaiting1 = true;
  166. QNetworkRequest *req = new QNetworkRequest();
  167. QString url = QString("https://weather01.market.alicloudapi.com/area-to-weather?area=历城区");
  168. req->setUrl(QUrl(url));
  169. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  170. req->setRawHeader("Accept","*/*");
  171. req->setRawHeader("Connection","keep-alive");
  172. req->setRawHeader("Cache-Control"," no-cache");
  173. req->setRawHeader("Authorization","APPCODE 0f2b7fce6e104ba8835358b7b59b4fb6");
  174. QNetworkReply *reply = networkManager1->get(*req);
  175. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  176. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout1()));
  177. }
  178. }
  179. if(!isWaiting && roomList.length() > 0){ //每小时获取3-12层每个房间设备列表心跳数据一次
  180. isWaiting = true;
  181. QString roomNo = roomList.first();
  182. QNetworkRequest *req = new QNetworkRequest();
  183. QString url = QString("http://www.limark.net:81/api/hotel/control/device/list.do?hotelCode=ff80808179c6599a0179c65caf9a0001&roomNo=%1").arg(roomNo);
  184. req->setUrl(QUrl(url));
  185. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  186. req->setRawHeader("Accept","*/*");
  187. req->setRawHeader("Connection","keep-alive");
  188. req->setRawHeader("Cache-Control"," no-cache");
  189. req->setRawHeader("api-key","ff1930560ed8405382d49c6991861e75");
  190. QNetworkReply *reply = networkManager->get(*req);
  191. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  192. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  193. }
  194. }