card.cpp 3.7 KB

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