card_base.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if(auto site_ptr=get_area_tool()->m_site)
  95. {
  96. area_id=site_ptr->m_area_id;
  97. map_id=site_ptr->m_map_id;
  98. site_id=site_ptr->m_id;
  99. }
  100. log_info("make_his_location: card_id: %d,map_id: %d,area_id: %d,site_id: %d",m_id,map_id,area_id,site_id);
  101. m_his_location_card->push(t,pt,area_id,map_id,site_id,bclose);
  102. }
  103. //坐标点输入业务入口
  104. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  105. {
  106. log_info("[business] card_location_base::on_location");
  107. loc_point pt = m_sel_tool->select_solution(vp, lm);
  108. pt.y = -pt.y;
  109. auto site_ptr = get_area_tool()->m_site;
  110. int sid = 0;
  111. if(site_ptr)
  112. sid = site_ptr->m_id;
  113. if(pt.m_useless)
  114. {
  115. x = tool_other::round(pt.x,3);
  116. y = tool_other::round(pt.y,3);
  117. double acc = lm[0].m_acc;
  118. m_acc = lm[0].m_acc;
  119. log_info("useful:type=%d,card=%d,site=%d,ct=%d,timestamp=%llu, loc_point,x=%f,y=%f acc=%.2f",m_type,m_id,sid,m_ct,m_time,x,y,acc);
  120. do_business(lm.front().m_sit, pt, acc);
  121. }
  122. else
  123. {
  124. 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);
  125. }
  126. }
  127. void card_location_base::on_message(zloop<task*>* loop, message_locinfo& loc,bool is_history)
  128. {
  129. m_ct = loc.m_card_ct;
  130. m_time = loc.m_time_stamp;
  131. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  132. if(!site_ptr)
  133. {
  134. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  135. return;
  136. }
  137. auto area_tool=get_area_tool();
  138. area_tool->set_site(site_ptr);
  139. handle_message(loc.m_card_ct,loc.m_batty_status);
  140. if(site_ptr->is_up_site())
  141. {
  142. log_info("card=%d被井上分站[site=%d]收到",m_id,site_ptr->id());
  143. area_tool->on_point(shared_from_this(),point(1,1));
  144. this->site_hover(loc.m_site_id);
  145. }
  146. else
  147. {
  148. if(site_ptr->is_path_empty())
  149. {
  150. log_warn("接收到分站的数据,site=%d,card=%d,ct=%d,但是分站路径为空",site_ptr->id(),m_id,loc.m_card_ct);
  151. return;
  152. }
  153. m_message_handle->on_message(loop,loc,is_history);
  154. }
  155. }
  156. /*
  157. * tdoa 调用算法库定位
  158. * */
  159. void card_location_base::on_message(zloop<task*>* loop, message_tdoa_locinfo& loc, bool is_history)
  160. {
  161. log_info("[tdoa] start calc location.");
  162. m_ct = loc.m_card_msg.m_sync_num;
  163. //m_time = loc.m_time_stamp;
  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. m_message_handle->on_message(loop, loc, is_history);
  170. }
  171. /*
  172. * pdoa 调用算法库定位
  173. *
  174. * */
  175. void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& loc, bool is_history)
  176. {
  177. //log_info("[pdoa] start calc location");
  178. m_ct = loc.m_card_ct;
  179. m_time = loc.m_time_stamp;
  180. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  181. if(!site_ptr){
  182. log_warn("[pdoa] 接收到分站的数据:site=%d, card=%d, ct=%d, 但是分站未定义", loc.m_site_id, m_id, loc.m_card_ct);
  183. return;
  184. }
  185. m_message_handle->on_message(loop, loc, is_history);
  186. }
  187. //前端推送位置函数.
  188. void card_location_base::upt_card_pos(YA::_CARD_POS_&cp, point &pt)
  189. {
  190. // YA::_CARD_POS_ cp;
  191. point _p;
  192. if(pt.empty())
  193. {
  194. _p=*this;pt=_p;
  195. }
  196. else
  197. _p=pt;
  198. //cp.z = pt.z;
  199. cp.Type=m_type;
  200. cp.ID = m_id;
  201. cp.speed = abs(ceil(m_speed));
  202. cp.x = tool_other::round(_p.x,3);
  203. cp.y = tool_other::round(_p.y,3);
  204. cp.running_stat = m_stat;
  205. cp.dept_id = m_deptid;
  206. cp.display=m_display;
  207. cp.rec_time=m_time;
  208. cp.level_id = m_level_id;
  209. cp.stat=m_pwr_stat;
  210. swsTimerThrd.upt_card_pos(cp);
  211. }
  212. void card_location_base::del_card_pos()
  213. {
  214. YA::_CARD_POS_ cp;
  215. cp.ID = m_id;
  216. cp.Type=m_type;
  217. swsTimerThrd.del_card_pos(cp);
  218. }
  219. int card_location_base::get_stat()
  220. {
  221. //盲区>呼救>呼叫>超时>超速>正常
  222. uint64_t now = time(0)*1000;
  223. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  224. if(tlost>CARD_LOST_TIME_OUT)
  225. {
  226. // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
  227. return STATUS_LOST;
  228. }
  229. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  230. {
  231. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  232. }
  233. else if(CALL_NONE != get_mine_tool()->m_status_call)
  234. {
  235. return STATUS_CALL;
  236. }
  237. else if(is_person())
  238. {
  239. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  240. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  241. return STATUS_AREA_OVER_TIME;
  242. }
  243. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  244. {
  245. return STATUS_OVER_SPEED;
  246. }
  247. return STATUS_NORMAL;
  248. }
  249. void card_location_base::clear()
  250. {
  251. // uint16_t m_display; //1显示0不显示,往前端推送
  252. m_speed=0; //速度
  253. m_stat=0; //运动静止状态
  254. //m_ct; //ct
  255. m_time=0; //时间戳
  256. }
  257. void card_location_base::put_three_rates(card_pos & cp)
  258. {
  259. if(!three_rates_flag)
  260. return;
  261. cp.rec_time = m_time;
  262. cp.type = m_type;
  263. cp.id = m_id;
  264. cp.identifier_id= m_cid;
  265. cp.running_stat = m_stat;
  266. cp.final_v = m_speed;
  267. cp.dpt_id = m_deptid;
  268. std::shared_ptr<area_tool> _areatool = get_area_tool();
  269. if(nullptr != _areatool && nullptr != _areatool->m_site)
  270. {
  271. cp.reader_x = _areatool->m_site->x;
  272. cp.reader_y = _areatool->m_site->y;
  273. cp.reader_id = _areatool->m_site->m_id;
  274. }
  275. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  276. three_rates::get_instance()->put(cp);
  277. }
  278. void card_location_base::put_traffic_light(card_pos& cp)
  279. {
  280. if(!traffic_light_flag){
  281. return;
  282. }
  283. light_message lm;
  284. lm.m_cmd = cmd_card_data;
  285. lm.m_pos.x = cp.x;
  286. lm.m_pos.y = cp.y;
  287. lm.m_pos.z = cp.z;
  288. lm.m_pos.m_card_id = m_id;
  289. lm.m_pos.m_type = m_type;
  290. auto site = site_list::instance()->get(cp.m_reader_id);
  291. lm.m_pos.m_area_id = site->m_site_id;
  292. //lm.m_pos.m_bigger = 0;
  293. lm.m_pos.m_speed = m_speed;
  294. log_info("[traffic_light] put light message to traffic light module, card_id=%d, ctype=%d, x=%.2f, y=%.2f", m_id, m_type, x, y);
  295. //traffic_light_manager::instance()->update(int(x), int(y), m_id);
  296. traffic_light_manager::instance()->put(lm);
  297. }
  298. bool card_location_base::is_person() const
  299. {
  300. return tool_other::is_person(m_type);
  301. }
  302. bool card_location_base::is_vehicle() const
  303. {
  304. return tool_other::is_vehicle(m_type);
  305. }
  306. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  307. m_cid=cid;m_type=type;m_deptid=deptid;m_level_id=level_id;
  308. m_his_location_card->set_cid(cid);
  309. }
  310. card_location_base::~card_location_base()
  311. {
  312. }
  313. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  314. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  315. {
  316. return std::make_shared<person>(type,cardid,needdisplay,t, deptid,level_id,cid,wl,sname,dname,worktype_id);
  317. }
  318. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  319. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  320. {
  321. return std::make_shared<car>(type,cardid,needdisplay,t, deptid, categoryid, type_id,level_id,cid);
  322. }