card_base.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "three_rates.h"
  12. #include "his_location.h"
  13. #include "event.h"
  14. #include "module_service/module_call.h"
  15. #include "mine.h"
  16. #include "common_tool.h"
  17. #include "ant.h"
  18. #include "area.h"
  19. #include "loc_point.h"
  20. #include "loc_message.h"
  21. #include "module_service/module_traffic_light_manager.h"
  22. #include "db/db_tool.h"
  23. #include "tool_time.h"
  24. extern config_file config;
  25. 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)
  26. :card(id, dis, t, deptid, level_id, cid)
  27. ,m_display_old(dis)
  28. {
  29. select_tool_manage::instance()->create_tool(type, m_sel_tool, m_smo_tool);
  30. m_his_location_card.reset(new location_card(m_id, m_type, cid));
  31. m_cb_pdoa.set_capacity(5);
  32. m_cb_tof.set_capacity(5);
  33. m_cb_point.set_capacity(5);
  34. for(int index = 0; index < 5; ++index){
  35. m_cb_pdoa.push_back(100);
  36. m_cb_tof.push_back(0);
  37. m_cb_point.push_back(point(0,0));
  38. }
  39. }
  40. void card_location_base::do_status(int st)
  41. {
  42. time_t now=time(0);
  43. bool help_flag=false;
  44. if((m_help_bit & 1) && (st & STATUS_HELP))
  45. {
  46. // 1111111111
  47. // ^
  48. m_help_last_time=now;
  49. }
  50. else if((m_help_bit & 1) && (st & STATUS_HELP)==0)
  51. {
  52. // 11111111100000
  53. // ^
  54. m_help_bit<<=1;
  55. }
  56. else if((m_help_bit & 1)==0 && (st & STATUS_HELP))
  57. {
  58. // 00000000011111
  59. // ^
  60. if((m_help_bit&0x3)==2)
  61. {
  62. log_warn("handle_m_help,card_id:%d\n",m_id);
  63. help_flag=true;
  64. }
  65. m_help_last_time=now;
  66. m_help_bit<<=1;
  67. m_help_bit|=1;
  68. }
  69. else
  70. {
  71. // 11111111100000
  72. // ^
  73. if(now-m_help_last_time>60)
  74. {
  75. m_help_bit=0;
  76. }
  77. }
  78. if(!help_flag)
  79. {
  80. st = st & (0xFFFFFFFF ^ STATUS_HELP);
  81. }
  82. if((STATUS_POWER_LOWER_SERIOUS & st) != 0)
  83. {
  84. m_pwr_stat=STATUS_POWER_LOWER_SERIOUS;
  85. }
  86. else
  87. {
  88. m_pwr_stat=0;
  89. }
  90. module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
  91. }
  92. //写入历史轨迹
  93. void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclose /*= false*/)
  94. {
  95. int area_id = 0, map_id = 0, site_id = 0;
  96. double scale = 0.0;
  97. if(auto site_ptr = get_area_tool()->m_site)
  98. {
  99. area_id = site_ptr->m_area_id;
  100. map_id = site_ptr->m_map_id;
  101. site_id = site_ptr->m_id;
  102. scale = site_ptr->m_scale;
  103. }
  104. //m_his_location_card->push(t,pt,area_id,map_id,site_id,bclose);
  105. m_his_location_card->insert(t, pt, area_id, map_id, site_id, scale);
  106. }
  107. //坐标点输入业务入口
  108. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  109. {
  110. loc_point pt = m_sel_tool->select_solution(vp, lm);
  111. //pt.y = pt.y;
  112. auto site_ptr = get_area_tool()->m_site;
  113. int sid = 0;
  114. if(site_ptr)
  115. sid = site_ptr->m_id;
  116. if(pt.m_useless)
  117. {
  118. x = tool_other::round(pt.x,3);
  119. y = tool_other::round(pt.y,3);
  120. double acc = lm[0].m_acc;
  121. m_acc = lm[0].m_acc;
  122. 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);
  123. do_business(lm.front().m_sit, pt, acc);
  124. }
  125. else
  126. {
  127. 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);
  128. }
  129. }
  130. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  131. {
  132. m_ct = loc.m_card_ct;
  133. m_time = loc.m_time_stamp;
  134. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  135. if(!site_ptr)
  136. {
  137. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  138. return;
  139. }
  140. auto area_tool=get_area_tool();
  141. area_tool->set_site(site_ptr);
  142. handle_message(loc.m_card_ct,loc.m_batty_status);
  143. if(site_ptr->is_up_site())
  144. {
  145. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  146. area_tool->on_point(shared_from_this(),point(1,1));
  147. this->site_hover(loc.m_site_id);
  148. }
  149. else
  150. {
  151. if(site_ptr->is_path_empty())
  152. {
  153. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  154. return;
  155. }
  156. m_message_handle->on_message(loop,loc,is_history);
  157. }
  158. }
  159. /*
  160. * tdoa 调用算法库定位
  161. * */
  162. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  163. {
  164. log_info("[tdoa] start calc location.");
  165. m_ct = loc.m_card_msg.m_sync_num;
  166. auto site_ptr = sit_list::instance()->get(loc.m_site_msg.m_site_id);
  167. if(!site_ptr){
  168. 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);
  169. return;
  170. }
  171. auto area_tool=get_area_tool();
  172. area_tool->set_site(site_ptr);
  173. handle_message(loc.m_card_msg.m_time_stamp, loc.m_card_msg.m_battery_status);
  174. if(site_ptr->is_up_site())
  175. {}
  176. else{
  177. m_message_handle->on_message(loop, loc, is_history);
  178. }
  179. }
  180. /*
  181. * pdoa 调用算法库定位
  182. *
  183. * */
  184. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  185. {
  186. m_ct = loc.m_card_ct;
  187. m_time = loc.m_time_stamp;
  188. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  189. if(!site_ptr){
  190. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  191. return;
  192. }
  193. auto area_tool=get_area_tool();
  194. area_tool->set_site(site_ptr);
  195. // 此条件用于解决井下有的基站不能送出电量值时,会将卡的电量值按0输出,这种情况,默认卡的电量为初始值或之前值
  196. if(loc.m_batty_status > 0){
  197. m_battery_value = loc.m_batty_status; // 电量值
  198. }
  199. handle_message(loc.m_card_ct, loc.m_batty_status);
  200. if(site_ptr->is_up_site()){
  201. log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
  202. area_tool->on_point(shared_from_this(), point(1,1));
  203. this->site_hover(loc.m_site_id);
  204. }
  205. else{
  206. char sql[1024] = {0};
  207. 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());
  208. db_tool::PushAsync(sql);
  209. log_info("[sql] %s", sql);
  210. m_message_handle->on_message(loop, loc, is_history);
  211. }
  212. }
  213. //前端推送位置函数.
  214. void card_location_base::upt_card_pos(sys::_CARD_POS_&cp, point &pt)
  215. {
  216. point _p;
  217. if(pt.empty())
  218. {
  219. _p = *this;
  220. pt = _p;
  221. }
  222. else
  223. _p = pt;
  224. cp.Type = m_type;
  225. cp.ID = m_id;
  226. cp.speed = abs(ceil(m_speed));
  227. cp.x = tool_other::round(_p.x,3);
  228. cp.y = tool_other::round(_p.y,3);
  229. cp.running_stat = m_stat;
  230. cp.dept_id = m_deptid;
  231. cp.display = m_display;
  232. cp.rec_time = m_time;
  233. cp.level_id = m_level_id;
  234. cp.battery_val = m_battery_value;
  235. if(m_pwr_stat > 0){
  236. cp.battery_stat = (m_pwr_stat<3?1:0);
  237. }
  238. cp.m_freq = m_freq;
  239. swsTimerThrd.upt_card_pos(cp);
  240. }
  241. void card_location_base::del_card_pos()
  242. {
  243. sys::_CARD_POS_ cp;
  244. cp.ID = m_id;
  245. cp.Type=m_type;
  246. swsTimerThrd.del_card_pos(cp);
  247. }
  248. int card_location_base::get_stat()
  249. {
  250. //盲区>呼救>呼叫>超时>超速>正常
  251. uint64_t now = time(0)*1000;
  252. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  253. if(tlost>CARD_LOST_TIME_OUT)
  254. {
  255. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  256. return STATUS_LOST;
  257. }
  258. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  259. {
  260. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  261. }
  262. else if(CALL_NONE != get_mine_tool()->m_status_call)
  263. {
  264. return STATUS_CALL;
  265. }
  266. else if(is_person())
  267. {
  268. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  269. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  270. return STATUS_AREA_OVER_TIME;
  271. }
  272. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  273. {
  274. return STATUS_OVER_SPEED;
  275. }
  276. return STATUS_NORMAL;
  277. }
  278. void card_location_base::clear()
  279. {
  280. // uint16_t m_display; //1显示0不显示,往前端推送
  281. m_speed=0; //速度
  282. m_stat=0; //运动静止状态
  283. //m_ct; //ct
  284. m_time=0; //时间戳
  285. //pdoa location info clear
  286. m_buff_size = 0;
  287. m_last_recv_time = 0;
  288. m_last_point = point(0,0);
  289. m_last_site_id = 0;
  290. m_last_site_dir = -1;
  291. m_last_ct = 0;
  292. m_last_dist = 0.0;
  293. m_pdoa_diff = m_last_pdoa_diff = 100.0;
  294. m_last_over_site = false;
  295. m_cache_nums = 0;
  296. m_cb_pdoa.clear();
  297. m_cb_tof.clear();
  298. m_cb_point.clear();
  299. }
  300. void card_location_base::put_three_rates(card_pos & cp)
  301. {
  302. if(!three_rates_flag)
  303. return;
  304. cp.rec_time = m_time;
  305. cp.type = m_type;
  306. cp.id = m_id;
  307. cp.identifier_id= m_cid;
  308. cp.running_stat = m_stat;
  309. cp.final_v = m_speed;
  310. cp.dpt_id = m_deptid;
  311. std::shared_ptr<area_tool> _areatool = get_area_tool();
  312. if(nullptr != _areatool && nullptr != _areatool->m_site)
  313. {
  314. cp.reader_x = _areatool->m_site->x;
  315. cp.reader_y = _areatool->m_site->y;
  316. cp.reader_id = _areatool->m_site->m_id;
  317. }
  318. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  319. three_rates::get_instance()->put(cp);
  320. }
  321. void card_location_base::put_traffic_light(card_pos& cp)
  322. {
  323. if(!traffic_light_flag){
  324. return;
  325. }
  326. light_message lm;
  327. lm.m_cmd = cmd_card_data;
  328. lm.m_pos.x = cp.x;
  329. lm.m_pos.y = cp.y;
  330. lm.m_pos.z = cp.z;
  331. lm.m_pos.m_card_id = m_id;
  332. lm.m_pos.m_type = m_type;
  333. lm.m_pos.m_site_id = cp.reader_id;
  334. auto site_ptr = get_area_tool()->m_site;
  335. if(site_ptr){
  336. lm.m_pos.m_area_id = site_ptr->m_area_id;
  337. }
  338. //lm.m_pos.m_bigger = 0;
  339. lm.m_pos.m_speed = m_speed;
  340. 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);
  341. traffic_light_manager::instance()->put(lm);
  342. }
  343. bool card_location_base::is_person() const
  344. {
  345. return tool_other::is_person(m_type);
  346. }
  347. bool card_location_base::is_vehicle() const
  348. {
  349. return tool_other::is_vehicle(m_type);
  350. }
  351. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  352. m_cid = cid;
  353. m_type = type;
  354. m_deptid = deptid;
  355. m_level_id = level_id;
  356. m_his_location_card->set_cid(cid);
  357. }
  358. card_location_base::~card_location_base()
  359. {
  360. }
  361. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  362. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  363. {
  364. return std::make_shared<person>(type, cardid, needdisplay, t, deptid, level_id, cid, wl, sname, dname, worktype_id);
  365. }
  366. /*
  367. * @brief 创建车辆对象
  368. * @param const std::string& type
  369. * @param uint32_t cardid 卡id
  370. * @param uint16_t needdisplay 是否需要显示
  371. * @param int16_t t
  372. * @param int32_t deptid 部门id
  373. * @param int32_t categoryid
  374. * @param int type_id 卡类型id
  375. * @param int32_t level_id 职级id
  376. * @param uint32_t cid
  377. * @return 车辆对象
  378. * @note
  379. * @bug
  380. * @warning
  381. * */
  382. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type, uint32_t cardid, uint16_t needdisplay, int16_t t,
  383. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  384. {
  385. return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
  386. }