httpthread.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "httpthread.h"
  2. #include "boxdeviceshm.h"
  3. #include "../AGBoxDog/boxshm.h"
  4. HttpThread::HttpThread(QObject *parent) : QObject(parent)
  5. {
  6. minute = 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. timer = new QTimer(this);
  13. connect(timer,&QTimer::timeout,this,&HttpThread::time_out);
  14. timer->start(5000);
  15. }
  16. void HttpThread::stop()
  17. {
  18. keep = false;
  19. }
  20. void HttpThread::finishedSlot(QNetworkReply *reply)
  21. {
  22. if(reply->error()==QNetworkReply::NoError){
  23. QString data = QString::fromUtf8(reply->readAll());
  24. QByteArray ba = data.toLocal8Bit();
  25. ba = QString::fromLocal8Bit(ba).toUtf8();
  26. QJsonParseError parseError;
  27. QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError);
  28. printf("parseError.error %d\n",parseError.error);
  29. if(parseError.error == QJsonParseError::NoError){
  30. QJsonObject obj_doc = doc.object();
  31. QJsonValue data_value = obj_doc.value("data");
  32. QJsonObject data_object = data_value.toObject();
  33. QJsonValue list_value = data_object.value("list");
  34. if(list_value.isArray()){
  35. QJsonArray list_array = list_value.toArray();
  36. for(int i=0;i<list_array.size();i++){
  37. QJsonObject list_object = list_array.at(i).toObject();
  38. QString device_code = list_object.value("indexCode").toString();
  39. int device_status = list_object.value("online").toInt();
  40. for(int i=0;i<1024;i++){
  41. if(boxDeviceShm->device[i].Enabled == 0x01){
  42. if(device_code.compare(QString(boxDeviceShm->device[i].device_code))==0){
  43. 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-vd\"}")
  44. .arg(QString(boxDeviceShm->device[i].device_id)).arg(device_code).arg(QString(boxDeviceShm->device[i].company_code)).arg(QDateTime::currentDateTime().toTime_t()).arg(device_status).arg(boxDeviceShm->device[i].device_type));
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }else{
  52. emit dataLog(QString("QJson Parse Error %1").arg(parseError.error));
  53. }
  54. emit dataLog(QString("[%1] QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  55. }else{
  56. emit dataLog(QString("[%1] QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error()));
  57. }
  58. isWaiting = false;
  59. reply->abort();
  60. reply->deleteLater();
  61. }
  62. void HttpThread::reply_timeout()
  63. {
  64. printf("[%s] reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  65. isWaiting = false;
  66. }
  67. void HttpThread::time_out()
  68. {
  69. agBoxShm->processStatus[1].t_time=QDateTime::currentDateTime().toTime_t();
  70. // if(((QDateTime::currentDateTime().time().minute())%5==0) && (QDateTime::currentDateTime().time().minute()!=minute)){//每5分钟执行一次
  71. if(QDateTime::currentDateTime().time().minute()!=minute){//每半小时执行一次
  72. minute = QDateTime::currentDateTime().time().minute();
  73. if(!isWaiting && ((minute==00)||(minute==30))){
  74. isWaiting = true;
  75. QNetworkRequest *req = new QNetworkRequest();
  76. req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/nms/v1/online/camera/get"));
  77. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  78. req->setRawHeader("Accept","*/*");
  79. req->setRawHeader("Connection","keep-alive");
  80. req->setRawHeader("Cache-Control"," no-cache");
  81. req->setRawHeader("X-Ca-Key","27997984");
  82. req->setRawHeader("X-Ca-Signature","a9kzGHD938rFhCfWVlGB6eO0bZ0SQbhzSwXlT+FMh+Q=");
  83. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  84. config = req->sslConfiguration();
  85. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  86. config.setProtocol(QSsl::TlsV1_0OrLater);
  87. req->setSslConfiguration(config);
  88. QString postdata = QString("{\"pageNo\": 1,\"pageSize\": 500}");
  89. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  90. QNetworkReply *reply = networkManager->post(*req,postdata.toUtf8());
  91. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  92. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  93. }
  94. }
  95. }