httpthread.cpp 11 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. keep = false;
  8. isWaiting = false;
  9. networkManager = new QNetworkAccessManager(this); //查询梯控历史事件信息
  10. connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
  11. isWaiting1 = false;
  12. networkManager1 = new QNetworkAccessManager(this); //获取梯控主机在线状态
  13. connect(networkManager1,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot1(QNetworkReply *)));
  14. timer = new QTimer(this);
  15. connect(timer,&QTimer::timeout,this,&HttpThread::time_out);
  16. timer->start(5000);
  17. }
  18. void HttpThread::stop()
  19. {
  20. keep = false;
  21. }
  22. void HttpThread::finishedSlot(QNetworkReply *reply)
  23. {
  24. if(reply->error()==QNetworkReply::NoError){
  25. QString data = QString::fromUtf8(reply->readAll());
  26. QByteArray ba = data.toLocal8Bit();
  27. ba = QString::fromLocal8Bit(ba).toUtf8();
  28. QJsonParseError parseError;
  29. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  30. printf("event parseError.error %d\n",parseError.error);
  31. if(parseError.error == QJsonParseError::NoError){
  32. QJsonObject obj_doc = doc.object();
  33. QJsonValue data_value = obj_doc.value("data");
  34. QJsonObject data_object = data_value.toObject();
  35. QJsonValue list_value = data_object.value("rows");
  36. if(list_value.isArray()){
  37. QJsonArray list_array = list_value.toArray();
  38. for(int i=0;i<list_array.size();i++){
  39. QJsonObject list_object = list_array.at(i).toObject();
  40. QString device_code = list_object.value("deviceCode").toString();
  41. QString deviceName = list_object.value("deviceName").toString();
  42. QString liftName = list_object.value("liftName").toString();
  43. QString eventId = list_object.value("eventId").toString();
  44. int eventType = list_object.value("eventType").toInt();
  45. QString eventName = list_object.value("eventName").toString();
  46. QDateTime date = QDateTime::fromString(list_object.value("eventTime").toString(), "yyyy-MM-ddTHH:mm:ss.zzz+08:00");
  47. QString eventTime = date.toLocalTime().toString("yyyy-MM-dd HH:mm:ss");
  48. QString personId = list_object.value("personId").toString();
  49. QString cardNo = list_object.value("cardNo").toString();
  50. QString personName = list_object.value("personName").toString();
  51. QString orgId = list_object.value("orgId").toString();
  52. QString orgName = list_object.value("orgName").toString();
  53. QDateTime date1 = QDateTime::fromString(list_object.value("receiveTime").toString(), "yyyy-MM-ddTHH:mm:ss.zzz+08:00");
  54. QString receiveTime = date.toLocalTime().toString("yyyy-MM-dd HH:mm:ss");
  55. for(int i=0;i<1024;i++){
  56. if(agBoxShm->device[i].Enabled == 0x01){
  57. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  58. emit mqttEvent(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"device_id\":\"%5\",\"device_code\":\"%6\",\"device_name\":\"%7\",\"lift_name\":\"%8\",\"event_id\":\"%9\",\"event_name\":\"%10\",\"event_time\":\"%11\",\"person_id\":\"%12\",\"card_no\":\"%13\",\"person_name\":\"%14\",\"org_id\":\"%15\",\"org_name\":\"%16\",\"receive_time\":\"%17\"},\"metrics\":{\"event_type\":%18},\"device_type\":\"%19-lc\"}")
  59. .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(deviceName).arg(liftName).arg(eventId).arg(eventName).arg(eventTime).arg(personId).arg(cardNo).arg(personName).arg(orgId).arg(orgName).arg(receiveTime).arg(eventType).arg(agBoxShm->device[i].device_type));
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }else{
  67. emit dataLog(QString("event QJson Parse Error %1").arg(parseError.error));
  68. }
  69. emit dataLog(QString("[%1] event QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  70. }else{
  71. emit dataLog(QString("[%1] event QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  72. }
  73. isWaiting = false;
  74. reply->abort();
  75. reply->deleteLater();
  76. }
  77. void HttpThread::reply_timeout()
  78. {
  79. printf("[%s] event reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  80. isWaiting = false;
  81. }
  82. void HttpThread::finishedSlot1(QNetworkReply *reply)
  83. {
  84. if(reply->error()==QNetworkReply::NoError){
  85. QString data = QString::fromUtf8(reply->readAll());
  86. QByteArray ba = data.toLocal8Bit();
  87. ba = QString::fromLocal8Bit(ba).toUtf8();
  88. QJsonParseError parseError;
  89. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  90. printf("status parseError.error %d\n",parseError.error);
  91. if(parseError.error == QJsonParseError::NoError){
  92. QJsonObject obj_doc = doc.object();
  93. QJsonValue data_value = obj_doc.value("data");
  94. QJsonObject data_object = data_value.toObject();
  95. QJsonValue list_value = data_object.value("list");
  96. if(list_value.isArray()){
  97. QJsonArray list_array = list_value.toArray();
  98. for(int i=0;i<list_array.size();i++){
  99. QJsonObject list_object = list_array.at(i).toObject();
  100. QString device_code = list_object.value("indexCode").toString();
  101. int deviceStatus = list_object.value("online").toInt();
  102. for(int i=0;i<1024;i++){
  103. if(agBoxShm->device[i].Enabled == 0x01){
  104. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  105. 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-lc\"}")
  106. .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));
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }else{
  114. emit dataLog(QString("status QJson Parse Error %1").arg(parseError.error));
  115. }
  116. 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));
  117. }else{
  118. 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()));
  119. }
  120. isWaiting1 = false;
  121. reply->abort();
  122. reply->deleteLater();
  123. }
  124. void HttpThread::reply_timeout1()
  125. {
  126. printf("[%s] status reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  127. isWaiting1 = false;
  128. }
  129. void HttpThread::app_info(QString url, QString key, QString signature, QString url1, QString key1, QString signature1)
  130. {
  131. url = url;
  132. key = key;
  133. signature = signature;
  134. url1 = url1;
  135. key1 = key1;
  136. signature1 = signature1;
  137. }
  138. void HttpThread::time_out()
  139. {
  140. agBoxShm->processStatus[3].t_time=QDateTime::currentDateTime().toTime_t();
  141. if(QDateTime::currentDateTime().time().minute()!=minute){//每分钟获取过去第八分钟这分钟的人员通行数据,封装成mqtt推送到写程序,然后由写程序处理
  142. minute = QDateTime::currentDateTime().time().minute();
  143. QString tempTime1 = QDateTime::currentDateTime().addSecs(-480).toString("yyyy-MM-ddTHH:mm");
  144. // QString tempTime2 = QDateTime::currentDateTime().addSecs(-60).toString("yyyy-MM-ddTHH:mm");
  145. startTime = QString("%1:00+08:00").arg(tempTime1);
  146. endTime = QString("%1:59+08:00").arg(tempTime1);
  147. printf("startTime: %s, endTime %s\n",startTime.toUtf8().data(),endTime.toUtf8().data());
  148. if(!isWaiting){
  149. isWaiting = true;
  150. QNetworkRequest *req = new QNetworkRequest();
  151. req->setUrl(QUrl(url));
  152. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  153. req->setRawHeader("Accept","*/*");
  154. req->setRawHeader("Connection","keep-alive");
  155. req->setRawHeader("Cache-Control"," no-cache");
  156. req->setRawHeader("X-Ca-Key",key);
  157. req->setRawHeader("X-Ca-Signature",signature);
  158. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  159. config = req->sslConfiguration();
  160. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  161. config.setProtocol(QSsl::TlsV1_0OrLater);
  162. req->setSslConfiguration(config);
  163. QString postdata = QString("{\"startTime\": \"%1\",\"endTime\": \"%2\",\"pageNo\": 1,\"pageSize\": 500}").arg(startTime).arg(endTime);
  164. emit dataLog(QString(" events QNetworkReply event [%1]").arg(postdata));
  165. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  166. QNetworkReply *reply = networkManager->post(*req,postdata.toUtf8());
  167. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  168. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  169. }
  170. }
  171. if(QDateTime::currentDateTime().time().minute()!=minute1){//每半个小时执行一次,同步设备状态
  172. minute1 = QDateTime::currentDateTime().time().minute();
  173. if(!isWaiting1 && ((minute1==00)||(minute1==30))){
  174. isWaiting1 = true;
  175. QNetworkRequest *req = new QNetworkRequest();
  176. req->setUrl(QUrl(url1));
  177. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  178. req->setRawHeader("Accept","*/*");
  179. req->setRawHeader("Connection","keep-alive");
  180. req->setRawHeader("Cache-Control"," no-cache");
  181. req->setRawHeader("X-Ca-Key",key1);
  182. req->setRawHeader("X-Ca-Signature",signature1);
  183. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  184. config = req->sslConfiguration();
  185. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  186. config.setProtocol(QSsl::TlsV1_0OrLater);
  187. req->setSslConfiguration(config);
  188. QString postdata = QString("{}");
  189. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  190. QNetworkReply *reply = networkManager1->post(*req,postdata.toUtf8());
  191. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  192. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout1()));
  193. }
  194. }
  195. }