card_base.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 "sys_setting.h"
  26. #include "ya_setting.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_cell_card_tdoa(uint64_t t, const sys::tdoa_2d_data& tdoa2d)
  125. {
  126. log_info("upt_tdoa_2d_data6");
  127. m_his_location_card->insert_cell_card_tdoa(tdoa2d, t);
  128. }
  129. //坐标点输入业务入口
  130. void card_location_base::on_location(const std::vector<point>&vp, const std::vector<loc_message> &lm)
  131. {
  132. auto site_ptr = get_area_tool()->m_site;
  133. int sid = 0;
  134. // 天线间间距默认为3米,实际取基站上的天线间距离配置参数
  135. double ant_dist = 3.0;
  136. if(site_ptr){
  137. sid = site_ptr->m_id;
  138. ant_dist = site_ptr->m_pdoa_offset;
  139. }else{
  140. return;
  141. }
  142. if (site_ptr->m_num_dims == 2) {
  143. if (vp.size() < 1) {
  144. return;
  145. }
  146. if (lm.size() < 1) {
  147. return;
  148. }
  149. }
  150. double ptx = 0.0;
  151. double pty = 0.0;
  152. bool useless = false;
  153. if(site_ptr->m_num_dims == 2){
  154. ptx = vp[0].x;
  155. pty = vp[0].y;
  156. useless = true;
  157. }
  158. if(useless)
  159. {
  160. if(site_ptr->m_num_dims == 2){
  161. // data inclue: site_id, dist, pdoa, speed;
  162. std::string str_info = "";
  163. // calc delay time by card's ct
  164. double dt = 0.0;
  165. if (m_ct > lm[0].m_card_ct) {
  166. dt = (m_ct - lm[0].m_card_ct) / 1000.0;
  167. }
  168. else {
  169. dt = (m_ct + 65535 - lm[0].m_card_ct) / 1000.0;
  170. }
  171. for (auto itl : lm) {
  172. auto its_info = d3_last_site_info.find(itl.m_sit->m_id);
  173. if (its_info == d3_last_site_info.end()) {
  174. sinfo si;
  175. si.sct = lm[0].m_card_ct;
  176. si.sdist = itl.m_num_ticks*15.65*2.996*1E-4;
  177. si.angle = 0.0;
  178. d3_last_site_info.insert(std::make_pair(itl.m_sit->m_id, si));
  179. its_info = d3_last_site_info.find(itl.m_sit->m_id);
  180. }
  181. // angle
  182. double angle = 0.0;
  183. // current distance for site
  184. double dist = itl.m_num_ticks*15.65*2.996*1E-4;
  185. // calc speed
  186. double v = (dist - its_info->second.sdist) / dt;
  187. // site info: id, dist, angle, speed
  188. str_info += std::to_string(itl.m_sit->m_id) + ",";
  189. str_info += std::to_string(dist) + ",";
  190. str_info += std::to_string(angle) + ",";
  191. str_info += std::to_string(v) + ";";
  192. // save data for next calc
  193. its_info->second.sct = lm[0].m_card_ct;
  194. its_info->second.sdist = dist;
  195. its_info->second.angle = angle;
  196. }
  197. // this is not add move state
  198. int move_state = 0;
  199. double dx, dy = 0.0;
  200. dx = (ptx - x) / dt;
  201. dy = (pty - y) / dt;
  202. auto lm0 = lm[0];
  203. double acc = lm0.m_ins_data.get_acc();
  204. double acc_x = lm0.m_ins_data.get_acc(0);
  205. double acc_y = lm0.m_ins_data.get_acc(1);
  206. double acc_z = lm0.m_ins_data.get_acc(2);
  207. double ang = lm0.m_ins_data.get_gyro();
  208. double ang_x = lm0.m_ins_data.get_gyro(0);
  209. double ang_y = lm0.m_ins_data.get_gyro(1);
  210. double ang_z = lm0.m_ins_data.get_gyro(2);
  211. // insert data to database
  212. char sql[1024] = { 0 };
  213. std::string cid = tool_byte::to_cardid(m_type, m_id);
  214. std::string cid_tmp = cid;
  215. std::string ctime = tool_time::to_str_ex(tool_time::now_to_ms());
  216. if (vp.size() == 1)
  217. {
  218. for (auto iter0 = lm.begin(); iter0 != lm.end(); ++iter0)
  219. {
  220. for (auto iter1 = iter0 + 1; iter1 != lm.end(); ++iter1)
  221. {
  222. for (auto iter2 = iter1 + 1; iter2 != lm.end(); ++iter2)
  223. {
  224. std::list<int> site_id_list;
  225. site_id_list.push_back(iter0->m_sit->id());
  226. site_id_list.push_back(iter1->m_sit->id());
  227. site_id_list.push_back(iter2->m_sit->id());
  228. site_id_list.sort();
  229. auto iter_begin = site_id_list.begin();
  230. int site1 = *iter_begin;
  231. iter_begin++;
  232. int site2 = *iter_begin;
  233. iter_begin++;
  234. int site3 = *iter_begin;
  235. if (true)
  236. {
  237. int i = -1;
  238. char site_list[1024] = { 0 };
  239. snprintf(site_list, 1024, "%d,%d,%d", site1, site2, site3);
  240. std::string str_site_list = site_list;
  241. int nSidIndex = CYaSetting::m_sys_setting.mp_sids_index[str_site_list];
  242. char sidIndex[1024] = { 0 };
  243. snprintf(sidIndex, 1024, "%d", nSidIndex);
  244. std::string strSidIndex = sidIndex;
  245. if (nSidIndex != 0)
  246. {
  247. cid = strSidIndex + cid_tmp;
  248. }
  249. for (int site_index = 0; site_index < vp.size(); site_index++)
  250. {
  251. std::list<int> id_list;
  252. id_list.push_back(vp[site_index].site1);
  253. id_list.push_back(vp[site_index].site2);
  254. id_list.push_back(vp[site_index].site3);
  255. id_list.sort();
  256. auto iter_ = id_list.begin();
  257. int site1_tmp = *iter_;
  258. iter_++;
  259. int site2_tmp = *iter_;
  260. iter_++;
  261. int site3_tmp = *iter_;
  262. if (site1 == site1_tmp && site2 == site2_tmp && site3 == site3_tmp)
  263. {
  264. i = site_index;
  265. }
  266. }
  267. if (i == -1)
  268. {
  269. continue;
  270. }
  271. sys::tdoa_2d_data tdoa2d(vp[i].x, vp[i].y, dx, dy, acc, acc_x, acc_y, acc_z, ang, ang_x, ang_y, ang_z, cid, vp[i].d1, vp[i].d2, vp[i].d3, ctime, str_info, move_state);
  272. swsTimerThrd.upt_tdoa_2d_data(std::move(tdoa2d));
  273. m_last_x = vp[i].x;
  274. m_last_y = vp[i].y;
  275. make_his_location_cell_card_tdoa(time(0) * 1000, tdoa2d);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. else if (vp.size() > 1)
  282. {
  283. int iSelected = 0;
  284. double min_distance = 0.0;
  285. for (auto iter0 = lm.begin(); iter0 != lm.end(); ++iter0)
  286. {
  287. for (auto iter1 = iter0 + 1; iter1 != lm.end(); ++iter1)
  288. {
  289. for (auto iter2 = iter1 + 1; iter2 != lm.end(); ++iter2)
  290. {
  291. std::list<int> site_id_list;
  292. site_id_list.push_back(iter0->m_sit->id());
  293. site_id_list.push_back(iter1->m_sit->id());
  294. site_id_list.push_back(iter2->m_sit->id());
  295. site_id_list.sort();
  296. auto iter_begin = site_id_list.begin();
  297. int site1 = *iter_begin;
  298. iter_begin++;
  299. int site2 = *iter_begin;
  300. iter_begin++;
  301. int site3 = *iter_begin;
  302. if (true)
  303. {
  304. int i = -1;
  305. char site_list[1024] = { 0 };
  306. snprintf(site_list, 1024, "%d,%d,%d", site1, site2, site3);
  307. std::string str_site_list = site_list;
  308. int nSidIndex = CYaSetting::m_sys_setting.mp_sids_index[str_site_list];
  309. char sidIndex[1024] = { 0 };
  310. snprintf(sidIndex, 1024, "%d", nSidIndex);
  311. std::string strSidIndex = sidIndex;
  312. if (nSidIndex != 0)
  313. {
  314. cid = strSidIndex + cid_tmp;
  315. }
  316. for (int site_index = 0; site_index < vp.size(); site_index++)
  317. {
  318. std::list<int> id_list;
  319. id_list.push_back(vp[site_index].site1);
  320. id_list.push_back(vp[site_index].site2);
  321. id_list.push_back(vp[site_index].site3);
  322. id_list.sort();
  323. auto iter_ = id_list.begin();
  324. int site1_tmp = *iter_;
  325. iter_++;
  326. int site2_tmp = *iter_;
  327. iter_++;
  328. int site3_tmp = *iter_;
  329. if (site1 == site1_tmp && site2 == site2_tmp && site3 == site3_tmp)
  330. {
  331. i = site_index;
  332. }
  333. log_info("site1%d,site2%d,site3%d,%d,%d,%d", site1, site2, site3, site1_tmp, site2_tmp, site3_tmp);
  334. }
  335. if (i == -1)
  336. {
  337. continue;
  338. }
  339. auto d = sqrt((m_last_x - vp[i].x) * (m_last_x - vp[i].x) + (m_last_y - vp[i].y) * (m_last_y - vp[i].y));
  340. if (d < min_distance) {
  341. iSelected = i;
  342. min_distance = d;
  343. }
  344. sys::tdoa_2d_data tdoa2d(vp[i].x, vp[i].y, dx, dy, acc, acc_x, acc_y, acc_z, ang, ang_x, ang_y, ang_z, cid, vp[i].d1, vp[i].d2, vp[i].d3, ctime, str_info, move_state);
  345. swsTimerThrd.upt_tdoa_2d_data(std::move(tdoa2d));
  346. m_last_x = vp[i].x;
  347. m_last_y = vp[i].y;
  348. }
  349. }
  350. }
  351. }
  352. // sys::tdoa_2d_data tdoa2d(vp[iSelected].x, vp[iSelected].y, dx, dy, acc, acc_x, acc_y, acc_z, ang, ang_x, ang_y, ang_z, cid, vp[iSelected].d1, vp[iSelected].d2, vp[iSelected].d3, ctime, str_info, move_state);
  353. // swsTimerThrd.upt_tdoa_2d_data(std::move(tdoa2d));
  354. // m_last_x = vp[iSelected].x;
  355. // m_last_y = vp[iSelected].y;
  356. }
  357. }
  358. }
  359. else
  360. {
  361. log_warn("坐标不可用:site=%d, type=%d, card=%d, ct=%d, x=%f, y=%f", sid, m_type, m_id, m_ct, ptx, pty);
  362. }
  363. }
  364. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  365. {
  366. m_ct = loc.m_card_ct;
  367. m_time = loc.m_time_stamp;
  368. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  369. if(!site_ptr)
  370. {
  371. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  372. return;
  373. }
  374. auto area_tool=get_area_tool();
  375. area_tool->set_site(site_ptr);
  376. handle_message(loc.m_card_ct,loc.m_batty_status);
  377. if(site_ptr->is_up_site())
  378. {
  379. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  380. area_tool->on_point(shared_from_this(),point(1,1));
  381. this->site_hover(loc.m_site_id);
  382. }
  383. else
  384. {
  385. if(site_ptr->is_path_empty())
  386. {
  387. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  388. return;
  389. }
  390. m_message_handle->on_message(loop,loc,is_history);
  391. }
  392. }
  393. /*
  394. * 实心球原始数据调用算法模块进行定位处理
  395. * */
  396. void card_location_base::on_message(zloop<task*>* loop, message_ins_locinfo& loc,bool is_history)
  397. {
  398. /*m_ct = loc.m_card_ct;
  399. m_time = loc.m_time_stamp;
  400. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  401. if(!site_ptr)
  402. {
  403. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  404. return;
  405. }*/
  406. if (false)
  407. if (loc.m_site_id == 22 || loc.m_site_id == 23)
  408. {
  409. //m_message_handle->on_message(loop, loc, is_history);
  410. log_info("[solid_ball] start calc location");
  411. auto itm = m_msg_list.find(loc.m_card_ct);
  412. if (itm == m_msg_list.end()) {
  413. std::vector<message_ins_locinfo> vtm;
  414. vtm.push_back(loc);
  415. m_msg_list.insert(std::make_pair(loc.m_card_ct, vtm));
  416. itm = m_msg_list.find(loc.m_card_ct);
  417. }
  418. else {
  419. itm->second.push_back(loc);
  420. }
  421. if (itm->second.size() < 2) {
  422. return;
  423. }
  424. // ct切换了
  425. m_ct = loc.m_card_ct;
  426. m_time = loc.m_time_stamp;
  427. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  428. if (!site_ptr)
  429. {
  430. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  431. return;
  432. }
  433. // 卡号
  434. std::string sid = tool_byte::to_cardid(m_type, m_id);
  435. std::string site_info = "";
  436. std::string now = tool_time::to_str_ex(tool_time::now_to_ms());
  437. int nIndex = 0;
  438. int siteId1 = 0;
  439. int siteId2 = 0;
  440. double distance1 = 0;
  441. double distance2 = 0;
  442. // 存入数据库
  443. for (auto itl : itm->second) {
  444. site_info += std::to_string(itl.m_site_id);
  445. double d = itl.m_tof * 15.65 * 2.996 * 1E-4;
  446. site_info += "," + std::to_string(d);
  447. site_info += ",0.0";
  448. double v = 0.0;
  449. auto its = last_site_info.find(itl.m_site_id);
  450. if (its == last_site_info.end()) {
  451. sinfo si;
  452. si.sct = m_ct;
  453. si.sdist = d;
  454. last_site_info.insert(std::make_pair(itl.m_site_id, si));
  455. v = 0.0;
  456. }
  457. else {
  458. double dt = 0.0;
  459. if (m_ct > its->second.sct) {
  460. dt = (m_ct - its->second.sct) / 1000.0;
  461. }
  462. else {
  463. dt = (m_ct + 65535 - its->second.sct) / 1000.0;
  464. }
  465. v = (d - its->second.sdist) / dt;
  466. its->second.sct = m_ct;
  467. its->second.sdist = d;
  468. }
  469. site_info += "," + std::to_string(v);
  470. site_info += ";";
  471. if (nIndex == 0)
  472. {
  473. siteId1 = itl.m_site_id;
  474. distance1 = d;
  475. nIndex++;
  476. }
  477. else if (nIndex == 1)
  478. {
  479. siteId2 = itl.m_site_id;
  480. distance2 = d;
  481. }
  482. }
  483. // 数据入库
  484. char sql[1024] = { 0 };
  485. snprintf(sql,
  486. 1024,
  487. "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')",
  488. sid.c_str(),
  489. siteId1,
  490. distance1,
  491. loc.m_ins_data.get_acc(),
  492. loc.m_ins_data.get_acc(0),
  493. loc.m_ins_data.get_acc(1),
  494. loc.m_ins_data.get_acc(2),
  495. loc.m_ins_data.get_gyro(),
  496. loc.m_ins_data.get_gyro(0),
  497. loc.m_ins_data.get_gyro(1),
  498. loc.m_ins_data.get_gyro(2),
  499. now.c_str()
  500. );
  501. db_tool::PushAsync(sql);
  502. log_info("[sql] %s", sql);
  503. memset(sql, 0, 1024);
  504. snprintf(sql,
  505. 1024,
  506. "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')",
  507. sid.c_str(),
  508. siteId2,
  509. distance2,
  510. loc.m_ins_data.get_acc(),
  511. loc.m_ins_data.get_acc(0),
  512. loc.m_ins_data.get_acc(1),
  513. loc.m_ins_data.get_acc(2),
  514. loc.m_ins_data.get_gyro(),
  515. loc.m_ins_data.get_gyro(0),
  516. loc.m_ins_data.get_gyro(1),
  517. loc.m_ins_data.get_gyro(2),
  518. now.c_str()
  519. );
  520. db_tool::PushAsync(sql);
  521. log_info("[sql] %s", sql);
  522. // 更新数据到发送线程
  523. sys::sb_data sd;
  524. sd.cid = sid;
  525. sd.speed = 0.0;
  526. sd.site_info = site_info;
  527. sd.acc = loc.m_ins_data.get_acc();
  528. sd.ang = loc.m_ins_data.get_gyro();
  529. std::copy(loc.m_ins_data.m_acc_data.begin(), loc.m_ins_data.m_acc_data.end(), sd.m_acc_data.begin());
  530. std::copy(loc.m_ins_data.m_ang_data.begin(), loc.m_ins_data.m_ang_data.end(), sd.m_ang_data.begin());
  531. sd.cur_time = now;
  532. swsTimerThrd.upt_sb_data(std::move(sd));
  533. logn_info(3,
  534. "[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')",
  535. sid.c_str(),
  536. m_ct,
  537. site_info.c_str(),
  538. loc.m_ins_data.get_acc(),
  539. loc.m_ins_data.get_acc(0),
  540. loc.m_ins_data.get_acc(1),
  541. loc.m_ins_data.get_acc(2),
  542. loc.m_ins_data.get_gyro(),
  543. loc.m_ins_data.get_gyro(0),
  544. loc.m_ins_data.get_gyro(1),
  545. loc.m_ins_data.get_gyro(2),
  546. now.c_str()
  547. );
  548. //清除数据
  549. m_msg_list.erase(itm);
  550. //m_message_handle->on_message(loop, loc, is_history);*/
  551. return;
  552. }
  553. m_ct = loc.m_card_ct;
  554. m_time = loc.m_time_stamp;
  555. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  556. if(!site_ptr)
  557. {
  558. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  559. return;
  560. }
  561. m_message_handle->on_message(loop, loc, is_history);
  562. }
  563. /*
  564. * tdoa 调用算法库定位
  565. * */
  566. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  567. {
  568. log_info("[tdoa] start calc location.");
  569. m_ct = loc.m_card_msg.m_sync_num;
  570. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  571. if(!site_ptr){
  572. 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);
  573. return;
  574. }
  575. auto area_tool=get_area_tool();
  576. area_tool->set_site(site_ptr);
  577. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  578. if(site_ptr->is_up_site())
  579. {}
  580. else{
  581. m_message_handle->on_message(loop, loc, is_history);
  582. }
  583. }
  584. /*
  585. * pdoa 调用算法库定位
  586. *
  587. * */
  588. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  589. {
  590. m_ct = loc.m_card_ct;
  591. m_time = loc.m_time_stamp;
  592. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  593. if(!site_ptr){
  594. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  595. return;
  596. }
  597. site_ptr->m_loc_time = time(0);
  598. // 基站覆盖距离检测
  599. do_site_coverage(loc);
  600. auto area_tool=get_area_tool();
  601. area_tool->set_site(site_ptr);
  602. m_battery_value = loc.m_batty_status; // 电量值
  603. handle_message(loc.m_card_ct, loc.m_batty_status);
  604. if(site_ptr->is_up_site()){
  605. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  606. area_tool->on_point(shared_from_this(), point(1,1));
  607. this->site_hover(loc.m_site_id);
  608. m_last_site_id = site_ptr->id();
  609. //m_last_down_stream_idx = site_ptr->m_down_stream_idx;
  610. m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
  611. }
  612. else{
  613. char sql[1024] = {0};
  614. 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());
  615. db_tool::PushAsync(sql);
  616. log_info("[sql] %s", sql);
  617. do_site_locate_event(loc);
  618. do_card_locate_event(loc);
  619. m_message_handle->on_message(loop, loc, is_history);
  620. }
  621. }
  622. //前端推送位置函数.
  623. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  624. {
  625. point _p;
  626. if(pt.empty())
  627. {
  628. _p = *this;
  629. pt = _p;
  630. }
  631. else
  632. _p = pt;
  633. cp.Type = m_type;
  634. cp.ID = m_id;
  635. cp.speed = abs(ceil(m_speed));
  636. cp.x = tool_other::round(_p.x,3);
  637. cp.y = tool_other::round(_p.y,3);
  638. cp.running_stat = m_stat;
  639. cp.dept_id = m_deptid;
  640. cp.display = m_display;
  641. cp.rec_time = m_time;
  642. cp.level_id = m_level_id;
  643. cp.battery_val = m_battery_value;
  644. if(m_pwr_stat > 0){
  645. cp.battery_stat = (m_pwr_stat<3?1:0);
  646. }
  647. cp.m_freq = m_freq;
  648. swsTimerThrd.upt_card_pos(cp);
  649. }
  650. void card_location_base::del_card_pos()
  651. {
  652. sys::_CARD_POS_ cp;
  653. cp.ID = m_id;
  654. cp.Type=m_type;
  655. swsTimerThrd.del_card_pos(cp);
  656. }
  657. int card_location_base::get_stat()
  658. {
  659. //盲区>呼救>呼叫>超时>超速>正常
  660. uint64_t now = time(0)*1000;
  661. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  662. if(tlost>CARD_LOST_TIME_OUT)
  663. {
  664. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  665. return STATUS_LOST;
  666. }
  667. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  668. {
  669. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  670. }
  671. else if(CALL_NONE != get_mine_tool()->m_status_call)
  672. {
  673. return STATUS_CALL;
  674. }
  675. else if(is_person())
  676. {
  677. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  678. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  679. return STATUS_AREA_OVER_TIME;
  680. }
  681. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  682. {
  683. return STATUS_OVER_SPEED;
  684. }
  685. return STATUS_NORMAL;
  686. }
  687. void card_location_base::clear()
  688. {
  689. // uint16_t m_display; //1显示0不显示,往前端推送
  690. m_speed=0; //速度
  691. m_stat=0; //运动静止状态
  692. //m_ct; //ct
  693. m_time=0; //时间戳
  694. //pdoa location info clear
  695. m_buff_size = 0;
  696. m_last_recv_time = 0;
  697. m_last_point = point(0,0);
  698. m_last_site_id = 0;
  699. m_last_site_dir = -1;
  700. m_last_ct = 0;
  701. m_last_dist = 0.0;
  702. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  703. m_last_over_site = false;
  704. m_cache_nums = 0;
  705. m_cb_pdoa.clear();
  706. m_cb_tof.clear();
  707. m_cb_point.clear();
  708. }
  709. void card_location_base::put_traffic_light(card_pos& cp)
  710. {
  711. if(!traffic_light_flag){
  712. return;
  713. }
  714. light_message lm;
  715. lm.m_cmd = cmd_card_data;
  716. lm.m_pos.x = cp.x;
  717. lm.m_pos.y = cp.y;
  718. lm.m_pos.z = cp.z;
  719. lm.m_pos.m_card_id = m_id;
  720. lm.m_pos.m_type = m_type;
  721. lm.m_pos.m_site_id = cp.reader_id;
  722. auto site_ptr = get_area_tool()->m_site;
  723. if(site_ptr){
  724. lm.m_pos.m_area_id = site_ptr->m_area_id;
  725. }
  726. //lm.m_pos.m_bigger = 0;
  727. lm.m_pos.m_speed = m_speed;
  728. 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);
  729. traffic_light_manager::instance()->put(lm);
  730. }
  731. bool card_location_base::is_person() const
  732. {
  733. return tool_other::is_person(m_type);
  734. }
  735. bool card_location_base::is_vehicle() const
  736. {
  737. return tool_other::is_vehicle(m_type);
  738. }
  739. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  740. m_cid = cid;
  741. m_type = type;
  742. m_deptid = deptid;
  743. m_level_id = level_id;
  744. m_his_location_card->set_cid(cid);
  745. }
  746. card_location_base::~card_location_base()
  747. {
  748. }
  749. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  750. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  751. {
  752. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  753. }
  754. /*
  755. * @brief 创建车辆对象
  756. * @param const std::string& type
  757. * @param uint32_t cardid 卡id
  758. * @param uint16_t needdisplay 是否需要显示
  759. * @param int16_t t
  760. * @param int32_t deptid 部门id
  761. * @param int32_t categoryid
  762. * @param int type_id 卡类型id
  763. * @param int32_t level_id 职级id
  764. * @param uint32_t cid
  765. * @return 车辆对象
  766. * @note
  767. * @bug
  768. * @warning
  769. * */
  770. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  771. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  772. {
  773. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  774. }
  775. /*
  776. * @brief 基站覆盖距离检测
  777. * @param message_pdoa_locinfo& loc 卡数据
  778. * @return 无
  779. * @note
  780. * @bug
  781. * @warning
  782. * */
  783. void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
  784. {
  785. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  786. if(!site_ptr){
  787. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  788. return;
  789. }
  790. double pdoa = tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset);
  791. double dist = loc.m_tof*15.65*2.996*1e-4;
  792. bool is_update = false;
  793. if(pdoa > 0){
  794. if(dist > site_ptr->m_coverage[0].m_distance){
  795. site_ptr->m_coverage[0].m_distance = dist;
  796. site_ptr->m_coverage[0].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  797. site_ptr->m_coverage[0].m_time = time(0);
  798. is_update = true;
  799. }
  800. }else{
  801. if(dist > site_ptr->m_coverage[1].m_distance){
  802. site_ptr->m_coverage[1].m_distance = dist;
  803. site_ptr->m_coverage[1].m_cid = tool_byte::to_cardid(loc.m_card_type, loc.m_card_id);
  804. site_ptr->m_coverage[1].m_time = time(0);
  805. is_update = true;
  806. }
  807. }
  808. if(is_update){
  809. char sql[1024] = {0};
  810. 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());
  811. db_tool::PushAsync(sql);
  812. logn_info(2, "[sql] %s", sql);
  813. }
  814. }
  815. /*
  816. * @brief 卡定位异常检测
  817. * @param message_pdoa_locinfo& loc 卡数据
  818. * @return 无
  819. * @note
  820. * @bug
  821. * @warning
  822. * */
  823. void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
  824. {
  825. bool status = false;
  826. status = ((loc.m_tof == 0)?true:false);
  827. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  828. event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);
  829. //log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
  830. }
  831. /*
  832. * @brief 基站定位异常检测
  833. * @param message_pdoa_locinfo& loc 卡数据
  834. * @return 无
  835. * @note
  836. * @bug
  837. * @warning
  838. * */
  839. void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
  840. {
  841. //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);
  842. if(m_last_ct <= 0 || m_last_site_id == 0){
  843. return;
  844. }
  845. if(loc.m_site_id != m_last_site_id){
  846. return;
  847. }
  848. int dt = 0;
  849. if(loc.m_card_ct > m_last_ct){
  850. dt = loc.m_card_ct - m_last_ct;
  851. }else{
  852. dt = 65535 + loc.m_card_ct - m_last_ct;
  853. }
  854. event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
  855. 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);
  856. }