1
0

card_base.cpp 27 KB

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