card.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "select_tool.h"
  8. #include "loc_tool.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. if(pt.m_useless)
  30. {
  31. std_info("loc_point,x:%.2f,y:%.2f",pt.x,pt.y);
  32. log_info("loc_point,x:%.2f,y:%.2f",pt.x,pt.y);
  33. }
  34. m_smo_tool->smooth_strategy();
  35. }
  36. void do_status(STA_TYPE st)
  37. {
  38. }
  39. virtual ~card_location_base(){};
  40. };
  41. //一张卡一个ct的所有不同天线的信息
  42. struct one_ct_message_handle
  43. {
  44. static loc_tool_main m_loc_tool;
  45. ev::timer m_min_timer,m_max_timer;
  46. //loc_message.
  47. std::vector<loc_message> m_msg_list;
  48. card_location_base*m_card;
  49. const algo_config*m_ac=nullptr;
  50. int m_ct;
  51. bool m_min_timeout=false;
  52. ev::dynamic_loop * m_loop = nullptr;
  53. one_ct_message_handle(card_location_base*card)
  54. {
  55. m_card=card;
  56. m_ct=-1;
  57. }
  58. void reset()
  59. {
  60. m_ct=-1;
  61. m_min_timeout=false;
  62. m_msg_list.clear();
  63. }
  64. void on_min_timer()
  65. {
  66. m_min_timer.stop();
  67. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  68. {
  69. m_max_timer.stop();
  70. calc_location();
  71. return;
  72. }
  73. m_min_timeout=true;
  74. }
  75. void on_max_timer()
  76. {
  77. m_max_timer.stop();
  78. calc_location();
  79. }
  80. void set(ev::dynamic_loop * loop)
  81. {
  82. m_loop = loop;
  83. m_min_timer.set(*m_loop);
  84. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  85. m_max_timer.set(*m_loop);
  86. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  87. }
  88. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  89. {
  90. if(m_loop == nullptr && loop!=nullptr)
  91. set(loop);
  92. else if(loop == nullptr)
  93. return;
  94. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  95. {
  96. m_msg_list.clear();
  97. }
  98. site s=(*sit_list::instance())[loc.m_site_id];
  99. if(m_msg_list.empty())
  100. {
  101. m_ct=loc.m_card_ct;
  102. m_ac=&s.config();
  103. m_min_timeout=false;
  104. //这里构造loc_message 保存数据
  105. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  106. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  107. loc.m_sync_ct,loc.m_rssi));
  108. //启动本CT的最小、最大两个定时器
  109. m_min_timer.start(m_ac->min_wait_time);
  110. m_max_timer.start(m_ac->min_wait_time);
  111. return;
  112. }
  113. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  114. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  115. loc.m_sync_ct,loc.m_rssi));
  116. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  117. {
  118. calc_location();
  119. m_max_timer.stop();
  120. }
  121. }
  122. void calc_location()
  123. {
  124. auto v = m_msg_list;
  125. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  126. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  127. reset();
  128. }
  129. };
  130. struct card_message_handle
  131. {
  132. card_location_base*m_card;
  133. std::array<one_ct_message_handle*,16> m_ct_list;
  134. card_message_handle(card_location_base*card)
  135. {
  136. m_card=card;
  137. for(size_t i=0;i<m_ct_list.size();i++)
  138. {
  139. m_ct_list[i]=new one_ct_message_handle(card);
  140. }
  141. }
  142. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  143. {
  144. if(is_history)
  145. {
  146. log_warn("%s","当前代码没有处理历史消息记录。");
  147. return;
  148. }
  149. if(loc.m_batty_status == 2)
  150. {
  151. m_card->do_status(STA_TYPE::STATUS_LOW_POWER);
  152. }
  153. else if(loc.m_callinfo == 0x80)
  154. {
  155. m_card->do_status(STA_TYPE::STATUS_HELP);
  156. }
  157. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  158. }
  159. };
  160. struct person:card_location_base
  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
  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. for(int i=0;i<(1<<16);i++)
  195. {
  196. card_location_base * mp = new car("car1");
  197. m_list.push_back(mp);
  198. }
  199. }
  200. card_location_base *get(uint64_t card_id)const
  201. {
  202. uint32_t cid=0xFFFF&card_id;
  203. if(cid>=m_list.size())
  204. return nullptr;
  205. return m_list[cid];
  206. }
  207. void on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
  208. {
  209. card_location_base *c=get(loc.m_card_id);
  210. if(c==nullptr)
  211. {
  212. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  213. return;
  214. }
  215. log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
  216. loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi,loc.m_time_stamp);
  217. c->on_message(loop,loc,is_history);
  218. }
  219. };
  220. //card_list *card_list::instance(zloop<task*>&loop)
  221. card_list *card_list::instance()
  222. {
  223. static card_list_impl _impl;
  224. return &_impl;
  225. }