tcpserver1.cpp 3.8 KB

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