card.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include <memory>
  2. #include <log.h>
  3. #include <zloop.h>
  4. #include "select_tool.h"
  5. #include "loc_tool.h"
  6. #include <area.h>
  7. #include <site_area.h>
  8. #include <card.h>
  9. #include "config_file.h"
  10. #include "db_api/CDBConnPool.h"
  11. #include "websocket/wsTimerThread.h"
  12. extern config_file config;
  13. //一张卡一个ct的所有不同天线的信息
  14. struct one_ct_message_handle
  15. {
  16. static loc_tool_main m_loc_tool;
  17. ev::timer m_min_timer,m_max_timer;
  18. //loc_message.
  19. std::vector<loc_message> m_msg_list;
  20. card_location_base*m_card;
  21. const algo_config*m_ac=nullptr;
  22. int m_ct;
  23. bool m_min_timeout=false;
  24. ev::dynamic_loop * m_loop = nullptr;
  25. one_ct_message_handle(card_location_base*card)
  26. {
  27. m_card=card;
  28. m_ct=-1;
  29. }
  30. void reset()
  31. {
  32. m_ct=-1;
  33. m_min_timeout=false;
  34. m_msg_list.clear();
  35. }
  36. void on_min_timer()
  37. {
  38. m_min_timer.stop();
  39. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  40. {
  41. m_max_timer.stop();
  42. calc_location();
  43. return;
  44. }
  45. m_min_timeout=true;
  46. }
  47. void on_max_timer()
  48. {
  49. m_max_timer.stop();
  50. calc_location();
  51. }
  52. void set(ev::dynamic_loop * loop)
  53. {
  54. m_loop = loop;
  55. m_min_timer.set(*m_loop);
  56. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  57. m_max_timer.set(*m_loop);
  58. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  59. }
  60. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  61. {
  62. if(m_loop == nullptr && loop!=nullptr)
  63. set(loop);
  64. else if(loop == nullptr)
  65. return;
  66. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  67. {
  68. m_msg_list.clear();
  69. }
  70. auto sitPtr = sit_list::instance()->get(loc.m_site_id);
  71. if(sitPtr==nullptr)
  72. return;
  73. const site &s=*(sit_list::instance()->get(loc.m_site_id));
  74. if(m_msg_list.empty())
  75. {
  76. m_ct=loc.m_card_ct;
  77. m_ac=&s.config();
  78. m_min_timeout=false;
  79. //这里构造loc_message 保存数据
  80. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  81. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  82. loc.m_sync_ct,loc.m_rssi));
  83. //启动本CT的最小、最大两个定时器
  84. m_min_timer.start(m_ac->min_wait_time);
  85. m_max_timer.start(m_ac->min_wait_time);
  86. return;
  87. }
  88. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  89. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  90. loc.m_sync_ct,loc.m_rssi));
  91. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  92. {
  93. calc_location();
  94. m_max_timer.stop();
  95. }
  96. }
  97. void calc_location()
  98. {
  99. auto v = m_msg_list;
  100. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  101. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  102. reset();
  103. }
  104. };
  105. struct card_message_handle
  106. {
  107. card_location_base*m_card;
  108. std::array<one_ct_message_handle*,16> m_ct_list;
  109. card_message_handle(card_location_base*card)
  110. {
  111. m_card=card;
  112. for(size_t i=0;i<m_ct_list.size();i++)
  113. {
  114. m_ct_list[i]=new one_ct_message_handle(card);
  115. }
  116. }
  117. ~card_message_handle()
  118. {
  119. for(auto&it:m_ct_list)
  120. {
  121. delete it;
  122. }
  123. }
  124. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  125. {
  126. if(is_history)
  127. {
  128. log_warn("%s","当前代码没有处理历史消息记录。");
  129. return;
  130. }
  131. if(loc.m_batty_status == 2)
  132. {
  133. m_card->do_status(STA_TYPE::STATUS_LOW_POWER);
  134. }
  135. else if(loc.m_callinfo == 0x80)
  136. {
  137. m_card->do_status(STA_TYPE::STATUS_HELP);
  138. }
  139. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  140. }
  141. };
  142. struct card_area
  143. {
  144. std::shared_ptr<site_area_hover> m_site_area;
  145. std::shared_ptr<area_tool> m_area_tool;
  146. };
  147. struct person:card_location_base,card_area
  148. {
  149. person(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t)
  150. :card_location_base(type,cardid,needdisplay,t)
  151. {
  152. m_message_handle=new card_message_handle(this);
  153. }
  154. ~person(){}
  155. };
  156. struct car:card_location_base,card_area
  157. {
  158. car(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t)
  159. :card_location_base(type,cardid,needdisplay,t)
  160. {
  161. m_message_handle=new card_message_handle(this);
  162. }
  163. ~car(){}
  164. };
  165. loc_tool_main one_ct_message_handle::m_loc_tool;
  166. uint64_t card_list::getId(uint32_t cardid,uint64_t type)
  167. {
  168. return type<<32|cardid;
  169. }
  170. void card_list::init_vehicle()
  171. {
  172. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  173. std::string strategy = config.get("person.strategy","car1");
  174. const char *sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
  175. ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
  176. vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
  177. vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed \
  178. FROM dat_vehicle_extend ve \
  179. LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
  180. LEFT JOIN dat_card c ON ve.card_id = c.card_id \
  181. LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
  182. LEFT JOIN dat_group g ON ve.group_id = g.group_id \
  183. LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
  184. LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
  185. WHERE c.card_type_id = 2 AND c.state_id = 0;";
  186. std::string Error;
  187. YADB::CDBHelper DBHelper;
  188. YADB::CDBResultSet DBRes;
  189. sDBConnPool.Query(sql,DBRes,Error);
  190. int nCount = DBRes.GetRecordCount( Error );
  191. if (nCount > 0)
  192. {
  193. log_info( "init_staffer. The record count=%d\n", nCount );
  194. while ( DBRes.GetNextRecod(Error) )
  195. {
  196. unsigned int vehicle_id = 0;
  197. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  198. unsigned int card_type_id = 0;
  199. DBRes.GetField( "card_type_id",card_type_id, Error );
  200. int dept_id = 0;
  201. DBRes.GetField( "dept_id",dept_id, Error );
  202. int group_id = 0;
  203. DBRes.GetField( "group_id",group_id, Error );
  204. int vehicle_type_id = 0;
  205. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  206. int vehicle_level_id = 0;
  207. DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );
  208. int need_display = 0;
  209. DBRes.GetField( "need_display",need_display, Error );
  210. int power_alarm = 0;
  211. DBRes.GetField( "power_alarm",power_alarm, Error );
  212. int vehicle_category_id = 0;
  213. DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );
  214. int bigger_car_flag= 0;
  215. DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );
  216. double over_speed= 0;
  217. DBRes.GetField( "over_speed",over_speed, Error );
  218. std::shared_ptr<card_location_base> clb = std::make_shared<car>(strategy,vehicle_id,need_display,card_type_id);
  219. uint64_t cardid = getId(vehicle_id,2);
  220. log_info("cardId:%llu,vehicle_id:%d dept_id:%d,need_display:%d",cardid,vehicle_id,dept_id,need_display);
  221. map.insert({cardid,clb});
  222. }
  223. }
  224. card_list::instance()->add(map);
  225. }
  226. void card_list::init_staffer()
  227. {
  228. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  229. std::string strategy = config.get("person.strategy","person1");
  230. const char *sql = "SELECT staff_id, s.card_id, c.card_type_id, s.dept_id, s.group_id, s.occupation_id, \
  231. ol.occupation_level_id,s.worktype_id,s.need_display \
  232. FROM dat_staff_extend s \
  233. LEFT JOIN dat_card c ON s.card_id = c.card_id \
  234. LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
  235. LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
  236. WHERE c.card_type_id = 1 AND s.duty_id = 0 AND c.state_id = 0;";
  237. std::string Error;
  238. YADB::CDBHelper DBHelper;
  239. YADB::CDBResultSet DBRes;
  240. sDBConnPool.Query(sql,DBRes,Error);
  241. int nCount = DBRes.GetRecordCount( Error );
  242. if (nCount > 0)
  243. {
  244. log_info( "init_staffer. The record count=%d\n", nCount );
  245. while ( DBRes.GetNextRecod(Error) )
  246. {
  247. unsigned int staff_id = 0;
  248. DBRes.GetField( "staff_id",staff_id, Error );
  249. unsigned int card_type_id = 0;
  250. DBRes.GetField( "card_type_id",card_type_id, Error );
  251. int dept_id = 0;
  252. DBRes.GetField( "dept_id",dept_id, Error );
  253. int group_id = 0;
  254. DBRes.GetField( "group_id",group_id, Error );
  255. int occupation_id = 0;
  256. DBRes.GetField( "occupation_id",occupation_id, Error );
  257. int occupation_level_id = 0;
  258. DBRes.GetField( "occupation_level_id",occupation_level_id, Error );
  259. int need_display = 0;
  260. DBRes.GetField( "need_display",need_display, Error );
  261. std::shared_ptr<card_location_base> clb = std::make_shared<person>(strategy,staff_id,need_display,card_type_id);
  262. uint64_t cardid = getId(staff_id,1);
  263. log_info("cardId:%llu,staff_id:%d dept_id:%d,need_display:%d",cardid,staff_id,dept_id,need_display);
  264. map.insert({cardid,clb});
  265. }
  266. }
  267. card_list::instance()->add(map);
  268. }
  269. void card_list::init_card_from_db()
  270. {
  271. init_staffer();
  272. init_vehicle();
  273. }
  274. void card_list::on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
  275. {
  276. //std::shared_ptr<card_location_base>c=get(loc.m_card_id);
  277. uint64_t cardid = getId(loc.m_card_id,loc.m_card_type);
  278. const auto c=get(cardid);
  279. if(c==nullptr)
  280. {
  281. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  282. return;
  283. }
  284. log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
  285. 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);
  286. c->on_message(loop,loc,is_history);
  287. }
  288. //-----------------card_location_base..
  289. card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t)
  290. :card(id,dis,t)
  291. {
  292. select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
  293. }
  294. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  295. {
  296. loc_point pt = m_sel_tool->select_solution(vp,lm);
  297. if(pt.m_useless)
  298. log_info("loc_point,x:%.2f,y:%.2f",pt.x,pt.y);
  299. }
  300. void card_location_base::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  301. {
  302. if(m_loop == nullptr && loop != nullptr)
  303. set(loop);
  304. m_message_handle->on_message(loop,loc,is_history);
  305. }
  306. void card_location_base::set(ev::dynamic_loop * loop)
  307. {
  308. m_loop = loop;
  309. m_timer.set(*m_loop);
  310. m_timer.set<card_location_base,&card_location_base::on_timer>(this);
  311. m_timer.start(1,1);
  312. }
  313. void card_location_base::on_timer()
  314. {
  315. //push_point..
  316. loc_point lp = m_smo_tool->smooth_strategy();
  317. m_speed = lp.m_speed;
  318. m_stat = lp.m_stat;
  319. YA::_CARD_POS_ cp;
  320. cp.x = x;
  321. cp.y = y;
  322. cp.z = z;
  323. cp.Type=m_type;
  324. cp.ID = m_id;
  325. cp.speed = m_speed;
  326. cp.stat = m_stat;
  327. swsTimerThrd.upt_card_pos(cp);
  328. //log_info("one seconds............later..............");
  329. }
  330. card_location_base::~card_location_base()
  331. {
  332. if(m_message_handle == nullptr)
  333. delete m_message_handle;
  334. }
  335. template<> std::shared_ptr<card_list>
  336. single_base<card_list, uint64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list>();