area.cpp 25 KB

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