1
0

card_message_handle.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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=nullptr,*m_max_timer=nullptr;
  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. ev::dynamic_loop * m_loop = nullptr;
  19. one_ct_message_handle(card_location_base*card)
  20. {
  21. m_card=card;
  22. m_ct=-1;
  23. }
  24. ~one_ct_message_handle()
  25. {
  26. delete m_min_timer;
  27. delete m_max_timer;
  28. }
  29. void on_min_timer()
  30. {
  31. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  32. {
  33. m_max_timer->stop();
  34. calc_location();
  35. }
  36. }
  37. void on_max_timer()
  38. {
  39. calc_location();
  40. }
  41. void set(ev::dynamic_loop * loop)
  42. {
  43. m_loop=loop;
  44. m_min_timer=new ev::timer(*loop);
  45. m_min_timer->set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  46. m_max_timer=new ev::timer(*loop);
  47. m_max_timer->set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  48. }
  49. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  50. {
  51. if(loop==nullptr)
  52. return;
  53. if(m_loop == nullptr)
  54. set(loop);
  55. if(m_ct==loc.m_card_ct && m_max_timer->remaining()<0) //超过max_timer的点,抛弃
  56. return;
  57. if(!m_msg_list.empty() && m_ct!=loc.m_card_ct) //ct已经回绕,重置
  58. {
  59. m_msg_list.clear();
  60. }
  61. auto sitPtr = sit_list::instance()->get(loc.m_site_id);
  62. if(sitPtr==nullptr)
  63. {
  64. log_warn("分站信息缺失,SitId:%d",loc.m_site_id);
  65. return;
  66. }
  67. auto s=sit_list::instance()->get(loc.m_site_id);
  68. //这里构造loc_message 保存数据
  69. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  70. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  71. loc.m_sync_ct,loc.m_rssi));
  72. if(m_msg_list.size()==1)//第一个点
  73. {
  74. m_ct=loc.m_card_ct;
  75. m_ac=&s->config();
  76. //启动本CT的最小、最大两个定时器
  77. m_min_timer->start(m_ac->min_wait_time);
  78. m_max_timer->start(m_ac->max_wait_time);
  79. return;
  80. }
  81. if(m_min_timer->remaining()<0 && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  82. {
  83. calc_location();
  84. m_max_timer->stop();
  85. }
  86. }
  87. void calc_location()
  88. {
  89. auto&v = m_msg_list;
  90. if(v.empty())
  91. {
  92. return;
  93. }
  94. log_info("calc_location_begin:card_id=%d,ct=%d,m_ct=%d",m_card->m_id,v[0].m_card_ct,m_ct);
  95. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  96. log_info("calc_location:%d size:%d",m_card->m_id,rc.size());
  97. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  98. m_msg_list.clear();
  99. log_info("calc_location_end:card_id=%d",m_card->m_id);
  100. }
  101. };
  102. loc_tool_main one_ct_message_handle::m_loc_tool;
  103. card_message_handle::card_message_handle(card_location_base*card)
  104. {
  105. m_card=card;
  106. for(size_t i=0;i<m_ct_list.size();i++)
  107. {
  108. m_ct_list[i]=new one_ct_message_handle(card);
  109. }
  110. }
  111. card_message_handle::~card_message_handle()
  112. {
  113. for(auto&it:m_ct_list)
  114. {
  115. delete it;
  116. }
  117. }
  118. void card_message_handle::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  119. {
  120. #ifndef _RELEASE_
  121. if(m_first_call)
  122. {
  123. m_first_call=false;
  124. m_first_call_thread=std::this_thread::get_id();
  125. }
  126. if(m_first_call_thread!=std::this_thread::get_id())
  127. {
  128. log_error("%s\n","card_message_handle::on_message 无法支持多线程调用");
  129. assert(false);
  130. }
  131. #endif
  132. if(is_history)
  133. {
  134. log_warn("%s","当前代码没有处理历史消息记录。");
  135. return;
  136. }
  137. STATUS_CARD c_status = STATUS_POWER_NOMARL;
  138. if(loc.m_batty_status == 2)
  139. {
  140. c_status = STATUS_POWER_LOWER_SERIOUS;
  141. }
  142. m_card->do_status(c_status);
  143. if(loc.m_callinfo & 0x80)
  144. {
  145. m_card->do_status(STATUS_HELP);
  146. }
  147. if((loc.m_callinfo & 0x01) || (loc.m_callinfo & 0x02))
  148. {
  149. m_card->do_status(STATUS_CALL);
  150. }
  151. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  152. }