168bfc8054c51fda45155177c7ce186225faf931.svn-base 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "datasender.h"
  2. extern YT_UNIT_SHM *ytShm;
  3. DataSender::DataSender(QObject *parent, QString serverPath, QString id) :
  4. QObject(parent)
  5. {
  6. waitting = false;
  7. ServerPath = serverPath;
  8. SelfId = id;
  9. timer = new QTimer(this);
  10. connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
  11. queryManager = new QNetworkAccessManager(this);
  12. connect(queryManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(query_finishedSlot(QNetworkReply*)));
  13. timer->start(5000);
  14. }
  15. void DataSender::query_finishedSlot(QNetworkReply *reply)
  16. {
  17. if(reply->error()==QNetworkReply::NoError){
  18. QByteArray qj = reply->readAll();
  19. }
  20. waitting = false;
  21. }
  22. void DataSender::reply_timeout()
  23. {
  24. waitting = false;
  25. }
  26. void DataSender::time_out()
  27. {
  28. if(!waitting){
  29. json_object *iotlist,*obj[5],*obj_list[5], *body, *json;
  30. int comm,adds,i;
  31. int count[5],alarm[5],out[5];
  32. char tmp[256];
  33. body = json_object_new_object();
  34. json = json_object_new_object();
  35. iotlist = json_object_new_array();
  36. for(i=0;i<5;i++)
  37. {
  38. count[i]=0;
  39. alarm[i]=0;
  40. out[i]=0;
  41. obj[i] = json_object_new_object();
  42. obj_list[i] = json_object_new_array();
  43. }
  44. for(comm=0;comm<4;comm++)
  45. {
  46. if(ytShm->commList.comm[comm].ENABLED==0x01){
  47. for(adds=0;adds<256;adds++)
  48. {
  49. if(ytShm->sPointList.sPoint[comm][adds].ENABLED==0x01)
  50. {
  51. json_object *sp = json_object_new_object();
  52. for(i=0;i<5;i++)
  53. {
  54. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  55. count[i]++;
  56. }
  57. if(ytShm->commStatusList.commStatus[comm].STATUS!=0x01)
  58. {
  59. if(ytShm->sPointList.sPoint[comm][adds].SUB_TYPE>0)
  60. {
  61. for(i=0;i<5;i++)
  62. {
  63. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  64. out[i]++;
  65. }
  66. json_object_object_add(sp,"STATUS_ID",json_object_new_int(1));
  67. json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("离线").toUtf8().data()));
  68. }
  69. }else{
  70. if(ytShm->spStatusList.spStatus[comm][adds].ALARM_STATUS!=0x00)
  71. {
  72. for(i=0;i<5;i++)
  73. {
  74. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  75. alarm[i]++;
  76. }
  77. json_object_object_add(sp,"STATUS_ID",json_object_new_int(2));
  78. json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("告警").toUtf8().data()));
  79. }else{
  80. json_object_object_add(sp,"STATUS_ID",json_object_new_int(0));
  81. json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("正常").toUtf8().data()));
  82. }
  83. }
  84. json_object_object_add(sp,"COMM",json_object_new_int(comm));
  85. json_object_object_add(sp,"ADDR",json_object_new_int(adds));
  86. json_object_object_add(sp,"PNO_NAME",
  87. json_object_new_string(ytShm->sPointList.sPoint[comm][adds].NAME));
  88. json_object_object_add(sp,"PNO_TYPE",json_object_new_int(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE));
  89. if(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE==0x01){
  90. if(ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE==0.0)
  91. json_object_object_add(sp,"VALUE",json_object_new_string("0"));
  92. else
  93. json_object_object_add(sp,"VALUE",json_object_new_string("1"));
  94. }else{
  95. sprintf(tmp,"%.2f",ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE);
  96. json_object_object_add(sp,"VALUE",json_object_new_string(tmp));
  97. }
  98. time_t t = ytShm->spStatusList.spStatus[comm][adds].REALTIME_TIMESTAMP;
  99. struct tm *loc_time = localtime(&t);
  100. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  101. loc_time->tm_year+1900,
  102. loc_time->tm_mon+1,
  103. loc_time->tm_mday,
  104. loc_time->tm_hour,
  105. loc_time->tm_min,
  106. loc_time->tm_sec);
  107. json_object_object_add(sp,"TIME",json_object_new_string(tmp));
  108. for(i=0;i<5;i++)
  109. {
  110. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  111. json_object_array_add(obj_list[i],sp);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. for(i=0;i<5;i++)
  118. {
  119. json_object_object_add(obj[i],"ID",json_object_new_int(i+1));
  120. switch(i){
  121. case 0:
  122. json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("火灾监控实时信息").toUtf8().data()));
  123. break;
  124. case 1:
  125. json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("电气火灾监控实时信息").toUtf8().data()));
  126. break;
  127. case 2:
  128. json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("消防水系统监控实时信息").toUtf8().data()));
  129. break;
  130. case 3:
  131. json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("通道监控实时信息").toUtf8().data()));
  132. break;
  133. case 4:
  134. json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("消防电源监控实时信息").toUtf8().data()));
  135. break;
  136. }
  137. json_object_object_add(obj[i],"COUNT",json_object_new_int(count[i]));
  138. json_object_object_add(obj[i],"OUT",json_object_new_int(out[i]));
  139. json_object_object_add(obj[i],"ALARM",json_object_new_int(alarm[i]));
  140. json_object_object_add(obj[i],"LIST",obj_list[i]);
  141. json_object_array_add(iotlist,obj[i]);
  142. }
  143. json_object_object_add(body,"IOT_LIST",iotlist);
  144. json_object_object_add(json,"RESULT",body);
  145. json_object_object_add(json,"Id",json_object_new_string(SelfId.toUtf8().data()));
  146. QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
  147. req->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded; charset=UTF-8");
  148. req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
  149. req->setRawHeader("Accept-Language","zh-CN");
  150. req->setRawHeader("Connection","keep-alive");
  151. req->setRawHeader("Cache-Control"," no-cache");
  152. QByteArray postdata;
  153. postdata.append(QString::fromUtf8(json_object_to_json_string(json)).toUtf8());
  154. req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
  155. QNetworkReply *reply = queryManager->post(*req,postdata);
  156. QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
  157. connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
  158. }
  159. }