123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #include "devinfotcpserver.h"
- #include "../AGBoxDog/boxshm.h"
- DevInfoTcpServer::DevInfoTcpServer(QObject *parent) : QObject(parent)
- {
- svr = new QTcpServer(this);
- connect(svr,SIGNAL(newConnection()),this,SLOT(new_incomming()));
- networkManager = new QNetworkAccessManager(this);
- connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
- }
- void DevInfoTcpServer::start()
- {
- if(!svr->listen(QHostAddress::Any,15228)){
- printf("devinfo tcp listen 15228 failed\n");
- exit(-1);
- }
- printf("devinfo tcp listen 15228 success\n");
- }
- void DevInfoTcpServer::finishedSlot(QNetworkReply *reply)
- {
- if(reply->error() == QNetworkReply::NoError){
- QString data = QString::fromUtf8(reply->readAll());
- QByteArray ba = data.toLocal8Bit();
- ba = QString::fromLocal8Bit(ba).toUtf8();
- emit tcp_log(QString("[%1] video devinfo tcp %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(data));
- QJsonParseError json_error;
- QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&json_error);
- printf("tcpServer parseErr.error %d\n",json_error.error);
- if(json_error.error==QJsonParseError::NoError){
- QJsonObject root = jsonDoc.object();
- QJsonValue data_val = root.value("data");
- QJsonObject data_obj = data_val.toObject();
- QJsonValue list_val = data_obj.value("list");
- QString msg = "";
- QStringList devTcpList;
- if(list_val.isArray()){
- QJsonArray list_array = list_val.toArray();
- for(int i=0;i<list_array.size();i++){
- QJsonObject list_object = list_array.at(i).toObject();
- QString device_code = list_object.value("indexCode").toString();
- QString device_name = list_object.value("name").toString();
- bool found = false;
- for(int i=0;i<1024;i++){
- if((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 501)){
- if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){
- found = true;
- msg.append(QString("{\"product_id\": \"1\",\"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));
- break;
- }
- }
- }
- if(!found){
- QString device_id = QCryptographicHash::hash(QString("1%1").arg(device_code).toUtf8(),QCryptographicHash::Md5).toHex();
- QString sql = QString("insert into yt_t_device values('%1','%2',501,'501_vd',null);").arg(device_code).arg(device_id);
- emit append_sql(sql);
- msg.append(QString("{\"product_id\": \"1\",\"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));
- }
- devTcpList.append(device_code);
- }
- }
- for(int i=0;i<1024;i++){
- if((agBoxShm->device[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 501)){
- bool found1 = false;
- QString device_code = QString(agBoxShm->device[i].device_code);
- if(devTcpList.size() > 0){
- for(int j=0;j<devTcpList.size();j++){
- if(device_code.compare(devTcpList.at(j))==0){
- found1 = true;
- break;
- }
- }
- }
- if(!found1){
- QString sql = QString("delete from yt_t_device where device_code = '%1';").arg(device_code);
- emit append_sql(sql);
- msg.append(QString("{\"product_id\": \"1\",\"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));
- }
- }
- }
- msg = msg.left(msg.length()-1);
- 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);
- so->write(msgrec.toUtf8());
- so->close();
- }
- }
- reply->abort();
- reply->deleteLater();
- }
- void DevInfoTcpServer::new_incomming()
- {
- so = svr->nextPendingConnection();
- connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
- }
- void DevInfoTcpServer::app_info(QString url,QString key,QString signature)
- {
- url = url;
- key = key;
- signature = signature;
- }
- void DevInfoTcpServer::read_data()
- {
- QString str = QString::fromUtf8(so->readAll());
- int begin = str.indexOf("{",0);
- int len = str.length();
- QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
- QString data = str.mid(begin,len-begin+1);
- emit tcp_log(QString("[%1] video deviceinfo tcp %2").arg(datetime).arg(data));
- QByteArray ba = data.toLocal8Bit();
- ba = QString::fromLocal8Bit(ba).toUtf8();
- QJsonParseError parse_error;
- QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
- printf("tcpserver parse_error %d\n",parse_error.error);
- if(parse_error.error == QJsonParseError::NoError){
- QJsonObject doc = jsonDoc.object();
- QJsonValue param_val = doc.value("params");
- QJsonObject param_obj = param_val.toObject();
- QJsonValue regionIndexCodes = param_obj.value("regionIndexCodes");
- QString regions = "";
- if(regionIndexCodes.isArray()){
- QJsonArray region_array = regionIndexCodes.toArray();
- for(int i=0;i<region_array.size();i++){
- regions.append(QString("\"%1\"").arg(region_array.at(i).toString())).append(",");
- }
- }
- if(regions.length() > 1){
- regions = regions.left(regions.length()-1);
- QNetworkRequest *req = new QNetworkRequest();
- req->setUrl(QUrl(url));
- req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
- req->setRawHeader("Accept","*/*");
- req->setRawHeader("Connection","keep-alive");
- req->setRawHeader("Cache-Control"," no-cache");
- req->setRawHeader("X-Ca-Key",key);
- req->setRawHeader("X-Ca-Signature",signature);
- req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
- config = req->sslConfiguration();
- config.setPeerVerifyMode(QSslSocket::VerifyNone);
- config.setProtocol(QSsl::TlsV1_0OrLater);
- req->setSslConfiguration(config);
- QString postdata = QString("{\"regionIndexCodes\": [%1],\"pageNo\": 1,\"pageSize\": 500}").arg(regions);
- emit tcp_log(QString("[%1] video deviceinfo tcp postdata %2").arg(datetime).arg(postdata));
- req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
- networkManager->post(*req,postdata.toUtf8());
- }
- }
- }
|