card_base.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. extern config_file config;
  26. 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)
  27. :card(id, dis, t, deptid, level_id, cid)
  28. ,m_display_old(dis)
  29. {
  30. select_tool_manage::instance()->create_tool(type, m_sel_tool, m_smo_tool);
  31. m_his_location_card.reset(new location_card(m_id, m_type, cid));
  32. m_cb_pdoa.set_capacity(5);
  33. m_cb_tof.set_capacity(5);
  34. m_cb_point.set_capacity(5);
  35. for(int index = 0; index < 5; ++index){
  36. m_cb_pdoa.push_back(100);
  37. m_cb_tof.push_back(0);
  38. m_cb_point.push_back(point(0,0));
  39. }
  40. m_cb_stream_idx.set_capacity(2);
  41. m_cb_stream_idx.push_back(0);
  42. m_cb_stream_idx.push_back(0);
  43. }
  44. void card_location_base::do_status(int st)
  45. {
  46. time_t now = time(0);
  47. //bool help_flag = false;
  48. //log_info("[help-battery] card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  49. // 呼救
  50. if((m_help_bit & 1) && (st & STATUS_HELP))
  51. {
  52. // 1111111111
  53. // ^
  54. m_help_last_time = now;
  55. }
  56. else if((m_help_bit & 1) && (st & STATUS_HELP)==0)
  57. {
  58. // 11111111100000
  59. // ^
  60. m_help_bit<<=1;
  61. }
  62. else if((m_help_bit & 1)==0 && (st & STATUS_HELP))
  63. {
  64. // 00000000011111
  65. // ^
  66. // 呼救开始
  67. if((m_help_bit&0x3)==2)
  68. {
  69. //log_warn("[help-battery] handle_m_help, card_id=%d",m_id);
  70. //help_flag=true;
  71. }
  72. m_help_last_time = now;
  73. m_help_bit<<=1;
  74. m_help_bit|=1;
  75. log_info("[help-battery] begin help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  76. }
  77. else
  78. {
  79. // 11111111100000
  80. // ^
  81. // 呼救结束后持续1分钟
  82. if(now - m_help_last_time > 60)
  83. {
  84. m_help_bit = 0;
  85. log_info("[help-battery] end help, card_id=%d, help_bit=%d, status=%d", m_id, m_help_bit, st);
  86. }
  87. }
  88. /*if(!help_flag)
  89. {
  90. st = st & (0xFFFFFFFF ^ STATUS_HELP);
  91. }*/
  92. if((STATUS_POWER_LOWER_SERIOUS & st) != 0)
  93. {
  94. m_pwr_stat = STATUS_POWER_LOWER_SERIOUS;
  95. }
  96. else
  97. {
  98. m_pwr_stat = 0;
  99. }
  100. module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
  101. }
  102. //写入历史轨迹
  103. void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclose /*= false*/)
  104. {
  105. int area_id = 0, map_id = 0, site_id = 0;
  106. double scale = 0.0;
  107. if(auto site_ptr = get_area_tool()->m_site)
  108. {
  109. area_id = site_ptr->m_area_id;
  110. map_id = site_ptr->m_map_id;
  111. site_id = site_ptr->m_id;
  112. scale = site_ptr->m_scale;
  113. }
  114. auto mine_ptr = get_mine_tool();
  115. if(m_time < mine_ptr->get_down_time())
  116. {
  117. return;
  118. }
  119. m_his_location_card->push(t, pt, area_id, map_id, site_id, bclose);
  120. m_his_location_card->insert(t, pt, area_id, map_id, site_id, scale);
  121. }
  122. //坐标点输入业务入口
  123. void card_location_base::on_location(const std::vector<point>&vp, const std::vector<loc_message> &lm)
  124. {
  125. /*std::for_each(vp.begin(), vp.end(), [](point it){
  126. logn_info(3, "possible solution: x = %.2f, y = %.2f", it.x, it.y);
  127. });*/
  128. if(lm.size() < 2){
  129. return;
  130. }
  131. loc_point pt = m_sel_tool->select_solution(vp, lm);
  132. //logn_info(3, "select solution: x = %.2f, y = %.2f", pt.x, pt.y);
  133. auto site_ptr = get_area_tool()->m_site;
  134. int sid = 0;
  135. // 天线间间距默认为3米,实际取基站上的天线间距离配置参数
  136. double ant_dist = 3.0;
  137. if(site_ptr){
  138. sid = site_ptr->m_id;
  139. ant_dist = site_ptr->m_pdoa_offset;
  140. }
  141. // first way
  142. sys::tof_data tdt;
  143. tdt.cid = tool_byte::to_cardid(m_type, m_id);
  144. tdt.sid = 0;
  145. tdt.cur_time = tool_time::to_str_ex(tool_time::now_to_ms());
  146. int aid = lm[0].m_ant_id;
  147. tdt.ant_dist[aid] = lm[0].m_num_ticks*15.65*2.996*1E-4;
  148. aid = lm[1].m_ant_id;
  149. tdt.ant_dist[aid] = lm[1].m_num_ticks*15.65*2.996*1E-4;
  150. if(tdt.ant_dist[0] > 1000 || tdt.ant_dist[1] > 1000){
  151. int a = 11;
  152. pt.x = a;
  153. }
  154. logn_info(3, "[tof_data] cid=%d, ct=%d, 1ant_dist=%.2f, 2ant_dist=%.2f", m_id, m_ct, tdt.ant_dist[0], tdt.ant_dist[1]);
  155. loc_point tp;
  156. if(tdt.ant_dist[0] > tdt.ant_dist[1] && tdt.ant_dist[0] > ant_dist){
  157. // 在0天线侧外部
  158. tp.y = -1.5 + tdt.ant_dist[0];
  159. }else if(tdt.ant_dist[1] > tdt.ant_dist[0] && tdt.ant_dist[1] > ant_dist){
  160. // 在1天线测外部
  161. tp.y = 1.5 - tdt.ant_dist[1];
  162. }else if(tdt.ant_dist[0] < 3.0 && tdt.ant_dist[1] < ant_dist) {
  163. // 在两天线之间
  164. tp.y = (-1.5 + tdt.ant_dist[0]) + (1.5 - tdt.ant_dist[1])/2.0;
  165. }
  166. logn_info(3, "another solution:cid =%d, ct=%d, x = %.2f, y = %.2f", m_id, m_ct, tp.x, tp.y);
  167. tdt.y = tp.y;
  168. pt = tp;
  169. pt.m_useless = true;
  170. if(pt.m_useless)
  171. {
  172. x = tool_other::round(pt.x, 3);
  173. y = tool_other::round(pt.y, 3);
  174. do_business(lm.front().m_sit, pt);
  175. sys::tof_data td;
  176. td.cid = tool_byte::to_cardid(m_type, m_id);
  177. td.y = y;
  178. td.sid = sid;
  179. td.cur_time = tool_time::to_str_ex(tool_time::now_to_ms());
  180. int aid = lm[0].m_ant_id;
  181. td.ant_dist[aid] = lm[0].m_num_ticks*15.65*2.996*1E-4;
  182. aid = lm[1].m_ant_id;
  183. td.ant_dist[aid] = lm[1].m_num_ticks*15.65*2.996*1E-4;
  184. td.ant_diff = site_ptr->m_pdoa_offset;
  185. // 将数据存入数据库中
  186. char sql[1024] = {0};
  187. snprintf(sql, 1024, "insert into his_tof_locate_data(card_id, ant_one_dist, ant_two_dist, ant_dist,site_id, y, cur_time) values('%s', %.2f, %.2f, %.2f, %d, %.2f, '%s')", tool_byte::to_cardid(m_type, m_id).c_str(), td.ant_dist[0], td.ant_dist[1], site_ptr->m_pdoa_offset, sid, y, tool_time::to_str_ex(tool_time::now_to_ms()).c_str());
  188. db_tool::PushAsync(sql);
  189. log_info("[sql] %s", sql);
  190. //add json to send to web
  191. swsTimerThrd.upt_tof_data(std::move(td));
  192. // 呼救128,正常0
  193. /*int val = (lm[0].m_rav?STATUS_HELP:STATUS_NORMAL);
  194. if(m_battery_value > 3){
  195. val += STATUS_POWER_NORMAL;
  196. }else{
  197. val += STATUS_POWER_LOWER_SERIOUS;
  198. }*/
  199. //log_info("[help-battery] card_id=%d, battery=%d, val=%d", m_id, m_battery_value, val);
  200. //do_status(STATUS_NORMAL);
  201. }
  202. else
  203. {
  204. 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);
  205. }
  206. }
  207. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  208. {
  209. m_ct = loc.m_card_ct;
  210. m_time = loc.m_time_stamp;
  211. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  212. if(!site_ptr)
  213. {
  214. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  215. return;
  216. }
  217. auto area_tool=get_area_tool();
  218. area_tool->set_site(site_ptr);
  219. handle_message(loc.m_card_ct,loc.m_batty_status);
  220. if(site_ptr->is_up_site())
  221. {
  222. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  223. area_tool->on_point(shared_from_this(),point(1,1));
  224. this->site_hover(loc.m_site_id);
  225. }
  226. else
  227. {
  228. if(site_ptr->is_path_empty())
  229. {
  230. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  231. return;
  232. }
  233. m_message_handle->on_message(loop,loc,is_history);
  234. }
  235. }
  236. /*
  237. * 实心球原始数据调用算法模块进行定位处理
  238. * */
  239. void card_location_base::on_message(zloop<task*>* loop, message_ins_locinfo& loc,bool is_history)
  240. {
  241. /*m_ct = loc.m_card_ct;
  242. m_time = loc.m_time_stamp;
  243. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  244. if(!site_ptr)
  245. {
  246. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  247. return;
  248. }*/
  249. //m_message_handle->on_message(loop, loc, is_history);
  250. log_info("[solid_ball] start calc location");
  251. auto itm = m_msg_list.find(loc.m_card_ct);
  252. if(itm == m_msg_list.end()){
  253. std::vector<message_ins_locinfo> vtm;
  254. vtm.push_back(loc);
  255. m_msg_list.insert(std::make_pair(loc.m_card_ct, vtm));
  256. itm = m_msg_list.find(loc.m_card_ct);
  257. }else{
  258. itm->second.push_back(loc);
  259. }
  260. if(itm->second.size() < 2){
  261. return;
  262. }
  263. // ct切换了
  264. m_ct = loc.m_card_ct;
  265. m_time = loc.m_time_stamp;
  266. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  267. if(!site_ptr)
  268. {
  269. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  270. return;
  271. }
  272. // 卡号
  273. std::string sid = tool_byte::to_cardid(m_type, m_id);
  274. std::string site_info = "";
  275. std::string now = tool_time::to_str_ex(tool_time::now_to_ms());
  276. int nIndex = 0;
  277. int siteId1 = 0;
  278. int siteId2 = 0;
  279. double distance1 = 0;
  280. double distance2 = 0;
  281. // 存入数据库
  282. for(auto itl : itm->second){
  283. site_info += std::to_string(itl.m_site_id);
  284. double d = itl.m_tof * 15.65 * 2.996 * 1E-4;
  285. site_info += "," + std::to_string(d);
  286. site_info += ",0.0";
  287. double v = 0.0;
  288. auto its = last_site_info.find(itl.m_site_id);
  289. if(its == last_site_info.end()){
  290. sinfo si;
  291. si.sct = m_ct;
  292. si.sdist = d;
  293. last_site_info.insert(std::make_pair(itl.m_site_id, si));
  294. v = 0.0;
  295. }else{
  296. double dt = 0.0;
  297. if(m_ct > its->second.sct ){
  298. dt = (m_ct - its->second.sct) / 1000.0;
  299. }else{
  300. dt = (m_ct + 65535 - its->second.sct) / 1000.0;
  301. }
  302. v = (d - its->second.sdist) / dt;
  303. its->second.sct = m_ct;
  304. its->second.sdist = d;
  305. }
  306. site_info += "," + std::to_string(v);
  307. site_info += ";";
  308. if (nIndex == 0)
  309. {
  310. siteId1 = itl.m_site_id;
  311. distance1 = d;
  312. nIndex++;
  313. }
  314. else if (nIndex == 1)
  315. {
  316. siteId2 = itl.m_site_id;
  317. distance2 = d;
  318. }
  319. }
  320. // 数据入库
  321. char sql[1024] = { 0 };
  322. snprintf(sql,
  323. 1024,
  324. "insert into his_raw_data_sb(card_id, site_id, distance, acc, acc_x, acc_y, acc_z, ang, ang_x, ang_y, ang_z, cur_time) values('%s', %d, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, '%s')",
  325. sid.c_str(),
  326. siteId1,
  327. distance1,
  328. loc.m_ins_data.get_acc(),
  329. loc.m_ins_data.get_acc(0),
  330. loc.m_ins_data.get_acc(1),
  331. loc.m_ins_data.get_acc(2),
  332. loc.m_ins_data.get_gyro(),
  333. loc.m_ins_data.get_gyro(0),
  334. loc.m_ins_data.get_gyro(1),
  335. loc.m_ins_data.get_gyro(2),
  336. now.c_str()
  337. );
  338. db_tool::PushAsync(sql);
  339. log_info("[sql] %s", sql);
  340. memset(sql, 0, 1024);
  341. snprintf(sql,
  342. 1024,
  343. "insert into his_raw_data_sb(card_id, site_id, distance, acc, acc_x, acc_y, acc_z, ang, ang_x, ang_y, ang_z, cur_time) values('%s', %d, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, '%s')",
  344. sid.c_str(),
  345. siteId2,
  346. distance2,
  347. loc.m_ins_data.get_acc(),
  348. loc.m_ins_data.get_acc(0),
  349. loc.m_ins_data.get_acc(1),
  350. loc.m_ins_data.get_acc(2),
  351. loc.m_ins_data.get_gyro(),
  352. loc.m_ins_data.get_gyro(0),
  353. loc.m_ins_data.get_gyro(1),
  354. loc.m_ins_data.get_gyro(2),
  355. now.c_str()
  356. );
  357. db_tool::PushAsync(sql);
  358. log_info("[sql] %s", sql);
  359. // 更新数据到发送线程
  360. sys::sb_data sd;
  361. sd.cid = sid;
  362. sd.speed = 0.0;
  363. sd.site_info = site_info;
  364. sd.acc = loc.m_ins_data.get_acc();
  365. sd.ang = loc.m_ins_data.get_gyro();
  366. std::copy(loc.m_ins_data.m_acc_data.begin(), loc.m_ins_data.m_acc_data.end(), sd.m_acc_data.begin());
  367. std::copy(loc.m_ins_data.m_ang_data.begin(), loc.m_ins_data.m_ang_data.end(), sd.m_ang_data.begin());
  368. sd.cur_time = now;
  369. swsTimerThrd.upt_sb_data(std::move(sd));
  370. logn_info(3,
  371. "[solid_ball] cid='%s', stamp=%d, sinfo='%s', acc = %.2f, acc_x = %.2f, acc_y = %.2f, acc_z = %.2f, ang = %.2f, ang_x = %.2f, ang_y = %.2f, ang_z = %.2f, t = '%s')",
  372. sid.c_str(),
  373. m_ct,
  374. site_info.c_str(),
  375. loc.m_ins_data.get_acc(),
  376. loc.m_ins_data.get_acc(0),
  377. loc.m_ins_data.get_acc(1),
  378. loc.m_ins_data.get_acc(2),
  379. loc.m_ins_data.get_gyro(),
  380. loc.m_ins_data.get_gyro(0),
  381. loc.m_ins_data.get_gyro(1),
  382. loc.m_ins_data.get_gyro(2),
  383. now.c_str()
  384. );
  385. //清除数据
  386. m_msg_list.erase(itm);
  387. //m_message_handle->on_message(loop, loc, is_history);*/
  388. }
  389. /*
  390. * tdoa 调用算法库定位
  391. * */
  392. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  393. {
  394. log_info("[tdoa] start calc location.");
  395. m_ct = loc.m_card_msg.m_sync_num;
  396. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  397. if(!site_ptr){
  398. 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);
  399. return;
  400. }
  401. auto area_tool=get_area_tool();
  402. area_tool->set_site(site_ptr);
  403. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  404. if(site_ptr->is_up_site())
  405. {}
  406. else{
  407. m_message_handle->on_message(loop, loc, is_history);
  408. }
  409. }
  410. /*
  411. * pdoa 调用算法库定位
  412. *
  413. * */
  414. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  415. {
  416. m_ct = loc.m_card_ct;
  417. m_time = loc.m_time_stamp;
  418. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  419. if(!site_ptr){
  420. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  421. return;
  422. }
  423. site_ptr->m_loc_time = time(0);
  424. // 基站覆盖距离检测
  425. do_site_coverage(loc);
  426. auto area_tool=get_area_tool();
  427. area_tool->set_site(site_ptr);
  428. m_battery_value = loc.m_batty_status; // 电量值
  429. handle_message(loc.m_card_ct, loc.m_batty_status);
  430. if(site_ptr->is_up_site()){
  431. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  432. area_tool->on_point(shared_from_this(), point(1,1));
  433. this->site_hover(loc.m_site_id);
  434. m_last_site_id = site_ptr->id();
  435. //m_last_down_stream_idx = site_ptr->m_down_stream_idx;
  436. m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
  437. }
  438. else{
  439. char sql[1024] = {0};
  440. 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());
  441. db_tool::PushAsync(sql);
  442. log_info("[sql] %s", sql);
  443. do_site_locate_event(loc);
  444. do_card_locate_event(loc);
  445. m_message_handle->on_message(loop, loc, is_history);
  446. }
  447. }
  448. //前端推送位置函数.
  449. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  450. {
  451. point _p;
  452. if(pt.empty())
  453. {
  454. _p = *this;
  455. pt = _p;
  456. }
  457. else
  458. _p = pt;
  459. cp.Type = m_type;
  460. cp.ID = m_id;
  461. cp.speed = abs(ceil(m_speed));
  462. cp.x = tool_other::round(_p.x,3);
  463. cp.y = tool_other::round(_p.y,3);
  464. cp.running_stat = m_stat;
  465. cp.dept_id = m_deptid;
  466. cp.display = m_display;
  467. cp.rec_time = m_time;
  468. cp.level_id = m_level_id;
  469. cp.battery_val = m_battery_value;
  470. if(m_pwr_stat > 0){
  471. cp.battery_stat = (m_pwr_stat<3?1:0);
  472. }
  473. cp.m_freq = m_freq;
  474. swsTimerThrd.upt_card_pos(cp);
  475. }
  476. void card_location_base::del_card_pos()
  477. {
  478. sys::_CARD_POS_ cp;
  479. cp.ID = m_id;
  480. cp.Type=m_type;
  481. swsTimerThrd.del_card_pos(cp);
  482. }
  483. int card_location_base::get_stat()
  484. {
  485. //盲区>呼救>呼叫>超时>超速>正常
  486. uint64_t now = time(0)*1000;
  487. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  488. if(tlost>CARD_LOST_TIME_OUT)
  489. {
  490. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  491. return STATUS_LOST;
  492. }
  493. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  494. {
  495. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  496. }
  497. else if(CALL_NONE != get_mine_tool()->m_status_call)
  498. {
  499. return STATUS_CALL;
  500. }
  501. else if(is_person())
  502. {
  503. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  504. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  505. return STATUS_AREA_OVER_TIME;
  506. }
  507. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  508. {
  509. return STATUS_OVER_SPEED;
  510. }
  511. return STATUS_NORMAL;
  512. }
  513. void card_location_base::clear()
  514. {
  515. // uint16_t m_display; //1显示0不显示,往前端推送
  516. m_speed=0; //速度
  517. m_stat=0; //运动静止状态
  518. //m_ct; //ct
  519. m_time=0; //时间戳
  520. //pdoa location info clear
  521. m_buff_size = 0;
  522. m_last_recv_time = 0;
  523. m_last_point = point(0,0);
  524. m_last_site_id = 0;
  525. m_last_site_dir = -1;
  526. m_last_ct = 0;
  527. m_last_dist = 0.0;
  528. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  529. m_last_over_site = false;
  530. m_cache_nums = 0;
  531. m_cb_pdoa.clear();
  532. m_cb_tof.clear();
  533. m_cb_point.clear();
  534. }
  535. void card_location_base::put_traffic_light(card_pos& cp)
  536. {
  537. if(!traffic_light_flag){
  538. return;
  539. }
  540. light_message lm;
  541. lm.m_cmd = cmd_card_data;
  542. lm.m_pos.x = cp.x;
  543. lm.m_pos.y = cp.y;
  544. lm.m_pos.z = cp.z;
  545. lm.m_pos.m_card_id = m_id;
  546. lm.m_pos.m_type = m_type;
  547. lm.m_pos.m_site_id = cp.reader_id;
  548. auto site_ptr = get_area_tool()->m_site;
  549. if(site_ptr){
  550. lm.m_pos.m_area_id = site_ptr->m_area_id;
  551. }
  552. //lm.m_pos.m_bigger = 0;
  553. lm.m_pos.m_speed = m_speed;
  554. 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);
  555. traffic_light_manager::instance()->put(lm);
  556. }
  557. bool card_location_base::is_person() const
  558. {
  559. return tool_other::is_person(m_type);
  560. }
  561. bool card_location_base::is_vehicle() const
  562. {
  563. return tool_other::is_vehicle(m_type);
  564. }
  565. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  566. m_cid = cid;
  567. m_type = type;
  568. m_deptid = deptid;
  569. m_level_id = level_id;
  570. m_his_location_card->set_cid(cid);
  571. }
  572. card_location_base::~card_location_base()
  573. {
  574. }
  575. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  576. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  577. {
  578. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  579. }
  580. /*
  581. * @brief 创建车辆对象
  582. * @param const std::string& type
  583. * @param uint32_t cardid 卡id
  584. * @param uint16_t needdisplay 是否需要显示
  585. * @param int16_t t
  586. * @param int32_t deptid 部门id
  587. * @param int32_t categoryid
  588. * @param int type_id 卡类型id
  589. * @param int32_t level_id 职级id
  590. * @param uint32_t cid
  591. * @return 车辆对象
  592. * @note
  593. * @bug
  594. * @warning
  595. * */
  596. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  597. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  598. {
  599. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  600. }
  601. /*
  602. * @brief 基站覆盖距离检测
  603. * @param message_pdoa_locinfo& loc 卡数据
  604. * @return 无
  605. * @note
  606. * @bug
  607. * @warning
  608. * */
  609. void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
  610. {
  611. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  612. if(!site_ptr){
  613. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  614. return;
  615. }
  616. double pdoa = tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset);
  617. double dist = loc.m_tof*15.65*2.996*1e-4;
  618. bool is_update = false;
  619. if(pdoa > 0){
  620. if(dist > site_ptr->m_coverage[0].m_distance){
  621. site_ptr->m_coverage[0].m_distance = dist;
  622. site_ptr->m_coverage[0].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  623. site_ptr->m_coverage[0].m_time = time(0);
  624. is_update = true;
  625. }
  626. }else{
  627. if(dist > site_ptr->m_coverage[1].m_distance){
  628. site_ptr->m_coverage[1].m_distance = dist;
  629. site_ptr->m_coverage[1].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  630. site_ptr->m_coverage[1].m_time = time(0);
  631. is_update = true;
  632. }
  633. }
  634. if(is_update){
  635. char sql[1024] = {0};
  636. 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());
  637. db_tool::PushAsync(sql);
  638. logn_info(2, "[sql] %s", sql);
  639. }
  640. }
  641. /*
  642. * @brief 卡定位异常检测
  643. * @param message_pdoa_locinfo& loc 卡数据
  644. * @return 无
  645. * @note
  646. * @bug
  647. * @warning
  648. * */
  649. void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
  650. {
  651. bool status = false;
  652. status = ((loc.m_tof == 0)?true:false);
  653. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  654. event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);
  655. //log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
  656. }
  657. /*
  658. * @brief 基站定位异常检测
  659. * @param message_pdoa_locinfo& loc 卡数据
  660. * @return 无
  661. * @note
  662. * @bug
  663. * @warning
  664. * */
  665. void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
  666. {
  667. //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);
  668. if(m_last_ct <= 0 || m_last_site_id == 0){
  669. return;
  670. }
  671. if(loc.m_site_id != m_last_site_id){
  672. return;
  673. }
  674. int dt = 0;
  675. if(loc.m_card_ct > m_last_ct){
  676. dt = loc.m_card_ct - m_last_ct;
  677. }else{
  678. dt = 65535 + loc.m_card_ct - m_last_ct;
  679. }
  680. event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
  681. 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);
  682. }