area.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #include <memory>
  2. #include <write-copy.h>
  3. #include "db_api/CDBSingletonDefine.h"
  4. #include "log.h"
  5. #include <area.h>
  6. #include "worker.h"
  7. #include "point.h"
  8. #include "tool_time.h"
  9. #include "common_tool.h"
  10. #include "db_tool.h"
  11. #include "monkey_car/monkeycar_area.h"
  12. #include "landmark.h"
  13. #include "area_business.h"
  14. #include <boost/algorithm/string/split.hpp>
  15. #include <boost/algorithm/string/classification.hpp>
  16. #include "area_business.h"
  17. #include "card.h"
  18. #include "tool_time.h"
  19. #include "common_tool.h"
  20. #include "websocket/ws_common.h"
  21. #include "websocket/wsClientMgr.h"
  22. #include "ant.h"
  23. //template<> std::shared_ptr<area_list> single_base<area_list, int, std::shared_ptr<area>>::m_instance=std::make_shared<area_list>();
  24. struct underground_area:area
  25. {
  26. underground_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  27. :area(id,limit_count_person,limit_time_person,scale,mapid,type)
  28. {
  29. }
  30. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
  31. {
  32. return s&&!s->is_up_site();
  33. }
  34. };
  35. struct special_area:area
  36. {
  37. special_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  38. :area(id,limit_count_person,limit_time_person,scale,mapid,type)
  39. {}
  40. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
  41. {
  42. bool f=false;
  43. int id = c->get_area();
  44. if(id == m_id)
  45. {
  46. f=true;
  47. sarid=m_id;
  48. }
  49. return f;
  50. }
  51. //推入特殊区域的是否需要其他信息
  52. virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  53. {
  54. area::on_enter(a, c);
  55. YA::_CARD_POS_ cp;
  56. cp.area_id = m_id;
  57. swsClientMgr.SendSpecialAreaProcess(cp);
  58. }
  59. };
  60. area::area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  61. :m_id(id)
  62. ,m_area_type(type)
  63. ,m_limit_person_second(limit_time_person)
  64. ,m_limit_person_count(limit_count_person)
  65. ,m_scale(scale)
  66. ,m_mapid(mapid)
  67. ,m_person_count(0)
  68. ,m_vehicle_count(0)
  69. ,m_person_show_count(0)
  70. ,m_vehicle_show_count(0)
  71. {
  72. m_area_business_list=area_business::get_instance_list(m_area_type,id);
  73. m_event_person_count = false;
  74. m_event_vehicle_count = false;
  75. m_event_person_show_count = false;
  76. m_event_vehicle_show_count = false;
  77. m_frozen_count.store(0);
  78. }
  79. void area::change_business(uint32_t new_bits)
  80. {
  81. worker*w=worker::instance();
  82. uint32_t del=((m_area_type^new_bits)|m_area_type)^m_area_type;
  83. uint32_t add=((m_area_type^new_bits)|new_bits)^new_bits;
  84. task*t =task::alloc<message_change_business>();
  85. t->m_cmd_code=0x10001; //区域业务类型修改的编码
  86. t->m_hash_id=0; //这个不用
  87. auto&mcb=t->body<message_change_business>();
  88. mcb.area_id=m_id;
  89. mcb.del_list=area_business::get_instance_list(del,m_id);
  90. mcb.add_list=area_business::get_instance_list(add,m_id);
  91. mcb.new_list=area_business::get_instance_list(new_bits,m_id);
  92. mcb.ref_count.store(w->num_thread());
  93. add_frozen_count(w->num_thread()+1);
  94. w->broadcast(t);
  95. }
  96. void area::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  97. {
  98. a->m_last_time=tool_time::now_to_ms();
  99. a->m_last_point=*c;
  100. for(const auto &i:m_area_business_list)
  101. {
  102. auto &x=a->get_business_data(i->area_business_type());
  103. i->on_hover(a,c,x);
  104. }
  105. }
  106. void area::on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  107. {
  108. log_info("on_enter..%d areaId:%d",c->m_id,m_id);
  109. a->m_enter_time=tool_time::now_to_ms();
  110. a->m_enter_point=*c;
  111. for(const auto &i:m_area_business_list)
  112. {
  113. auto &x=a->get_business_data(i->area_business_type());
  114. i->on_enter(a,c,x);
  115. }
  116. }
  117. void area::on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  118. {
  119. log_info("on_load_his..%d areaId:%d",c->m_id,m_id);
  120. //a->m_last_time=tool_time::now_to_ms();
  121. a->m_last_point=*c;
  122. for(const auto &i:m_area_business_list)
  123. {
  124. auto &x=a->get_business_data(i->area_business_type());
  125. i->on_load_his(a,c,x);
  126. }
  127. }
  128. void area::on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  129. {
  130. log_info("on_leave..%d areaId:%d",c->m_id,m_id);
  131. a->m_last_time=tool_time::now_to_ms();
  132. a->m_last_point=*c;
  133. for(const auto &i:m_area_business_list)
  134. {
  135. auto &x=a->get_business_data(i->area_business_type());
  136. i->on_leave(a,c,x);
  137. }
  138. }
  139. bool area::in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int & sarid)
  140. {
  141. if(m_bound.empty())
  142. return false;
  143. int counter = 0;
  144. double xinters;
  145. point p1,p2;
  146. p1 = m_bound[0];
  147. int size = m_bound.size();
  148. for (int i=1;i<= size;i++) {
  149. p2 = m_bound[i%size];
  150. if (p.y > std::min(p1.y,p2.y)) {
  151. if (p.y <= std::max(p1.y,p2.y)) {
  152. if (p.x <= std::max(p1.x,p2.x)) {
  153. if (p1.y != p2.y) {
  154. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  155. if (p1.x == p2.x || p.x <= xinters)
  156. counter++;
  157. }
  158. }
  159. }
  160. }
  161. p1 = p2;
  162. }
  163. return (counter % 2 == 0) ? false : true;
  164. }
  165. area_list::area_list()
  166. {
  167. }
  168. void area_list::init_monkeycar_area(int id)
  169. {
  170. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  171. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle,\
  172. over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed,a.business_type \
  173. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  174. where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id";
  175. if(-1 == id)
  176. {
  177. sql.append(";");
  178. }
  179. else
  180. {
  181. sql.append(" AND a.area_id=");
  182. sql.append(std::to_string(id));
  183. sql.append(";");
  184. log_info("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
  185. }
  186. std::string Error;
  187. YADB::CDBResultSet DBRes;
  188. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  189. int nCount = DBRes.GetRecordCount( Error );
  190. if (nCount < 1)
  191. {
  192. log_error("基础数据 monkeycar area 增加或修改失败,数据库中找不到: area_id=%d", id);
  193. return ;
  194. }
  195. std::unordered_map<int,std::shared_ptr<area>> map;
  196. while ( DBRes.GetNextRecod(Error) )
  197. {
  198. int area_id = 0;
  199. DBRes.GetField( "area_id",area_id, Error );
  200. int map_id = 0;
  201. DBRes.GetField( "map_id",map_id, Error );
  202. unsigned int area_type_id = 0;
  203. DBRes.GetField( "area_type_id",area_type_id, Error );
  204. int over_count_person = 0;
  205. DBRes.GetField( "over_count_person",over_count_person, Error );
  206. int over_count_vehicle = 0;
  207. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  208. int over_time_person = 0;
  209. DBRes.GetField( "over_time_person",over_time_person, Error );
  210. int over_time_vehicle = 0;
  211. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  212. std::string path;
  213. DBRes.GetField( "path",path, Error );
  214. double monkeycar_speed = 0;
  215. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  216. std::string monkeycar_coor;
  217. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  218. double scale=0;
  219. DBRes.GetField( "scale",scale, Error );
  220. uint32_t b_type =0;
  221. DBRes.GetField( "business_type",b_type, Error );
  222. log_info("monkeycar area init_area : id:%d,path:%s",area_id, path.c_str());
  223. if(-1 == id)
  224. {
  225. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  226. da->m_default_speed = monkeycar_speed;
  227. da->m_point = init_path(monkeycar_coor,area_id);
  228. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  229. ap->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  230. ap->m_bound=init_path(path,area_id);
  231. for(const auto &p : ap->m_bound)
  232. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  233. for(const auto &p : da->m_point)
  234. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  235. map.insert({area_id,ap});
  236. }
  237. else
  238. {
  239. //这里后续需要把猴车得信息传递过去
  240. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  241. da->m_default_speed = monkeycar_speed;
  242. da->m_point = init_path(monkeycar_coor,area_id);
  243. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  244. if(auto area_=area_list::instance()->get(id))
  245. area_=ap;
  246. else
  247. area_list::instance()->add(area_id,ap);
  248. ap->m_bound=init_path(path,area_id);
  249. ap->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  250. for(const auto &p : ap->m_bound)
  251. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  252. for(const auto &p : da->m_point)
  253. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  254. log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  255. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  256. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  257. over_count_vehicle,over_time_vehicle);
  258. }
  259. }
  260. if(-1 == id)
  261. {
  262. log_info( "monkeycar area init_area. The record count=%d\n", nCount );
  263. area_list::instance()->add(map);
  264. }
  265. }
  266. std::shared_ptr<area> area_list::create(int type,int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type)
  267. {
  268. if(type==AREA_TYPE_FORBIDDEN)
  269. {
  270. return std::make_shared<special_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  271. }
  272. else if(type == AREA_TYPE_DOWNMINE)
  273. {
  274. return std::make_shared<underground_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  275. }
  276. else
  277. {
  278. return std::make_shared<area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  279. }
  280. }
  281. void area_list::init_from_db(int id/*=-1*/)
  282. {
  283. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  284. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
  285. over_speed_vehicle, is_attendance,business_type, a.is_work_area\
  286. FROM dat_area a,dat_map b\
  287. where a.map_id = b.map_id and area_id not in \
  288. (select monkeycar_base_info_id from dat_monkeycar_base_info)";
  289. if(-1 == id)
  290. {
  291. sql.append(";");
  292. }
  293. else
  294. {
  295. sql.append(" AND s.area_id=");
  296. sql.append(std::to_string(id));
  297. sql.append(";");
  298. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  299. }
  300. std::string Error;
  301. YADB::CDBResultSet DBRes;
  302. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  303. int nCount = DBRes.GetRecordCount( Error );
  304. if (nCount < 1)
  305. {
  306. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
  307. return ;
  308. }
  309. std::unordered_map<int,std::shared_ptr<area>> map;
  310. while ( DBRes.GetNextRecod(Error) )
  311. {
  312. int area_id = 0;
  313. DBRes.GetField( "area_id",area_id, Error );
  314. int map_id = 0;
  315. DBRes.GetField( "map_id",map_id, Error );
  316. unsigned int area_type_id = 0;
  317. DBRes.GetField( "area_type_id",area_type_id, Error );
  318. int over_count_person = 0;
  319. DBRes.GetField( "over_count_person",over_count_person, Error );
  320. int over_count_vehicle = 0;
  321. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  322. int over_time_person = 0;
  323. DBRes.GetField( "over_time_person",over_time_person, Error );
  324. int over_time_vehicle = 0;
  325. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  326. std::string over_speed_vehicle;
  327. DBRes.GetField( "over_speed_vehicle",over_speed_vehicle, Error );
  328. std::string path;
  329. DBRes.GetField( "path",path, Error );
  330. double scale = 0;
  331. DBRes.GetField( "scale",scale, Error );
  332. uint32_t b_type =0;
  333. DBRes.GetField( "business_type",b_type, Error );
  334. int is_work_area = 0;
  335. DBRes.GetField( "is_work_area",is_work_area, Error );
  336. log_info("init_area : id:%d,path:%s..speed:%s",area_id, path.c_str(),over_speed_vehicle.c_str());
  337. std::map<int,double> map_;
  338. auto vp=init_path(over_speed_vehicle,area_id);
  339. for(const auto &v:vp)
  340. map_.insert({v.x,v.y});
  341. if(-1 == id)
  342. {
  343. std::shared_ptr<area> ap = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  344. ap->m_limit_vehicle_second = over_time_vehicle;
  345. ap->m_limit_vehicle_count = over_count_vehicle;
  346. ap->m_speed=std::move(map_);
  347. ap->m_is_work_area = is_work_area;
  348. ap->m_bound=init_path(path,area_id);
  349. for(const auto &p : ap->m_bound)
  350. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  351. map.insert({area_id,ap});
  352. }
  353. else
  354. {
  355. auto tmp_ptr = area_list::instance()->get(id);
  356. bool newobj=false;
  357. if(!tmp_ptr)
  358. {
  359. tmp_ptr = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  360. newobj=true;
  361. }
  362. tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  363. tmp_ptr->m_speed=std::move(map_);
  364. tmp_ptr->m_bound=init_path(path,area_id);
  365. tmp_ptr->m_is_work_area = is_work_area;
  366. for(const auto &p : tmp_ptr->m_bound)
  367. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  368. log_info("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  369. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  370. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  371. over_count_vehicle,over_time_vehicle);
  372. if(newobj)
  373. {
  374. area_list::instance()->add(id, tmp_ptr);
  375. }
  376. else
  377. {
  378. tmp_ptr->change_business(b_type);
  379. }
  380. }
  381. }
  382. if(-1 == id)
  383. {
  384. log_info( "init_area. The record count=%d\n", nCount );
  385. area_list::instance()->add(map);
  386. }
  387. init_monkeycar_area(id);
  388. }
  389. std::vector<point> area_list::init_path(std::string &str_0,int area_id)
  390. {
  391. const std::string str=str_0;
  392. if(str.empty())
  393. {
  394. log_error("area path is empty,area_id=%d.",area_id);
  395. }
  396. std::vector<point> vp;
  397. std::vector<std::string> vs;
  398. std::vector<std::string> vs1;
  399. boost::split(vs,str,boost::is_any_of("ML ;"));
  400. for(auto & s:vs)
  401. {
  402. if(s.empty())
  403. continue;
  404. boost::split(vs1,s,boost::is_any_of(","));
  405. if(vs1.size()!=2)
  406. {
  407. log_error("area path data Error.pls check data table,area_id=%d",area_id);
  408. continue;
  409. }
  410. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  411. }
  412. return std::move(vp);
  413. }
  414. std::vector<std::shared_ptr<area>> area_list::get_area(const std::shared_ptr<site>& s,const std::shared_ptr<card_location_base> &c,const point&pt,int & sarid)
  415. {
  416. std::vector<std::shared_ptr<area>> ret;
  417. if(tool_other::is_person(c->type_())&&s&&s->is_up_site())
  418. return std::move(ret);
  419. auto map = area_list::instance()->m_map;
  420. for(const auto &a:map)
  421. if(a.second->in_area(s,c,pt,sarid))
  422. ret.push_back(a.second);
  423. if(s)
  424. ret.push_back(s->get_area());
  425. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  426. return std::move(ret);
  427. }
  428. area_hover::area_hover(const std::shared_ptr<area>&area,const point&pt)
  429. :m_area(area)
  430. {
  431. m_enter_time=m_last_time=tool_time::now_to_ms();
  432. m_enter_point=m_last_point=pt;
  433. }
  434. void area_tool::on_point(const std::shared_ptr<card_location_base>& c,const point&pt)
  435. {
  436. log_info("on_point...cardid:%d,type:%d,x:%.2f,y:%.2f",c->m_id,c->m_type,pt.x,pt.y);
  437. if(pt.empty())
  438. return;
  439. int special_area=-1;
  440. std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(m_site,c, pt,special_area);
  441. if(special_area != -1)
  442. {
  443. areas.clear();
  444. auto area_=area_list::instance()->get(special_area);
  445. areas.push_back(area_);
  446. }
  447. else
  448. {
  449. std::sort(areas.begin(),areas.end(),[](const std::shared_ptr<area>&l,const std::shared_ptr<area>&r){
  450. return l->id()<r->id();
  451. });
  452. }
  453. auto c1=m_hover_list.begin(),ce=m_hover_list.end();
  454. auto a1=areas.begin() ,ae=areas.end();
  455. std::vector<std::shared_ptr<area_hover>> nlist;
  456. while (c1!=ce && a1!=ae)
  457. {
  458. if ((*c1)->id()<(*a1)->id()) //离开区域
  459. {
  460. if((*c1)->m_area->get_frozen_count()==0)//如果该区域未在修改中,调用on_leave
  461. {
  462. (*c1)->m_area->on_leave(*c1, c);
  463. }
  464. else//否则,该区域持续保存在卡的区域列表,等待修改完成
  465. {
  466. nlist.push_back(*c1);
  467. log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  468. }
  469. ++c1;
  470. }
  471. else if ((*a1)->id()<(*c1)->id()) //进入新区域
  472. {
  473. if((*a1)->get_frozen_count()==0)////如果该区域未在修改中,执行正常逻辑
  474. {
  475. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  476. (*a1)->on_enter(nlist.back(),c);
  477. }
  478. else//丢弃本次进入的点,等待修改完成
  479. {
  480. log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
  481. }
  482. ++a1;
  483. }
  484. else //需要持续的区域
  485. {
  486. nlist.push_back(*c1);
  487. if((*c1)->m_area->get_frozen_count()==0)//正常情况
  488. {
  489. (*c1)->m_area->on_hover(*c1,c);
  490. }
  491. else//丢弃本次on_hover时间
  492. {
  493. log_warn("丢弃区域行走事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  494. }
  495. ++c1,++a1;
  496. }
  497. }
  498. while(c1!=ce)
  499. {
  500. if((*c1)->m_area->get_frozen_count()==0)
  501. {
  502. (*c1)->m_area->on_leave(*c1, c);
  503. }
  504. else
  505. {
  506. log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  507. }
  508. ++c1;
  509. }
  510. while(a1!=ae)
  511. {
  512. if((*a1)->get_frozen_count()==0)
  513. {
  514. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  515. (*a1)->on_enter(nlist.back(),c);
  516. }
  517. else
  518. {
  519. log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
  520. }
  521. ++a1;
  522. }
  523. m_hover_list=std::move(nlist);
  524. //更新到数据库
  525. if(m_site && !m_hover_list.empty())
  526. {
  527. std::string strAreaInfo = "";
  528. for (std::vector<std::shared_ptr<area_hover>>::iterator itvec = m_hover_list.begin(); itvec != m_hover_list.end(); ++itvec)
  529. {
  530. if ((*itvec)->id() >= 0)
  531. {
  532. char tmpArea[128] = {0};
  533. snprintf(tmpArea, 128, "%d,%lu|", (*itvec)->id(), (*itvec)->m_enter_time);
  534. strAreaInfo += tmpArea;
  535. }
  536. }
  537. char sql[1024] = {0};
  538. snprintf(sql, 1024, "REPLACE INTO rt_location (card_id, site_id, x, y, z, state, area_info) VALUES (%s, %d, %lf, %lf, %lf, 0, '%s');",
  539. tool_other::type_id_to_str(c->m_type, c->m_id).c_str(), m_site->id(), pt.x, pt.y, pt.z, strAreaInfo.c_str());
  540. db_tool::PushAsync(sql);
  541. }
  542. }
  543. void area_tool::on_leave(const std::shared_ptr<card_location_base>& c)
  544. {
  545. for(auto& t:m_hover_list)
  546. {
  547. t->m_area->on_leave(t, c);
  548. }
  549. }
  550. void area_tool::set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type)
  551. {
  552. if(0==type)
  553. {
  554. m_mapid = mapid;
  555. m_scale=scale;
  556. set_area_info(mapid,areaid,pt,t);
  557. }
  558. else if(1==type)
  559. {
  560. set_area_info(mapid,areaid,pt,t);
  561. }
  562. else if(2==type)
  563. {
  564. auto it =m_area_info.find(areaid);
  565. if(it != m_area_info.end())
  566. m_area_info.erase(it);
  567. }
  568. else
  569. {
  570. log_info("wrong type..");
  571. }
  572. }
  573. void area_tool::set_area_info(int mapid,int areaid,const point &pt,uint64_t t)
  574. {
  575. auto lm = Landmark_list::instance()->get(mapid,areaid,pt);
  576. int landmark_id = std::get<0>(lm);
  577. int landmark_dir = std::get<1>(lm);
  578. double landmark_dis = std::get<2>(lm)*m_scale;
  579. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,m_scale);
  580. std::tuple<int,int,int,double,uint64_t> tinfo=std::make_tuple(areaid,landmark_id,landmark_dir,landmark_dis,t);
  581. m_area_info[areaid].swap(tinfo);
  582. }
  583. void area_tool::on_change_business(const std::shared_ptr<card_location_base>& c, const task&t)
  584. {
  585. auto&mcb=t.body<message_change_business>();
  586. auto i=std::lower_bound(m_hover_list.begin(),m_hover_list.end(),mcb.area_id ,[&mcb](std::shared_ptr<area_hover>&ah,int id){
  587. return ah->m_area->m_id<id;
  588. });
  589. if(i==m_hover_list.end())
  590. return ;
  591. if((*i)->m_area->m_id!=mcb.area_id)
  592. return;
  593. log_info("调用调整的区域业务,card_id=%d,area_id=%d", c->m_id, mcb.area_id);
  594. for(auto&biz:mcb.add_list)
  595. {
  596. biz->on_enter(*i,c,(*i)->get_business_data(biz->area_business_type()));
  597. }
  598. for(auto&biz:mcb.del_list)
  599. {
  600. biz->on_leave(*i,c,(*i)->get_business_data(biz->area_business_type()));
  601. }
  602. }