card_message_handle.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <vector>
  2. #include <ev++.h>
  3. #include "card_base.h"
  4. #include "loc_tool.h"
  5. #include "message.h"
  6. #include "zloop.h"
  7. #include "card_message_handle.h"
  8. //一张卡一个ct的所有不同天线的信息
  9. struct one_ct_message_handle
  10. {
  11. static loc_tool_main m_loc_tool;
  12. ev::timer m_min_timer,m_max_timer;
  13. //loc_message.
  14. std::vector<loc_message> m_msg_list;
  15. card_location_base*m_card;
  16. const algo_config*m_ac=nullptr;
  17. int m_ct;
  18. bool m_min_timeout=false;
  19. ev::dynamic_loop * m_loop = nullptr;
  20. one_ct_message_handle(card_location_base*card)
  21. {
  22. m_card=card;
  23. m_ct=-1;
  24. }
  25. void reset()
  26. {
  27. m_ct=-1;
  28. m_min_timeout=false;
  29. m_msg_list.clear();
  30. }
  31. void on_min_timer()
  32. {
  33. m_min_timer.stop();
  34. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  35. {
  36. m_max_timer.stop();
  37. calc_location();
  38. return;
  39. }
  40. m_min_timeout=true;
  41. }
  42. void on_max_timer()
  43. {
  44. m_max_timer.stop();
  45. calc_location();
  46. }
  47. void set(ev::dynamic_loop * loop)
  48. {
  49. m_loop = loop;
  50. m_min_timer.set(*m_loop);
  51. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  52. m_max_timer.set(*m_loop);
  53. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  54. }
  55. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  56. {
  57. if(m_loop == nullptr && loop!=nullptr)
  58. set(loop);
  59. else if(loop == nullptr)
  60. return;
  61. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  62. {
  63. m_msg_list.clear();
  64. }
  65. auto sitPtr = sit_list::instance()->get(loc.m_site_id);
  66. if(sitPtr==nullptr)
  67. {
  68. log_warn("分站信息缺失,SitId:%d",loc.m_site_id);
  69. return;
  70. }
  71. auto s=sit_list::instance()->get(loc.m_site_id);
  72. if(m_msg_list.empty())
  73. {
  74. m_ct=loc.m_card_ct;
  75. m_ac=&s->config();
  76. m_min_timeout=false;
  77. //这里构造loc_message 保存数据
  78. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  79. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  80. loc.m_sync_ct,loc.m_rssi));
  81. //启动本CT的最小、最大两个定时器
  82. m_min_timer.start(m_ac->min_wait_time);
  83. m_max_timer.start(m_ac->max_wait_time);
  84. return;
  85. }
  86. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  87. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  88. loc.m_sync_ct,loc.m_rssi));
  89. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  90. {
  91. calc_location();
  92. m_max_timer.stop();
  93. }
  94. }
  95. void calc_location()
  96. {
  97. log_info("calc_location:%d begin",m_card->m_id);
  98. auto v = m_msg_list;
  99. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  100. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  101. reset();
  102. log_info("calc_location:%d end",m_card->m_id);
  103. }
  104. };
  105. loc_tool_main one_ct_message_handle::m_loc_tool;
  106. card_message_handle::card_message_handle(card_location_base*card)
  107. {
  108. m_card=card;
  109. for(size_t i=0;i<m_ct_list.size();i++)
  110. {
  111. m_ct_list[i]=new one_ct_message_handle(card);
  112. }
  113. }
  114. card_message_handle::~card_message_handle()
  115. {
  116. for(auto&it:m_ct_list)
  117. {
  118. delete it;
  119. }
  120. }
  121. void card_message_handle::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  122. {
  123. if(is_history)
  124. {
  125. log_warn("%s","当前代码没有处理历史消息记录。");
  126. return;
  127. }
  128. //
  129. m_card->site_hover(loc.m_site_id);
  130. if(loc.m_batty_status == 2)
  131. {
  132. m_card->do_status(STATUS_POWER_LOWER_SERIOUS);
  133. }
  134. else
  135. {
  136. m_card->do_status(STATUS_POWER_NOMARL);
  137. }
  138. if(loc.m_callinfo & 0x80)
  139. {
  140. m_card->do_status(STATUS_HELP);
  141. }
  142. if((loc.m_callinfo & 0x01) || (loc.m_callinfo & 0x02))
  143. {
  144. m_card->do_status(STATUS_CALL);
  145. }
  146. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  147. }