devinfotcpserver.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "devinfotcpserver.h"
  2. #include "boxdeviceshm.h"
  3. DevInfoTcpServer::DevInfoTcpServer(QObject *parent) : QObject(parent)
  4. {
  5. svr = new QTcpServer(this);
  6. connect(svr,SIGNAL(newConnection()),this,SLOT(new_incomming()));
  7. networkManager = new QNetworkAccessManager(this);
  8. connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
  9. }
  10. void DevInfoTcpServer::start()
  11. {
  12. if(!svr->listen(QHostAddress::Any,15228)){
  13. printf("devinfo tcp listen 15228 failed\n");
  14. exit(-1);
  15. }
  16. printf("devinfo tcp listen 15228 success\n");
  17. }
  18. void DevInfoTcpServer::finishedSlot(QNetworkReply *reply)
  19. {
  20. if(reply->error() == QNetworkReply::NoError){
  21. QString data = QString::fromUtf8(reply->readAll());
  22. QByteArray ba = data.toLocal8Bit();
  23. ba = QString::fromLocal8Bit(ba).toUtf8();
  24. emit tcp_log(QString("[%1] video devinfo tcp %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(data));
  25. QJsonParseError json_error;
  26. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&json_error);
  27. printf("tcpServer parseErr.error %d\n",json_error.error);
  28. if(json_error.error==QJsonParseError::NoError){
  29. QJsonObject root = jsonDoc.object();
  30. QJsonValue data_val = root.value("data");
  31. QJsonObject data_obj = data_val.toObject();
  32. QJsonValue list_val = data_obj.value("list");
  33. QString msg = "";
  34. QStringList devTcpList;
  35. if(list_val.isArray()){
  36. QJsonArray list_array = list_val.toArray();
  37. for(int i=0;i<list_array.size();i++){
  38. QJsonObject list_object = list_array.at(i).toObject();
  39. QString device_code = list_object.value("indexCode").toString();
  40. QString device_name = list_object.value("name").toString();
  41. bool found = false;
  42. for(int i=0;i<1024;i++){
  43. if((boxDeviceShm->device[i].Enabled == 0x01) && (boxDeviceShm->device[i].device_type == 501)){
  44. if(device_code.compare(QString(boxDeviceShm->device[i].device_code))==0){
  45. found = true;
  46. msg.append(QString("{\"product_id\": \"1\",\"company_code\": \"10000001\",\"device_id\": \"%1\",\"device_code\": \"%2\",\"device_name\": \"%3\",\"flag\": 0},").arg(boxDeviceShm->device[i].device_id).arg(device_code).arg(device_name));
  47. break;
  48. }
  49. }
  50. }
  51. if(!found){
  52. QString device_id = QCryptographicHash::hash(QString("1%1").arg(device_code).toUtf8(),QCryptographicHash::Md5).toHex();
  53. QString sql = QString("insert into yt_t_device values('%1','%2',501,'10000001',null);").arg(device_code).arg(device_id);
  54. emit append_sql(sql);
  55. msg.append(QString("{\"product_id\": \"1\",\"company_code\": \"10000001\",\"device_id\": \"%1\",\"device_code\": \"%2\",\"device_name\": \"%3\",\"flag\": 1},").arg(device_id).arg(device_code).arg(device_name));
  56. }
  57. devTcpList.append(device_code);
  58. }
  59. }
  60. for(int i=0;i<1024;i++){
  61. if((boxDeviceShm->device[i].Enabled == 0x01) && (boxDeviceShm->device[i].device_type == 501)){
  62. bool found1 = false;
  63. QString device_code = QString(boxDeviceShm->device[i].device_code);
  64. if(devTcpList.size() > 0){
  65. for(int j=0;j<devTcpList.size();j++){
  66. if(device_code.compare(devTcpList.at(j))==0){
  67. found1 = true;
  68. break;
  69. }
  70. }
  71. }
  72. if(!found1){
  73. QString sql = QString("delete from yt_t_device where device_code = '%1';").arg(device_code);
  74. emit append_sql(sql);
  75. msg.append(QString("{\"product_id\": \"1\",\"company_code\": \"10000001\",\"device_id\": \"%1\",\"device_code\": \"%2\",\"device_name\": \"\",\"flag\": -1},").arg(boxDeviceShm->device[i].device_id).arg(device_code));
  76. }
  77. }
  78. }
  79. msg = msg.left(msg.length()-1);
  80. QString msgrec = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n{\"code\": \"0\",\"msg\": \"success\",\"data\": [%1]}").arg(msg);
  81. so->write(msgrec.toUtf8());
  82. so->close();
  83. }
  84. }
  85. reply->abort();
  86. reply->deleteLater();
  87. }
  88. void DevInfoTcpServer::new_incomming()
  89. {
  90. so = svr->nextPendingConnection();
  91. connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
  92. }
  93. void DevInfoTcpServer::read_data()
  94. {
  95. QString str = QString::fromUtf8(so->readAll());
  96. int begin = str.indexOf("{",0);
  97. int len = str.length();
  98. QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  99. QString data = str.mid(begin,len-begin+1);
  100. emit tcp_log(QString("[%1] video deviceinfo tcp %2").arg(datetime).arg(data));
  101. QByteArray ba = data.toLocal8Bit();
  102. ba = QString::fromLocal8Bit(ba).toUtf8();
  103. QJsonParseError parse_error;
  104. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
  105. printf("tcpserver parse_error %d\n",parse_error.error);
  106. if(parse_error.error == QJsonParseError::NoError){
  107. QJsonObject doc = jsonDoc.object();
  108. QJsonValue param_val = doc.value("params");
  109. QJsonObject param_obj = param_val.toObject();
  110. QJsonValue regionIndexCodes = param_obj.value("regionIndexCodes");
  111. QString regions = "";
  112. if(regionIndexCodes.isArray()){
  113. QJsonArray region_array = regionIndexCodes.toArray();
  114. for(int i=0;i<region_array.size();i++){
  115. regions.append(QString("\"%1\"").arg(region_array.at(i).toString())).append(",");
  116. }
  117. }
  118. if(regions.length() > 1){
  119. regions = regions.left(regions.length()-1);
  120. QNetworkRequest *req = new QNetworkRequest();
  121. req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/resource/v2/camera/search"));
  122. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  123. req->setRawHeader("Accept","*/*");
  124. req->setRawHeader("Connection","keep-alive");
  125. req->setRawHeader("Cache-Control"," no-cache");
  126. req->setRawHeader("X-Ca-Key","27997984");
  127. req->setRawHeader("X-Ca-Signature","/f3F+ekIwc5hFwP2edSOdAU2zVoowOogCMSRPNxKmKE=");
  128. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  129. config = req->sslConfiguration();
  130. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  131. config.setProtocol(QSsl::TlsV1_0OrLater);
  132. req->setSslConfiguration(config);
  133. QString postdata = QString("{\"regionIndexCodes\": [%1],\"pageNo\": 1,\"pageSize\": 500}").arg(regions);
  134. emit tcp_log(QString("[%1] video deviceinfo tcp postdata %2").arg(datetime).arg(postdata));
  135. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  136. networkManager->post(*req,postdata.toUtf8());
  137. }
  138. }
  139. }