#include "smartlightserver.h" #include "smartlightshm.h" SmartLightServer::SmartLightServer(QObject *parent) : QTcpServer(parent) { threadList.clear(); } void SmartLightServer::start(){ if(!this->listen(QHostAddress::Any,ServerPort)){ exit(-1); } } void SmartLightServer::incomingConnection(qintptr socketDescriptor){ SmartLightCommThread *thread = new SmartLightCommThread(socketDescriptor,this); connect(thread,&SmartLightCommThread::CommData,this,&SmartLightServer::eCommData); connect(thread,&SmartLightCommThread::getEAlarm,this,&SmartLightServer::egetEAlarm); connect(thread,&SmartLightCommThread::getEAnalogValue,this,&SmartLightServer::egetEAnalogValue); connect(thread,&SmartLightCommThread::getERealtimeValue,this,&SmartLightServer::getERealtimeValue); connect(thread,&SmartLightCommThread::finished,this,&SmartLightServer::closeThread); thread->start(); threadList.append(thread); } void SmartLightServer::eCommData(QString DeviceID, int dir, QString data) { emit CommData(DeviceID, dir, data); } void SmartLightServer::egetEAlarm(QString DeviceCode, int erc, int sta, int arg1, int arg2, int arg3, float value, float v2, QString others, QDateTime t) { emit getEAlarm(DeviceCode,erc,sta,arg1,arg2,arg3,value,v2,others,t); } void SmartLightServer::egetEAnalogValue(QString DeviceCode, int point, float ave, float rtv, QDateTime t, float maxv, QDateTime maxt, float minv, QDateTime mint) { emit getEAnalogValue(DeviceCode,point,ave,rtv,t,maxv,maxt,minv,mint); } void SmartLightServer::egetERealtimeValue(QString DeviceCode, int point, float rtv, QDateTime t) { emit getERealtimeValue(DeviceCode, point, rtv,t); } void SmartLightServer::mk_EventList_qry(QString deviceId, quint8 type, quint8 cur, quint8 num) { QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(QString::compare(thread->deviceID(),deviceId)==0){ thread->mk_EventList_qry(deviceId,type,cur,num); break; } } } void SmartLightServer::mk_ResetList_qry(QString deviceId, int commandType, int parameterType, int pseq) { QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(QString::compare(thread->deviceID(),deviceId)==0){ emit CommData(deviceId, 1, "servicedeviceId"); if(commandType==2){ thread->reset_command(deviceId); break; }else if(commandType==11){ if(parameterType==9){ thread->query_parameters_commandf9(deviceId,parameterType); break; }else{ thread->query_parameters_command(deviceId,parameterType,pseq); break; } } } } } void SmartLightServer::mk_realtime_qry(QString deviceId, int commandType, int parameterType, int pn, int queryNumber, int startPoint) { QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(QString::compare(thread->deviceID(),deviceId)==0){ if(commandType==12){ if(parameterType==2||parameterType==3||parameterType==4||parameterType==25){ thread->request_real_time_data_command(deviceId,parameterType); break; }else if(parameterType==9||parameterType==10||parameterType==11||parameterType==12||parameterType==17||parameterType==33||parameterType==34||parameterType==35||parameterType==36||parameterType==37||parameterType==38||parameterType==39||parameterType==100){ thread->request_real_time_data_command2(deviceId,parameterType,pn,queryNumber,startPoint); break; } }else if(commandType==5){ if(parameterType==2||parameterType==1||parameterType==3||parameterType==9){ thread->request_control_command(deviceId,parameterType,pn,queryNumber,startPoint); break; } }else if(commandType==14){ thread->request_event_data_command(deviceId,parameterType,pn,queryNumber); break; } } } } void SmartLightServer::mk_set_parameters_qry(QString deviceId, int commandType, int parameterType, QString data_list) { QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(QString::compare(thread->deviceID(),deviceId)==0){ if(commandType==4){ if(parameterType==9){ thread->request_set_parameters_command(deviceId,parameterType,data_list); emit CommData(deviceId, 2, "++++++++++ceshi+++++++++++"); break; } } } } } void SmartLightServer::mk_hisData_qry(QString deviceId, quint16 f_pno, quint16 f_fno, QDateTime t1, QDateTime t2, quint8 sep, quint8 num) { QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(QString::compare(thread->deviceID(),deviceId)==0){ thread->mk_hisData_qry(deviceId, f_pno, f_fno, t1, t2, sep, num); break; } } } void SmartLightServer::closeThread() { SmartLightCommThread *pThread = static_cast(sender()); if(pThread){ QList::iterator i; for(i=threadList.begin();i!=threadList.end();i++){ SmartLightCommThread *thread = *i; if(thread==pThread){ threadList.removeAll(thread); pThread->deleteLater(); break; } } } }