1
0

card_base.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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_avg_speed_last = speed_avg;
  475. m_direction_last = pt.m_direction;
  476. m_time_tmp_last = lm[0].m_time_tmp;
  477. pt.x = point_cell.x;
  478. pt.y = point_cell.y;
  479. //make_his_location_simplify(time(0) * 1000, pt);
  480. make_his_location_cell_card(pt.m_time_tmp, pt);
  481. make_his_location_cell_reader(pt.m_time_tmp, pt);
  482. }
  483. //pt.m_speed *= 20;
  484. double acc = lm[0].m_acc;
  485. m_acc = lm[0].m_acc;
  486. 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);
  487. do_business(lm.front().m_sit, pt, acc, pt.m_cell_index);
  488. // 呼救128,正常0
  489. int val = (lm[0].m_rav ? STATUS_HELP : STATUS_NORMAL);
  490. if (m_battery_value > 3) {
  491. val += STATUS_POWER_NORMAL;
  492. }
  493. else {
  494. val += STATUS_POWER_LOWER_SERIOUS;
  495. }
  496. log_info("[help-battery] card_id=%d, battery=%d, val=%d", m_id, m_battery_value, val);
  497. do_status(val);
  498. }
  499. else
  500. {
  501. 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);
  502. }
  503. }
  504. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  505. {
  506. m_ct = loc.m_card_ct;
  507. m_time = loc.m_time_stamp;
  508. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  509. if(!site_ptr)
  510. {
  511. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  512. return;
  513. }
  514. auto area_tool=get_area_tool();
  515. area_tool->set_site(site_ptr);
  516. handle_message(loc.m_card_ct,loc.m_batty_status);
  517. if(site_ptr->is_up_site())
  518. {
  519. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  520. area_tool->on_point(shared_from_this(),point(1,1));
  521. this->site_hover(loc.m_site_id);
  522. }
  523. else
  524. {
  525. if(site_ptr->is_path_empty())
  526. {
  527. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  528. return;
  529. }
  530. m_message_handle->on_message(loop,loc,is_history);
  531. }
  532. }
  533. /*
  534. * tdoa 调用算法库定位
  535. * */
  536. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  537. {
  538. log_info("[tdoa] start calc location.");
  539. m_ct = loc.m_card_msg.m_sync_num;
  540. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  541. if(!site_ptr){
  542. 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);
  543. return;
  544. }
  545. auto area_tool=get_area_tool();
  546. area_tool->set_site(site_ptr);
  547. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  548. if(site_ptr->is_up_site())
  549. {}
  550. else{
  551. m_message_handle->on_message(loop, loc, is_history);
  552. }
  553. }
  554. /*
  555. * pdoa 调用算法库定位
  556. *
  557. * */
  558. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  559. {
  560. m_ct = loc.m_card_ct;
  561. m_time = loc.m_time_stamp;
  562. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  563. if(!site_ptr){
  564. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  565. return;
  566. }
  567. site_ptr->m_loc_time = time(0);
  568. // 基站覆盖距离检测
  569. do_site_coverage(loc);
  570. auto area_tool=get_area_tool();
  571. area_tool->set_site(site_ptr);
  572. m_battery_value = loc.m_batty_status; // 电量值
  573. handle_message(loc.m_card_ct, loc.m_batty_status);
  574. if(site_ptr->is_up_site()){
  575. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  576. area_tool->on_point(shared_from_this(), point(1,1));
  577. this->site_hover(loc.m_site_id);
  578. m_last_site_id = site_ptr->id();
  579. //m_last_down_stream_idx = site_ptr->m_down_stream_idx;
  580. m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
  581. }
  582. else{
  583. char sql[1024] = {0};
  584. 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());
  585. //db_tool::PushAsync(sql);
  586. log_info("[sql] %s", sql);
  587. do_site_locate_event(loc);
  588. do_card_locate_event(loc);
  589. m_message_handle->on_message(loop, loc, is_history);
  590. }
  591. }
  592. //前端推送位置函数.
  593. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  594. {
  595. point _p;
  596. if(pt.empty())
  597. {
  598. _p = *this;
  599. pt = _p;
  600. }
  601. else
  602. _p = pt;
  603. cp.Type = m_type;
  604. cp.ID = m_id;
  605. cp.speed = abs(ceil(m_speed));
  606. cp.x = tool_other::round(_p.x,3);
  607. cp.y = tool_other::round(_p.y,3);
  608. cp.running_stat = m_stat;
  609. cp.dept_id = m_deptid;
  610. cp.display = m_display;
  611. cp.rec_time = m_time;
  612. cp.level_id = m_level_id;
  613. cp.battery_val = m_battery_value;
  614. if(m_pwr_stat > 0){
  615. cp.battery_stat = (m_pwr_stat<3?1:0);
  616. }
  617. cp.m_freq = m_freq;
  618. swsTimerThrd.upt_card_pos(cp);
  619. }
  620. void card_location_base::del_card_pos()
  621. {
  622. sys::_CARD_POS_ cp;
  623. cp.ID = m_id;
  624. cp.Type=m_type;
  625. swsTimerThrd.del_card_pos(cp);
  626. }
  627. int card_location_base::get_stat()
  628. {
  629. //盲区>呼救>呼叫>超时>超速>正常
  630. uint64_t now = time(0)*1000;
  631. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  632. if(tlost>CARD_LOST_TIME_OUT)
  633. {
  634. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  635. return STATUS_LOST;
  636. }
  637. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  638. {
  639. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  640. }
  641. else if(CALL_NONE != get_mine_tool()->m_status_call)
  642. {
  643. return STATUS_CALL;
  644. }
  645. else if(is_person())
  646. {
  647. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  648. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  649. return STATUS_AREA_OVER_TIME;
  650. }
  651. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  652. {
  653. return STATUS_OVER_SPEED;
  654. }
  655. return STATUS_NORMAL;
  656. }
  657. void card_location_base::clear()
  658. {
  659. // uint16_t m_display; //1显示0不显示,往前端推送
  660. m_speed=0; //速度
  661. m_stat=0; //运动静止状态
  662. //m_ct; //ct
  663. m_time=0; //时间戳
  664. //pdoa location info clear
  665. m_buff_size = 0;
  666. m_last_recv_time = 0;
  667. m_last_point = point(0,0);
  668. m_last_site_id = 0;
  669. m_last_site_dir = -1;
  670. m_last_ct = 0;
  671. m_last_dist = 0.0;
  672. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  673. m_last_over_site = false;
  674. m_cache_nums = 0;
  675. m_cb_pdoa.clear();
  676. m_cb_tof.clear();
  677. m_cb_point.clear();
  678. }
  679. /*void card_location_base::put_three_rates(card_pos & cp)
  680. {
  681. if(!three_rates_flag)
  682. return;
  683. cp.rec_time = m_time;
  684. cp.type = m_type;
  685. cp.id = m_id;
  686. cp.identifier_id= m_cid;
  687. cp.running_stat = m_stat;
  688. cp.final_v = m_speed;
  689. cp.dpt_id = m_deptid;
  690. std::shared_ptr<area_tool> _areatool = get_area_tool();
  691. if(nullptr != _areatool && nullptr != _areatool->m_site)
  692. {
  693. cp.reader_x = _areatool->m_site->x;
  694. cp.reader_y = _areatool->m_site->y;
  695. cp.reader_id = _areatool->m_site->m_id;
  696. }
  697. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  698. three_rates::get_instance()->put(cp);
  699. }*/
  700. void card_location_base::put_traffic_light(card_pos& cp)
  701. {
  702. if(!traffic_light_flag){
  703. return;
  704. }
  705. light_message lm;
  706. lm.m_cmd = cmd_card_data;
  707. lm.m_pos.x = cp.x;
  708. lm.m_pos.y = cp.y;
  709. lm.m_pos.z = cp.z;
  710. lm.m_pos.m_card_id = m_id;
  711. lm.m_pos.m_type = m_type;
  712. lm.m_pos.m_site_id = cp.reader_id;
  713. auto site_ptr = get_area_tool()->m_site;
  714. if(site_ptr){
  715. lm.m_pos.m_area_id = site_ptr->m_area_id;
  716. }
  717. //lm.m_pos.m_bigger = 0;
  718. lm.m_pos.m_speed = m_speed;
  719. 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);
  720. traffic_light_manager::instance()->put(lm);
  721. }
  722. bool card_location_base::is_person() const
  723. {
  724. return tool_other::is_person(m_type);
  725. }
  726. bool card_location_base::is_vehicle() const
  727. {
  728. return tool_other::is_vehicle(m_type);
  729. }
  730. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  731. m_cid = cid;
  732. m_type = type;
  733. m_deptid = deptid;
  734. m_level_id = level_id;
  735. m_his_location_card->set_cid(cid);
  736. }
  737. card_location_base::~card_location_base()
  738. {
  739. }
  740. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  741. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  742. {
  743. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  744. }
  745. /*
  746. * @brief 创建车辆对象
  747. * @param const std::string& type
  748. * @param uint32_t cardid 卡id
  749. * @param uint16_t needdisplay 是否需要显示
  750. * @param int16_t t
  751. * @param int32_t deptid 部门id
  752. * @param int32_t categoryid
  753. * @param int type_id 卡类型id
  754. * @param int32_t level_id 职级id
  755. * @param uint32_t cid
  756. * @return 车辆对象
  757. * @note
  758. * @bug
  759. * @warning
  760. * */
  761. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  762. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  763. {
  764. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  765. }
  766. /*
  767. * @brief 基站覆盖距离检测
  768. * @param message_pdoa_locinfo& loc 卡数据
  769. * @return 无
  770. * @note
  771. * @bug
  772. * @warning
  773. * */
  774. void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
  775. {
  776. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  777. if(!site_ptr){
  778. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  779. return;
  780. }
  781. double pdoa = tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset);
  782. double dist = loc.m_tof*15.65*2.996*1e-4;
  783. bool is_update = false;
  784. if(pdoa > 0){
  785. if(dist > site_ptr->m_coverage[0].m_distance){
  786. site_ptr->m_coverage[0].m_distance = dist;
  787. site_ptr->m_coverage[0].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  788. site_ptr->m_coverage[0].m_time = time(0);
  789. is_update = true;
  790. }
  791. }else{
  792. if(dist > site_ptr->m_coverage[1].m_distance){
  793. site_ptr->m_coverage[1].m_distance = dist;
  794. site_ptr->m_coverage[1].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  795. site_ptr->m_coverage[1].m_time = time(0);
  796. is_update = true;
  797. }
  798. }
  799. if(is_update){
  800. char sql[1024] = {0};
  801. 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());
  802. db_tool::PushAsync(sql);
  803. logn_info(2, "[sql] %s", sql);
  804. }
  805. }
  806. /*
  807. * @brief 卡定位异常检测
  808. * @param message_pdoa_locinfo& loc 卡数据
  809. * @return 无
  810. * @note
  811. * @bug
  812. * @warning
  813. * */
  814. void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
  815. {
  816. bool status = false;
  817. status = ((loc.m_tof == 0)?true:false);
  818. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  819. event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);
  820. //log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
  821. }
  822. /*
  823. * @brief 基站定位异常检测
  824. * @param message_pdoa_locinfo& loc 卡数据
  825. * @return 无
  826. * @note
  827. * @bug
  828. * @warning
  829. * */
  830. void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
  831. {
  832. //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);
  833. if(m_last_ct <= 0 || m_last_site_id == 0){
  834. return;
  835. }
  836. if(loc.m_site_id != m_last_site_id){
  837. return;
  838. }
  839. int dt = 0;
  840. if(loc.m_card_ct > m_last_ct){
  841. dt = loc.m_card_ct - m_last_ct;
  842. }else{
  843. dt = 65535 + loc.m_card_ct - m_last_ct;
  844. }
  845. event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
  846. 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);
  847. }