#include "mynetmanager.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; MyNetManager::MyNetManager(QObject *parent): QObject(parent){ m_pManager = new QNetworkAccessManager(this); keep = nullptr; keepNetState = NET_DISCONNECT; keepTimer = new QTimer(this); connect(m_pManager, &QNetworkAccessManager::finished,this, &MyNetManager::onKeepConnectFinished); } MyNetManager::~MyNetManager() { } void MyNetManager::SetIp(QString ip) { qDebug() << QString("SetIp : %1\n").arg(ip); this->ip = ip; } bool MyNetManager::SetTempraturePara(uint16_t reflectiveTemperature, float emissivity, uint16_t distance) { char buff[1024]; qDebug() << QString("SetTempraturePara : reflectiveTemperature:%1, emissivity:%2, distance:%3\n") .arg(reflectiveTemperature).arg(emissivity).arg(distance); sprintf(buff, "\ \ 2\ 400\ false\ %d\ %f\ %d\ 80\ centimeter\ 4\ \ true\ true\ false\ 800*600\ 160*120\ \ ", reflectiveTemperature,emissivity,distance); QByteArray array = QByteArray(buff); bool rtn = putWait(ip + "/ISAPI/Thermal/channels/2/thermometry/pixelToPixelParam", array); qDebug()<< "SetTempraturePara rtn =" << rtn << endl; return rtn; } bool MyNetManager::SetAlarmPara(uint16_t alert, uint16_t alarm, uint16_t alarm_time) { char buff[4096]; qDebug() << QString("SetAlarmPara : alarm:%1, emissivity:%2, alarm_time:%3\n") .arg(alert).arg(alarm).arg(alarm_time); sprintf(buff, R"( 2 true true false 0~550 degreeCentigrade 0.95 centimeter centerPoint 20 false %d %d true 1.000 20.0 1 false 1 false 0 0 true false false rules_around false false V1.0.6.0 build201231 %d alarm alarm_alert 0 255 0 )", alert,alarm,alarm_time); QByteArray array = QByteArray(buff); bool rtn = putWait(ip + "/ISAPI/Thermal/channels/2/thermometry/basicParam", array); qDebug()<< "SetAlarmPara rtn =" << rtn << endl; return rtn; } bool MyNetManager::GetPicture() { qDebug() << QString("GetPicture")<< endl; QNetworkRequest request; QUrl qurl = QUrl(ip + "/ISAPI/Streaming/channels/2/picture"); qurl.setUserName("admin"); qurl.setPassword("YN123456789"); request.setRawHeader("Connection","Keep-Alive"); request.setUrl(qurl); QNetworkReply *reply = m_pManager->get(request); connect(reply,&QNetworkReply::finished,this,&MyNetManager::onCapPicRelyfinished); return true; } //23.8.14 bool MyNetManager::GetRealTimeTemperature() { qDebug() << QString("GetRealTimeTemperature")<< endl; QNetworkRequest request; QUrl qurl = QUrl(ip + "/ISAPI/Thermal/channels/2/thermometry/1/rulesTemperatureInfo?format=json"); qurl.setUserName("admin"); qurl.setPassword("YN123456789"); request.setRawHeader("Connection","Keep-Alive"); request.setUrl(qurl); QNetworkReply *reply = m_pManager->get(request); connect(reply,&QNetworkReply::finished,this,&MyNetManager::onGetTemperatureRelyfinished); return true; } bool MyNetManager::KeepConnect() { qDebug() << QString("KeepConnect"); QNetworkRequest request; QUrl qurl = QUrl(ip + "/ISAPI/Event/notification/alertStream"); qurl.setUserName("admin"); qurl.setPassword("YN123456789"); request.setRawHeader("Connection","Keep-Alive"); request.setUrl(qurl); QNetworkReply* reply = m_pManager->get(request); connect(reply,SIGNAL(readyRead()),this,SLOT(onKeepRelyfinished())); connect(reply, static_cast(&QNetworkReply::error), [=](QNetworkReply::NetworkError e){ qDebug() << QString("onKeepConnectErr: %1\n").arg(e); emit sigKeepBrokeAlarm(); KeepDisconnect(); }); keep = reply; qDebug() << "keep:" << keep; //启动倒计数15秒 disconnect(keepTimer,0,0,0); connect(keepTimer, &QTimer::timeout, this, &MyNetManager::onKeepConnectTimeOut); keepTimer->start(connect_over_time_seconds); keepNetState = NET_CONNECTING; qDebug() << "KeepConnect start...\n"; return true; } bool MyNetManager::KeepDisconnect() { qDebug() << QString("KeepDisconnect\n"); //m_pManager->clearConnectionCache(); m_pManager->clearAccessCache(); keepNetState = NET_DISCONNECT; keepTimer->stop(); disconnect(keepTimer,0,0,0); if(keep == nullptr){ return true; } //qDebug() << QString("disconnect\n"); disconnect(keep,0,0,0); /* if(keep->isRunning()){ qDebug()<< "keepRelayErrProcess keep->isRunning() true" << endl; }else{ qDebug()<< "keepRelayErrProcess keep->isRunning() false" << endl; } qDebug() << QString("keep->close()\n"); keep->close(); qDebug() << QString("keep->abort()\n"); keep->abort(); */ qDebug() << QString("keep->deleteLater()\n"); keep->deleteLater(); //keep = nullptr; return true; } bool MyNetManager::KeepReconnect() { qDebug() << QString("KeepReconnect\n"); KeepDisconnect(); KeepConnect(); return true; } NetState MyNetManager::KeepState() { return keepNetState; } void MyNetManager::onKeepRelyfinished() { QNetworkReply* reply = (QNetworkReply*)sender(); if(reply->error() == QNetworkReply::NoError){ qDebug() << QString("onKeepRelyfinished\n"); keepRelayOkProcess(reply); }else{ qDebug()<< "!!!!!! onKeepRelyfinished err:"<errorString()<error() == QNetworkReply::NoError){ qDebug() << QString("onCapPicRelyfinished")<readAll(); emit sigCapPicture(capPicData); }else{ qDebug()<< "onCapPicRelyfinished err: " << reply->errorString()<close(); reply->deleteLater(); } //23.8.14 //获取实时温度,解析数据方法未写 void MyNetManager::onGetTemperatureRelyfinished() { qDebug() << "获取到实时温度!"; QDateTime dateTime; dateTime = QDateTime::currentDateTime(); QString currentdatetime = dateTime.toString("yyyy_MM_dd hh_mm_ss"); QNetworkReply* reply = (QNetworkReply*)sender(); if(reply->error() == QNetworkReply::NoError){ qDebug() << QString("onGetTemperatureRelyfinished")<readAll(); emit sigGetTemperature(responseData); }else{ qDebug()<< "onGetTemperatureRelyfinished: " << reply->errorString()<close(); reply->deleteLater(); } bool MyNetManager::putWait(QString url, QByteArray &bytes) { QNetworkRequest request; QUrl qurl = QUrl(url); qurl.setUserName("admin"); qurl.setPassword("YN123456789"); request.setUrl(qurl); request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); QNetworkReply *reply = m_pManager->put(request, bytes); QEventLoop eventLoop; connect(reply, &QNetworkReply ::finished, &eventLoop, &QEventLoop::quit); eventLoop.exec(); if(reply->error()!=QNetworkReply::NoError){ //处理中的错误信息 qDebug()<<"reply error:"<errorString()<deleteLater(); return true; } void MyNetManager::keepRelayOkProcess(QNetworkReply *reply) { qDebug() << "reply :" << reply << endl; if(keepNetState == NET_CONNECTING){ keepNetState = NET_CONNECTED; qDebug() << "keepRelayOkProcess State == NET_CONNECTING\n" << endl; qDebug()<<"operation:"<operation(); qDebug()<<"status code:"<attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<"reply:"<start(11000); qDebug() << "KeepConnect ok\n"; }else if(keepNetState == NET_CONNECTED){ qDebug() << "keepRelayOkProcess State == NET_CONNECTED\n"; qDebug()<<"reply:"<start(11000); QByteArray bytes = reply->readAll(); //qDebug() << "recv:" << bytes << endl; QString qstr_byte = qPrintable(bytes); if (qstr_byte.contains("Temperature Measurement Alarm")) { emit sigTempAlarm(1); qDebug() << "Temperature Measurement Alarm1\n"; }else{ qDebug() << "recv heart beat!\n"; } } } void MyNetManager::keepRelayErrProcess(QNetworkReply *reply) { Q_UNUSED(reply); qDebug() << QString("keepRelayErrProcess\n"); //当前处于连接状态,出错,需要告警 if(keepNetState == NET_CONNECTED){ emit sigKeepBrokeAlarm(); } KeepDisconnect(); } void MyNetManager::onKeepTimerOverProcess() { qDebug() << "onKeepTimerOverProcess\n"; //连接失败, 或者连接超时未接收到任何数据,告警 emit sigKeepBrokeAlarm(); KeepDisconnect(); } static uint32_t time_add(uint32_t t) { float val = t * 1.5; return (uint32_t)val; } void MyNetManager::onKeepConnectTimeOut() { qDebug() << "onKeepConnectTimeOut\n"; connect_over_time_seconds = time_add(connect_over_time_seconds) ; emit sigKeepBrokeAlarm(); KeepDisconnect(); } void MyNetManager::onKeepConnectFinished(QNetworkReply *reply) { qDebug() << ">>onKeepConnectFinished:" << reply << endl; }