area.cpp 22 KB

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