1
0

card.cpp 5.7 KB

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