devinfotcpserver.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "devinfotcpserver.h"
  2. #include "../AGBoxDog/boxshm.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,15229)){
  13. printf("devinfo tcp listen 15229 failed\n");
  14. exit(-1);
  15. }
  16. printf("devinfo tcp listen 15229 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] guard 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((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 502)){
  44. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  45. found = true;
  46. msg.append(QString("{\"product_id\": \"2\",\"product_code\": \"%1\",\"device_id\": \"%2\",\"device_code\": \"%3\",\"device_name\": \"%4\",\"flag\": 0},").arg(agBoxShm->device[i].product_code).arg(agBoxShm->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("2%1").arg(device_code).toUtf8(),QCryptographicHash::Md5).toHex();
  53. QString sql = QString("insert into yt_t_device values('%1','%2',502,'502_eg',null);").arg(device_code).arg(device_id);
  54. emit append_sql(sql);
  55. msg.append(QString("{\"product_id\": \"2\",\"product_code\": \"%1\",\"device_id\": \"%2\",\"device_code\": \"%3\",\"device_name\": \"%4\",\"flag\": 1},").arg(agBoxShm->device[i].product_code).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((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 502)){
  62. bool found1 = false;
  63. QString device_code = QString(agBoxShm->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\": \"2\",\"product_code\": \"%1\",\"device_id\": \"%2\",\"device_code\": \"%3\",\"device_name\": \"\",\"flag\": -1},").arg(agBoxShm->device[i].product_code).arg(agBoxShm->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::app_info(QString url,QString key,QString signature)
  94. {
  95. url = url;
  96. key = key;
  97. signature = signature;
  98. }
  99. void DevInfoTcpServer::read_data()
  100. {
  101. QString str = QString::fromUtf8(so->readAll());
  102. int begin = str.indexOf("{",0);
  103. int len = str.length();
  104. QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  105. QString data = str.mid(begin,len-begin+1);
  106. emit tcp_log(QString("[%1] guard deviceinfo tcp %2").arg(datetime).arg(data));
  107. QByteArray ba = data.toLocal8Bit();
  108. ba = QString::fromLocal8Bit(ba).toUtf8();
  109. QJsonParseError parse_error;
  110. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
  111. printf("tcpserver parse_error %d\n",parse_error.error);
  112. if(parse_error.error == QJsonParseError::NoError){
  113. QJsonObject doc = jsonDoc.object();
  114. QJsonValue param_val = doc.value("params");
  115. QJsonObject param_obj = param_val.toObject();
  116. QJsonValue regionIndexCodes = param_obj.value("regionIndexCodes");
  117. QString regions = "";
  118. if(regionIndexCodes.isArray()){
  119. QJsonArray region_array = regionIndexCodes.toArray();
  120. for(int i=0;i<region_array.size();i++){
  121. regions.append(QString("\"%1\"").arg(region_array.at(i).toString())).append(",");
  122. }
  123. }
  124. if(regions.length() > 1){
  125. regions = regions.left(regions.length()-1);
  126. QNetworkRequest *req = new QNetworkRequest();
  127. req->setUrl(QUrl(url));
  128. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  129. req->setRawHeader("Accept","*/*");
  130. req->setRawHeader("Connection","keep-alive");
  131. req->setRawHeader("Cache-Control"," no-cache");
  132. req->setRawHeader("X-Ca-Key",key);
  133. req->setRawHeader("X-Ca-Signature",signature);
  134. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  135. config = req->sslConfiguration();
  136. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  137. config.setProtocol(QSsl::TlsV1_0OrLater);
  138. req->setSslConfiguration(config);
  139. QString postdata = QString("{\"regionIndexCodes\": [%1],\"pageNo\": 1,\"pageSize\": 500}").arg(regions);
  140. emit tcp_log(QString("[%1] guard deviceinfo tcp postdata %2").arg(datetime).arg(postdata));
  141. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  142. networkManager->post(*req,postdata.toUtf8());
  143. }
  144. }
  145. }