123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include <memory>
- #include <ant.h>
- #include <log.h>
- #include <card.h>
- #include <zloop.h>
- #include <ev++.h>
- struct card_location_base
- {
- virtual void on_location(const std::vector<point>&vp)=0;
- virtual ~card_location_base(){};
- };
- //一张卡一个ct的所有不同天线的信息
- struct one_ct_message_handle
- {
- static loc_tool_main m_loc_tool;
- ev::timer m_min_timer,m_max_timer;
- std::vector<loc_message> m_msg_list;
- card_location_base*m_card;
- const algo_config*m_ac=nullptr;
- int m_ct;
- bool m_min_timeout=false;
- one_ct_message_handle(ev::dynamic_loop&loop,card_location_base*card)
- {
- m_card=card;
- m_ct=-1;
- m_min_timer.set(loop);
- m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
- m_max_timer.set(loop);
- m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
- }
- void reset()
- {
- m_ct=-1;
- m_min_timeout=false;
- m_msg_list.clear();
- }
- void on_min_timer()
- {
- m_min_timer.stop();
- if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
- {
- m_max_timer.stop();
- calc_location();
- }
- }
- void on_max_timer()
- {
- m_max_timer.stop();
- calc_location();
- }
- void on_message(const message_locinfo&loc)
- {
- if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
- m_msg_list.clear();
- ant*a=ant_list::instance()->get(loc.m_site_id, loc.m_ant_id);
- if(m_msg_list.empty())
- {
- m_ct=loc.m_card_ct;
- m_ac=&a->config();
- m_min_timeout=false;
- m_msg_list.push_back(loc_message(a,loc.m_tof));
- //启动本CT的最小、最大两个定时器
- m_min_timer.start(m_ac->min_wait_time);
- m_max_timer.start(m_ac->min_wait_time);
- return;
- }
- m_msg_list.push_back(loc_message(a,loc.m_tof));
- if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
- {
- calc_location();
- m_max_timer.stop();
- }
- }
- void calc_location()
- {
- std::vector<point> rc=std::move(m_loc_tool.calc_location(m_msg_list));
- if(!rc.empty()) m_card->on_location(std::move(rc));
- reset();
- }
- };
- struct card_message_handle
- {
- card_location_base*m_card;
- std::vector<loc_message> m_msg_list;
- std::array<one_ct_message_handle*,16> m_ct_list;
- card_message_handle(zloop_base&loop,card_location_base*card)
- {
- m_card=card;
- for(size_t i=0;i<m_ct_list.size();i++)
- {
- m_ct_list[i]=new one_ct_message_handle(loop,card);
- }
- }
- void on_message(const message_locinfo&loc,bool is_history)
- {
- if(is_history)
- {
- log_warn("%s","当前代码没有处理历史消息记录。");
- return;
- }
- m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loc);
- }
- };
- struct card:card_location_base
- {
- card_message_handle m_message_handle;
- card(zloop_base&loop)
- :m_message_handle(loop,this)
- {
- }
- void on_message(const message_locinfo&loc,bool is_history)
- {
- m_message_handle.on_message(loc,is_history);
- }
- void on_location(const std::vector<point>&points)
- {
- }
- ~card(){}
- };
- loc_tool_main one_ct_message_handle::m_loc_tool;
- struct card_list_impl:card_list
- {
- zloop<task*>&m_loop;
- std::vector<card*> m_list;
- card_list_impl(zloop<task*>&loop)
- :m_loop(loop)
- {
- m_list.reserve(1<<16);
- }
- void init_card_from_db()
- {
- card _(m_loop);
- }
- card*get(uint64_t card_id)const
- {
- uint32_t cid=0xFFFF&card_id;
- if(cid>=m_list.size())
- return nullptr;
- return m_list[cid];
- }
- void on_message(const message_locinfo&loc,bool is_history)
- {
- card*c=get(loc.m_card_id);
- if(c==nullptr)
- {
- log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
- return;
- }
- log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d",
- loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi);
- c->on_message(loc,is_history);
- }
- };
- card_list *card_list::instance(zloop<task*>&loop)
- {
- static card_list_impl _impl(loop);
- return &_impl;
- }
|