card.cpp 4.1 KB

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