1
0

card_base.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. module_mgr::do_status((STATUS_CARD)st, m_id, m_type);
  72. }
  73. void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclose /*= false*/)
  74. {
  75. int sid=0,mapid=0;
  76. if(auto site_ptr=get_area_tool()->m_site)
  77. {
  78. sid=site_ptr->m_area_id;
  79. mapid=site_ptr->m_map_id;
  80. }
  81. m_his_location_card->push(t,pt,sid,mapid,bclose);
  82. }
  83. void card_location_base::on_location(const std::vector<point>&vp,const std::vector<loc_message> &lm )
  84. {
  85. loc_point pt = m_sel_tool->select_solution(vp,lm);
  86. pt.y=-pt.y;
  87. auto site_ptr=get_area_tool()->m_site;
  88. int sid=0;
  89. if(site_ptr)sid=site_ptr->m_id;
  90. if(pt.m_useless)
  91. {
  92. x = tool_other::round(pt.x,3);
  93. y = tool_other::round(pt.y,3);
  94. double acc = lm[0].m_acc;
  95. m_acc = lm[0].m_acc;
  96. log_info("useful:type:%d,card_id:%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);
  97. do_business(lm.front().m_sit, pt, acc);
  98. }
  99. else
  100. {
  101. log_warn("坐标不可用:site_id:%d,type:%d,card_id:%d,ct:%d,x:%f,y:%f",sid,m_type,m_id,m_ct,pt.x,pt.y);
  102. }
  103. }
  104. void card_location_base::on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
  105. {
  106. m_ct = loc.m_card_ct;
  107. m_time = loc.m_time_stamp;
  108. auto site_ptr = sit_list::instance()->get(loc.m_site_id);
  109. if(!site_ptr)
  110. {
  111. log_warn("接收到分站%d的数据,CARD=%d, CT=%d,但是分站未定义",loc.m_site_id,m_id,loc.m_card_ct);
  112. return;
  113. }
  114. auto area_tool=get_area_tool();
  115. area_tool->set_site(site_ptr);
  116. if(site_ptr->is_up_site())
  117. {
  118. log_info("%d被井上分站[%d]收到",m_id,site_ptr->id());
  119. area_tool->on_point(shared_from_this(),point(1,1));
  120. this->site_hover(loc.m_site_id);
  121. }
  122. else
  123. {
  124. if(site_ptr->is_path_empty())
  125. {
  126. log_warn("接收到分站%d的数据,CT=%d,但是分站路径为空",site_ptr->id(),loc.m_card_ct);
  127. }
  128. m_message_handle->on_message(loop,loc,is_history);
  129. }
  130. }
  131. struct ld_1318
  132. {
  133. ld_1318(double a,double b,double c)
  134. :x(a)
  135. ,y(b)
  136. ,speed(c)
  137. {}
  138. double x;
  139. double y;
  140. double speed;
  141. };
  142. //前端推送位置函数.
  143. void card_location_base::upt_card_pos(YA::_CARD_POS_&cp, point &pt)
  144. {
  145. // YA::_CARD_POS_ cp;
  146. point _p;
  147. if(pt.empty())
  148. {
  149. _p=*this;pt=_p;
  150. }
  151. else
  152. _p=pt;
  153. //cp.z = pt.z;
  154. cp.Type=m_type;
  155. cp.ID = m_id;
  156. // if(m_type==4 && m_id==1318)
  157. // {
  158. // static std::list<ld_1318> list_1318;
  159. // if(list_1318.size()>=60)
  160. // {
  161. // double tx=0,ty=0,ts=0;
  162. // std::for_each(list_1318.begin(),list_1318.end(),[&tx,&ty,&ts](const ld_1318& ld){
  163. // tx+=ld.x;ty=ld.y;ts+=ld.speed;
  164. // });
  165. // size_t n=list_1318.size();
  166. // tx/=n;ts/=n;
  167. // _p.set(tx,ty,0);
  168. // cp.speed = ts;
  169. // list_1318.pop_front();
  170. // log_info("get_speed:%d (%f,%f), %f",m_id,tx,ty,ts);
  171. // }
  172. // else
  173. // _p=*this;
  174. // double speed = abs((int)(m_speed*10000)/10000.);
  175. // list_1318.emplace_back(x,y,speed);
  176. // }
  177. // else
  178. cp.speed = abs(ceil(m_speed));
  179. cp.x = tool_other::round(_p.x,3);
  180. cp.y = tool_other::round(_p.y,3);
  181. cp.running_stat = m_stat;
  182. cp.dept_id = m_deptid;
  183. cp.display=m_display;
  184. cp.rec_time=m_time;
  185. cp.level_id = m_level_id;
  186. swsTimerThrd.upt_card_pos(cp);
  187. }
  188. void card_location_base::del_card_pos()
  189. {
  190. YA::_CARD_POS_ cp;
  191. cp.ID = m_id;
  192. cp.Type=m_type;
  193. swsTimerThrd.del_card_pos(cp);
  194. }
  195. int card_location_base::get_stat()
  196. {
  197. //盲区>呼救>呼叫>超时>超速>正常
  198. uint64_t now = time(0)*1000;
  199. uint64_t tlost=now>m_time?now-m_time:m_time-now;
  200. log_info("card_lost:%03d%010d,[%lu-%lu]tlost:%lu---%s",m_type,m_id,now,m_time,tlost,tlost>CARD_LOST_TIME_OUT?"True":"False");
  201. if(tlost>CARD_LOST_TIME_OUT)
  202. {
  203. return STATUS_LOST;
  204. }
  205. else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))
  206. {
  207. return (ES_DEAL_HELP == ev_ptr->m_status) ? STATUS_HELP_DEALED : STATUS_HELP;
  208. }
  209. else if(CALL_NONE != get_mine_tool()->m_status_call)
  210. {
  211. return STATUS_CALL;
  212. }
  213. else if(is_person())
  214. {
  215. if(event_list::instance()->get_event_card(m_id, m_type,ET_CARD_AREA_OVER_TIME_PERSON)||
  216. event_list::instance()->get_event_card(m_id, m_type,ET_CARD_OVER_TIME_PERSON))
  217. return STATUS_AREA_OVER_TIME;
  218. }
  219. else if(event_list::instance()->get_event_card(m_id, m_type, ET_CARD_OVER_SPEED))
  220. {
  221. return STATUS_OVER_SPEED;
  222. }
  223. return STATUS_NORMAL;
  224. }
  225. void card_location_base::clear()
  226. {
  227. // uint16_t m_display; //1显示0不显示,往前端推送
  228. m_speed=0; //速度
  229. m_stat=0; //运动静止状态
  230. //m_ct; //ct
  231. m_time=0; //时间戳
  232. }
  233. void card_location_base::put_three_rates(card_pos & cp)
  234. {
  235. if(!three_rates_flag)return;
  236. cp.rec_time=m_time;cp.type=m_type;cp.id=m_id;
  237. cp.identifier_id=m_cid;cp.running_stat=m_stat;cp.final_v=m_speed;
  238. cp.dpt_id = m_deptid;
  239. std::shared_ptr<area_tool> _areatool = get_area_tool();
  240. if(nullptr != _areatool && nullptr != _areatool->m_site)
  241. {
  242. cp.reader_x = _areatool->m_site->x;
  243. cp.reader_y = _areatool->m_site->y;
  244. cp.reader_id = _areatool->m_site->m_id;
  245. }
  246. log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
  247. three_rates::get_instance()->put(cp);
  248. }
  249. bool card_location_base::is_person() const
  250. {
  251. return tool_other::is_person(m_type);
  252. }
  253. bool card_location_base::is_vehicle() const
  254. {
  255. return tool_other::is_vehicle(m_type);
  256. }
  257. void card_location_base::set_base_data(uint32_t cid,uint16_t type,uint32_t deptid,int32_t level_id) {
  258. m_cid=cid;m_type=type;m_deptid=deptid;m_level_id=level_id;
  259. m_his_location_card->set_cid(cid);
  260. }
  261. card_location_base::~card_location_base()
  262. {
  263. }
  264. std::shared_ptr<card_location_base> card_location_base::make_person(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  265. int32_t deptid,int32_t level_id,uint32_t cid,int wl,const std::string &sname,const std::string & dname,int worktype_id)
  266. {
  267. return std::make_shared<person>(type,cardid,needdisplay,t, deptid,level_id,cid,wl,sname,dname,worktype_id);
  268. }
  269. std::shared_ptr<card_location_base> card_location_base::make_car(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,
  270. int32_t deptid, int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  271. {
  272. return std::make_shared<car>(type,cardid,needdisplay,t, deptid, categoryid, type_id,level_id,cid);
  273. }