devinfotcpserver.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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,15230)){
  13. printf("devinfo tcp listen 15230 failed\n");
  14. exit(-1);
  15. }
  16. printf("devinfo tcp listen 15230 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] elevator 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. QString productCode = "";
  42. int deviceType;
  43. bool found = false;
  44. for(int i=0;i<1024;i++){
  45. if((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 503)){
  46. if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
  47. productCode = QString("%1").arg(agBoxShm->device[i].product_code);
  48. deviceType = agBoxShm->device[i].device_type;
  49. found = true;
  50. msg.append(QString("{\"product_id\": \"3\",\"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));
  51. break;
  52. }
  53. }
  54. }
  55. if(!found){
  56. QString device_id = QCryptographicHash::hash(QString("3%1").arg(device_code).toUtf8(),QCryptographicHash::Md5).toHex();
  57. QString sql = QString("insert into yt_t_device values('%1','%2',%3,'%4',null);").arg(device_code).arg(device_id).arg(deviceType).arg(productCode);
  58. emit append_sql(sql);
  59. msg.append(QString("{\"product_id\": \"3\",\"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));
  60. }
  61. devTcpList.append(device_code);
  62. }
  63. }
  64. for(int i=0;i<1024;i++){
  65. if((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 503)){
  66. bool found1 = false;
  67. QString device_code = QString(agBoxShm->device[i].device_code);
  68. if(devTcpList.size() > 0){
  69. for(int j=0;j<devTcpList.size();j++){
  70. if(device_code.compare(devTcpList.at(j))==0){
  71. found1 = true;
  72. break;
  73. }
  74. }
  75. }
  76. if(!found1){
  77. QString sql = QString("delete from yt_t_device where device_code = '%1';").arg(device_code);
  78. emit append_sql(sql);
  79. msg.append(QString("{\"product_id\": \"3\",\"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));
  80. }
  81. }
  82. }
  83. msg = msg.left(msg.length()-1);
  84. 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);
  85. so->write(msgrec.toUtf8());
  86. so->close();
  87. }
  88. }
  89. reply->abort();
  90. reply->deleteLater();
  91. }
  92. void DevInfoTcpServer::new_incomming()
  93. {
  94. so = svr->nextPendingConnection();
  95. connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
  96. }
  97. void DevInfoTcpServer::app_info(QString url, QString key, QString signature)
  98. {
  99. url = url;
  100. key = key;
  101. signature = signature;
  102. }
  103. void DevInfoTcpServer::read_data()
  104. {
  105. QString str = QString::fromUtf8(so->readAll());
  106. int begin = str.indexOf("{",0);
  107. int len = str.length();
  108. QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  109. QString data = str.mid(begin,len-begin+1);
  110. emit tcp_log(QString("[%1] elevator deviceinfo tcp %2").arg(datetime).arg(data));
  111. QByteArray ba = data.toLocal8Bit();
  112. ba = QString::fromLocal8Bit(ba).toUtf8();
  113. QJsonParseError parse_error;
  114. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
  115. printf("tcpserver parse_error %d\n",parse_error.error);
  116. if(parse_error.error == QJsonParseError::NoError){
  117. QJsonObject doc = jsonDoc.object();
  118. QJsonValue param_val = doc.value("params");
  119. QJsonObject param_obj = param_val.toObject();
  120. QJsonValue regionIndexCodes = param_obj.value("regionIndexCodes");
  121. QString regions = "";
  122. if(regionIndexCodes.isArray()){
  123. QJsonArray region_array = regionIndexCodes.toArray();
  124. for(int i=0;i<region_array.size();i++){
  125. regions.append(QString("\"%1\"").arg(region_array.at(i).toString())).append(",");
  126. }
  127. }
  128. if(regions.length() > 1){
  129. regions = regions.left(regions.length()-1);
  130. QNetworkRequest *req = new QNetworkRequest();
  131. req->setUrl(QUrl(url));
  132. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  133. req->setRawHeader("Accept","*/*");
  134. req->setRawHeader("Connection","keep-alive");
  135. req->setRawHeader("Cache-Control"," no-cache");
  136. req->setRawHeader("X-Ca-Key",key);
  137. req->setRawHeader("X-Ca-Signature",signature);
  138. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  139. config = req->sslConfiguration();
  140. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  141. config.setProtocol(QSsl::TlsV1_0OrLater);
  142. req->setSslConfiguration(config);
  143. QString postdata = QString("{\"regionIndexCodes\": [%1],\"pageNo\": 1,\"pageSize\": 500}").arg(regions);
  144. emit tcp_log(QString("[%1] elevator deviceinfo tcp postdata %2").arg(datetime).arg(postdata));
  145. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  146. networkManager->post(*req,postdata.toUtf8());
  147. }
  148. }
  149. }