card.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <memory>
  2. #include <ant.h>
  3. #include <log.h>
  4. #include <zloop.h>
  5. #include <ev++.h>
  6. #include "select_tool.h"
  7. #include "loc_tool.h"
  8. #include <area.h>
  9. #include <site_area.h>
  10. #include <card.h>
  11. enum STA_TYPE
  12. {
  13. STATUS_HELP=0,
  14. STATUS_LOW_POWER,
  15. };
  16. struct card_message_handle;
  17. struct card_location_base
  18. {
  19. std::shared_ptr<select_tool> m_sel_tool=nullptr;
  20. std::shared_ptr<smooth_tool> m_smo_tool=nullptr;
  21. card_message_handle *m_message_handle=nullptr;
  22. card_location_base()=default;
  23. card_location_base(std::string type)
  24. {
  25. select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
  26. }
  27. virtual void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)=0;
  28. virtual void on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  29. {
  30. loc_point pt = m_sel_tool->select_solution(vp,lm);
  31. m_smo_tool->smooth_strategy();
  32. }
  33. void do_status(STA_TYPE st)
  34. {
  35. }
  36. virtual ~card_location_base(){};
  37. };
  38. //一张卡一个ct的所有不同天线的信息
  39. struct one_ct_message_handle
  40. {
  41. static loc_tool_main m_loc_tool;
  42. ev::timer m_min_timer,m_max_timer;
  43. //loc_message.
  44. std::vector<loc_message> m_msg_list;
  45. card_location_base*m_card;
  46. const algo_config*m_ac=nullptr;
  47. int m_ct;
  48. bool m_min_timeout=false;
  49. ev::dynamic_loop * m_loop = nullptr;
  50. one_ct_message_handle(card_location_base*card)
  51. {
  52. m_card=card;
  53. m_ct=-1;
  54. }
  55. void reset()
  56. {
  57. m_ct=-1;
  58. m_min_timeout=false;
  59. m_msg_list.clear();
  60. }
  61. void on_min_timer()
  62. {
  63. m_min_timer.stop();
  64. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  65. {
  66. m_max_timer.stop();
  67. calc_location();
  68. return;
  69. }
  70. m_min_timeout=true;
  71. }
  72. void on_max_timer()
  73. {
  74. m_max_timer.stop();
  75. calc_location();
  76. }
  77. void set(ev::dynamic_loop * loop)
  78. {
  79. m_loop = loop;
  80. m_min_timer.set(*m_loop);
  81. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  82. m_max_timer.set(*m_loop);
  83. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  84. }
  85. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  86. {
  87. if(m_loop == nullptr && loop!=nullptr)
  88. set(loop);
  89. else if(loop == nullptr)
  90. return;
  91. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  92. m_msg_list.clear();
  93. site s=(*sit_list::instance())[loc.m_site_id];
  94. if(m_msg_list.empty())
  95. {
  96. m_ct=loc.m_card_ct;
  97. m_ac=&s.config();
  98. m_min_timeout=false;
  99. //这里构造loc_message 保存数据
  100. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,loc.m_sync_ct,loc.m_rssi));
  101. //启动本CT的最小、最大两个定时器
  102. m_min_timer.start(m_ac->min_wait_time);
  103. m_max_timer.start(m_ac->min_wait_time);
  104. return;
  105. }
  106. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,loc.m_sync_ct,loc.m_rssi));
  107. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  108. {
  109. calc_location();
  110. m_max_timer.stop();
  111. }
  112. }
  113. void calc_location()
  114. {
  115. auto v = m_msg_list;
  116. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  117. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  118. reset();
  119. }
  120. };
  121. //card 类的handle -->on_message..
  122. //handle message.
  123. struct card_message_handle
  124. {
  125. card_location_base*m_card;
  126. //std::vector<loc_message> m_msg_list;
  127. //here handle ct message...one ct one handle operator.
  128. std::array<one_ct_message_handle*,16> m_ct_list;
  129. card_message_handle(card_location_base*card)
  130. {
  131. m_card=card;
  132. for(size_t i=0;i<m_ct_list.size();i++)
  133. {
  134. m_ct_list[i]=new one_ct_message_handle(card);
  135. }
  136. }
  137. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  138. {
  139. if(is_history)
  140. {
  141. log_warn("%s","当前代码没有处理历史消息记录。");
  142. return;
  143. }
  144. if(loc.m_batty_status == 2)
  145. {
  146. m_card->do_status(STA_TYPE::STATUS_LOW_POWER);
  147. }
  148. else if(loc.m_callinfo == 0x80)
  149. {
  150. m_card->do_status(STA_TYPE::STATUS_HELP);
  151. }
  152. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  153. }
  154. };
  155. struct card_area
  156. {
  157. std::shared_ptr<site_area_hover> m_site_area;
  158. std::shared_ptr<area_tool> m_area_tool;
  159. };
  160. struct person:card_location_base,card_area
  161. {
  162. person(std::string type)
  163. :card_location_base(type)
  164. {
  165. m_message_handle = new card_message_handle(this);
  166. }
  167. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  168. {
  169. m_message_handle->on_message(loop,loc,is_history);
  170. }
  171. };
  172. struct car:card_location_base,card_area
  173. {
  174. car(std::string type)
  175. :card_location_base(type)
  176. {
  177. m_message_handle = new card_message_handle(this);
  178. }
  179. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  180. {
  181. m_message_handle->on_message(loop,loc,is_history);
  182. }
  183. };
  184. loc_tool_main one_ct_message_handle::m_loc_tool;
  185. struct card_list_impl:card_list
  186. {
  187. std::vector<card_location_base*> m_list;
  188. card_list_impl()
  189. {
  190. m_list.reserve(1<<16);
  191. }
  192. void init_card_from_db()
  193. {
  194. }
  195. card_location_base *get(uint64_t card_id)const
  196. {
  197. uint32_t cid=0xFFFF&card_id;
  198. if(cid>=m_list.size())
  199. return nullptr;
  200. return m_list[cid];
  201. }
  202. void on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
  203. {
  204. card_location_base *c=get(loc.m_card_id);
  205. if(c==nullptr)
  206. {
  207. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  208. return;
  209. }
  210. log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d",
  211. loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi);
  212. c->on_message(loop,loc,is_history);
  213. }
  214. };
  215. template<> std::shared_ptr<card_list>
  216. single_base<card_list, int64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list_impl>();