tcpserver1.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "tcpserver1.h"
  2. #include "boxdeviceshm.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::read_data()
  35. {
  36. QString str = QString::fromUtf8(so->readAll());
  37. int begin = str.indexOf("{",0);
  38. int len = str.length();
  39. QString data = str.mid(begin,len-begin+1);
  40. QString dt = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  41. emit tcp_log(QString("[%1] tcpserver1 data %2").arg(dt).arg(data));
  42. QByteArray ba = data.toLocal8Bit();
  43. ba = QString::fromLocal8Bit(ba).toUtf8();
  44. QJsonParseError parse_error;
  45. QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
  46. printf("parse_error.error %d\n",parse_error.error);
  47. if(parse_error.error == QJsonParseError::NoError){
  48. QJsonObject doc_obj = jsonDoc.object();
  49. QJsonValue param_val = doc_obj.value("params");
  50. QJsonObject param_obj = param_val.toObject();
  51. QString deviceId = param_obj.value("device_id").toString();
  52. int action = param_obj.value("action").toInt();
  53. QString command = param_obj.value("command").toString();
  54. int speed = param_obj.value("speed").toInt();
  55. int presetIndex = param_obj.value("presetIndex").toInt();
  56. for(int i=0;i<1024;i++){
  57. if(boxDeviceShm->device[i].Enabled == 0x01){
  58. if(deviceId.compare(QString(boxDeviceShm->device[i].device_id))==0){
  59. QNetworkRequest *req = new QNetworkRequest();
  60. req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/video/v1/ptzs/controlling"));
  61. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  62. req->setRawHeader("Accept","*/*");
  63. req->setRawHeader("Connection","keep-alive");
  64. req->setRawHeader("Cache-Control"," no-cache");
  65. req->setRawHeader("X-Ca-Key","27997984");
  66. req->setRawHeader("X-Ca-Signature","C/1aEVkfDZhGXuCH64tGv1vfySvDMTSMkqKd5PXgOM8=");
  67. req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
  68. config = req->sslConfiguration();
  69. config.setPeerVerifyMode(QSslSocket::VerifyNone);
  70. config.setProtocol(QSsl::TlsV1_0OrLater);
  71. req->setSslConfiguration(config);
  72. QString postdata = QString("{\"cameraIndexCode\": \"%1\",\"action\": %2,\"command\":\"%3\",\"speed\":%4,\"presetIndex\":%5}")
  73. .arg(boxDeviceShm->device[i].device_code).arg(action).arg(command).arg(speed).arg(presetIndex);
  74. req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
  75. networkManager->post(*req,postdata.toUtf8());
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. }