area.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. void area::add_persons_thre(const SArea_Persons_Thre & thre)
  202. {
  203. m_area_persons_thre[thre.db_id] = thre;
  204. }
  205. void area::del_persons_thre(int thre_db_id)
  206. {
  207. m_area_persons_thre.erase(thre_db_id);
  208. }
  209. void area::clear_persons_thre()
  210. {
  211. m_area_persons_thre.clear();
  212. }
  213. //
  214. int area::get_limit_person_count()
  215. {
  216. return get_limit_person_count(tool_time::now_to_seconds());
  217. }
  218. int area::get_limit_person_count(uint64_t cur_time)
  219. {
  220. std::string sz_cur_time = tool_time::to_str_time(cur_time);
  221. for (auto it : m_area_persons_thre)
  222. {
  223. SArea_Persons_Thre &d = it.second;
  224. if (sz_cur_time >= d.start_time && sz_cur_time <= d.end_time)
  225. {
  226. return d.thre_value;
  227. }
  228. }
  229. return m_limit_person_count;
  230. }
  231. // 判断区域中是否有时间段超员设定
  232. bool area::is_time_person_thre()
  233. {
  234. return m_area_persons_thre.size() > 0;
  235. }
  236. int area::update_persons_thre()
  237. {
  238. uint32_t cur_time = tool_time::now_to_seconds();
  239. std::string sz_cur_time = tool_time::to_str_time(cur_time);
  240. for (auto &it : m_area_persons_thre)
  241. {
  242. SArea_Persons_Thre &d = it.second;
  243. int t_state = d.in_time_state;
  244. if (sz_cur_time < d.start_time)
  245. {
  246. d.in_time_state = EIN_TIME_STATE::NotYet;
  247. log_info("area::update_persons_thre : Init areaid=%d db_id=%d time=%s -> %s val=%d "
  248. ,m_id,d.db_id,d.start_time.c_str(),sz_cur_time.c_str(),d.thre_value);
  249. }
  250. else if (sz_cur_time >= d.start_time && sz_cur_time <= d.end_time)
  251. {
  252. d.in_time_state = EIN_TIME_STATE::Middle;
  253. if (t_state == EIN_TIME_STATE::NotYet)
  254. {
  255. d.check_state_time =cur_time;
  256. log_info("area::update_persons_thre : areaid=%d db_id=%d time=%s -> %s val=%d state=%d -> %d"
  257. ,m_id,d.db_id,d.start_time.c_str(),d.end_time.c_str(),d.thre_value,t_state,d.in_time_state);
  258. return d.in_time_state;
  259. }
  260. }
  261. else
  262. {
  263. d.in_time_state = EIN_TIME_STATE::Overtime;
  264. if (t_state == EIN_TIME_STATE::Middle)
  265. {
  266. d.check_state_time =cur_time;
  267. log_info("area::update_persons_thre : areaid=%d db_id=%d time=%s -> %s val=%d state=%d -> %d"
  268. ,m_id,d.db_id,d.start_time.c_str(),d.end_time.c_str(),d.thre_value,t_state,d.in_time_state);
  269. return d.in_time_state;
  270. }
  271. }
  272. }
  273. return EIN_TIME_STATE::NotYet;
  274. }
  275. void area::init_persons_thre()
  276. {
  277. std::string sz_cur_time = tool_time::to_str_time(tool_time::now_to_seconds());
  278. for (auto &it : m_area_persons_thre)
  279. {
  280. SArea_Persons_Thre &d = it.second;
  281. if (sz_cur_time < d.start_time)
  282. {
  283. d.in_time_state = EIN_TIME_STATE::NotYet;
  284. }
  285. else if (sz_cur_time >= d.start_time && sz_cur_time <= d.end_time)
  286. {
  287. if (d.old_in_time_state == EIN_TIME_STATE::Middle)
  288. {
  289. d.in_time_state = EIN_TIME_STATE::Middle;
  290. }
  291. }
  292. else
  293. {
  294. d.in_time_state = EIN_TIME_STATE::Overtime;
  295. if (d.old_in_time_state == EIN_TIME_STATE::Middle)
  296. {
  297. d.in_time_state = EIN_TIME_STATE::Middle;
  298. }
  299. }
  300. log_info("area::init_persons_thre : areaid=%d time=%s -> %s val=%d state=%d -> %d"
  301. ,m_id,d.start_time.c_str(),d.end_time.c_str(),d.thre_value,d.old_in_time_state,d.in_time_state);
  302. }
  303. log_info("area::init_persons_thre : areaid=%d count=%d",m_id,m_area_persons_thre.size());
  304. }
  305. /////////////////// area_list //////////////////////////////////////////////////
  306. area_list::area_list()
  307. {
  308. }
  309. void area_list::init_monkeycar_area(int id)
  310. {
  311. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  312. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle,\
  313. over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed,a.business_type \
  314. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  315. where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id";
  316. if(-1 == id)
  317. {
  318. sql.append(";");
  319. }
  320. else
  321. {
  322. sql.append(" AND a.area_id=");
  323. sql.append(std::to_string(id));
  324. sql.append(";");
  325. log_info("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
  326. }
  327. std::string Error;
  328. YADB::CDBResultSet DBRes;
  329. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  330. int nCount = DBRes.GetRecordCount( Error );
  331. if (nCount < 1)
  332. {
  333. log_error("基础数据 monkeycar area 增加或修改失败,数据库中找不到: area_id=%d", id);
  334. return ;
  335. }
  336. std::unordered_map<int,std::shared_ptr<area>> map;
  337. while ( DBRes.GetNextRecod(Error) )
  338. {
  339. int area_id = 0;
  340. DBRes.GetField( "area_id",area_id, Error );
  341. int map_id = 0;
  342. DBRes.GetField( "map_id",map_id, Error );
  343. unsigned int area_type_id = 0;
  344. DBRes.GetField( "area_type_id",area_type_id, Error );
  345. int over_count_person = 0;
  346. DBRes.GetField( "over_count_person",over_count_person, Error );
  347. int over_count_vehicle = 0;
  348. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  349. int over_time_person = 0;
  350. DBRes.GetField( "over_time_person",over_time_person, Error );
  351. int over_time_vehicle = 0;
  352. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  353. std::string path;
  354. DBRes.GetField( "path",path, Error );
  355. double monkeycar_speed = 0;
  356. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  357. std::string monkeycar_coor;
  358. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  359. double scale=0;
  360. DBRes.GetField( "scale",scale, Error );
  361. uint32_t b_type =0;
  362. DBRes.GetField( "business_type",b_type, Error );
  363. log_info("monkeycar area init_area : id:%d,path:%s",area_id, path.c_str());
  364. if(-1 == id)
  365. {
  366. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  367. da->m_default_speed = monkeycar_speed;
  368. da->m_point = init_path(monkeycar_coor,area_id);
  369. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  370. ap->update(over_count_person, over_time_person,scale,map_id,over_count_vehicle,over_time_vehicle);
  371. ap->m_area_type=area_type_id;
  372. ap->m_bound=init_path(path,area_id);
  373. for(const auto &p : ap->m_bound)
  374. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  375. for(const auto &p : da->m_point)
  376. log_info("point:monkey_coor: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. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  386. da->m_default_speed = monkeycar_speed;
  387. da->m_point = init_path(monkeycar_coor,area_id);
  388. tmp_ptr=std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  389. newobj=true;
  390. }
  391. tmp_ptr->m_bound=init_path(path,area_id);
  392. tmp_ptr->update(over_count_person, over_time_person,scale,map_id, over_count_vehicle,over_time_vehicle);
  393. tmp_ptr->m_area_type=area_type_id;
  394. if(newobj)
  395. {
  396. area_list::instance()->add(id, tmp_ptr);
  397. }
  398. else
  399. {
  400. tmp_ptr->change_business(b_type);
  401. }
  402. log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  403. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  404. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  405. over_count_vehicle,over_time_vehicle);
  406. }
  407. }
  408. if(-1 == id)
  409. {
  410. log_info( "monkeycar area init_area. The record count=%d\n", nCount );
  411. area_list::instance()->add(map);
  412. }
  413. }
  414. 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)
  415. {
  416. if(type==AREA_TYPE_NO_COVER)
  417. {
  418. return std::make_shared<special_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  419. }
  420. else if(type == AREA_TYPE_DOWNMINE)
  421. {
  422. return std::make_shared<underground_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  423. }
  424. else
  425. {
  426. return std::make_shared<area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  427. }
  428. }
  429. void area_list::init_from_db(int id/*=-1*/)
  430. {
  431. do{
  432. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  433. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
  434. over_speed_vehicle, is_attendance,business_type, a.is_work_area\
  435. FROM dat_area a,dat_map b\
  436. where a.map_id = b.map_id and area_id not in \
  437. (select monkeycar_base_info_id from dat_monkeycar_base_info)";
  438. if(-1 == id)
  439. {
  440. sql.append(";");
  441. }
  442. else
  443. {
  444. sql.append(" AND a.area_id=");
  445. sql.append(std::to_string(id));
  446. sql.append(";");
  447. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  448. }
  449. std::string Error;
  450. YADB::CDBResultSet DBRes;
  451. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  452. int nCount = DBRes.GetRecordCount( Error );
  453. if (nCount < 1)
  454. {
  455. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
  456. break;
  457. }
  458. std::unordered_map<int,std::shared_ptr<area>> map;
  459. while ( DBRes.GetNextRecod(Error) )
  460. {
  461. int area_id = 0;
  462. DBRes.GetField( "area_id",area_id, Error );
  463. std::string area_name = "";
  464. DBRes.GetField( "name",area_name, Error );
  465. int map_id = 0;
  466. DBRes.GetField( "map_id",map_id, Error );
  467. unsigned int area_type_id = 0;
  468. DBRes.GetField( "area_type_id",area_type_id, Error );
  469. int over_count_person = 0;
  470. DBRes.GetField( "over_count_person",over_count_person, Error );
  471. int over_count_vehicle = 0;
  472. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  473. int over_time_person = 0;
  474. DBRes.GetField( "over_time_person",over_time_person, Error );
  475. int over_time_vehicle = 0;
  476. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  477. std::string over_speed_vehicle;
  478. DBRes.GetField( "over_speed_vehicle",over_speed_vehicle, Error );
  479. std::string path;
  480. DBRes.GetField( "path",path, Error );
  481. double scale = 0;
  482. DBRes.GetField( "scale",scale, Error );
  483. uint32_t b_type =0;
  484. DBRes.GetField( "business_type",b_type, Error );
  485. int is_work_area = 0;
  486. DBRes.GetField( "is_work_area",is_work_area, Error );
  487. log_info("init_area : id:%d,path:%s..speed:%s",area_id, path.c_str(),over_speed_vehicle.c_str());
  488. std::map<int,double> map_;
  489. auto vp=init_path(over_speed_vehicle,area_id);
  490. for(const auto &v:vp)
  491. map_.insert({v.x,v.y});
  492. if(-1 == id)
  493. {
  494. std::shared_ptr<area> ap = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  495. ap->m_limit_vehicle_min = over_time_vehicle;
  496. ap->m_limit_vehicle_count = over_count_vehicle;
  497. ap->m_speed=std::move(map_);
  498. ap->m_is_work_area = is_work_area;
  499. ap->m_area_type=area_type_id;
  500. ap->m_bound=init_path(path,area_id);
  501. for(const auto &p : ap->m_bound)
  502. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  503. map.insert({area_id,ap});
  504. }
  505. else
  506. {
  507. auto tmp_ptr = area_list::instance()->get(id);
  508. bool newobj=false;
  509. if(!tmp_ptr)
  510. {
  511. tmp_ptr = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  512. newobj=true;
  513. }
  514. tmp_ptr->update(over_count_person, over_time_person,scale,map_id, over_count_vehicle,over_time_vehicle);
  515. tmp_ptr->m_speed=std::move(map_);
  516. tmp_ptr->m_bound=init_path(path,area_id);
  517. tmp_ptr->m_is_work_area = is_work_area;
  518. tmp_ptr->m_area_type=area_type_id;
  519. for(const auto &p : tmp_ptr->m_bound)
  520. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  521. log_info("基础数据 增加或修改区域成功:区域id:%d-%s,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  522. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  523. id,area_name.c_str(),over_count_person, over_time_person,scale,map_id,area_type_id,
  524. over_count_vehicle,over_time_vehicle);
  525. if(newobj)
  526. {
  527. area_list::instance()->add(id, tmp_ptr);
  528. }
  529. else
  530. {
  531. tmp_ptr->change_business(b_type);
  532. }
  533. }
  534. }
  535. if(-1 == id)
  536. {
  537. log_info( "init_area. The record count=%d\n", nCount );
  538. area_list::instance()->add(map);
  539. }
  540. }while(0);
  541. init_monkeycar_area(id);
  542. }
  543. std::vector<point> area_list::init_path(std::string &str_0,int area_id)
  544. {
  545. const std::string str=str_0;
  546. if(str.empty())
  547. {
  548. log_error("area path is empty,area_id=%d.",area_id);
  549. }
  550. std::vector<point> vp;
  551. std::vector<std::string> vs;
  552. std::vector<std::string> vs1;
  553. boost::split(vs,str,boost::is_any_of("ML ;"));
  554. for(auto & s:vs)
  555. {
  556. if(s.empty())
  557. continue;
  558. boost::split(vs1,s,boost::is_any_of(","));
  559. if(vs1.size()!=2)
  560. {
  561. log_error("area path data Error.pls check data table,area_id=%d",area_id);
  562. continue;
  563. }
  564. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  565. }
  566. return std::move(vp);
  567. }
  568. 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)
  569. {
  570. std::vector<std::shared_ptr<area>> ret;
  571. if(tool_other::is_person(c->type_())&&s&&s->is_up_site())
  572. {
  573. ret.push_back(s->get_area());//地面分站的区域
  574. return std::move(ret);
  575. }
  576. auto&map = area_list::instance()->m_map;
  577. for(const auto &a:map)
  578. {
  579. if(a.second->in_area(s,c,pt,sarid))
  580. ret.push_back(a.second);
  581. }
  582. if(s){
  583. ret.push_back(s->get_area());
  584. int area_id=s->m_area_id;
  585. if(auto area_=area_list::instance()->get(area_id))
  586. ret.push_back(area_);
  587. }
  588. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  589. return std::move(ret);
  590. }
  591. // 区域时间段超员设置
  592. void area_list::init_area_persons_dynamic_thre_from_db(int area_id/* = -1*/)
  593. {
  594. std::string sql = "SELECT area_id,adpt_id,start_time,end_time,thre_value from dat_area_persons_dynamic_thre ";
  595. if (area_id > -1 )
  596. {
  597. sql.append(" where area_id=");
  598. sql.append(std::to_string(area_id));
  599. }
  600. sql.append(" order by area_id asc,start_time asc ;");
  601. log_info("加载区域:%d 时间段人员超员设置sql: %s",area_id, sql.c_str());
  602. std::string Error;
  603. YADB::CDBResultSet DBRes;
  604. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  605. int nCount = DBRes.GetRecordCount( Error );
  606. if (nCount < 1)
  607. {
  608. return;
  609. }
  610. MAP_AREA_PERSONS_THRE map_thre;
  611. std::shared_ptr<area> tmp_area = nullptr;
  612. while ( DBRes.GetNextRecod(Error) )
  613. {
  614. SArea_Persons_Thre td;
  615. DBRes.GetField("area_id", td.area_id, Error);
  616. DBRes.GetField("adpt_id", td.db_id, Error);
  617. DBRes.GetField("start_time", td.start_time, Error);
  618. DBRes.GetField("end_time", td.end_time, Error);
  619. DBRes.GetField("thre_value", td.thre_value, Error);
  620. if (nullptr == tmp_area || tmp_area->m_id != td.area_id)
  621. {
  622. if (nullptr != tmp_area )
  623. {
  624. tmp_area->init_persons_thre();
  625. }
  626. tmp_area = area_list::instance()->get(td.area_id);
  627. if (nullptr == tmp_area)
  628. {
  629. continue;
  630. }
  631. map_thre = tmp_area->m_area_persons_thre;
  632. tmp_area->clear_persons_thre();
  633. }
  634. auto iter = map_thre.find(td.db_id);
  635. if (iter != map_thre.end())
  636. {
  637. td.old_in_time_state = iter->second.in_time_state;
  638. td.check_state_time = iter->second.check_state_time;
  639. }
  640. tmp_area->add_persons_thre(td);
  641. }
  642. if (nullptr != tmp_area )
  643. {
  644. tmp_area->init_persons_thre();
  645. }
  646. }
  647. /////////////////////////////////////////////////////////////////////
  648. area_hover::area_hover(const std::shared_ptr<area>&area,const point&pt)
  649. :m_area(area)
  650. {
  651. m_enter_time=m_last_time=tool_time::now_to_ms();
  652. m_enter_point=m_last_point=pt;
  653. }
  654. void area_tool::on_point(const std::shared_ptr<card_location_base>& c,const point&pt)
  655. {
  656. log_info("on_point...cardid:%d,type:%d,x:%.2f,y:%.2f",c->m_id,c->m_type,pt.x,pt.y);
  657. if(pt.empty()) return;
  658. int special_area=-1;
  659. std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(m_site,c, pt,special_area);
  660. if(special_area != -1)
  661. {
  662. areas.clear();
  663. auto area_=area_list::instance()->get(special_area);
  664. areas.push_back(area_);
  665. }
  666. else
  667. {
  668. std::sort(areas.begin(),areas.end(),[](const std::shared_ptr<area>&l,const std::shared_ptr<area>&r){
  669. return l->id()<r->id();
  670. });
  671. }
  672. auto c1=m_hover_list.begin(),ce=m_hover_list.end();
  673. auto a1=areas.begin() ,ae=areas.end();
  674. std::vector<std::shared_ptr<area_hover>> nlist;
  675. while (c1!=ce && a1!=ae)
  676. {
  677. if ((*c1)->id()<(*a1)->id()) //离开区域
  678. {
  679. if((*c1)->m_area->get_frozen_count()==0)//如果该区域未在修改中,调用on_leave
  680. {
  681. (*c1)->m_area->on_leave(*c1, c);
  682. }
  683. else//否则,该区域持续保存在卡的区域列表,等待修改完成
  684. {
  685. nlist.push_back(*c1);
  686. log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  687. }
  688. ++c1;
  689. }
  690. else if ((*a1)->id()<(*c1)->id()) //进入新区域
  691. {
  692. if((*a1)->get_frozen_count()==0)////如果该区域未在修改中,执行正常逻辑
  693. {
  694. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  695. (*a1)->on_enter(nlist.back(),c);
  696. }
  697. else//丢弃本次进入的点,等待修改完成
  698. {
  699. log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
  700. }
  701. ++a1;
  702. }
  703. else //需要持续的区域
  704. {
  705. nlist.push_back(*c1);
  706. if((*c1)->m_area->get_frozen_count()==0)//正常情况
  707. {
  708. (*c1)->m_area->on_hover(*c1,c);
  709. }
  710. else//丢弃本次on_hover时间
  711. {
  712. log_warn("丢弃区域行走事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  713. }
  714. ++c1,++a1;
  715. }
  716. }
  717. while(c1!=ce)
  718. {
  719. if((*c1)->m_area->get_frozen_count()==0)
  720. {
  721. (*c1)->m_area->on_leave(*c1, c);
  722. }
  723. else
  724. {
  725. log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
  726. }
  727. ++c1;
  728. }
  729. while(a1!=ae)
  730. {
  731. if((*a1)->get_frozen_count()==0)
  732. {
  733. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  734. (*a1)->on_enter(nlist.back(),c);
  735. }
  736. else
  737. {
  738. log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
  739. }
  740. ++a1;
  741. }
  742. m_hover_list=std::move(nlist);
  743. //更新到数据库
  744. if(m_site && !m_hover_list.empty())
  745. {
  746. std::string strAreaInfo = "";
  747. for (std::vector<std::shared_ptr<area_hover>>::iterator itvec = m_hover_list.begin(); itvec != m_hover_list.end(); ++itvec)
  748. {
  749. if ((*itvec)->id() >= 0)
  750. {
  751. char tmpArea[128] = {0};
  752. snprintf(tmpArea, 128, "%d,%lu|", (*itvec)->id(), (*itvec)->m_enter_time);
  753. strAreaInfo += tmpArea;
  754. }
  755. }
  756. char sql[1024] = {0};
  757. std::string _time=tool_time::to_str_ex(c->time_());
  758. 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');",
  759. 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());
  760. db_tool::PushAsync(sql);
  761. }
  762. }
  763. void area_tool::on_leave(const std::shared_ptr<card_location_base>& c)
  764. {
  765. for(const auto& t:m_hover_list)
  766. {
  767. t->m_area->on_leave(t, c);
  768. }
  769. }
  770. void area_tool::set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type)
  771. {
  772. if(0==type)
  773. {
  774. m_mapid = mapid;
  775. m_scale=scale;
  776. set_area_info(mapid,areaid,pt,t);
  777. }
  778. else if(1==type)
  779. {
  780. if(m_mapid != mapid || m_scale!=scale){
  781. m_mapid=mapid;m_scale=scale;
  782. }
  783. set_area_info(mapid,areaid,pt,t);
  784. }
  785. else if(2==type)
  786. {
  787. auto it =m_area_info.find(areaid);
  788. if(it != m_area_info.end())
  789. m_area_info.erase(it);
  790. }
  791. else
  792. {
  793. log_info("wrong type..");
  794. }
  795. }
  796. void area_tool::set_area_info(int mapid,int areaid,const point &pt,uint64_t t)
  797. {
  798. auto lm = Landmark_list::instance()->get_landmark(mapid,areaid,pt);
  799. int landmark_id = std::get<0>(lm);
  800. int landmark_dir = std::get<1>(lm);
  801. double landmark_dis = std::get<2>(lm)*m_scale;
  802. std::tuple<int,int,int,double,uint64_t> tinfo=std::make_tuple(areaid,landmark_id,landmark_dir,landmark_dis,t);
  803. m_area_info[areaid].swap(tinfo);
  804. }
  805. void area_tool::on_change_business(const std::shared_ptr<card_location_base>& c, const task&t)
  806. {
  807. auto&mcb=t.body<message_change_business>();
  808. 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){
  809. return ah->m_area->m_id<id;
  810. });
  811. if(i==m_hover_list.end())
  812. return ;
  813. if((*i)->m_area->m_id!=mcb.area_id)
  814. return;
  815. log_info("调用调整的区域业务,card_id=%d,area_id=%d", c->m_id, mcb.area_id);
  816. for(auto&biz:mcb.add_list)
  817. {
  818. biz->on_enter(*i,c,(*i)->get_business_data(biz->area_business_type()));
  819. }
  820. for(auto&biz:mcb.del_list)
  821. {
  822. biz->on_leave(*i,c,(*i)->get_business_data(biz->area_business_type()));
  823. }
  824. }