site_message_handle.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Created by Administrator on 2019/1/14.
  3. //
  4. #include "site_message_handle.h"
  5. #include "system_basic_info/SystemAnalysis.h"
  6. #include "common_tool.h"
  7. #include "ant.h"
  8. #include "ya_event.h"
  9. /** 解析 分站发送过来的数据中(分站的信息数据)*/
  10. void site_message_handle::parse_data_locate_reader(char * DataBuffer, int nLen, int dwConnID,int& nCurPos
  11. , int& reader_id )
  12. {
  13. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_15);
  14. #pragma pack(1)
  15. // 7字节 从第一个字节开始,分别表示秒、分、时、天、周、月、年
  16. struct _LocateInfo
  17. {
  18. unsigned int site_id; //分站ID
  19. unsigned short time_stamp; //时间戳(0-65535)
  20. unsigned char sec;
  21. unsigned char min;
  22. unsigned char hour;
  23. unsigned char day;
  24. unsigned char week;
  25. unsigned char mon;
  26. unsigned char year;
  27. unsigned char site_relation; //分站类型
  28. unsigned char site_state; //分站状态char
  29. unsigned char site_power; //分站电源类型
  30. };
  31. #pragma pack()
  32. _LocateInfo *_pLocateInfo = (_LocateInfo *)(DataBuffer + nCurPos);
  33. if (tool_other::IsBigEnd()) //大端
  34. {
  35. _pLocateInfo->site_id = ntohl(_pLocateInfo->site_id);
  36. _pLocateInfo->time_stamp = ntohs(_pLocateInfo->time_stamp);
  37. }
  38. nCurPos += sizeof(_LocateInfo);
  39. reader_id = _pLocateInfo->site_id;
  40. std::shared_ptr<site> pSite = sit_list::instance()->get(_pLocateInfo->site_id);
  41. bool isErrorReader = false;
  42. if(pSite){
  43. //add_socket_to_list(dwConnID, reader_id, DEVICE_TYPE::DT_CARD_READER);
  44. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_16);
  45. }else{
  46. pSite = std::make_shared<site>();
  47. pSite->m_id = reader_id;
  48. LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_17);
  49. }
  50. // 接收数据时间
  51. pSite->m_rec_time = time(NULL);
  52. m_time_last_rec = pSite->m_rec_time; // 更新最后信号时间
  53. // 分站时间戳
  54. pSite->m_tick_count = _pLocateInfo->time_stamp;
  55. // 分站时间
  56. // 7字节 从第一个字节开始,分别表示秒、分、时、天、周、月、年
  57. struct tm reader_time;
  58. reader_time.tm_sec = _pLocateInfo->sec;
  59. reader_time.tm_min = _pLocateInfo->min;
  60. reader_time.tm_hour = _pLocateInfo->hour;
  61. reader_time.tm_mday = _pLocateInfo->day;
  62. reader_time.tm_wday = _pLocateInfo->week;
  63. reader_time.tm_mon = _pLocateInfo->mon - 1;
  64. reader_time.tm_year = _pLocateInfo->year + 100; // + 2000 - 1900
  65. pSite->m_time = mktime(&reader_time);
  66. //改为大小分站信息,最高位第7位表示大小分站,1表示大分站,0表示小分站
  67. //高6位表示分站(大或小)在大分站的左侧或右侧,1代表在大分站的左侧,0表示在大分站的右侧
  68. //高5位预留位,用于区分是读卡分站还是大小分站
  69. uint8_t reader_type = _pLocateInfo->site_relation >>7; //大小分站的区别
  70. uint8_t reader_dir = (_pLocateInfo->site_relation >>6)&0x01;
  71. uint8_t reader_type_locate = (_pLocateInfo->site_relation >>5)&0x01; //大小分站与读卡分站的区别
  72. uint8_t relay_counts = _pLocateInfo->site_relation&0x0F;
  73. pSite->m_reader_dir = reader_dir;
  74. pSite->m_relay_counts = relay_counts;
  75. reader_power_battery_alarm(_pLocateInfo->site_id,_pLocateInfo->site_power);
  76. log_info("id:%d, n:%d, %.2d-%.2d-%.2d %.2d:%.2d:%.2d, sta: %d,reader_power: %d"
  77. ",reader_type: %d,is_big_reader: %d,is_left: %d, counts: %d",
  78. _pLocateInfo->site_id, _pLocateInfo->time_stamp, _pLocateInfo->year, _pLocateInfo->mon, _pLocateInfo->day\
  79. , _pLocateInfo->hour, _pLocateInfo->min,_pLocateInfo->sec\
  80. , _pLocateInfo->site_state,_pLocateInfo->site_power,reader_type_locate,reader_type,reader_dir,relay_counts);/*tep:%d,*/
  81. }
  82. /** 解析单个卡数据 */
  83. int site_message_handle::parse_data_locate_card(char * DataBuffer, int reader_id
  84. , unsigned short wChr,int& nCurPos, unsigned long long ct_time/* = 0*/,int pos/* = 0*/)
  85. {
  86. return 0;
  87. }
  88. void site_message_handle::reader_power_battery_alarm(unsigned int site_id, int powerType)
  89. {
  90. std::shared_ptr<site> pSite = sit_list::instance()->get(site_id);
  91. if (pSite == nullptr )
  92. {
  93. return ;
  94. }
  95. std::shared_ptr<ya_event> ev_ptr = event_list::get_event_reader(site_id,ET_READER_POWER_BY_BATTERY);
  96. int oldEvState = -1;
  97. int usePowerType = powerType;
  98. if (!pSite->m_needpower_alarm)
  99. {
  100. //如果分站不需要电池供电告警,而告警事件列表中存在告警信息,需要发送结束告警事件
  101. if (ev_ptr && ev_ptr->m_status == ES_START)
  102. {
  103. oldEvState = ES_START;
  104. usePowerType = 1;
  105. }
  106. else
  107. {
  108. return;
  109. }
  110. }
  111. switch (usePowerType)
  112. {
  113. case 1 : //交流电 取消警告
  114. {
  115. if(nullptr == ev_ptr || oldEvState == ES_END)
  116. {
  117. return ; //不出在警告并且警告已是取消状态
  118. }
  119. event_list::copy_event(pSite,ev_ptr);
  120. ev_ptr->m_status = ES_END;
  121. ev_ptr->m_cur_time = std::chrono::system_clock::now();
  122. ev_ptr->m_cur_value = usePowerType;
  123. ev_ptr->m_is_display = true;
  124. event_list::save_event(ev_ptr);
  125. log_info("[event warning: restore reader power supply] reader_id: %d.",site_id);
  126. }
  127. break;
  128. case 0: //直流电警告
  129. {
  130. if(nullptr != ev_ptr && oldEvState == ES_START)
  131. {
  132. return ; //已有警告在发送不需要再次添加
  133. }
  134. if (nullptr == ev_ptr)
  135. {
  136. ev_ptr = event_list::create_event_reader(site_id,ET_READER_POWER_BY_BATTERY);
  137. event_list::copy_event(pSite,ev_ptr);
  138. event_list::instance()->add(ev_ptr->get_list_id(),ev_ptr);
  139. }
  140. ev_ptr->status = ES_START;
  141. ev_ptr->m_limit_value = 0;
  142. ev_ptr->m_cur_value = usePowerType;
  143. event_list::save_event(ev_ptr);
  144. log_info("[event warning: reader power supply by battery] reader_id: %d.",site_id);
  145. }
  146. break;
  147. }
  148. }