#include "httpthread.h" #include "../AGBoxDog/boxshm.h" HttpThread::HttpThread(QObject *parent) : QObject(parent) { minute = 255; minute1 = 255; minute2 = 255; list<<245<<246<<247<<248<<249<<250; keep = false; isWaiting = false; networkManager = new QNetworkAccessManager(this); //机房系统设备心跳数据 connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *))); isWaiting1 = false; networkManager1 = new QNetworkAccessManager(this); //机房系统设备状态 connect(networkManager1,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot1(QNetworkReply *))); isWaiting2 = false; networkManager2 = new QNetworkAccessManager(this); //机房系统设备告警数据 connect(networkManager2,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot2(QNetworkReply *))); timer = new QTimer(this); connect(timer,&QTimer::timeout,this,&HttpThread::time_out); timer->start(5000); } void HttpThread::stop() { keep = false; } void HttpThread::finishedSlot(QNetworkReply *reply) { if(reply->error()==QNetworkReply::NoError){ QString data = QString::fromUtf8(reply->readAll()); QByteArray ba = data.toLocal8Bit(); ba = QString::fromLocal8Bit(ba).toUtf8(); QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError); printf("attribute data parseError.error %d\n",parseError.error); if(parseError.error == QJsonParseError::NoError){ QString nodeInfo = ""; QString deviceId = ""; QString companyCode = ""; QString deviceType = ""; QJsonObject obj_doc = doc.object(); QJsonValue data_value = obj_doc.value("data"); QJsonObject data_object = data_value.toObject(); QJsonValue list_value = data_object.value("data"); if(list_value.isArray()){ QJsonArray list_array = list_value.toArray(); QJsonObject list_object0 = list_array.at(0).toObject(); QString device_code = QString::number(list_object0.value("node_id").toInt()); printf("attribute data device_code %s\n",device_code.toUtf8().data()); for(int i=0;i<1024;i++){ if(agBoxShm->device[i].Enabled == 0x01){ if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){ deviceId = QString(agBoxShm->device[i].device_id); companyCode = QString(agBoxShm->device[i].product_code); deviceType = QString::number(agBoxShm->device[i].device_type); break; } } } for(int i=0;iprocuctattrbute[j].attribute_name))==0){ nodeInfo.append(QString("\"%1\":%2,").arg(QString(agBoxShm->procuctattrbute[j].attribute_code)).arg(value)); break; } } } nodeInfo=nodeInfo.left(nodeInfo.length()-1); emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-gr\"}") .arg(deviceId).arg(device_code).arg(companyCode).arg(QDateTime::currentDateTime().toTime_t()).arg(nodeInfo).arg(deviceType)); } }else{ emit dataLog(QString("attribute data QJson Parse Error %1").arg(parseError.error)); } emit dataLog(QString("[%1] attribute data QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data)); }else{ emit dataLog(QString("[%1] attribute data QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error())); } isWaiting = false; reply->abort(); reply->deleteLater(); } void HttpThread::reply_timeout() { printf("[%s] attribute data reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); isWaiting = false; } void HttpThread::finishedSlot1(QNetworkReply *reply) { if(reply->error()==QNetworkReply::NoError){ QString data = QString::fromUtf8(reply->readAll()); QByteArray ba = data.toLocal8Bit(); ba = QString::fromLocal8Bit(ba).toUtf8(); QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError); printf("status parseError.error %d\n",parseError.error); if(parseError.error == QJsonParseError::NoError){ QJsonObject obj_doc = doc.object(); QJsonValue data_value = obj_doc.value("data"); QJsonObject data_object = data_value.toObject(); QJsonValue list_value = data_object.value("data"); if(list_value.isArray()){ QJsonArray list_array = list_value.toArray(); for(int i=0;idevice[i].Enabled == 0x01){ if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){ emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":%5},\"device_type\":\"%6-gr\"}") .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(QDateTime::currentDateTime().toTime_t()).arg(deviceStatus).arg(agBoxShm->device[i].device_type)); break; } } } } } }else{ emit dataLog(QString("status QJson Parse Error %1").arg(parseError.error)); } emit dataLog(QString("[%1] status QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data)); }else{ emit dataLog(QString("[%1] status QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error())); } isWaiting1 = false; reply->abort(); reply->deleteLater(); } void HttpThread::reply_timeout1() { printf("[%s] status reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); isWaiting1 = false; } void HttpThread::finishedSlot2(QNetworkReply *reply) { if(reply->error()==QNetworkReply::NoError){ QString data = QString::fromUtf8(reply->readAll()); QByteArray ba = data.toLocal8Bit(); ba = QString::fromLocal8Bit(ba).toUtf8(); QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(ba,&parseError); printf("alarm parseError.error %d\n",parseError.error); if(parseError.error == QJsonParseError::NoError){ QJsonObject obj_doc = doc.object(); QJsonValue data_value = obj_doc.value("data"); QJsonObject data_object = data_value.toObject(); QJsonValue list_value = data_object.value("data"); if(list_value.isArray()){ QJsonArray list_array = list_value.toArray(); for(int i=0;idevice[i].Enabled == 0x01){ if(device_code.compare(QString(agBoxShm->device[i].device_code))==0){ emit mqttAlarm(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"device_id\":\"%5\",\"device_code\":\"%6\",\"device_name\":\"%7\",\"alarm_type\":\"%8\",\"alarm_desc\":\"%9\",\"alarm_time\":\"%10\"},\"metrics\":{\"alarm_value\":%11,\"handling_status\":%12},\"device_type\":\"%13-gr\"}") .arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(QString(agBoxShm->device[i].product_code)).arg(QDateTime::currentDateTime().toTime_t()).arg(QString(agBoxShm->device[i].device_id)).arg(device_code).arg(device_name).arg(alarmLevel).arg(alarmDesc).arg(alarmTime).arg(alarmValue).arg(handlingStatus).arg(agBoxShm->device[i].device_type)); break; } } } } } }else{ emit dataLog(QString("alarm QJson Parse Error %1").arg(parseError.error)); } emit dataLog(QString("[%1] alarm QNetworkReply Status %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(data)); }else{ emit dataLog(QString("[%1] alarm QNetworkReply Status Err %2").arg(QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t()).toString("yyyy-MM-dd HH:mm:ss")).arg(reply->error())); } isWaiting2 = false; reply->abort(); reply->deleteLater(); } void HttpThread::reply_timeout2() { printf("[%s] alarm reply_timeout\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); isWaiting2 = false; } void HttpThread::time_out() { agBoxShm->processStatus[4].t_time=QDateTime::currentDateTime().toTime_t(); if(QDateTime::currentDateTime().time().minute()!=minute){//每半个小时执行一次,同步设备属性数据 minute = QDateTime::currentDateTime().time().minute(); if(!isWaiting && ((minute==00)||(minute==30))){ for(int i=0;isetUrl(QUrl("http://172.17.200.209:8080/service/page/sensor.json?paged=1&pageSize=30")); req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3"); QString postdata = QString("{\"node_id\": %1}").arg(list.at(i)); req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8()); QNetworkReply *reply = networkManager->post(*req,postdata.toUtf8()); QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000); connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout())); } } } if(QDateTime::currentDateTime().time().minute()!=minute1){//每半个小时执行一次,同步设备状态 minute1 = QDateTime::currentDateTime().time().minute(); if(!isWaiting1 && ((minute1==00)||(minute1==30))){ isWaiting1 = true; QNetworkRequest *req = new QNetworkRequest(); req->setUrl(QUrl("http://172.17.200.209:8080/service/page/node.json?paged=1")); req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3"); QString postdata = QString("{\"scene_id\": 31}"); req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8()); QNetworkReply *reply = networkManager1->post(*req,postdata.toUtf8()); QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000); connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout1())); } } if(QDateTime::currentDateTime().time().minute()!=minute2){//每分钟获取过去第八分钟这分钟的机房报警数据,封装成mqtt推送到写程序,然后由写程序处理 minute2 = QDateTime::currentDateTime().time().minute(); QString tempTime1 = QDateTime::currentDateTime().addSecs(-480).toString("yyyy-MM-ddTHH:mm"); // QString tempTime2 = QDateTime::currentDateTime().addSecs(-60).toString("yyyy-MM-dd HH:mm"); startTime = QString("%1:00").arg(tempTime1); endTime = QString("%1:59").arg(tempTime1); printf("startTime: %s, endTime %s\n",startTime.toUtf8().data(),endTime.toUtf8().data()); if(!isWaiting2){ isWaiting2 = true; QNetworkRequest *req = new QNetworkRequest(); req->setUrl(QUrl("http://172.17.200.209:8080/service/page/alarm.json?paged=1&pageSize=50")); req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); req->setRawHeader("USER-KEY","426aad8a150a4d85a8fa7221085edca3"); QString postdata = QString("{\"start_time\": \"%1\",\"end_time\": \"%2\"}").arg(startTime).arg(endTime); emit dataLog(QString(" events QNetworkReply event [%1]").arg(postdata)); req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8()); QNetworkReply *reply = networkManager2->post(*req,postdata.toUtf8()); QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000); connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout2())); } } }