httpthread.cpp 11 KB

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