card.cpp 15 KB

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