card.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #include "websocket/wsClientMgr.h"
  13. #include "timestamp.h"
  14. #include "monkey_car/monkeycar_area.h"
  15. #include "monkey_car/monkeycar_person.h"
  16. #include "special_area.h"
  17. extern config_file config;
  18. //一张卡一个ct的所有不同天线的信息
  19. struct one_ct_message_handle
  20. {
  21. static loc_tool_main m_loc_tool;
  22. ev::timer m_min_timer,m_max_timer;
  23. //loc_message.
  24. std::vector<loc_message> m_msg_list;
  25. card_location_base*m_card;
  26. const algo_config*m_ac=nullptr;
  27. int m_ct;
  28. bool m_min_timeout=false;
  29. ev::dynamic_loop * m_loop = nullptr;
  30. one_ct_message_handle(card_location_base*card)
  31. {
  32. m_card=card;
  33. m_ct=-1;
  34. }
  35. void reset()
  36. {
  37. m_ct=-1;
  38. m_min_timeout=false;
  39. m_msg_list.clear();
  40. }
  41. void on_min_timer()
  42. {
  43. m_min_timer.stop();
  44. if((int)m_msg_list.size()>=m_ac->best_msg_cnt)
  45. {
  46. m_max_timer.stop();
  47. calc_location();
  48. return;
  49. }
  50. m_min_timeout=true;
  51. }
  52. void on_max_timer()
  53. {
  54. m_max_timer.stop();
  55. calc_location();
  56. }
  57. void set(ev::dynamic_loop * loop)
  58. {
  59. m_loop = loop;
  60. m_min_timer.set(*m_loop);
  61. m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
  62. m_max_timer.set(*m_loop);
  63. m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
  64. }
  65. void on_message(ev::dynamic_loop *loop,const message_locinfo&loc)
  66. {
  67. if(m_loop == nullptr && loop!=nullptr)
  68. set(loop);
  69. else if(loop == nullptr)
  70. return;
  71. if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
  72. {
  73. m_msg_list.clear();
  74. }
  75. auto sitPtr = sit_list::instance()->get(loc.m_site_id);
  76. if(sitPtr==nullptr)
  77. {
  78. log_warn("分站信息缺失,SitId:%d",loc.m_site_id);
  79. return;
  80. }
  81. const site &s=*(sit_list::instance()->get(loc.m_site_id));
  82. if(m_msg_list.empty())
  83. {
  84. m_ct=loc.m_card_ct;
  85. m_ac=&s.config();
  86. m_min_timeout=false;
  87. //这里构造loc_message 保存数据
  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. //启动本CT的最小、最大两个定时器
  92. m_min_timer.start(m_ac->min_wait_time);
  93. m_max_timer.start(m_ac->min_wait_time);
  94. return;
  95. }
  96. m_msg_list.push_back(loc_message(s,loc.m_tof,loc.m_time_stamp,loc.m_card_id,
  97. loc.m_card_ct,loc.m_card_type,loc.m_ant_id,loc.m_rav,loc.m_acc,
  98. loc.m_sync_ct,loc.m_rssi));
  99. if(m_min_timeout && (int)m_msg_list.size()>=m_ac->best_msg_cnt)
  100. {
  101. calc_location();
  102. m_max_timer.stop();
  103. }
  104. }
  105. void calc_location()
  106. {
  107. auto v = m_msg_list;
  108. std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
  109. if(!rc.empty()) m_card->on_location(std::move(rc),v);
  110. reset();
  111. }
  112. };
  113. struct card_message_handle
  114. {
  115. card_location_base*m_card;
  116. std::array<one_ct_message_handle*,16> m_ct_list;
  117. card_message_handle(card_location_base*card)
  118. {
  119. m_card=card;
  120. for(size_t i=0;i<m_ct_list.size();i++)
  121. {
  122. m_ct_list[i]=new one_ct_message_handle(card);
  123. }
  124. }
  125. ~card_message_handle()
  126. {
  127. for(auto&it:m_ct_list)
  128. {
  129. delete it;
  130. }
  131. }
  132. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  133. {
  134. if(is_history)
  135. {
  136. log_warn("%s","当前代码没有处理历史消息记录。");
  137. return;
  138. }
  139. //
  140. m_card->site_hover(loc.m_site_id);
  141. if(loc.m_batty_status == 2)
  142. {
  143. m_card->do_status(STA_TYPE::STATUS_LOW_POWER_);
  144. }
  145. else if(loc.m_callinfo == 0x80)
  146. {
  147. m_card->do_status(STA_TYPE::STATUS_HELP_);
  148. }
  149. m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop,loc);
  150. }
  151. };
  152. struct card_area
  153. {
  154. card_area()
  155. {
  156. m_site_area.reset(new site_area_hover);
  157. m_area_tool.reset(new area_tool);
  158. }
  159. std::shared_ptr<site_area_hover> m_site_area=nullptr;
  160. std::shared_ptr<area_tool> m_area_tool=nullptr;
  161. };
  162. struct person:card_location_base,private card_area
  163. {
  164. std::weak_ptr<monkey_person> m_monkeyPerson;
  165. person(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid)
  166. :card_location_base(type,cardid,needdisplay,t,deptid)
  167. {
  168. m_message_handle = new card_message_handle(this);
  169. }
  170. void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  171. {
  172. m_message_handle->on_message(loop,loc,is_history);
  173. }
  174. virtual void site_hover(int sid)
  175. {
  176. m_site_area->on_point(m_id,sid,0);
  177. }
  178. virtual void do_business(const point &pt)
  179. {
  180. //区域
  181. m_area_tool->on_point(m_id,pt,m_speed,m_type);
  182. //考勤
  183. m_site_area->on_point(m_id,0,this);
  184. //
  185. }
  186. //人卡升井后该线程要停掉
  187. //手动升级需要补全区域得同时,需要进区域走区域业务
  188. void set(ev::dynamic_loop * loop)
  189. {
  190. log_info("cardid_id %d",m_id);
  191. m_loop = loop;
  192. m_timer.set(*m_loop);
  193. m_timer.set<person,&person::on_timer>(this);
  194. m_timer.start(1,1);
  195. }
  196. void reset(std::shared_ptr<monkey_person> mp)
  197. {
  198. m_monkeyPerson = mp;
  199. }
  200. ~person(){}
  201. private:
  202. void on_timer()
  203. {
  204. YA::_CARD_POS_ cp;
  205. point pt = getSmoothPoint();
  206. const auto lm = m_area_tool->getLandmark();
  207. cp.enter_area_time = std::get<0>(lm);
  208. cp.rec_time = std::get<1>(lm);
  209. cp.map_id = std::get<2>(lm);
  210. cp.area_id = std::get<3>(lm);
  211. cp.landmark_id = std::get<4>(lm);
  212. cp.lm_direction = std::get<5>(lm);
  213. cp.landmark_dis=std::get<6>(lm);
  214. cp.biz_stat = state();
  215. upt_card_pos(cp,pt);
  216. }
  217. int state()
  218. {
  219. int status = get_stat();
  220. if(status == STATUS_NORMAL)
  221. {
  222. //超时
  223. }
  224. return status;
  225. }
  226. point getSmoothPoint()
  227. {
  228. point pt;
  229. loc_point lp = m_smo_tool->smooth_strategy();
  230. m_speed = lp.m_speed;
  231. m_stat = lp.m_stat;
  232. pt.x = lp.x;
  233. pt.y = -lp.y;
  234. if(auto p = m_monkeyPerson.lock() )
  235. {
  236. if(p->is_on_bus())
  237. {
  238. m_stat = 7;
  239. pt = p->getPoint();
  240. }
  241. }
  242. return pt;
  243. }
  244. };
  245. struct car:card_location_base,private card_area
  246. {
  247. int m_vehicle_category_id=0;
  248. car(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,int32_t categoryid)
  249. :card_location_base(type,cardid,needdisplay,t,deptid)
  250. ,m_vehicle_category_id(categoryid)
  251. {
  252. m_message_handle=new card_message_handle(this);
  253. }
  254. virtual void site_hover(int sid)
  255. {
  256. m_site_area->on_point(m_id,sid,0);
  257. }
  258. virtual void do_business(const point &pt)
  259. {
  260. m_area_tool->on_point(m_id,pt,m_speed,m_type);
  261. m_site_area->on_point(m_id,0,this);
  262. }
  263. void set(ev::dynamic_loop * loop)
  264. {
  265. log_info("cardid_id %d",m_id);
  266. m_loop = loop;
  267. m_timer.set(*m_loop);
  268. m_timer.set<car,&car::on_timer>(this);
  269. m_timer.start(1,1);
  270. }
  271. ~car(){}
  272. private:
  273. void on_timer()
  274. {
  275. make_package();
  276. }
  277. int statbiz(int32_t special_id)
  278. {
  279. int status = get_stat();
  280. if(status==STATUS_NORMAL)
  281. {
  282. //超速
  283. //超时
  284. //顺序不能变
  285. }
  286. if(status == STATUS_LOST)
  287. {
  288. if(!m_area_tool->special_area())
  289. {
  290. special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
  291. log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
  292. if(special_id != -1)
  293. m_area_tool->change_area(m_id,m_speed,m_type,special_id);//自动拖车
  294. }
  295. }
  296. return status;
  297. }
  298. void make_package()
  299. {
  300. int32_t special_id=-1;
  301. YA::_CARD_POS_ cp;
  302. point pt = getSmoothPoint();
  303. const auto lm = m_area_tool->getLandmark();
  304. cp.enter_area_time = std::get<0>(lm);
  305. cp.rec_time = std::get<1>(lm);
  306. cp.map_id = std::get<2>(lm);
  307. cp.area_id = std::get<3>(lm);
  308. cp.landmark_id = std::get<4>(lm);
  309. cp.lm_direction = std::get<5>(lm);
  310. cp.landmark_dis=std::get<6>(lm);
  311. int32_t biz_stat=statbiz(special_id);
  312. cp.biz_stat=biz_stat;
  313. //cp.down_time
  314. //cp.work_time;
  315. //cp.level_id;
  316. //for now
  317. cp.is_on_duty=1;
  318. upt_card_pos(cp,pt);
  319. if(biz_stat==STATUS_LOST && special_id != -1 && m_display==1)
  320. {
  321. cp.area_id = special_id;
  322. swsClientMgr.SendSpecialAreaProcess(cp);
  323. }
  324. }
  325. point getSmoothPoint()
  326. {
  327. loc_point lp = m_smo_tool->smooth_strategy();
  328. m_speed = lp.m_speed;
  329. m_stat = lp.m_stat;
  330. lp.y = -lp.y;
  331. return lp;
  332. }
  333. };
  334. loc_tool_main one_ct_message_handle::m_loc_tool;
  335. uint64_t card_list::getId(uint32_t cardid,uint64_t type)
  336. {
  337. return type<<32|cardid;
  338. }
  339. void card_list::init_vehicle()
  340. {
  341. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  342. std::string strategy = config.get("person.strategy","car1");
  343. const char *sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
  344. ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
  345. vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
  346. vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed \
  347. FROM dat_vehicle_extend ve \
  348. LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
  349. LEFT JOIN dat_card c ON ve.card_id = c.card_id \
  350. LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
  351. LEFT JOIN dat_group g ON ve.group_id = g.group_id \
  352. LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
  353. LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
  354. WHERE c.card_type_id = 2 AND c.state_id = 0;";
  355. std::string Error;
  356. YADB::CDBResultSet DBRes;
  357. sDBConnPool.Query(sql,DBRes,Error);
  358. int nCount = DBRes.GetRecordCount( Error );
  359. if (nCount > 0)
  360. {
  361. log_info( "init_staffer. The record count=%d\n", nCount );
  362. while ( DBRes.GetNextRecod(Error) )
  363. {
  364. unsigned int vehicle_id = 0;
  365. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  366. std::string card_id;
  367. DBRes.GetField( "card_id",card_id, Error );
  368. uint32_t vsid = atoi(card_id.substr(3).c_str());
  369. unsigned int card_type_id = 0;
  370. DBRes.GetField( "card_type_id",card_type_id, Error );
  371. int dept_id = 0;
  372. DBRes.GetField( "dept_id",dept_id, Error );
  373. int group_id = 0;
  374. DBRes.GetField( "group_id",group_id, Error );
  375. int vehicle_type_id = 0;
  376. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  377. int vehicle_level_id = 0;
  378. DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );
  379. int need_display = 0;
  380. DBRes.GetField( "need_display",need_display, Error );
  381. int power_alarm = 0;
  382. DBRes.GetField( "power_alarm",power_alarm, Error );
  383. int vehicle_category_id = 0;
  384. DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );
  385. int bigger_car_flag= 0;
  386. DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );
  387. double over_speed= 0;
  388. DBRes.GetField( "over_speed",over_speed, Error );
  389. //for now
  390. vehicle_id = vsid;
  391. std::shared_ptr<card_location_base> clb = std::make_shared<car>(strategy,vehicle_id,need_display,card_type_id,dept_id,vehicle_category_id);
  392. uint64_t cardid = getId(vehicle_id,2);
  393. log_info("cardId:%llu,vehicle_id:%d dept_id:%d,need_display:%d---cardid:%s,categoryid:%d",cardid,vehicle_id,dept_id,need_display,card_id.c_str(),vehicle_category_id);
  394. map.insert({cardid,clb});
  395. }
  396. }
  397. card_list::instance()->add(map);
  398. }
  399. void card_list::init_staffer()
  400. {
  401. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  402. std::string strategy = config.get("person.strategy","person1");
  403. const char *sql = "SELECT staff_id, s.card_id, c.card_type_id, s.dept_id, s.group_id, s.occupation_id, \
  404. ol.occupation_level_id,s.worktype_id,s.need_display \
  405. FROM dat_staff_extend s \
  406. LEFT JOIN dat_card c ON s.card_id = c.card_id \
  407. LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
  408. LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
  409. WHERE c.card_type_id = 1 AND s.duty_id = 0 AND c.state_id = 0;";
  410. std::string Error;
  411. YADB::CDBResultSet DBRes;
  412. sDBConnPool.Query(sql,DBRes,Error);
  413. int nCount = DBRes.GetRecordCount( Error );
  414. if (nCount > 0)
  415. {
  416. log_info( "init_staffer. The record count=%d\n", nCount );
  417. while ( DBRes.GetNextRecod(Error) )
  418. {
  419. unsigned int staff_id = 0;
  420. DBRes.GetField( "staff_id",staff_id, Error );
  421. std::string card_id;
  422. DBRes.GetField( "card_id",card_id, Error );
  423. uint32_t vsid = atoi(card_id.substr(3).c_str());
  424. unsigned int card_type_id = 0;
  425. DBRes.GetField( "card_type_id",card_type_id, Error );
  426. int dept_id = 0;
  427. DBRes.GetField( "dept_id",dept_id, Error );
  428. int group_id = 0;
  429. DBRes.GetField( "group_id",group_id, Error );
  430. int occupation_id = 0;
  431. DBRes.GetField( "occupation_id",occupation_id, Error );
  432. int occupation_level_id = 0;
  433. DBRes.GetField( "occupation_level_id",occupation_level_id, Error );
  434. int need_display = 0;
  435. DBRes.GetField( "need_display",need_display, Error );
  436. //for now;
  437. staff_id = vsid;
  438. std::shared_ptr<card_location_base> clb = std::make_shared<person>(strategy,staff_id,need_display,card_type_id,dept_id);
  439. uint64_t cardid = getId(staff_id,1);
  440. log_info("cardId:%llu,staff_id:%d dept_id:%d,need_display:%d--c-ard:%s",cardid,staff_id,dept_id,need_display,card_id.c_str());
  441. map.insert({cardid,clb});
  442. }
  443. }
  444. card_list::instance()->add(map);
  445. }
  446. void card_list::init_card_from_db()
  447. {
  448. init_staffer();
  449. init_vehicle();
  450. }
  451. void card_list::on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
  452. {
  453. //std::shared_ptr<card_location_base>c=get(loc.m_card_id);
  454. uint64_t cardid = getId(loc.m_card_id,loc.m_card_type);
  455. const auto c=get(cardid);
  456. if(c==nullptr)
  457. {
  458. log_warn("数据库中未定义该卡的信息,card_id=%d", loc.m_card_id);
  459. return;
  460. }
  461. log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
  462. 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);
  463. c->on_message(loop,loc,is_history);
  464. }
  465. //-----------------card_location_base..
  466. card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t deptid)
  467. :card(id,dis,t,deptid)
  468. {
  469. select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
  470. }
  471. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  472. {
  473. //ct timestamp;
  474. m_ct = lm[0].m_card_ct;
  475. m_time = lm[0].m_loc_time;
  476. loc_point pt = m_sel_tool->select_solution(vp,lm);
  477. pt.y=-pt.y;
  478. if(pt.m_useless)
  479. {
  480. x = pt.x;
  481. y = pt.y;
  482. log_info("card_id:%d,ct:%d,timestamp:%llu, loc_point,x:%.2f,y:%.2f ",m_id,m_ct,m_time,pt.x,pt.y);
  483. do_business(pt);
  484. }
  485. }
  486. void card_location_base::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  487. {
  488. if(m_loop == nullptr && loop != nullptr)
  489. set(loop);
  490. m_message_handle->on_message(loop,loc,is_history);
  491. }
  492. //前端推送位置函数
  493. void card_location_base::upt_card_pos(YA::_CARD_POS_ &cp,const point &pt)
  494. {
  495. cp.x = pt.x;
  496. cp.y = pt.y;
  497. cp.z = pt.z;
  498. cp.Type=m_type;
  499. cp.ID = m_id;
  500. cp.speed = abs(ceil(m_speed));
  501. cp.running_stat = m_stat;
  502. cp.dept_id = m_deptid;
  503. cp.display=m_display;
  504. cp.rec_time=m_time;
  505. swsTimerThrd.upt_card_pos(cp);
  506. }
  507. int card_location_base::get_stat()
  508. {
  509. //盲区>呼救>呼叫>超时>超速>正常
  510. int status=STATUS_NORMAL;
  511. if(false)
  512. status = STATUS_CALL;
  513. if(false)
  514. status = STATUS_HELP;
  515. uint64_t now = time(0)*1000;
  516. status=(now-m_time>CARD_LOST_TIME_OUT)?STATUS_LOST:STATUS_NORMAL;
  517. return status;
  518. }
  519. card_location_base::~card_location_base()
  520. {
  521. }
  522. template<> std::shared_ptr<card_list>
  523. single_base<card_list, uint64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list>();