card_base.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. #include <memory>
  2. #include <message.h>
  3. #include "card_message_handle.h"
  4. #include "card_person.h"
  5. #include "card_car.h"
  6. #include "config_file.h"
  7. #include "select_tool.h"
  8. #include "module_service/module_mgr.h"
  9. #include "websocket/wsClientMgr.h"
  10. #include "websocket/wsTimerThread.h"
  11. //#include "three_rates.h"
  12. #include "his_location.h"
  13. #include "event.h"
  14. #include "module_service/module_call.h"
  15. #include "mine.h"
  16. #include "common_tool.h"
  17. #include "ant.h"
  18. #include "area.h"
  19. #include "loc_point.h"
  20. #include "loc_message.h"
  21. #include "module_service/module_traffic_light_manager.h"
  22. #include "db/db_tool.h"
  23. #include "tool_time.h"
  24. #include "tool_byte.h"
  25. #include "struct_def.h"
  26. extern config_file config;
  27. card_location_base::card_location_base(const std::string&type,uint32_t id,uint16_t dis,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid)
  28. :card(id, dis, t, deptid, level_id, cid)
  29. ,m_display_old(dis)
  30. {
  31. select_tool_manage::instance()->create_tool(type, m_sel_tool, m_smo_tool);
  32. m_his_location_card.reset(new location_card(m_id, m_type, cid));
  33. m_cb_pdoa.set_capacity(5);
  34. m_cb_tof.set_capacity(5);
  35. m_cb_point.set_capacity(5);
  36. for(int index = 0; index < 5; ++index){
  37. m_cb_pdoa.push_back(100);
  38. m_cb_tof.push_back(0);
  39. m_cb_point.push_back(point(0,0));
  40. }
  41. m_cb_stream_idx.set_capacity(2);
  42. m_cb_stream_idx.push_back(0);
  43. m_cb_stream_idx.push_back(0);
  44. }
  45. void card_location_base::do_status(int st)
  46. {
  47. time_t now = time(0);
  48. //bool help_flag = false;
  49. log_info("[help-battery] card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  50. // 呼救
  51. if((m_help_bit & 1) && (st & STATUS_HELP))
  52. {
  53. // 1111111111
  54. // ^
  55. m_help_last_time = now;
  56. }
  57. else if((m_help_bit & 1) && (st & STATUS_HELP)==0)
  58. {
  59. // 11111111100000
  60. // ^
  61. m_help_bit<<=1;
  62. }
  63. else if((m_help_bit & 1)==0 && (st & STATUS_HELP))
  64. {
  65. // 00000000011111
  66. // ^
  67. // 呼救开始
  68. if((m_help_bit&0x3)==2)
  69. {
  70. //log_warn("[help-battery] handle_m_help, card_id=%d",m_id);
  71. //help_flag=true;
  72. }
  73. m_help_last_time = now;
  74. m_help_bit<<=1;
  75. m_help_bit|=1;
  76. log_info("[help-battery] begin help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  77. }
  78. else
  79. {
  80. // 11111111100000
  81. // ^
  82. // 呼救结束后持续1分钟
  83. if(now - m_help_last_time > 60)
  84. {
  85. m_help_bit = 0;
  86. log_info("[help-battery] end help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  87. }
  88. }
  89. /*if(!help_flag)
  90. {
  91. st = st & (0xFFFFFFFF ^ STATUS_HELP);
  92. }*/
  93. if((STATUS_POWER_LOWER_SERIOUS & st) != 0)
  94. {
  95. m_pwr_stat = STATUS_POWER_LOWER_SERIOUS;
  96. }
  97. else
  98. {
  99. m_pwr_stat = 0;
  100. }
  101. module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
  102. }
  103. //写入历史轨迹
  104. void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclose /*= false*/)
  105. {
  106. int area_id = 0, map_id = 0, site_id = 0;
  107. double scale = 0.0;
  108. if(auto site_ptr = get_area_tool()->m_site)
  109. {
  110. area_id = site_ptr->m_area_id;
  111. map_id = site_ptr->m_map_id;
  112. site_id = site_ptr->m_id;
  113. scale = site_ptr->m_scale;
  114. }
  115. auto mine_ptr = get_mine_tool();
  116. if(m_time < mine_ptr->get_down_time())
  117. {
  118. return;
  119. }
  120. m_his_location_card->push(t, pt, area_id, map_id, site_id, bclose);
  121. m_his_location_card->insert(t, pt, area_id, map_id, site_id, scale);
  122. }
  123. //坐标点输入业务入口
  124. /*
  125. 定位的逻辑处理
  126. 车辆和人员都是走这个接口,然后分别进行处理。
  127. 输入:
  128. vp 定位结果(硬件的原始位置数据)
  129. lm 定位消息缓存区,用于tof,pdoa
  130. is_v_map 是否为车辆地图
  131. 输出:
  132. point m_v_point; //车辆定位系统的定位结果
  133. 定位结果相关的一些成员变量如:x、y
  134. 姿态:m_stat
  135. 人员定位结果(包含人员地图定位结果(x、y)和车辆地图定位结果m_v_point)发给WEB的server
  136. 车辆将定位结果(车辆地图定位结果)和姿态发送给WEB的server
  137. Added by zengminguo
  138. */
  139. void card_location_base::on_location(const std::vector<point>&vp, const std::vector<loc_message> &lm, bool is_v_map)
  140. {
  141. m_isVehicleMap = false;
  142. if (is_vehicle())
  143. {
  144. if (!is_v_map)
  145. {
  146. return;
  147. }
  148. }
  149. if (is_person())
  150. {
  151. if (is_v_map)
  152. {
  153. m_isVehicleMap = true;
  154. }
  155. }
  156. loc_point pt = m_sel_tool->select_solution(vp, lm);
  157. //pt.y = pt.y;
  158. auto site_ptr = get_area_tool()->m_site;
  159. int sid = 0;
  160. if(site_ptr)
  161. sid = site_ptr->m_id;
  162. if(pt.m_useless)
  163. {
  164. if (is_person())
  165. {
  166. if (!is_v_map)
  167. {
  168. x = tool_other::round(pt.x, 3);
  169. y = tool_other::round(pt.y, 3);
  170. }
  171. else
  172. {
  173. m_v_point.x = tool_other::round(pt.x, 3);
  174. m_v_point.y = tool_other::round(pt.y, 3);
  175. log_info("useful loc m_v_point: type=%d, card=%d, site=%d, ct=%d, timestamp=%llu, x=%f, y=%f, speed=%.2f, acc=%.2f", m_type, m_id, sid, m_ct, m_time, m_v_point.x, m_v_point.y, m_speed, lm[0].m_acc);
  176. return;
  177. }
  178. }
  179. else if (is_vehicle())
  180. {
  181. if (is_v_map)
  182. {
  183. x = tool_other::round(pt.x, 3);
  184. y = tool_other::round(pt.y, 3);
  185. }
  186. else
  187. {
  188. return;
  189. }
  190. }
  191. double acc = lm[0].m_acc;
  192. m_acc = lm[0].m_acc;
  193. log_info("useful loc point: type=%d, card=%d, site=%d, ct=%d, timestamp=%llu, x=%f, y=%f, speed=%.2f, acc=%.2f",m_type,m_id,sid,m_ct,m_time,x,y, m_speed, acc);
  194. do_business(lm.front().m_sit, pt, acc);
  195. // 呼救128,正常0
  196. int val = (lm[0].m_rav?STATUS_HELP:STATUS_NORMAL);
  197. if(m_battery_value > 3){
  198. val += STATUS_POWER_NORMAL;
  199. }else{
  200. val += STATUS_POWER_LOWER_SERIOUS;
  201. }
  202. log_info("[help-battery] card_id=%d, battery=%d, val=%d", m_id, m_battery_value, val);
  203. do_status(val);
  204. // 姿态判断
  205. if (is_vehicle())
  206. {
  207. if (m_stat_last == ENUM_STATUS_BACK)
  208. {
  209. m_stat = ENUM_STATUS_BACK;
  210. }
  211. if (m_speed > 1 && m_stat_last != ENUM_STATUS_BACK)
  212. {
  213. m_stat = ENUM_STATUS_FORWARD;
  214. }
  215. if (m_stat_last == ENUM_STATUS_FORWARD ||
  216. m_stat_last == ENUM_STATUS_SPEED_UP ||
  217. m_stat_last == ENUM_STATUS_SPEED_DOWN
  218. )
  219. {
  220. m_stat = ENUM_STATUS_FORWARD;
  221. }
  222. if (m_stat_last == ENUM_STATUS_LEFT ||
  223. m_stat_last == ENUM_STATUS_RIGHT)
  224. {
  225. m_stat = m_statusBeforeTurning;
  226. }
  227. if ((lm[0].m_acc <= -0.009 && m_stat_last == ENUM_STATUS_STATIC) || m_stat_last == ENUM_STATUS_BACK)
  228. {
  229. m_stat = ENUM_STATUS_BACK;
  230. }
  231. if (lm[0].m_acc >= 0.009 &&
  232. (m_stat_last == ENUM_STATUS_FORWARD ||
  233. m_stat_last == ENUM_STATUS_SPEED_UP ||
  234. m_stat_last == ENUM_STATUS_SPEED_DOWN ||
  235. m_stat_last == ENUM_STATUS_STATIC))
  236. {
  237. m_stat = ENUM_STATUS_SPEED_UP;
  238. }
  239. if (lm[0].m_acc <= -0.009 &&
  240. (m_stat_last == ENUM_STATUS_FORWARD ||
  241. m_stat_last == ENUM_STATUS_SPEED_UP ||
  242. m_stat_last == ENUM_STATUS_SPEED_DOWN ||
  243. (m_speed_last - m_speed > 0.5)))
  244. {
  245. m_stat = ENUM_STATUS_SPEED_DOWN;
  246. }
  247. if (lm[0].m_rav > 0)
  248. {
  249. if (m_stat_last == ENUM_STATUS_LEFT)
  250. {
  251. }
  252. else
  253. {
  254. m_statusBeforeTurning = m_stat_last;
  255. }
  256. m_stat = ENUM_STATUS_LEFT;
  257. }
  258. if (lm[0].m_rav < 0)
  259. {
  260. if (m_stat_last == ENUM_STATUS_RIGHT)
  261. {
  262. }
  263. else
  264. {
  265. m_statusBeforeTurning = m_stat_last;
  266. }
  267. m_stat = ENUM_STATUS_RIGHT;
  268. }
  269. if (m_stat_last != ENUM_STATUS_BACK)
  270. {
  271. if (lm[0].m_acc == 0 && lm[0].m_rav == 0 && m_speed <= 1)
  272. {
  273. m_stat = ENUM_STATUS_STATIC;
  274. }
  275. }
  276. else
  277. {
  278. if (lm[0].m_acc == 0 && lm[0].m_rav == 0 && m_speed <= 0.3)
  279. {
  280. m_stat = ENUM_STATUS_STATIC;
  281. }
  282. }
  283. switch (m_stat)
  284. {
  285. case ENUM_STATUS_STATIC:
  286. log_info("[car-motion] card_id=%d, val=%d 静止 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  287. break;
  288. case ENUM_STATUS_MOVE:
  289. log_info("[car-motion] card_id=%d, val=%d 移动 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  290. break;
  291. case ENUM_STATUS_FORWARD:
  292. log_info("[car-motion] card_id=%d, val=%d 前进 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  293. break;
  294. case ENUM_STATUS_BACK:
  295. log_info("[car-motion] card_id=%d, val=%d 后退 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  296. break;
  297. case ENUM_STATUS_LEFT:
  298. log_info("[car-motion] card_id=%d, val=%d 左转 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  299. break;
  300. case ENUM_STATUS_RIGHT:
  301. log_info("[car-motion] card_id=%d, val=%d 右转 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  302. break;
  303. case ENUM_STATUS_SPEED_UP:
  304. log_info("[car-motion] card_id=%d, val=%d 加速 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  305. break;
  306. case ENUM_STATUS_SPEED_DOWN:
  307. log_info("[car-motion] card_id=%d, val=%d 减速 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  308. break;
  309. default:
  310. log_info("[car-motion] card_id=%d, val=%d 未知 lm[0].m_acc=%.2f lm[0].m_rav=%d m_speed=%.2f", m_id, m_stat, lm[0].m_acc, lm[0].m_rav, m_speed);
  311. break;
  312. }
  313. m_speed_last = m_speed;
  314. m_stat_last = m_stat;
  315. }
  316. }
  317. else
  318. {
  319. log_warn("坐标不可用:site=%d, type=%d, card=%d, ct=%d, x=%f, y=%f", sid, m_type, m_id, m_ct, pt.x, pt.y);
  320. }
  321. }
  322. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  323. {
  324. m_ct = loc.m_card_ct;
  325. m_time = loc.m_time_stamp;
  326. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  327. if(!site_ptr)
  328. {
  329. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  330. return;
  331. }
  332. auto area_tool=get_area_tool();
  333. area_tool->set_site(site_ptr);
  334. handle_message(loc.m_card_ct,loc.m_batty_status);
  335. if(site_ptr->is_up_site())
  336. {
  337. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  338. area_tool->on_point(shared_from_this(),point(1,1));
  339. this->site_hover(loc.m_site_id);
  340. }
  341. else
  342. {
  343. if(site_ptr->is_path_empty())
  344. {
  345. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  346. return;
  347. }
  348. m_message_handle->on_message(loop,loc,is_history);
  349. }
  350. }
  351. /*
  352. * tdoa 调用算法库定位
  353. * */
  354. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  355. {
  356. log_info("[tdoa] start calc location.");
  357. m_ct = loc.m_card_msg.m_sync_num;
  358. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  359. if(!site_ptr){
  360. log_warn("[tdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_msg.m_site_id, m_id, loc.m_card_msg.m_time_stamp);
  361. return;
  362. }
  363. auto area_tool=get_area_tool();
  364. area_tool->set_site(site_ptr);
  365. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  366. if(site_ptr->is_up_site())
  367. {}
  368. else{
  369. m_message_handle->on_message(loop, loc, is_history);
  370. }
  371. }
  372. /*
  373. * pdoa 调用算法库定位
  374. *
  375. * */
  376. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  377. {
  378. m_ct = loc.m_card_ct;
  379. m_time = loc.m_time_stamp;
  380. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  381. if(!site_ptr){
  382. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  383. return;
  384. }
  385. site_ptr->m_loc_time = time(0);
  386. // 基站覆盖距离检测
  387. do_site_coverage(loc);
  388. auto area_tool=get_area_tool();
  389. area_tool->set_site(site_ptr);
  390. m_battery_value = loc.m_batty_status; // 电量值
  391. handle_message(loc.m_card_ct, loc.m_batty_status);
  392. if(site_ptr->is_up_site()){
  393. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  394. area_tool->on_point(shared_from_this(), point(1,1));
  395. this->site_hover(loc.m_site_id);
  396. m_last_site_id = site_ptr->id();
  397. //m_last_down_stream_idx = site_ptr->m_down_stream_idx;
  398. m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
  399. }
  400. else{
  401. char sql[1024] = {0};
  402. snprintf(sql, 1024, "insert into his_distance_reader(card_type, ident, reader_id, dist, dir, cur_time) values(%d, %d, %d, %.2f, %d, '%s')", loc.m_card_type, loc.m_card_id, loc.m_site_id, loc.m_tof*15.65*2.996*1E-4, (tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset)>=0?1:-1), tool_time::to_str(time(NULL)).c_str());
  403. db_tool::PushAsync(sql);
  404. log_info("[sql] %s", sql);
  405. do_site_locate_event(loc);
  406. do_card_locate_event(loc);
  407. m_message_handle->on_message(loop, loc, is_history);
  408. }
  409. }
  410. //前端推送位置函数.
  411. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  412. {
  413. point _p;
  414. if(pt.empty())
  415. {
  416. _p = *this;
  417. pt = _p;
  418. }
  419. else
  420. _p = pt;
  421. cp.Type = m_type;
  422. cp.ID = m_id;
  423. cp.speed = abs(ceil(m_speed));
  424. cp.x = tool_other::round(_p.x,3);
  425. cp.y = tool_other::round(_p.y,3);
  426. cp.running_stat = m_stat;
  427. cp.dept_id = m_deptid;
  428. cp.display = m_display;
  429. cp.rec_time = m_time;
  430. cp.level_id = m_level_id;
  431. cp.battery_val = m_battery_value;
  432. if(m_pwr_stat > 0){
  433. cp.battery_stat = (m_pwr_stat<3?1:0);
  434. }
  435. cp.m_freq = m_freq;
  436. swsTimerThrd.upt_card_pos(cp);
  437. }
  438. void card_location_base::del_card_pos()
  439. {
  440. sys::_CARD_POS_ cp;
  441. cp.ID = m_id;
  442. cp.Type=m_type;
  443. swsTimerThrd.del_card_pos(cp);
  444. }
  445. int card_location_base::get_stat()
  446. {
  447. //盲区>呼救>呼叫>超时>超速>正常
  448. uint64_t now = time(0)*1000;
  449. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  450. if(tlost>CARD_LOST_TIME_OUT)
  451. {
  452. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  453. return STATUS_LOST;
  454. }
  455. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  456. {
  457. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  458. }
  459. else if(CALL_NONE != get_mine_tool()->m_status_call)
  460. {
  461. return STATUS_CALL;
  462. }
  463. else if(is_person())
  464. {
  465. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  466. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  467. return STATUS_AREA_OVER_TIME;
  468. }
  469. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  470. {
  471. return STATUS_OVER_SPEED;
  472. }
  473. return STATUS_NORMAL;
  474. }
  475. void card_location_base::clear()
  476. {
  477. // uint16_t m_display; //1显示0不显示,往前端推送
  478. m_speed=0; //速度
  479. m_stat=0; //运动静止状态
  480. //m_ct; //ct
  481. m_time=0; //时间戳
  482. //pdoa location info clear
  483. m_buff_size = 0;
  484. m_last_recv_time = 0;
  485. m_last_point = point(0,0);
  486. m_last_site_id = 0;
  487. m_last_site_dir = -1;
  488. m_last_ct = 0;
  489. m_last_dist = 0.0;
  490. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  491. m_last_over_site = false;
  492. m_cache_nums = 0;
  493. m_cb_pdoa.clear();
  494. m_cb_tof.clear();
  495. m_cb_point.clear();
  496. }
  497. /*void card_location_base::put_three_rates(card_pos & cp)
  498. {
  499. if(!three_rates_flag)
  500. return;
  501. cp.rec_time = m_time;
  502. cp.type = m_type;
  503. cp.id = m_id;
  504. cp.identifier_id= m_cid;
  505. cp.running_stat = m_stat;
  506. cp.final_v = m_speed;
  507. cp.dpt_id = m_deptid;
  508. std::shared_ptr<area_tool> _areatool = get_area_tool();
  509. if(nullptr != _areatool && nullptr != _areatool->m_site)
  510. {
  511. cp.reader_x = _areatool->m_site->x;
  512. cp.reader_y = _areatool->m_site->y;
  513. cp.reader_id = _areatool->m_site->m_id;
  514. }
  515. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  516. three_rates::get_instance()->put(cp);
  517. }*/
  518. void card_location_base::put_traffic_light(card_pos& cp)
  519. {
  520. if(!traffic_light_flag){
  521. return;
  522. }
  523. light_message lm;
  524. lm.m_cmd = cmd_card_data;
  525. lm.m_pos.x = cp.x;
  526. lm.m_pos.y = cp.y;
  527. lm.m_pos.z = cp.z;
  528. lm.m_pos.m_card_id = m_id;
  529. lm.m_pos.m_type = m_type;
  530. lm.m_pos.m_site_id = cp.reader_id;
  531. auto site_ptr = get_area_tool()->m_site;
  532. if(site_ptr){
  533. lm.m_pos.m_area_id = site_ptr->m_area_id;
  534. }
  535. //lm.m_pos.m_bigger = 0;
  536. lm.m_pos.m_speed = m_speed;
  537. log_info("[traffic_light] put locate info into traffic light module, card_id=%d, ctype=%d, x=%.2f, y=%.2f, site_id=%d", m_id, m_type, x, y, cp.reader_id);
  538. traffic_light_manager::instance()->put(lm);
  539. }
  540. bool card_location_base::is_person() const
  541. {
  542. return tool_other::is_person(m_type);
  543. }
  544. bool card_location_base::is_vehicle() const
  545. {
  546. return tool_other::is_vehicle(m_type);
  547. }
  548. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  549. m_cid = cid;
  550. m_type = type;
  551. m_deptid = deptid;
  552. m_level_id = level_id;
  553. m_his_location_card->set_cid(cid);
  554. }
  555. card_location_base::~card_location_base()
  556. {
  557. }
  558. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  559. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  560. {
  561. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  562. }
  563. /*
  564. * @brief 创建车辆对象
  565. * @param const std::string& type
  566. * @param uint32_t cardid 卡id
  567. * @param uint16_t needdisplay 是否需要显示
  568. * @param int16_t t
  569. * @param int32_t deptid 部门id
  570. * @param int32_t categoryid
  571. * @param int type_id 卡类型id
  572. * @param int32_t level_id 职级id
  573. * @param uint32_t cid
  574. * @return 车辆对象
  575. * @note
  576. * @bug
  577. * @warning
  578. * */
  579. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  580. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  581. {
  582. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  583. }
  584. /*
  585. * @brief 基站覆盖距离检测
  586. * @param message_pdoa_locinfo& loc 卡数据
  587. * @return 无
  588. * @note
  589. * @bug
  590. * @warning
  591. * */
  592. void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
  593. {
  594. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  595. if(!site_ptr){
  596. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  597. return;
  598. }
  599. double pdoa = tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset);
  600. double dist = loc.m_tof*15.65*2.996*1e-4;
  601. bool is_update = false;
  602. if(pdoa > 0){
  603. if(dist > site_ptr->m_coverage[0].m_distance){
  604. site_ptr->m_coverage[0].m_distance = dist;
  605. site_ptr->m_coverage[0].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  606. site_ptr->m_coverage[0].m_time = time(0);
  607. is_update = true;
  608. }
  609. }else{
  610. if(dist > site_ptr->m_coverage[1].m_distance){
  611. site_ptr->m_coverage[1].m_distance = dist;
  612. site_ptr->m_coverage[1].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  613. site_ptr->m_coverage[1].m_time = time(0);
  614. is_update = true;
  615. }
  616. }
  617. if(is_update){
  618. char sql[1024] = {0};
  619. snprintf(sql, 1024, "REPLACE INTO his_reader_coverage(reader_id, plus_dist, plus_card_id, plus_occur_time, minus_dist, minus_card_id, minus_occur_time, last_update) VALUES(%d,%.2f,'%s','%s',%.2f,'%s','%s','%s');", site_ptr->m_id, site_ptr->m_coverage[0].m_distance, site_ptr->m_coverage[0].m_cid.c_str(), tool_time::to_str(site_ptr->m_coverage[0].m_time).c_str(), site_ptr->m_coverage[1].m_distance, site_ptr->m_coverage[1].m_cid.c_str(), tool_time::to_str(site_ptr->m_coverage[1].m_time).c_str(), tool_time::to_str(time(0)).c_str());
  620. db_tool::PushAsync(sql);
  621. logn_info(2, "[sql] %s", sql);
  622. }
  623. }
  624. /*
  625. * @brief 卡定位异常检测
  626. * @param message_pdoa_locinfo& loc 卡数据
  627. * @return 无
  628. * @note
  629. * @bug
  630. * @warning
  631. * */
  632. void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
  633. {
  634. bool status = false;
  635. status = ((loc.m_tof == 0)?true:false);
  636. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  637. event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);
  638. //log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
  639. }
  640. /*
  641. * @brief 基站定位异常检测
  642. * @param message_pdoa_locinfo& loc 卡数据
  643. * @return 无
  644. * @note
  645. * @bug
  646. * @warning
  647. * */
  648. void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
  649. {
  650. //log_info("[site-locate-event] cur_ct=%d, ct=%d, m_last_ct=%d, m_last_site_id=%d, loc.m_site_id=%d", loc.m_card_ct, ct, m_last_ct, m_last_site_id, loc.m_site_id);
  651. if(m_last_ct <= 0 || m_last_site_id == 0){
  652. return;
  653. }
  654. if(loc.m_site_id != m_last_site_id){
  655. return;
  656. }
  657. int dt = 0;
  658. if(loc.m_card_ct > m_last_ct){
  659. dt = loc.m_card_ct - m_last_ct;
  660. }else{
  661. dt = 65535 + loc.m_card_ct - m_last_ct;
  662. }
  663. event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
  664. log_info("[site-locate-event] site_id=%d, card_id=%d, ct=%d, dt=%d", loc.m_site_id, m_id, loc.m_card_ct, dt);
  665. }