card.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <memory>
  2. #include <ant.h>
  3. #include <log.h>
  4. #include <card.h>
  5. #include <zloop.h>
  6. #include <ev++.h>
  7. #include <message.h>
  8. struct card_location_base
  9. {
  10. virtual void on_location(const std::vector<point>&vp)=0;
  11. virtual ~card_location_base(){};
  12. };
  13. //一张卡一个ct的所有不同天线的信息
  14. struct one_ct_message_handle
  15. {
  16. static loc_tool_main m_loc_tool;
  17. ev::timer m_min_timer,m_max_timer;
  18. std::vector<loc_message> m_msg_list;
  19. card_location_base*m_card;
  20. const algo_config*m_ac=nullptr;
  21. int m_ct;
  22. bool m_min_timeout=false;
  23. one_ct_message_handle(card_location_base*card)
  24. {
  25. m_card=card;
  26. m_ct=-1;
  27. }
  28. void reset()
  29. {
  30. m_ct=-1;
  31. m_min_timeout=false;
  32. m_msg_list.clear();
  33. }
  34. void on_min_timer()
  35. {
  36. m_min_timer.stop();
  37. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  38. {
  39. m_max_timer.stop();
  40. calc_location();
  41. }
  42. }
  43. void on_max_timer()
  44. {
  45. m_max_timer.stop();
  46. calc_location();
  47. }
  48. bool once_flag=false;
  49. void once_init(ev::dynamic_loop&loop)
  50. {
  51. if(once_flag)
  52. return;
  53. once_flag=true;
  54. m_min_timer.set(loop);
  55. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  56. m_max_timer.set(loop);
  57. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  58. }
  59. void on_message(ev::dynamic_loop&loop, const message_locinfo&loc)
  60. {
  61. once_init(loop);
  62. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  63. {
  64. m_msg_list.clear();
  65. }
  66. ant*a=ant_list::instance()->get(loc.m_site_id, loc.m_ant_id);
  67. m_loc_tool.on_loc_message(a, loc);
  68. if(m_msg_list.empty())
  69. {
  70. m_ct=loc.m_card_ct;
  71. m_ac=&a->config();
  72. m_min_timeout=false;
  73. m_msg_list.push_back(loc_message(a,loc.m_tof));
  74. //启动本CT的最小、最大两个定时器
  75. m_min_timer.start(m_ac->min_wait_time);
  76. m_max_timer.start(m_ac->min_wait_time);
  77. return;
  78. }
  79. m_msg_list.push_back(loc_message(a,loc.m_tof));
  80. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  81. {
  82. calc_location();
  83. m_max_timer.stop();
  84. }
  85. }
  86. void calc_location()
  87. {
  88. std::vector<point> rc=std::move(m_loc_tool.calc_location(m_msg_list));
  89. if(!rc.empty()) m_card->on_location(std::move(rc));
  90. reset();
  91. }
  92. };
  93. struct card_message_handle
  94. {
  95. card_location_base*m_card;
  96. std::vector<loc_message> m_msg_list;
  97. std::array<one_ct_message_handle*,16> m_ct_list;
  98. card_message_handle(card_location_base*card)
  99. {
  100. m_card=card;
  101. for(size_t i=0;i<m_ct_list.size();i++)
  102. {
  103. m_ct_list[i]=new one_ct_message_handle(card);
  104. }
  105. }
  106. void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
  107. {
  108. if(is_history)
  109. {
  110. log_warn("%s","当前代码没有处理历史消息记录。");
  111. return;
  112. }
  113. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop, loc);
  114. }
  115. };
  116. struct card:card_location_base
  117. {
  118. card_message_handle m_message_handle;
  119. card()
  120. :m_message_handle(this)
  121. {
  122. }
  123. ~card()
  124. {
  125. }
  126. void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
  127. {
  128. m_message_handle.on_message(loop, loc, is_history);
  129. }
  130. void on_location(const std::vector<point>&points)
  131. {
  132. }
  133. };
  134. loc_tool_main one_ct_message_handle::m_loc_tool;
  135. struct card_list_impl:card_list
  136. {
  137. std::vector<card*> m_list;
  138. card_list_impl()
  139. {
  140. m_list.reserve(1<<16);
  141. }
  142. void init_card_from_db()
  143. {
  144. }
  145. card*get(uint64_t card_id)const
  146. {
  147. uint32_t cid=0xFFFF&card_id;
  148. if(cid>=m_list.size())
  149. return nullptr;
  150. return m_list[cid];
  151. }
  152. void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
  153. {
  154. card*c=get(loc.m_card_id);
  155. if(c==nullptr)
  156. {
  157. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  158. return;
  159. }
  160. c->on_message(loop, loc, is_history);
  161. }
  162. };
  163. card_list *card_list::instance()
  164. {
  165. static card_list_impl _impl;
  166. return &_impl;
  167. }