1
0

card_message_handle.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <vector>
  2. #include <ev++.h>
  3. #include "card_base.h"
  4. #include "loc_tool.h"
  5. #include "message.h"
  6. #include "zloop.h"
  7. #include "card_message_handle.h"
  8. //一张卡一个ct的所有不同天线的信息
  9. struct one_ct_message_handle
  10. {
  11. static loc_tool_main m_loc_tool;
  12. ev::timer m_min_timer,m_max_timer;
  13. //loc_message.
  14. std::vector<loc_message> m_msg_list;
  15. card_location_base*m_card;
  16. const algo_config*m_ac=nullptr;
  17. int m_ct;
  18. bool m_min_timeout=false;
  19. ev::dynamic_loop * m_loop = nullptr;
  20. one_ct_message_handle(card_location_base*card)
  21. {
  22. m_card=card;
  23. m_ct=-1;
  24. }
  25. void reset()
  26. {
  27. m_ct=-1;
  28. m_min_timeout=false;
  29. m_msg_list.clear();
  30. }
  31. void on_min_timer()
  32. {
  33. m_min_timer.stop();
  34. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  35. {
  36. m_max_timer.stop();
  37. calc_location();
  38. return;
  39. }
  40. m_min_timeout=true;
  41. }
  42. void on_max_timer()
  43. {
  44. m_max_timer.stop();
  45. calc_location();
  46. }
  47. void set(ev::dynamic_loop * loop)
  48. {
  49. m_loop = loop;
  50. m_min_timer.set(*m_loop);
  51. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  52. m_max_timer.set(*m_loop);
  53. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  54. }
  55. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  56. {
  57. if(m_loop == nullptr && loop!=nullptr)
  58. set(loop);
  59. else if(loop == nullptr)
  60. return;
  61. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  62. {
  63. m_msg_list.clear();
  64. }
  65. auto sitPtr = sit_list::instance()->get(loc.m_site_id);
  66. if(sitPtr==nullptr)
  67. {
  68. log_warn("分站信息缺失,SitId:%d",loc.m_site_id);
  69. return;
  70. }
  71. auto s=sit_list::instance()->get(loc.m_site_id);
  72. if(m_msg_list.empty())
  73. {
  74. m_ct=loc.m_card_ct;
  75. m_ac=&s->config();
  76. m_min_timeout=false;
  77. //这里构造loc_message 保存数据
  78. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  79. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  80. loc.m_sync_ct,loc.m_rssi));
  81. //启动本CT的最小、最大两个定时器
  82. m_min_timer.start(m_ac->min_wait_time);
  83. m_max_timer.start(m_ac->max_wait_time);
  84. return;
  85. }
  86. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  87. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  88. loc.m_sync_ct,loc.m_rssi));
  89. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  90. {
  91. calc_location();
  92. m_max_timer.stop();
  93. }
  94. }
  95. void calc_location()
  96. {
  97. auto v = m_msg_list;
  98. if(v.empty())
  99. {
  100. return;
  101. }
  102. log_info("calc_location_begin:card_id=%d,ct=%d,m_ct=%d",m_card->m_id,v[0].m_card_ct,m_ct);
  103. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  104. log_info("calc_location:%d size:%d",m_card->m_id,rc.size());
  105. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  106. reset();
  107. log_info("calc_location_end:card_id=%d",m_card->m_id);
  108. }
  109. };
  110. loc_tool_main one_ct_message_handle::m_loc_tool;
  111. card_message_handle::card_message_handle(card_location_base*card)
  112. {
  113. m_card=card;
  114. for(size_t i=0;i<m_ct_list.size();i++)
  115. {
  116. m_ct_list[i]=new one_ct_message_handle(card);
  117. }
  118. }
  119. card_message_handle::~card_message_handle()
  120. {
  121. for(auto&it:m_ct_list)
  122. {
  123. delete it;
  124. }
  125. }
  126. void card_message_handle::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  127. {
  128. if(is_history)
  129. {
  130. log_warn("%s","当前代码没有处理历史消息记录。");
  131. return;
  132. }
  133. STATUS_CARD c_status = STATUS_POWER_NOMARL;
  134. if(loc.m_batty_status == 2)
  135. {
  136. c_status = STATUS_POWER_LOWER_SERIOUS;
  137. }
  138. m_card->do_status(c_status);
  139. if(loc.m_callinfo & 0x80)
  140. {
  141. m_card->do_status(STATUS_HELP);
  142. }
  143. if((loc.m_callinfo & 0x01) || (loc.m_callinfo & 0x02))
  144. {
  145. m_card->do_status(STATUS_CALL);
  146. }
  147. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  148. }