card.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  118. {
  119. if(is_history)
  120. {
  121. log_warn("%s","当前代码没有处理历史消息记录。");
  122. return;
  123. }
  124. if(loc.m_batty_status == 2)
  125. {
  126. m_card->do_status(STA_TYPE::STATUS_LOW_POWER);
  127. }
  128. else if(loc.m_callinfo == 0x80)
  129. {
  130. m_card->do_status(STA_TYPE::STATUS_HELP);
  131. }
  132. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  133. }
  134. };
  135. struct card_area
  136. {
  137. std::shared_ptr<site_area_hover> m_site_area;
  138. std::shared_ptr<area_tool> m_area_tool;
  139. };
  140. struct person:card_location_base,card_area
  141. {
  142. person(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t)
  143. :card_location_base(type,cardid,needdisplay,t)
  144. {
  145. m_message_handle = new card_message_handle(this);
  146. }
  147. ~person(){}
  148. };
  149. struct car:card_location_base,card_area
  150. {
  151. car(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t)
  152. :card_location_base(type,cardid,needdisplay,t)
  153. {
  154. m_message_handle = new card_message_handle(this);
  155. }
  156. ~car(){}
  157. };
  158. loc_tool_main one_ct_message_handle::m_loc_tool;
  159. uint64_t card_list::getId(uint32_t cardid,uint64_t type)
  160. {
  161. return type<<32|cardid;
  162. }
  163. void card_list::init_vehicle()
  164. {
  165. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  166. std::string strategy = config.get("person.strategy","car1");
  167. const char *sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
  168. ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
  169. vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
  170. vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed \
  171. FROM dat_vehicle_extend ve \
  172. LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
  173. LEFT JOIN dat_card c ON ve.card_id = c.card_id \
  174. LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
  175. LEFT JOIN dat_group g ON ve.group_id = g.group_id \
  176. LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
  177. LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
  178. WHERE c.card_type_id = 2 AND c.state_id = 0;";
  179. std::string Error;
  180. YADB::CDBHelper DBHelper;
  181. YADB::CDBResultSet DBRes;
  182. sDBConnPool.Query(sql,DBRes,Error);
  183. int nCount = DBRes.GetRecordCount( Error );
  184. if (nCount > 0)
  185. {
  186. log_info( "init_staffer. The record count=%d\n", nCount );
  187. while ( DBRes.GetNextRecod(Error) )
  188. {
  189. unsigned int vehicle_id = 0;
  190. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  191. unsigned int card_type_id = 0;
  192. DBRes.GetField( "card_type_id",card_type_id, Error );
  193. int dept_id = 0;
  194. DBRes.GetField( "dept_id",dept_id, Error );
  195. int group_id = 0;
  196. DBRes.GetField( "group_id",group_id, Error );
  197. int vehicle_type_id = 0;
  198. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  199. int vehicle_level_id = 0;
  200. DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );
  201. int need_display = 0;
  202. DBRes.GetField( "need_display",need_display, Error );
  203. int power_alarm = 0;
  204. DBRes.GetField( "power_alarm",power_alarm, Error );
  205. int vehicle_category_id = 0;
  206. DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );
  207. int bigger_car_flag= 0;
  208. DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );
  209. double over_speed= 0;
  210. DBRes.GetField( "over_speed",over_speed, Error );
  211. std::shared_ptr<card_location_base> clb = std::make_shared<car>(strategy,vehicle_id,need_display,card_type_id);
  212. uint64_t cardid = getId(vehicle_id,2);
  213. log_info("cardId:%llu,vehicle_id:%d dept_id:%d,need_display:%d",cardid,vehicle_id,dept_id,need_display);
  214. map.insert({cardid,clb});
  215. }
  216. }
  217. card_list::instance()->add(map);
  218. }
  219. void card_list::init_staffer()
  220. {
  221. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  222. std::string strategy = config.get("person.strategy","person1");
  223. const char *sql = "SELECT staff_id, s.card_id, c.card_type_id, s.dept_id, s.group_id, s.occupation_id, \
  224. ol.occupation_level_id,s.worktype_id,s.need_display \
  225. FROM dat_staff_extend s \
  226. LEFT JOIN dat_card c ON s.card_id = c.card_id \
  227. LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
  228. LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
  229. WHERE c.card_type_id = 1 AND s.duty_id = 0 AND c.state_id = 0;";
  230. std::string Error;
  231. YADB::CDBHelper DBHelper;
  232. YADB::CDBResultSet DBRes;
  233. sDBConnPool.Query(sql,DBRes,Error);
  234. int nCount = DBRes.GetRecordCount( Error );
  235. if (nCount > 0)
  236. {
  237. log_info( "init_staffer. The record count=%d\n", nCount );
  238. while ( DBRes.GetNextRecod(Error) )
  239. {
  240. unsigned int staff_id = 0;
  241. DBRes.GetField( "staff_id",staff_id, Error );
  242. unsigned int card_type_id = 0;
  243. DBRes.GetField( "card_type_id",card_type_id, Error );
  244. int dept_id = 0;
  245. DBRes.GetField( "dept_id",dept_id, Error );
  246. int group_id = 0;
  247. DBRes.GetField( "group_id",group_id, Error );
  248. int occupation_id = 0;
  249. DBRes.GetField( "occupation_id",occupation_id, Error );
  250. int occupation_level_id = 0;
  251. DBRes.GetField( "occupation_level_id",occupation_level_id, Error );
  252. int need_display = 0;
  253. DBRes.GetField( "need_display",need_display, Error );
  254. std::shared_ptr<card_location_base> clb = std::make_shared<person>(strategy,staff_id,need_display,card_type_id);
  255. uint64_t cardid = getId(staff_id,1);
  256. log_info("cardId:%llu,staff_id:%d dept_id:%d,need_display:%d",cardid,staff_id,dept_id,need_display);
  257. map.insert({cardid,clb});
  258. }
  259. }
  260. card_list::instance()->add(map);
  261. }
  262. void card_list::init_card_from_db()
  263. {
  264. init_staffer();
  265. init_vehicle();
  266. }
  267. void card_list::on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
  268. {
  269. //std::shared_ptr<card_location_base>c=get(loc.m_card_id);
  270. uint64_t cardid = getId(loc.m_card_id,loc.m_card_type);
  271. const auto c=get(cardid);
  272. if(c==nullptr)
  273. {
  274. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  275. return;
  276. }
  277. log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
  278. 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);
  279. c->on_message(loop,loc,is_history);
  280. }
  281. //-----------------card_location_base..
  282. card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t)
  283. :card(id,dis,t)
  284. {
  285. select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
  286. }
  287. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  288. {
  289. loc_point pt = m_sel_tool->select_solution(vp,lm);
  290. if(pt.m_useless)
  291. log_info("loc_point,x:%.2f,y:%.2f",pt.x,pt.y);
  292. }
  293. void card_location_base::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  294. {
  295. if(m_loop == nullptr && loop != nullptr)
  296. set(loop);
  297. m_message_handle->on_message(loop,loc,is_history);
  298. }
  299. void card_location_base::set(ev::dynamic_loop * loop)
  300. {
  301. m_loop = loop;
  302. m_timer.set(*m_loop);
  303. m_timer.set<card_location_base,&card_location_base::on_timer>(this);
  304. m_timer.start(1,1);
  305. }
  306. void card_location_base::on_timer()
  307. {
  308. //push_point..
  309. loc_point lp = m_smo_tool->smooth_strategy();
  310. m_speed = lp.m_speed;
  311. m_stat = lp.m_stat;
  312. YA::_CARD_POS_ cp;
  313. cp.x = x;
  314. cp.y = y;
  315. cp.z = z;
  316. cp.Type=m_type;
  317. cp.ID = m_id;
  318. cp.speed = m_speed;
  319. cp.stat = m_stat;
  320. swsTimerThrd.upt_card_pos(cp);
  321. //log_info("one seconds............later..............");
  322. }
  323. card_location_base::~card_location_base()
  324. {
  325. if(m_message_handle == nullptr)
  326. delete m_message_handle;
  327. }
  328. template<> std::shared_ptr<card_list>
  329. single_base<card_list, uint64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list>();