httpthread.cpp 16 KB

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