1
0

card_base.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 "his_location.h"
  12. #include "event.h"
  13. #include "module_service/module_call.h"
  14. #include "mine.h"
  15. #include "common_tool.h"
  16. #include "ant.h"
  17. #include "area.h"
  18. #include "loc_point.h"
  19. #include "loc_message.h"
  20. #include "module_service/module_traffic_light_manager.h"
  21. #include "db/db_tool.h"
  22. #include "tool_time.h"
  23. #include "tool_byte.h"
  24. #include "struct_def.h"
  25. #include "load_raw.h"
  26. #include "tunnel.h"
  27. extern config_file config;
  28. 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)
  29. :card(id, dis, t, deptid, level_id, cid)
  30. ,m_display_old(dis)
  31. {
  32. select_tool_manage::instance()->create_tool(type, m_sel_tool, m_smo_tool);
  33. m_his_location_card.reset(new location_card(m_id, m_type, cid));
  34. m_cb_pdoa.set_capacity(5);
  35. m_cb_tof.set_capacity(5);
  36. m_cb_point.set_capacity(5);
  37. for(int index = 0; index < 5; ++index){
  38. m_cb_pdoa.push_back(100);
  39. m_cb_tof.push_back(0);
  40. m_cb_point.push_back(point(0,0));
  41. }
  42. m_cb_stream_idx.set_capacity(2);
  43. m_cb_stream_idx.push_back(0);
  44. m_cb_stream_idx.push_back(0);
  45. }
  46. void card_location_base::do_status(int st)
  47. {
  48. time_t now = time(0);
  49. //bool help_flag = false;
  50. log_info("[help-battery] card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  51. // 呼救
  52. if((m_help_bit & 1) && (st & STATUS_HELP))
  53. {
  54. // 1111111111
  55. // ^
  56. m_help_last_time = now;
  57. }
  58. else if((m_help_bit & 1) && (st & STATUS_HELP)==0)
  59. {
  60. // 11111111100000
  61. // ^
  62. m_help_bit<<=1;
  63. }
  64. else if((m_help_bit & 1)==0 && (st & STATUS_HELP))
  65. {
  66. // 00000000011111
  67. // ^
  68. // 呼救开始
  69. if((m_help_bit&0x3)==2)
  70. {
  71. //log_warn("[help-battery] handle_m_help, card_id=%d",m_id);
  72. //help_flag=true;
  73. }
  74. m_help_last_time = now;
  75. m_help_bit<<=1;
  76. m_help_bit|=1;
  77. log_info("[help-battery] begin help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  78. }
  79. else
  80. {
  81. // 11111111100000
  82. // ^
  83. // 呼救结束后持续1分钟
  84. if(now - m_help_last_time > 60)
  85. {
  86. m_help_bit = 0;
  87. log_info("[help-battery] end help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  88. }
  89. }
  90. /*if(!help_flag)
  91. {
  92. st = st & (0xFFFFFFFF ^ STATUS_HELP);
  93. }*/
  94. if((STATUS_POWER_LOWER_SERIOUS & st) != 0)
  95. {
  96. m_pwr_stat = STATUS_POWER_LOWER_SERIOUS;
  97. }
  98. else
  99. {
  100. m_pwr_stat = 0;
  101. }
  102. module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
  103. }
  104. //写入历史轨迹
  105. void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclose /*= false*/)
  106. {
  107. int area_id = 0, map_id = 0, site_id = 0;
  108. double scale = 0.0;
  109. if(auto site_ptr = get_area_tool()->m_site)
  110. {
  111. area_id = site_ptr->m_area_id;
  112. map_id = site_ptr->m_map_id;
  113. site_id = site_ptr->m_id;
  114. scale = site_ptr->m_scale;
  115. }
  116. auto mine_ptr = get_mine_tool();
  117. if(m_time < mine_ptr->get_down_time())
  118. {
  119. return;
  120. }
  121. m_his_location_card->push(t, pt, area_id, map_id, site_id, bclose);
  122. m_his_location_card->insert(t, pt, area_id, map_id, site_id, scale);
  123. }
  124. void card_location_base::make_his_location_simplify(uint64_t t, const loc_point & pt)
  125. {
  126. m_his_location_card->insert_simplify(pt, t);
  127. }
  128. void card_location_base::make_his_location_cell_card(uint64_t t, const loc_point & pt)
  129. {
  130. m_his_location_card->insert_cell_card(pt, t);
  131. }
  132. void card_location_base::make_his_location_cell_reader(uint64_t t, const loc_point & pt)
  133. {
  134. m_his_location_card->insert_cell_reader(pt, t);
  135. }
  136. float get_pdoa(const double poa[], const double& offset)
  137. {
  138. if (poa == nullptr) {
  139. return -10.0;
  140. }
  141. float poa1 = poa[0];
  142. float poa2 = poa[1];
  143. //float poa3 = poa[2];
  144. float pdoa = poa2 - poa1 - offset;
  145. while (pdoa >= TPI) {
  146. pdoa -= TPI;
  147. }
  148. while (pdoa < 0) {
  149. pdoa += TPI;
  150. }
  151. pdoa -= PI;
  152. return pdoa;
  153. }
  154. //坐标点输入业务入口
  155. /*
  156. 定位的逻辑处理
  157. 车辆和人员都是走这个接口,然后分别进行处理。
  158. 输入:
  159. vp 定位结果(硬件的原始位置数据)
  160. lm 定位消息缓存区,用于tof,pdoa
  161. is_v_map 是否为车辆地图
  162. 输出:
  163. point m_v_point; //车辆定位系统的定位结果
  164. 定位结果相关的一些成员变量如:x、y
  165. 姿态:m_stat
  166. 人员定位结果(包含人员地图定位结果(x、y)和车辆地图定位结果m_v_point)发给WEB的server
  167. 车辆将定位结果(车辆地图定位结果)和姿态发送给WEB的server
  168. Added by zengminguo
  169. */
  170. void card_location_base::on_location(const std::vector<point>&vp, const std::vector<loc_message> &lm, bool is_v_map)
  171. {
  172. m_isVehicleMap = false;
  173. if (is_vehicle())
  174. {
  175. if (!is_v_map)
  176. {
  177. return;
  178. }
  179. }
  180. if (is_person())
  181. {
  182. if (is_v_map)
  183. {
  184. m_isVehicleMap = true;
  185. }
  186. }
  187. if (m_last_site_id != lm[0].m_sit->m_id)
  188. {
  189. m_deque_speed.clear();
  190. }
  191. loc_point pt = m_sel_tool->select_solution(vp, lm);
  192. //pt.y = pt.y;
  193. auto site_ptr = get_area_tool()->m_site;
  194. int sid = 0;
  195. if(site_ptr)
  196. sid = site_ptr->m_id;
  197. if(pt.m_useless)
  198. {
  199. if (is_person())
  200. {
  201. if (!is_v_map)
  202. {
  203. x = tool_other::round(pt.x, 3);
  204. y = tool_other::round(pt.y, 3);
  205. }
  206. else
  207. {
  208. int cell_index_diff = int(pt.m_dist);
  209. m_v_point.x = tool_other::round(pt.x, 3);
  210. m_v_point.y = tool_other::round(pt.y, 3);
  211. 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);
  212. pt.m_site_cell_index = lm[0].m_sit->m_cell_index;
  213. int cell_index_current = pt.m_site_cell_index - cell_index_diff;
  214. m_v_cell_index = cell_index_current;
  215. return;
  216. }
  217. }
  218. else if (is_vehicle())
  219. {
  220. if (is_v_map)
  221. {
  222. x = tool_other::round(pt.x, 3);
  223. y = tool_other::round(pt.y, 3);
  224. }
  225. else
  226. {
  227. return;
  228. }
  229. }
  230. // 姿态判断
  231. if (is_vehicle())
  232. {
  233. if (m_stat_last == ENUM_STATUS_BACK)
  234. {
  235. m_stat = ENUM_STATUS_BACK;
  236. }
  237. if (m_speed > 1 && m_stat_last != ENUM_STATUS_BACK)
  238. {
  239. m_stat = ENUM_STATUS_FORWARD;
  240. }
  241. if (m_stat_last == ENUM_STATUS_FORWARD ||
  242. m_stat_last == ENUM_STATUS_SPEED_UP ||
  243. m_stat_last == ENUM_STATUS_SPEED_DOWN
  244. )
  245. {
  246. m_stat = ENUM_STATUS_FORWARD;
  247. }
  248. if (m_stat_last == ENUM_STATUS_LEFT ||
  249. m_stat_last == ENUM_STATUS_RIGHT)
  250. {
  251. m_stat = m_statusBeforeTurning;
  252. }
  253. if ((lm[0].m_acc <= -0.009 && m_stat_last == ENUM_STATUS_STATIC) || m_stat_last == ENUM_STATUS_BACK)
  254. {
  255. m_stat = ENUM_STATUS_BACK;
  256. }
  257. if (lm[0].m_acc >= 0.009 &&
  258. (m_stat_last == ENUM_STATUS_FORWARD ||
  259. m_stat_last == ENUM_STATUS_SPEED_UP ||
  260. m_stat_last == ENUM_STATUS_SPEED_DOWN ||
  261. m_stat_last == ENUM_STATUS_STATIC))
  262. {
  263. m_stat = ENUM_STATUS_SPEED_UP;
  264. }
  265. if (lm[0].m_acc <= -0.009 &&
  266. (m_stat_last == ENUM_STATUS_FORWARD ||
  267. m_stat_last == ENUM_STATUS_SPEED_UP ||
  268. m_stat_last == ENUM_STATUS_SPEED_DOWN ||
  269. (m_speed_last - m_speed > 0.5)))
  270. {
  271. m_stat = ENUM_STATUS_SPEED_DOWN;
  272. }
  273. if (lm[0].m_rav > 0)
  274. {
  275. if (m_stat_last == ENUM_STATUS_LEFT)
  276. {
  277. }
  278. else
  279. {
  280. m_statusBeforeTurning = m_stat_last;
  281. }
  282. m_stat = ENUM_STATUS_LEFT;
  283. }
  284. if (lm[0].m_rav < 0)
  285. {
  286. if (m_stat_last == ENUM_STATUS_RIGHT)
  287. {
  288. }
  289. else
  290. {
  291. m_statusBeforeTurning = m_stat_last;
  292. }
  293. m_stat = ENUM_STATUS_RIGHT;
  294. }
  295. if (m_stat_last != ENUM_STATUS_BACK)
  296. {
  297. if (lm[0].m_acc == 0 && lm[0].m_rav == 0 && m_speed <= 1)
  298. {
  299. m_stat = ENUM_STATUS_STATIC;
  300. }
  301. }
  302. else
  303. {
  304. if (lm[0].m_acc == 0 && lm[0].m_rav == 0 && m_speed <= 0.3)
  305. {
  306. m_stat = ENUM_STATUS_STATIC;
  307. }
  308. }
  309. switch (m_stat)
  310. {
  311. case ENUM_STATUS_STATIC:
  312. 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);
  313. break;
  314. case ENUM_STATUS_MOVE:
  315. 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);
  316. break;
  317. case ENUM_STATUS_FORWARD:
  318. 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);
  319. break;
  320. case ENUM_STATUS_BACK:
  321. 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);
  322. break;
  323. case ENUM_STATUS_LEFT:
  324. 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);
  325. break;
  326. case ENUM_STATUS_RIGHT:
  327. 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);
  328. break;
  329. case ENUM_STATUS_SPEED_UP:
  330. 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);
  331. break;
  332. case ENUM_STATUS_SPEED_DOWN:
  333. 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);
  334. break;
  335. default:
  336. 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);
  337. break;
  338. }
  339. m_speed_last = m_speed;
  340. m_stat_last = m_stat;
  341. pt.m_stat = m_stat;
  342. pt.m_cid = lm[0].m_card_id;
  343. pt.m_sid = lm[0].m_sit->m_id;
  344. pt.m_site_cell_index = lm[0].m_sit->m_cell_index;
  345. std::string str_time = "";
  346. if (load_raw::m_is_history)
  347. {
  348. str_time = load_raw::m_vec_date[load_raw::m_index];
  349. }
  350. double pdoa = get_pdoa(lm[0].m_poa, lm[0].m_sit->m_pdoa_offset);
  351. int cell_index_diff = int(pt.m_dist);
  352. while (m_deque_speed.size() >= 5)
  353. {
  354. m_deque_speed.pop_front();
  355. }
  356. m_deque_speed.push_back(pt.m_speed);
  357. double speed_avg = 0.0;
  358. for (auto iter = m_deque_speed.begin(); iter != m_deque_speed.end(); iter++)
  359. {
  360. speed_avg += *iter;
  361. }
  362. speed_avg /= m_deque_speed.size();
  363. point point_cell;
  364. int cell_index_current = pt.m_site_cell_index - cell_index_diff;
  365. s_tunnel.get_position_by_cell_index(cell_index_current, point_cell);
  366. double dt = (lm[0].m_time_tmp - m_time_tmp_last)*1.0 / 1000.0;
  367. if (dt == 0)
  368. {
  369. dt = 1;
  370. }
  371. double seconds_diff = 1.0;
  372. if (m_time_tmp_last != 0)
  373. {
  374. seconds_diff = (lm[0].m_time_tmp - m_time_tmp_last) / 1000.0;
  375. }
  376. if (m_cell_index_last != -1000)
  377. {
  378. speed_avg = fabs(cell_index_current - m_cell_index_last) / seconds_diff;
  379. pt.m_speed = speed_avg;
  380. }
  381. else
  382. {
  383. speed_avg = 0.0;
  384. }
  385. //速度大于3倍平均速度,判断相反距离的速度
  386. if (speed_avg > m_avg_speed_for_count * 3)
  387. {
  388. int cell_index_diff_tmp = int(-pt.m_dist);
  389. int cell_index_current_tmp = pt.m_site_cell_index - cell_index_diff_tmp;
  390. int speed_avg_tmp = fabs(cell_index_current_tmp - m_cell_index_last) / seconds_diff;
  391. //相反距离平均速度在正常范围内,则取相反距离
  392. if (speed_avg_tmp <= m_avg_speed_for_count * 3)
  393. {
  394. speed_avg = speed_avg_tmp;
  395. cell_index_current = cell_index_current_tmp;
  396. }
  397. }
  398. double speed_diff = 0.0;
  399. if (seconds_diff != 0.0)
  400. {
  401. speed_diff = (speed_avg - m_avg_speed_last) / seconds_diff;
  402. }
  403. if (speed_avg > 0)
  404. {
  405. //最多累积一万个速度求平均
  406. if (m_speed_count < 10000)
  407. {
  408. m_speed_count++;
  409. }
  410. speed_avg = m_avg_speed_for_count + (speed_avg - m_avg_speed_for_count) / m_speed_count;
  411. m_avg_speed_for_count = speed_avg;
  412. }
  413. if (cell_index_current - m_cell_index_last == 0)
  414. {
  415. pt.m_direction = m_direction_last;
  416. }
  417. else
  418. {
  419. pt.m_direction = (cell_index_current - m_cell_index_last) > 0 ? "-" : "+";
  420. }
  421. //如果在基站附近
  422. if (fabs(pt.m_dist) < 10.0)
  423. {
  424. //掉头了
  425. if (pt.m_direction != m_direction_last)
  426. {
  427. int cell_index_diff_tmp = int(-pt.m_dist);
  428. int cell_index_current_tmp = pt.m_site_cell_index - cell_index_diff_tmp;
  429. int speed_avg_tmp = fabs(cell_index_current_tmp - m_cell_index_last) / seconds_diff;
  430. //取反试试速度,正常则不掉头。
  431. if (speed_avg_tmp <= m_avg_speed_for_count * 3)
  432. {
  433. speed_avg = speed_avg_tmp;
  434. cell_index_current = cell_index_current_tmp;
  435. }
  436. }
  437. }
  438. pt.m_time_tmp = lm[0].m_time_tmp;
  439. pt.m_speed_avg = speed_avg;
  440. pt.m_acc = lm[0].m_acc;
  441. pt.m_rav = lm[0].m_rav;
  442. pt.m_cell_index = cell_index_current;
  443. pt.m_speed_change_rate = speed_diff;
  444. log_info("[pdoa] card_location_base::on_location ,obj_id=%d, time=%s, reader_id=%d, ct=%d, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f, speed=%f, avg_speed=%f, begin_pt.x=%f, begin_pt.y=%f, gesture=%d, dist_original=%f, dist=%f, angle=%f, acc=%.2f, rav=%d, cell_index=%d, cell_x=%f, cell_y=%f, site_cell_index=%d, cell_count=%d, moving_direct=%s, speed_diff=%f",
  445. pt.m_cid,
  446. tool_time::to_str_ex(lm[0].m_time_tmp).c_str(),
  447. pt.m_sid,
  448. m_ct,
  449. lm[0].m_poa[0],
  450. lm[0].m_poa[1],
  451. lm[0].m_poa[2],
  452. pdoa,
  453. lm[0].m_sit->m_pdoa_offset,
  454. pt.m_speed,
  455. speed_avg,
  456. pt.x,
  457. pt.y,
  458. pt.m_stat,
  459. (pdoa > 0 ? 1 : -1) * fabs(pt.m_dist),
  460. pt.m_dist,
  461. pt.m_angle,
  462. lm[0].m_acc,
  463. lm[0].m_rav,
  464. cell_index_current,
  465. point_cell.x,
  466. point_cell.y,
  467. pt.m_site_cell_index,
  468. s_tunnel.get_cells_count(),
  469. pt.m_direction.c_str(),
  470. speed_diff);
  471. m_speed = pt.m_speed;
  472. m_stat = pt.m_stat;
  473. m_cell_index_last = cell_index_current;
  474. m_cell_index = cell_index_current;
  475. m_avg_speed_last = speed_avg;
  476. m_direction_last = pt.m_direction;
  477. m_time_tmp_last = lm[0].m_time_tmp;
  478. pt.x = point_cell.x;
  479. pt.y = point_cell.y;
  480. //make_his_location_simplify(time(0) * 1000, pt);
  481. make_his_location_cell_card(pt.m_time_tmp, pt);
  482. make_his_location_cell_reader(pt.m_time_tmp, pt);
  483. }
  484. //pt.m_speed *= 20;
  485. double acc = lm[0].m_acc;
  486. m_acc = lm[0].m_acc;
  487. 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);
  488. do_business(lm.front().m_sit, pt, acc, pt.m_cell_index);
  489. // 呼救128,正常0
  490. int val = (lm[0].m_rav ? STATUS_HELP : STATUS_NORMAL);
  491. if (m_battery_value > 3) {
  492. val += STATUS_POWER_NORMAL;
  493. }
  494. else {
  495. val += STATUS_POWER_LOWER_SERIOUS;
  496. }
  497. log_info("[help-battery] card_id=%d, battery=%d, val=%d", m_id, m_battery_value, val);
  498. do_status(val);
  499. }
  500. else
  501. {
  502. 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);
  503. }
  504. }
  505. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  506. {
  507. m_ct = loc.m_card_ct;
  508. m_time = loc.m_time_stamp;
  509. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  510. if(!site_ptr)
  511. {
  512. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  513. return;
  514. }
  515. auto area_tool=get_area_tool();
  516. area_tool->set_site(site_ptr);
  517. handle_message(loc.m_card_ct,loc.m_batty_status);
  518. if(site_ptr->is_up_site())
  519. {
  520. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  521. area_tool->on_point(shared_from_this(),point(1,1));
  522. this->site_hover(loc.m_site_id);
  523. }
  524. else
  525. {
  526. if(site_ptr->is_path_empty())
  527. {
  528. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  529. return;
  530. }
  531. m_message_handle->on_message(loop,loc,is_history);
  532. }
  533. }
  534. /*
  535. * tdoa 调用算法库定位
  536. * */
  537. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  538. {
  539. log_info("[tdoa] start calc location.");
  540. m_ct = loc.m_card_msg.m_sync_num;
  541. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  542. if(!site_ptr){
  543. 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);
  544. return;
  545. }
  546. auto area_tool=get_area_tool();
  547. area_tool->set_site(site_ptr);
  548. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  549. if(site_ptr->is_up_site())
  550. {}
  551. else{
  552. m_message_handle->on_message(loop, loc, is_history);
  553. }
  554. }
  555. /*
  556. * pdoa 调用算法库定位
  557. *
  558. * */
  559. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  560. {
  561. m_ct = loc.m_card_ct;
  562. m_time = loc.m_time_stamp;
  563. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  564. if(!site_ptr){
  565. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  566. return;
  567. }
  568. site_ptr->m_loc_time = time(0);
  569. // 基站覆盖距离检测
  570. do_site_coverage(loc);
  571. auto area_tool=get_area_tool();
  572. area_tool->set_site(site_ptr);
  573. m_battery_value = loc.m_batty_status; // 电量值
  574. handle_message(loc.m_card_ct, loc.m_batty_status);
  575. if(site_ptr->is_up_site()){
  576. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  577. area_tool->on_point(shared_from_this(), point(1,1));
  578. this->site_hover(loc.m_site_id);
  579. m_last_site_id = site_ptr->id();
  580. //m_last_down_stream_idx = site_ptr->m_down_stream_idx;
  581. m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
  582. }
  583. else{
  584. char sql[1024] = {0};
  585. 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());
  586. //db_tool::PushAsync(sql);
  587. log_info("[sql] %s", sql);
  588. do_site_locate_event(loc);
  589. do_card_locate_event(loc);
  590. m_message_handle->on_message(loop, loc, is_history);
  591. }
  592. }
  593. //前端推送位置函数.
  594. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  595. {
  596. point _p;
  597. if(pt.empty())
  598. {
  599. _p = *this;
  600. pt = _p;
  601. }
  602. else
  603. _p = pt;
  604. cp.Type = m_type;
  605. cp.ID = m_id;
  606. cp.speed = abs(ceil(m_speed));
  607. cp.x = tool_other::round(_p.x,3);
  608. cp.y = tool_other::round(_p.y,3);
  609. cp.running_stat = m_stat;
  610. cp.dept_id = m_deptid;
  611. cp.display = m_display;
  612. cp.rec_time = m_time;
  613. cp.level_id = m_level_id;
  614. cp.battery_val = m_battery_value;
  615. if(m_pwr_stat > 0){
  616. cp.battery_stat = (m_pwr_stat<3?1:0);
  617. }
  618. cp.m_freq = m_freq;
  619. swsTimerThrd.upt_card_pos(cp);
  620. }
  621. void card_location_base::del_card_pos()
  622. {
  623. sys::_CARD_POS_ cp;
  624. cp.ID = m_id;
  625. cp.Type=m_type;
  626. swsTimerThrd.del_card_pos(cp);
  627. }
  628. int card_location_base::get_stat()
  629. {
  630. //盲区>呼救>呼叫>超时>超速>正常
  631. uint64_t now = time(0)*1000;
  632. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  633. if(tlost>CARD_LOST_TIME_OUT)
  634. {
  635. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  636. return STATUS_LOST;
  637. }
  638. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  639. {
  640. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  641. }
  642. else if(CALL_NONE != get_mine_tool()->m_status_call)
  643. {
  644. return STATUS_CALL;
  645. }
  646. else if(is_person())
  647. {
  648. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  649. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  650. return STATUS_AREA_OVER_TIME;
  651. }
  652. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  653. {
  654. return STATUS_OVER_SPEED;
  655. }
  656. return STATUS_NORMAL;
  657. }
  658. void card_location_base::clear()
  659. {
  660. // uint16_t m_display; //1显示0不显示,往前端推送
  661. m_speed=0; //速度
  662. m_stat=0; //运动静止状态
  663. //m_ct; //ct
  664. m_time=0; //时间戳
  665. //pdoa location info clear
  666. m_buff_size = 0;
  667. m_last_recv_time = 0;
  668. m_last_point = point(0,0);
  669. m_last_site_id = 0;
  670. m_last_site_dir = -1;
  671. m_last_ct = 0;
  672. m_last_dist = 0.0;
  673. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  674. m_last_over_site = false;
  675. m_cache_nums = 0;
  676. m_cb_pdoa.clear();
  677. m_cb_tof.clear();
  678. m_cb_point.clear();
  679. }
  680. /*void card_location_base::put_three_rates(card_pos & cp)
  681. {
  682. if(!three_rates_flag)
  683. return;
  684. cp.rec_time = m_time;
  685. cp.type = m_type;
  686. cp.id = m_id;
  687. cp.identifier_id= m_cid;
  688. cp.running_stat = m_stat;
  689. cp.final_v = m_speed;
  690. cp.dpt_id = m_deptid;
  691. std::shared_ptr<area_tool> _areatool = get_area_tool();
  692. if(nullptr != _areatool && nullptr != _areatool->m_site)
  693. {
  694. cp.reader_x = _areatool->m_site->x;
  695. cp.reader_y = _areatool->m_site->y;
  696. cp.reader_id = _areatool->m_site->m_id;
  697. }
  698. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  699. three_rates::get_instance()->put(cp);
  700. }*/
  701. void card_location_base::put_traffic_light(card_pos& cp)
  702. {
  703. if(!traffic_light_flag){
  704. return;
  705. }
  706. light_message lm;
  707. lm.m_cmd = cmd_card_data;
  708. lm.m_pos.x = cp.x;
  709. lm.m_pos.y = cp.y;
  710. lm.m_pos.z = cp.z;
  711. lm.m_pos.m_card_id = m_id;
  712. lm.m_pos.m_type = m_type;
  713. lm.m_pos.m_site_id = cp.reader_id;
  714. auto site_ptr = get_area_tool()->m_site;
  715. if(site_ptr){
  716. lm.m_pos.m_area_id = site_ptr->m_area_id;
  717. }
  718. //lm.m_pos.m_bigger = 0;
  719. lm.m_pos.m_speed = m_speed;
  720. 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);
  721. traffic_light_manager::instance()->put(lm);
  722. }
  723. bool card_location_base::is_person() const
  724. {
  725. return tool_other::is_person(m_type);
  726. }
  727. bool card_location_base::is_vehicle() const
  728. {
  729. return tool_other::is_vehicle(m_type);
  730. }
  731. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  732. m_cid = cid;
  733. m_type = type;
  734. m_deptid = deptid;
  735. m_level_id = level_id;
  736. m_his_location_card->set_cid(cid);
  737. }
  738. card_location_base::~card_location_base()
  739. {
  740. }
  741. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  742. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  743. {
  744. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  745. }
  746. /*
  747. * @brief 创建车辆对象
  748. * @param const std::string& type
  749. * @param uint32_t cardid 卡id
  750. * @param uint16_t needdisplay 是否需要显示
  751. * @param int16_t t
  752. * @param int32_t deptid 部门id
  753. * @param int32_t categoryid
  754. * @param int type_id 卡类型id
  755. * @param int32_t level_id 职级id
  756. * @param uint32_t cid
  757. * @return 车辆对象
  758. * @note
  759. * @bug
  760. * @warning
  761. * */
  762. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  763. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  764. {
  765. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  766. }
  767. /*
  768. * @brief 基站覆盖距离检测
  769. * @param message_pdoa_locinfo& loc 卡数据
  770. * @return 无
  771. * @note
  772. * @bug
  773. * @warning
  774. * */
  775. void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
  776. {
  777. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  778. if(!site_ptr){
  779. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  780. return;
  781. }
  782. double pdoa = tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset);
  783. double dist = loc.m_tof*15.65*2.996*1e-4;
  784. bool is_update = false;
  785. if(pdoa > 0){
  786. if(dist > site_ptr->m_coverage[0].m_distance){
  787. site_ptr->m_coverage[0].m_distance = dist;
  788. site_ptr->m_coverage[0].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  789. site_ptr->m_coverage[0].m_time = time(0);
  790. is_update = true;
  791. }
  792. }else{
  793. if(dist > site_ptr->m_coverage[1].m_distance){
  794. site_ptr->m_coverage[1].m_distance = dist;
  795. site_ptr->m_coverage[1].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  796. site_ptr->m_coverage[1].m_time = time(0);
  797. is_update = true;
  798. }
  799. }
  800. if(is_update){
  801. char sql[1024] = {0};
  802. 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());
  803. db_tool::PushAsync(sql);
  804. logn_info(2, "[sql] %s", sql);
  805. }
  806. }
  807. /*
  808. * @brief 卡定位异常检测
  809. * @param message_pdoa_locinfo& loc 卡数据
  810. * @return 无
  811. * @note
  812. * @bug
  813. * @warning
  814. * */
  815. void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
  816. {
  817. bool status = false;
  818. status = ((loc.m_tof == 0)?true:false);
  819. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  820. event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);
  821. //log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
  822. }
  823. /*
  824. * @brief 基站定位异常检测
  825. * @param message_pdoa_locinfo& loc 卡数据
  826. * @return 无
  827. * @note
  828. * @bug
  829. * @warning
  830. * */
  831. void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
  832. {
  833. //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);
  834. if(m_last_ct <= 0 || m_last_site_id == 0){
  835. return;
  836. }
  837. if(loc.m_site_id != m_last_site_id){
  838. return;
  839. }
  840. int dt = 0;
  841. if(loc.m_card_ct > m_last_ct){
  842. dt = loc.m_card_ct - m_last_ct;
  843. }else{
  844. dt = 65535 + loc.m_card_ct - m_last_ct;
  845. }
  846. event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
  847. 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);
  848. }