tcpserver.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "tcpserver.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. TcpServer::TcpServer(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 TcpServer::start()
  11. {
  12. if(!svr->listen(QHostAddress::Any,15227)){
  13. printf("tcpserver listen 15227 failed\n");
  14. exit(-1);
  15. }
  16. printf("tcpserver listen 15227 success\n");
  17. }
  18. void TcpServer::new_incomming()
  19. {
  20. so = svr->nextPendingConnection();
  21. connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
  22. }
  23. void TcpServer::finishedSlot(QNetworkReply *reply)
  24. {
  25. if(reply->error() == QNetworkReply::NoError){
  26. QString str = QString::fromUtf8(reply->readAll());
  27. QJsonParseError parse_error;
  28. QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(),&parse_error);
  29. if(parse_error.error==QJsonParseError::NoError){
  30. QJsonObject obj = doc.object();
  31. QString code = obj.value("code").toString();
  32. QString msg = obj.value("msg").toString();
  33. QString msgrec = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n{\"code\":\"%1\",\"msg\":\"%2\",\"data\":\"\"}").arg(code).arg(msg);
  34. so->write(msgrec.toUtf8());
  35. so->close();
  36. }
  37. }
  38. reply->abort();
  39. reply->deleteLater();
  40. }
  41. void TcpServer::app_info(QString url, QString key, QString signature)
  42. {
  43. url = url;
  44. key = key;
  45. signature = signature;
  46. }
  47. void TcpServer::read_data()
  48. {
  49. QString str = QString::fromUtf8(so->readAll());
  50. int begin = str.indexOf("{",0);
  51. int len = str.length();
  52. QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  53. QString data = str.mid(begin,len-begin+1);
  54. emit tcp_log(QString("[%1] video tcp data %2").arg(datetime).arg(data));
  55. QByteArray ba = data.toLocal8Bit();
  56. ba = QString::fromLocal8Bit(ba).toUtf8();
  57. QJsonParseError parse_error;
  58. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
  59. printf("tcpserver parse_error %d\n",parse_error.error);
  60. if(parse_error.error == QJsonParseError::NoError){
  61. QJsonObject doc = jsonDoc.object();
  62. QJsonValue param_val = doc.value("params");
  63. QJsonObject param_obj = param_val.toObject();
  64. QString deviceId = param_obj.value("device_id").toString();
  65. int controlType = param_obj.value("controlType").toInt();
  66. for(int i=0;i<1024;i++){
  67. if(agBoxShm->device[i].Enabled == 0x01){
  68. if(deviceId.compare(QString(agBoxShm->device[i].device_id))==0){
  69. QNetworkRequest *req = new QNetworkRequest();
  70. req->setUrl(QUrl(url));
  71. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  72. req->setRawHeader("Accept","*/*");
  73. req->setRawHeader("Connection","keep-alive");
  74. req->setRawHeader("Cache-Control"," no-cache");
  75. req->setRawHeader("X-Ca-Key",key);
  76. req->setRawHeader("X-Ca-Signature",signature);
  77. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  78. config = req->sslConfiguration();
  79. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  80. config.setProtocol(QSsl::TlsV1_0OrLater);
  81. req->setSslConfiguration(config);
  82. QString postdata = QString("{\"doorIndexCodes\": [\"%1\"],\"controlType\": %2}")
  83. .arg(agBoxShm->device[i].device_code).arg(controlType);
  84. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  85. networkManager->post(*req,postdata.toUtf8());
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }