#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,15229)){ printf("devinfo tcp listen 15229 failed\n"); exit(-1); } printf("devinfo tcp listen 15229 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] guard 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;idevice[i].Enabled == 0x01) && (agBoxShm->device[i].device_type == 502)){ if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){ found = true; msg.append(QString("{\"product_id\": \"2\",\"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("2%1").arg(device_code).toUtf8(),QCryptographicHash::Md5).toHex(); QString sql = QString("insert into yt_t_device values('%1','%2',502,'502_eg',null);").arg(device_code).arg(device_id); emit append_sql(sql); msg.append(QString("{\"product_id\": \"2\",\"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 == 502)){ bool found1 = false; QString device_code = QString(agBoxShm->device[i].device_code); if(devTcpList.size() > 0){ for(int j=0;jdevice[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] guard 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 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] guard deviceinfo tcp postdata %2").arg(datetime).arg(postdata)); req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8()); networkManager->post(*req,postdata.toUtf8()); } } }