area.cpp 22 KB

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