httpthread.cpp 12 KB

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