httpthread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. hour = 25;
  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. 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. QJsonObject obj_doc = doc.object();
  35. QJsonValue data_value = obj_doc.value("data");
  36. QJsonObject data_object = data_value.toObject();
  37. QJsonValue list_value = data_object.value("list");
  38. if(list_value.isArray()){
  39. QJsonArray list_array = list_value.toArray();
  40. for(int i=0;i<list_array.size();i++){
  41. QJsonObject list_object = list_array.at(i).toObject();
  42. QString device_code = list_object.value("doorIndexCode").toString();
  43. QString doorName = list_object.value("doorName").toString();
  44. QString personName = list_object.value("personName").toString();
  45. QString cardNo = list_object.value("cardNo").toString();
  46. QString orgName = list_object.value("orgName").toString();
  47. int eventType = list_object.value("eventType").toInt();
  48. int inAndOutType = list_object.value("inAndOutType").toInt();
  49. QDateTime date = QDateTime::fromString(list_object.value("eventTime").toString(), "yyyy-MM-ddTHH:mm:ss+08:00");
  50. QString eventTime = date.toLocalTime().toString("yyyy-MM-dd HH:mm:ss");
  51. //printf("222 list_object.value(eventTime).toString() %s, eventTime %s \n",list_object.value("eventTime").toString().toUtf8().data(),eventTime.toUtf8().data());
  52. for(int i=0;i<1024;i++){
  53. if(boxDeviceShm->device[i].Enabled == 0x01){
  54. if(device_code.compare(QString(boxDeviceShm->device[i].device_code))==0){
  55. emit mqttEvent(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"device_id\":\"%5\",\"device_code\":\"%6\",\"device_name\":\"%7\",\"install_address\":\"\",\"person_name\":\"%8\",\"card_no\":\"%9\",\"org_name\":\"%10\",\"event_time\":\"%11\"},\"metrics\":{\"event_type\": %12,\"in_out_type\": %13},\"device_type\":\"%14-eg\"}")
  56. .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(doorName).arg(personName).arg(cardNo).arg(orgName).arg(eventTime).arg(eventType).arg(inAndOutType).arg(boxDeviceShm->device[i].device_type));
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }else{
  64. emit dataLog(QString("event QJson Parse Error %1").arg(parseError.error));
  65. }
  66. 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));
  67. }else{
  68. 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()));
  69. }
  70. isWaiting = false;
  71. reply->abort();
  72. reply->deleteLater();
  73. }
  74. void HttpThread::reply_timeout()
  75. {
  76. printf("[%s] event reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  77. isWaiting = false;
  78. }
  79. void HttpThread::finishedSlot1(QNetworkReply *reply)
  80. {
  81. if(reply->error()==QNetworkReply::NoError){
  82. QString data = QString::fromUtf8(reply->readAll());
  83. QByteArray ba = data.toLocal8Bit();
  84. ba = QString::fromLocal8Bit(ba).toUtf8();
  85. QJsonParseError parseError;
  86. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  87. printf("status parseError.error %d\n",parseError.error);
  88. if(parseError.error == QJsonParseError::NoError){
  89. QJsonObject obj_doc = doc.object();
  90. QJsonValue data_value = obj_doc.value("data");
  91. QJsonObject data_object = data_value.toObject();
  92. QJsonValue list_value = data_object.value("authDoorList");
  93. if(list_value.isArray()){
  94. QJsonArray list_array = list_value.toArray();
  95. for(int i=0;i<list_array.size();i++){
  96. QJsonObject list_object = list_array.at(i).toObject();
  97. QString device_code = list_object.value("doorIndexCode").toString();
  98. int doorState = list_object.value("doorState").toInt();
  99. int deviceStatus = 0;
  100. if(doorState==3){
  101. deviceStatus = 0;
  102. }else{
  103. deviceStatus = 1;
  104. }
  105. for(int i=0;i<1024;i++){
  106. if(boxDeviceShm->device[i].Enabled == 0x01){
  107. if(device_code.compare(QString(boxDeviceShm->device[i].device_code))==0){
  108. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":%5,\"door_status\":%6},\"device_type\":\"%7-eg\"}")
  109. .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(doorState).arg(boxDeviceShm->device[i].device_type));
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }else{
  117. emit dataLog(QString("status QJson Parse Error %1").arg(parseError.error));
  118. }
  119. 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));
  120. }else{
  121. 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()));
  122. }
  123. isWaiting1 = false;
  124. reply->abort();
  125. reply->deleteLater();
  126. }
  127. void HttpThread::reply_timeout1()
  128. {
  129. printf("[%s] status reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  130. isWaiting1 = false;
  131. }
  132. void HttpThread::time_out()
  133. {
  134. agBoxShm->processStatus[2].t_time=QDateTime::currentDateTime().toTime_t();
  135. if(QDateTime::currentDateTime().time().minute()!=minute){//每分钟获取过去第八分钟这分钟的人员通行数据,封装成mqtt推送到写程序,然后由写程序处理
  136. minute = QDateTime::currentDateTime().time().minute();
  137. QString tempTime1 = QDateTime::currentDateTime().addSecs(-480).toString("yyyy-MM-ddTHH:mm");
  138. // QString tempTime2 = QDateTime::currentDateTime().addSecs(-60).toString("yyyy-MM-ddTHH:mm");
  139. startTime = QString("%1:00+08:00").arg(tempTime1);
  140. endTime = QString("%1:59+08:00").arg(tempTime1);
  141. printf("startTime: %s, endTime %s\n",startTime.toUtf8().data(),endTime.toUtf8().data());
  142. if(!isWaiting){
  143. isWaiting = true;
  144. QNetworkRequest *req = new QNetworkRequest();
  145. req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/acs/v1/door/events"));
  146. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  147. req->setRawHeader("Accept","*/*");
  148. req->setRawHeader("Connection","keep-alive");
  149. req->setRawHeader("Cache-Control"," no-cache");
  150. req->setRawHeader("X-Ca-Key","27997984");
  151. req->setRawHeader("X-Ca-Signature","zocGT3eKtbbUyljDBPf4yoQPoFdN97+Mg4fCc9Ki4jE=");
  152. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  153. config = req->sslConfiguration();
  154. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  155. config.setProtocol(QSsl::TlsV1_0OrLater);
  156. req->setSslConfiguration(config);
  157. QString postdata = QString("{\"startTime\": \"%1\",\"endTime\": \"%2\",\"pageNo\": 1,\"pageSize\": 500}").arg(startTime).arg(endTime);
  158. emit dataLog(QString(" events QNetworkReply event [%1]").arg(postdata));
  159. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  160. QNetworkReply *reply = networkManager->post(*req,postdata.toUtf8());
  161. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  162. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  163. }
  164. }
  165. if(QDateTime::currentDateTime().time().minute()!=minute1){//每半个小时执行一次,同步设备状态
  166. minute1 = QDateTime::currentDateTime().time().minute();
  167. if(!isWaiting1 && ((minute1==00)||(minute1==30))){
  168. isWaiting1 = true;
  169. QNetworkRequest *req = new QNetworkRequest();
  170. req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/acs/v1/door/states"));
  171. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  172. req->setRawHeader("Accept","*/*");
  173. req->setRawHeader("Connection","keep-alive");
  174. req->setRawHeader("Cache-Control"," no-cache");
  175. req->setRawHeader("X-Ca-Key","27997984");
  176. req->setRawHeader("X-Ca-Signature","FZRWBzzvzp8mDxFSpLHGdtNaot1PfNvpMbCaoT+dDLw=");
  177. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  178. config = req->sslConfiguration();
  179. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  180. config.setProtocol(QSsl::TlsV1_0OrLater);
  181. req->setSslConfiguration(config);
  182. QString postdata = QString("{}");
  183. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  184. QNetworkReply *reply = networkManager1->post(*req,postdata.toUtf8());
  185. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  186. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout1()));
  187. }
  188. }
  189. }