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