area.cpp 26 KB

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