httpthread.cpp 4.9 KB

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