httpthread.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "httpthread.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. HttpThread::HttpThread(QObject *parent) : QObject(parent)
  4. {
  5. minute = 255;
  6. minute1 = 255;
  7. minute2 = 255;
  8. list<<245<<246<<247<<248<<249<<250;
  9. keep = false;
  10. isWaiting = false;
  11. networkManager = new QNetworkAccessManager(this); //机房系统设备心跳数据
  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. isWaiting2 = false;
  17. networkManager2 = new QNetworkAccessManager(this); //机房系统设备告警数据
  18. connect(networkManager2,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot2(QNetworkReply *)));
  19. timer = new QTimer(this);
  20. connect(timer,&QTimer::timeout,this,&HttpThread::time_out);
  21. timer->start(5000);
  22. }
  23. void HttpThread::stop()
  24. {
  25. keep = false;
  26. }
  27. void HttpThread::finishedSlot(QNetworkReply *reply)
  28. {
  29. if(reply->error()==QNetworkReply::NoError){
  30. QString data = QString::fromUtf8(reply->readAll());
  31. QByteArray ba = data.toLocal8Bit();
  32. ba = QString::fromLocal8Bit(ba).toUtf8();
  33. QJsonParseError parseError;
  34. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  35. printf("attribute data parseError.error %d\n",parseError.error);
  36. if(parseError.error == QJsonParseError::NoError){
  37. QString nodeInfo = "";
  38. QString deviceId = "";
  39. QString companyCode = "";
  40. QString deviceType = "";
  41. QJsonObject obj_doc = doc.object();
  42. QJsonValue data_value = obj_doc.value("data");
  43. QJsonObject data_object = data_value.toObject();
  44. QJsonValue list_value = data_object.value("data");
  45. if(list_value.isArray()){
  46. QJsonArray list_array = list_value.toArray();
  47. QJsonObject list_object0 = list_array.at(0).toObject();
  48. QString device_code = QString::number(list_object0.value("node_id").toInt());
  49. printf("attribute data device_code %s\n",device_code.toUtf8().data());
  50. for(int i=0;i<1024;i++){
  51. if(agBoxShm->device[i].Enabled == 0x01){
  52. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  53. deviceId = QString(agBoxShm->device[i].device_id);
  54. companyCode = QString(agBoxShm->device[i].product_code);
  55. deviceType = QString::number(agBoxShm->device[i].device_type);
  56. break;
  57. }
  58. }
  59. }
  60. for(int i=0;i<list_array.size();i++){
  61. QJsonObject list_object = list_array.at(i).toObject();
  62. QString name = list_object.value("name").toString();
  63. double value = list_object.value("sdata").toDouble();
  64. for(int j=0;j<300;j++){
  65. if(name.compare(QString(agBoxShm->procuctattrbute[j].attribute_name))==0){
  66. nodeInfo.append(QString("\"%1\":%2,").arg(QString(agBoxShm->procuctattrbute[j].attribute_code)).arg(value));
  67. break;
  68. }
  69. }
  70. }
  71. nodeInfo=nodeInfo.left(nodeInfo.length()-1);
  72. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-gr\"}")
  73. .arg(deviceId).arg(device_code).arg(companyCode).arg(QDateTime::currentDateTime().toTime_t()).arg(nodeInfo).arg(deviceType));
  74. }
  75. }else{
  76. emit dataLog(QString("attribute data QJson Parse Error %1").arg(parseError.error));
  77. }
  78. emit dataLog(QString("[%1] attribute data QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  79. }else{
  80. emit dataLog(QString("[%1] attribute data QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  81. }
  82. isWaiting = false;
  83. reply->abort();
  84. reply->deleteLater();
  85. }
  86. void HttpThread::reply_timeout()
  87. {
  88. printf("[%s] attribute data reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  89. isWaiting = false;
  90. }
  91. void HttpThread::finishedSlot1(QNetworkReply *reply)
  92. {
  93. if(reply->error()==QNetworkReply::NoError){
  94. QString data = QString::fromUtf8(reply->readAll());
  95. QByteArray ba = data.toLocal8Bit();
  96. ba = QString::fromLocal8Bit(ba).toUtf8();
  97. QJsonParseError parseError;
  98. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  99. printf("status parseError.error %d\n",parseError.error);
  100. if(parseError.error == QJsonParseError::NoError){
  101. QJsonObject obj_doc = doc.object();
  102. QJsonValue data_value = obj_doc.value("data");
  103. QJsonObject data_object = data_value.toObject();
  104. QJsonValue list_value = data_object.value("data");
  105. if(list_value.isArray()){
  106. QJsonArray list_array = list_value.toArray();
  107. for(int i=0;i<list_array.size();i++){
  108. QJsonObject list_object = list_array.at(i).toObject();
  109. QString device_code = QString::number(list_object.value("id").toInt());
  110. QJsonValue status_value = list_object.value("data");
  111. QJsonObject status_object = status_value.toObject();
  112. QString status = status_object.value("iot_node_status").toString();
  113. int deviceStatus = 0;
  114. if(status.compare("在线")==0){
  115. deviceStatus = 1;
  116. }else{
  117. deviceStatus = 0;
  118. }
  119. for(int i=0;i<1024;i++){
  120. if(agBoxShm->device[i].Enabled == 0x01){
  121. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  122. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":%5},\"device_type\":\"%6-gr\"}")
  123. .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(QDateTime::currentDateTime().toTime_t()).arg(deviceStatus).arg(agBoxShm->device[i].device_type));
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }else{
  131. emit dataLog(QString("status QJson Parse Error %1").arg(parseError.error));
  132. }
  133. emit dataLog(QString("[%1] status QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  134. }else{
  135. emit dataLog(QString("[%1] status QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  136. }
  137. isWaiting1 = false;
  138. reply->abort();
  139. reply->deleteLater();
  140. }
  141. void HttpThread::reply_timeout1()
  142. {
  143. printf("[%s] status reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  144. isWaiting1 = false;
  145. }
  146. void HttpThread::finishedSlot2(QNetworkReply *reply)
  147. {
  148. if(reply->error()==QNetworkReply::NoError){
  149. QString data = QString::fromUtf8(reply->readAll());
  150. QByteArray ba = data.toLocal8Bit();
  151. ba = QString::fromLocal8Bit(ba).toUtf8();
  152. QJsonParseError parseError;
  153. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  154. printf("alarm parseError.error %d\n",parseError.error);
  155. if(parseError.error == QJsonParseError::NoError){
  156. QJsonObject obj_doc = doc.object();
  157. QJsonValue data_value = obj_doc.value("data");
  158. QJsonObject data_object = data_value.toObject();
  159. QJsonValue list_value = data_object.value("data");
  160. if(list_value.isArray()){
  161. QJsonArray list_array = list_value.toArray();
  162. for(int i=0;i<list_array.size();i++){
  163. QJsonObject list_object = list_array.at(i).toObject();
  164. QString device_name = list_object.value("node_name").toString();
  165. QString device_code = "";
  166. if(device_name.compare("温湿度监控")==0){
  167. device_code = QString("245");
  168. }else if(device_name.compare("数据采集主机")==0){
  169. device_code = QString("246");
  170. }else if(device_name.compare("市电监测")==0){
  171. device_code = QString("247");
  172. }else if(device_name.compare("UPS监测")==0){
  173. device_code = QString("248");
  174. }else if(device_name.compare("黑盾精密空调1")==0){
  175. device_code = QString("249");
  176. }else if(device_name.compare("黑盾精密空调2")==0){
  177. device_code = QString("250");
  178. }
  179. QJsonValue alarm_value = list_object.value("data");
  180. QJsonObject alarm_object = alarm_value.toObject();
  181. QString alarmLevel = alarm_object.value("iot_trigger_alarm_level").toString();
  182. QString handling_status = alarm_object.value("iot_alarm_process_status").toString();
  183. int handlingStatus = 0;
  184. emit dataLog(QString("alarmLevel %1, handling_status %2").arg(alarmLevel).arg(handling_status));
  185. if(handling_status.compare("未处理")==0){
  186. handlingStatus = 0;
  187. }else{
  188. handlingStatus = 1;
  189. }
  190. QString alarmDesc = list_object.value("description").toString();
  191. QString alarmTime = list_object.value("atimestr").toString();
  192. double alarmValue = list_object.value("sdata").toDouble();
  193. for(int i=0;i<1024;i++){
  194. if(agBoxShm->device[i].Enabled == 0x01){
  195. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  196. emit mqttAlarm(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"device_id\":\"%5\",\"device_code\":\"%6\",\"device_name\":\"%7\",\"alarm_type\":\"%8\",\"alarm_desc\":\"%9\",\"alarm_time\":\"%10\"},\"metrics\":{\"alarm_value\":%11,\"handling_status\":%12},\"device_type\":\"%13-gr\"}")
  197. .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(QDateTime::currentDateTime().toTime_t()).arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(device_name).arg(alarmLevel).arg(alarmDesc).arg(alarmTime).arg(alarmValue).arg(handlingStatus).arg(agBoxShm->device[i].device_type));
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }else{
  205. emit dataLog(QString("alarm QJson Parse Error %1").arg(parseError.error));
  206. }
  207. emit dataLog(QString("[%1] alarm QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  208. }else{
  209. emit dataLog(QString("[%1] alarm QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  210. }
  211. isWaiting2 = false;
  212. reply->abort();
  213. reply->deleteLater();
  214. }
  215. void HttpThread::reply_timeout2()
  216. {
  217. printf("[%s] alarm reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  218. isWaiting2 = false;
  219. }
  220. void HttpThread::time_out()
  221. {
  222. agBoxShm->processStatus[4].t_time=QDateTime::currentDateTime().toTime_t();
  223. if(QDateTime::currentDateTime().time().minute()!=minute){//每半个小时执行一次,同步设备属性数据
  224. minute = QDateTime::currentDateTime().time().minute();
  225. if(!isWaiting && ((minute==00)||(minute==30))){
  226. for(int i=0;i<list.size();i++){
  227. isWaiting = true;
  228. QNetworkRequest *req = new QNetworkRequest();
  229. req->setUrl(QUrl("http://172.17.200.209:8080/service/page/sensor.json?paged=1&pageSize=30"));
  230. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  231. req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3");
  232. QString postdata = QString("{\"node_id\": %1}").arg(list.at(i));
  233. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  234. QNetworkReply *reply = networkManager->post(*req,postdata.toUtf8());
  235. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  236. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  237. }
  238. }
  239. }
  240. if(QDateTime::currentDateTime().time().minute()!=minute1){//每半个小时执行一次,同步设备状态
  241. minute1 = QDateTime::currentDateTime().time().minute();
  242. if(!isWaiting1 && ((minute1==00)||(minute1==30))){
  243. isWaiting1 = true;
  244. QNetworkRequest *req = new QNetworkRequest();
  245. req->setUrl(QUrl("http://172.17.200.209:8080/service/page/node.json?paged=1"));
  246. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  247. req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3");
  248. QString postdata = QString("{\"scene_id\": 31}");
  249. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  250. QNetworkReply *reply = networkManager1->post(*req,postdata.toUtf8());
  251. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  252. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout1()));
  253. }
  254. }
  255. if(QDateTime::currentDateTime().time().minute()!=minute2){//每分钟获取过去第八分钟这分钟的机房报警数据,封装成mqtt推送到写程序,然后由写程序处理
  256. minute2 = QDateTime::currentDateTime().time().minute();
  257. QString tempTime1 = QDateTime::currentDateTime().addSecs(-480).toString("yyyy-MM-ddTHH:mm");
  258. // QString tempTime2 = QDateTime::currentDateTime().addSecs(-60).toString("yyyy-MM-dd HH:mm");
  259. startTime = QString("%1:00").arg(tempTime1);
  260. endTime = QString("%1:59").arg(tempTime1);
  261. printf("startTime: %s, endTime %s\n",startTime.toUtf8().data(),endTime.toUtf8().data());
  262. if(!isWaiting2){
  263. isWaiting2 = true;
  264. QNetworkRequest *req = new QNetworkRequest();
  265. req->setUrl(QUrl("http://172.17.200.209:8080/service/page/alarm.json?paged=1&pageSize=50"));
  266. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  267. req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3");
  268. QString postdata = QString("{\"start_time\": \"%1\",\"end_time\": \"%2\"}").arg(startTime).arg(endTime);
  269. emit dataLog(QString(" events QNetworkReply event [%1]").arg(postdata));
  270. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  271. QNetworkReply *reply = networkManager2->post(*req,postdata.toUtf8());
  272. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  273. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout2()));
  274. }
  275. }
  276. }