smartlightserver.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "smartlightserver.h"
  2. #include "smartlightshm.h"
  3. SmartLightServer::SmartLightServer(QObject *parent) : QTcpServer(parent)
  4. {
  5. threadList.clear();
  6. }
  7. void SmartLightServer::start(){
  8. if(!this->listen(QHostAddress::Any,ServerPort)){
  9. exit(-1);
  10. }
  11. }
  12. void SmartLightServer::incomingConnection(qintptr socketDescriptor){
  13. SmartLightCommThread *thread = new SmartLightCommThread(socketDescriptor,this);
  14. connect(thread,&SmartLightCommThread::CommData,this,&SmartLightServer::eCommData);
  15. connect(thread,&SmartLightCommThread::getEAlarm,this,&SmartLightServer::egetEAlarm);
  16. connect(thread,&SmartLightCommThread::getEAnalogValue,this,&SmartLightServer::egetEAnalogValue);
  17. connect(thread,&SmartLightCommThread::getERealtimeValue,this,&SmartLightServer::getERealtimeValue);
  18. connect(thread,&SmartLightCommThread::finished,this,&SmartLightServer::closeThread);
  19. thread->start();
  20. threadList.append(thread);
  21. }
  22. void SmartLightServer::eCommData(QString DeviceID, int dir, QString data)
  23. {
  24. emit CommData(DeviceID, dir, data);
  25. }
  26. void SmartLightServer::egetEAlarm(QString DeviceCode, int erc, int sta, int arg1, int arg2, int arg3, float value, float v2, QString others, QDateTime t)
  27. {
  28. emit getEAlarm(DeviceCode,erc,sta,arg1,arg2,arg3,value,v2,others,t);
  29. }
  30. void SmartLightServer::egetEAnalogValue(QString DeviceCode, int point, float ave, float rtv, QDateTime t, float maxv, QDateTime maxt, float minv, QDateTime mint)
  31. {
  32. emit getEAnalogValue(DeviceCode,point,ave,rtv,t,maxv,maxt,minv,mint);
  33. }
  34. void SmartLightServer::egetERealtimeValue(QString DeviceCode, int point, float rtv, QDateTime t)
  35. {
  36. emit getERealtimeValue(DeviceCode, point, rtv,t);
  37. }
  38. void SmartLightServer::mk_EventList_qry(QString deviceId, quint8 type, quint8 cur, quint8 num)
  39. {
  40. QList<SmartLightCommThread *>::iterator i;
  41. for(i=threadList.begin();i!=threadList.end();i++){
  42. SmartLightCommThread *thread = *i;
  43. if(QString::compare(thread->deviceID(),deviceId)==0){
  44. thread->mk_EventList_qry(deviceId,type,cur,num);
  45. break;
  46. }
  47. }
  48. }
  49. void SmartLightServer::mk_ResetList_qry(QString deviceId, int commandType, QString parameterType, int pseq)
  50. {
  51. QList<SmartLightCommThread *>::iterator i;
  52. for(i=threadList.begin();i!=threadList.end();i++){
  53. SmartLightCommThread *thread = *i;
  54. if(QString::compare(thread->deviceID(),deviceId)==0){
  55. emit CommData(deviceId, 1, "servicedeviceId");
  56. if(commandType==2){
  57. thread->reset_command(deviceId);
  58. break;
  59. }else if(commandType==11){
  60. if(QString::compare(parameterType,"f9")==0){
  61. thread->query_parameters_commandf9(deviceId,parameterType);
  62. break;
  63. }else{
  64. thread->query_parameters_command(deviceId,parameterType,pseq);
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. void SmartLightServer::mk_realtime_qry(QString deviceId, int commandType, int parameterType, int pn, int queryNumber, int startPoint)
  72. {
  73. QList<SmartLightCommThread *>::iterator i;
  74. for(i=threadList.begin();i!=threadList.end();i++){
  75. SmartLightCommThread *thread = *i;
  76. if(QString::compare(thread->deviceID(),deviceId)==0){
  77. if(commandType==12){
  78. if(parameterType==2||parameterType==3||parameterType==4||parameterType==25){
  79. thread->request_real_time_data_command(deviceId,parameterType);
  80. break;
  81. }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){
  82. thread->request_real_time_data_command2(deviceId,parameterType,pn,queryNumber,startPoint);
  83. break;
  84. }
  85. }else if(commandType==5){
  86. if(parameterType==2||parameterType==1||parameterType==3||parameterType==9){
  87. thread->request_control_command(deviceId,parameterType,pn,queryNumber,startPoint);
  88. break;
  89. }
  90. }else if(commandType==14){
  91. thread->request_event_data_command(deviceId,parameterType,pn,queryNumber);
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. void SmartLightServer::mk_set_parameters_qry(QString deviceId, int commandType, int parameterType, QString data_list)
  98. {
  99. QList<SmartLightCommThread *>::iterator i;
  100. for(i=threadList.begin();i!=threadList.end();i++){
  101. SmartLightCommThread *thread = *i;
  102. if(QString::compare(thread->deviceID(),deviceId)==0){
  103. if(commandType==4){
  104. if(parameterType==9){
  105. thread->request_set_parameters_command(deviceId,parameterType,data_list);
  106. emit CommData(deviceId, 2, "++++++++++ceshi+++++++++++");
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. void SmartLightServer::mk_hisData_qry(QString deviceId, quint16 f_pno, quint16 f_fno, QDateTime t1, QDateTime t2, quint8 sep, quint8 num)
  114. {
  115. QList<SmartLightCommThread *>::iterator i;
  116. for(i=threadList.begin();i!=threadList.end();i++){
  117. SmartLightCommThread *thread = *i;
  118. if(QString::compare(thread->deviceID(),deviceId)==0){
  119. thread->mk_hisData_qry(deviceId, f_pno, f_fno, t1, t2, sep, num);
  120. break;
  121. }
  122. }
  123. }
  124. void SmartLightServer::closeThread()
  125. {
  126. SmartLightCommThread *pThread = static_cast<SmartLightCommThread *>(sender());
  127. if(pThread){
  128. QList<SmartLightCommThread *>::iterator i;
  129. for(i=threadList.begin();i!=threadList.end();i++){
  130. SmartLightCommThread *thread = *i;
  131. if(thread==pThread){
  132. threadList.removeAll(thread);
  133. pThread->deleteLater();
  134. break;
  135. }
  136. }
  137. }
  138. }