1
0

card_base.cpp 11 KB

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