card_base.cpp 9.0 KB

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