1
0

area.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include <memory>
  2. #include <write-copy.h>
  3. #include "db_api/CDBSingletonDefine.h"
  4. #include "log.h"
  5. #include <area.h>
  6. #include "point.h"
  7. #include "tool_time.h"
  8. #include "monkey_car/monkeycar_area.h"
  9. #include "landmark.h"
  10. #include "area_business.h"
  11. #include <boost/algorithm/string/split.hpp>
  12. #include <boost/algorithm/string/classification.hpp>
  13. #include "area_business.h"
  14. #include "site_area.h"
  15. #include "card.h"
  16. #include "tool_time.h"
  17. #include "common_tool.h"
  18. #include "websocket/ws_common.h"
  19. #include "websocket/wsClientMgr.h"
  20. #include "ant.h"
  21. //template<> std::shared_ptr<area_list> single_base<area_list, int, std::shared_ptr<area>>::m_instance=std::make_shared<area_list>();
  22. struct underground_area:area
  23. {
  24. underground_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  25. :area(id,limit_count_person,limit_time_person,scale,mapid,type)
  26. {
  27. }
  28. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
  29. {
  30. return s&&!s->is_up_site();
  31. }
  32. };
  33. struct special_area:area
  34. {
  35. special_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  36. :area(id,limit_count_person,limit_time_person,scale,mapid,type)
  37. {}
  38. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
  39. {
  40. bool f=false;
  41. int id = c->get_area();
  42. if(id == m_id)
  43. {
  44. f=true;
  45. sarid=m_id;
  46. }
  47. return f;
  48. }
  49. //推入特殊区域的是否需要其他信息
  50. virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  51. {
  52. area::on_enter(a, c);
  53. YA::_CARD_POS_ cp;
  54. cp.area_id = m_id;
  55. swsClientMgr.SendSpecialAreaProcess(cp);
  56. }
  57. };
  58. area::area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  59. :m_id(id)
  60. ,m_area_type(type)
  61. ,m_limit_person_second(limit_time_person)
  62. ,m_limit_person_count(limit_count_person)
  63. ,m_scale(scale)
  64. ,m_mapid(mapid)
  65. ,m_person_count(0)
  66. ,m_vehicle_count(0)
  67. {
  68. m_area_business_list=area_business::get_instance_list(m_area_type);
  69. m_event_person_count = false;
  70. m_event_vehicle_count = false;
  71. }
  72. void area::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  73. {
  74. a->m_last_time=tool_time::now_to_ms();
  75. a->m_last_point=*c;
  76. for(const auto &i:m_area_business_list)
  77. {
  78. auto &x=a->get_business_data(i->area_business_type());
  79. i->on_hover(a,c,x);
  80. }
  81. }
  82. void area::on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  83. {
  84. log_info("on_enter..%d areaId:%d",c->m_id,m_id);
  85. a->m_enter_time=tool_time::now_to_ms();
  86. a->m_enter_point=*c;
  87. for(const auto &i:m_area_business_list)
  88. {
  89. auto &x=a->get_business_data(i->area_business_type());
  90. i->on_enter(a,c,x);
  91. }
  92. }
  93. void area::on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  94. {
  95. log_info("on_leave..%d areaId:%d",c->m_id,m_id);
  96. a->m_last_time=tool_time::now_to_ms();
  97. a->m_last_point=*c;
  98. for(const auto &i:m_area_business_list)
  99. {
  100. auto &x=a->get_business_data(i->area_business_type());
  101. i->on_leave(a,c,x);
  102. }
  103. }
  104. void area::on_load(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
  105. {
  106. for(const auto &i:m_area_business_list)
  107. {
  108. auto &x=a->get_business_data(i->area_business_type());
  109. i->on_load(a,c,x);
  110. }
  111. }
  112. bool area::in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int & sarid)
  113. {
  114. if(m_bound.empty())
  115. return false;
  116. int counter = 0;
  117. double xinters;
  118. point p1,p2;
  119. p1 = m_bound[0];
  120. int size = m_bound.size();
  121. for (int i=1;i<= size;i++) {
  122. p2 = m_bound[i%size];
  123. if (p.y > std::min(p1.y,p2.y)) {
  124. if (p.y <= std::max(p1.y,p2.y)) {
  125. if (p.x <= std::max(p1.x,p2.x)) {
  126. if (p1.y != p2.y) {
  127. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  128. if (p1.x == p2.x || p.x <= xinters)
  129. counter++;
  130. }
  131. }
  132. }
  133. }
  134. p1 = p2;
  135. }
  136. return (counter % 2 == 0) ? false : true;
  137. }
  138. area_list::area_list()
  139. {
  140. }
  141. void area_list::init_monkeycar_area(int id)
  142. {
  143. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  144. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle,\
  145. over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed,a.business_type \
  146. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  147. where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id";
  148. if(-1 == id)
  149. {
  150. sql.append(";");
  151. }
  152. else
  153. {
  154. sql.append(" AND a.area_id=");
  155. sql.append(std::to_string(id));
  156. sql.append(";");
  157. log_info("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
  158. }
  159. std::string Error;
  160. YADB::CDBResultSet DBRes;
  161. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  162. int nCount = DBRes.GetRecordCount( Error );
  163. if (nCount < 1)
  164. {
  165. log_error("基础数据 monkeycar area 增加或修改失败,数据库中找不到: area_id=%d", id);
  166. return ;
  167. }
  168. std::unordered_map<int,std::shared_ptr<area>> map;
  169. while ( DBRes.GetNextRecod(Error) )
  170. {
  171. int area_id = 0;
  172. DBRes.GetField( "area_id",area_id, Error );
  173. int map_id = 0;
  174. DBRes.GetField( "map_id",map_id, Error );
  175. unsigned int area_type_id = 0;
  176. DBRes.GetField( "area_type_id",area_type_id, Error );
  177. int over_count_person = 0;
  178. DBRes.GetField( "over_count_person",over_count_person, Error );
  179. int over_count_vehicle = 0;
  180. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  181. int over_time_person = 0;
  182. DBRes.GetField( "over_time_person",over_time_person, Error );
  183. int over_time_vehicle = 0;
  184. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  185. std::string path;
  186. DBRes.GetField( "path",path, Error );
  187. double monkeycar_speed = 0;
  188. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  189. std::string monkeycar_coor;
  190. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  191. double scale=0;
  192. DBRes.GetField( "scale",scale, Error );
  193. uint32_t b_type =0;
  194. DBRes.GetField( "business_type",b_type, Error );
  195. log_info("monkeycar area init_area : id:%d,path:%s",area_id, path.c_str());
  196. if(-1 == id)
  197. {
  198. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  199. da->m_default_speed = monkeycar_speed;
  200. da->m_point = init_path(monkeycar_coor);
  201. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  202. ap->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  203. ap->m_bound=init_path(path);
  204. for(const auto &p : ap->m_bound)
  205. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  206. for(const auto &p : da->m_point)
  207. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  208. map.insert({area_id,ap});
  209. }
  210. else
  211. {
  212. //这里后续需要把猴车得信息传递过去
  213. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  214. da->m_default_speed = monkeycar_speed;
  215. da->m_point = init_path(monkeycar_coor);
  216. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
  217. if(auto area_=area_list::instance()->get(id))
  218. area_=ap;
  219. else
  220. area_list::instance()->add(area_id,ap);
  221. ap->m_bound=init_path(path);
  222. ap->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  223. for(const auto &p : ap->m_bound)
  224. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  225. for(const auto &p : da->m_point)
  226. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  227. log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  228. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  229. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  230. over_count_vehicle,over_time_vehicle);
  231. }
  232. }
  233. if(-1 == id)
  234. {
  235. log_info( "monkeycar area init_area. The record count=%d\n", nCount );
  236. area_list::instance()->add(map);
  237. }
  238. }
  239. 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)
  240. {
  241. if(type==AREA_TYPE_FORBIDDEN)
  242. {
  243. return std::make_shared<special_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  244. }
  245. else if(type == AREA_TYPE_DOWNMINE)
  246. {
  247. return std::make_shared<underground_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  248. }
  249. else
  250. {
  251. return std::make_shared<area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
  252. }
  253. }
  254. void area_list::init_from_db(int id/*=-1*/)
  255. {
  256. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  257. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
  258. over_speed_vehicle, is_attendance,business_type, a.is_work_area\
  259. FROM dat_area a,dat_map b\
  260. where a.map_id = b.map_id and area_id not in \
  261. (select monkeycar_base_info_id from dat_monkeycar_base_info)";
  262. if(-1 == id)
  263. {
  264. sql.append(";");
  265. }
  266. else
  267. {
  268. sql.append(" AND s.area_id=");
  269. sql.append(std::to_string(id));
  270. sql.append(";");
  271. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  272. }
  273. std::string Error;
  274. YADB::CDBResultSet DBRes;
  275. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  276. int nCount = DBRes.GetRecordCount( Error );
  277. if (nCount < 1)
  278. {
  279. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
  280. return ;
  281. }
  282. std::unordered_map<int,std::shared_ptr<area>> map;
  283. while ( DBRes.GetNextRecod(Error) )
  284. {
  285. int area_id = 0;
  286. DBRes.GetField( "area_id",area_id, Error );
  287. int map_id = 0;
  288. DBRes.GetField( "map_id",map_id, Error );
  289. unsigned int area_type_id = 0;
  290. DBRes.GetField( "area_type_id",area_type_id, Error );
  291. int over_count_person = 0;
  292. DBRes.GetField( "over_count_person",over_count_person, Error );
  293. int over_count_vehicle = 0;
  294. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  295. int over_time_person = 0;
  296. DBRes.GetField( "over_time_person",over_time_person, Error );
  297. int over_time_vehicle = 0;
  298. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  299. double over_speed_vehicle=0;
  300. DBRes.GetField( "over_speed_vehicle",over_speed_vehicle, Error );
  301. std::string path;
  302. DBRes.GetField( "path",path, Error );
  303. double scale = 0;
  304. DBRes.GetField( "scale",scale, Error );
  305. uint32_t b_type =0;
  306. DBRes.GetField( "business_type",b_type, Error );
  307. int is_work_area = 0;
  308. DBRes.GetField( "is_work_area",is_work_area, Error );
  309. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  310. if(-1 == id)
  311. {
  312. std::shared_ptr<area> ap = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  313. ap->m_limit_vehicle_second = over_time_vehicle;
  314. ap->m_limit_vehicle_count = over_count_vehicle;
  315. ap->m_over_speed_vehicle = over_speed_vehicle;
  316. ap->m_is_work_area = is_work_area;
  317. ap->m_bound=init_path(path);
  318. for(const auto &p : ap->m_bound)
  319. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  320. map.insert({area_id,ap});
  321. }
  322. else
  323. {
  324. auto tmp_ptr = area_list::instance()->get(id);
  325. if(!tmp_ptr)
  326. {
  327. tmp_ptr = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
  328. area_list::instance()->add(id, tmp_ptr);
  329. }
  330. tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id, over_count_vehicle,over_time_vehicle);
  331. tmp_ptr->m_bound=init_path(path);
  332. tmp_ptr->m_over_speed_vehicle = over_speed_vehicle;
  333. tmp_ptr->m_is_work_area = is_work_area;
  334. for(const auto &p : tmp_ptr->m_bound)
  335. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  336. log_info("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  337. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  338. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  339. over_count_vehicle,over_time_vehicle);
  340. }
  341. }
  342. if(-1 == id)
  343. {
  344. log_info( "init_area. The record count=%d\n", nCount );
  345. area_list::instance()->add(map);
  346. }
  347. init_monkeycar_area(id);
  348. }
  349. std::vector<point> area_list::init_path(std::string &str)
  350. {
  351. if(str.empty())
  352. log_error("area path empty()...");
  353. std::vector<point> vp;
  354. std::vector<std::string> vs;
  355. std::vector<std::string> vs1;
  356. boost::split(vs,str,boost::is_any_of("ML ;"));
  357. for(auto & s:vs)
  358. {
  359. if(s.empty())
  360. continue;
  361. boost::split(vs1,s,boost::is_any_of(","));
  362. if(vs1.size()!=2)
  363. log_error("area path data Error.pls check data table.");
  364. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  365. }
  366. return std::move(vp);
  367. }
  368. 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)
  369. {
  370. std::vector<std::shared_ptr<area>> ret;
  371. if(tool_other::is_person(c->type_())&&s&&s->is_up_site())
  372. return std::move(ret);
  373. auto map = area_list::instance()->m_map;
  374. for(const auto &a:map)
  375. if(a.second->in_area(s,c,pt,sarid))
  376. ret.push_back(a.second);
  377. if(s)
  378. ret.push_back(s->get_area());
  379. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  380. return std::move(ret);
  381. }
  382. area_hover::area_hover(std::shared_ptr<area>&area,const point&pt)
  383. :m_area(area)
  384. {
  385. m_enter_time=m_last_time=tool_time::now_to_ms();
  386. m_enter_point=m_last_point=pt;
  387. }
  388. void area_tool::on_point(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>& c,const point&pt)
  389. {
  390. log_info("on_point...cardid:%d,type:%d",c->m_id,c->m_type);
  391. int special_area=-1;
  392. std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(s,c, pt,special_area);
  393. if(special_area != -1)
  394. {
  395. areas.clear();
  396. auto area_=area_list::instance()->get(special_area);
  397. areas.push_back(area_);
  398. }
  399. else
  400. std::sort(areas.begin(),areas.end(),[](const std::shared_ptr<area>&l,const std::shared_ptr<area>&r){
  401. return l->id()<r->id();
  402. });
  403. auto c1=m_hover_list.begin(),ce=m_hover_list.end();
  404. auto a1=areas.begin() ,ae=areas.end();
  405. std::vector<std::shared_ptr<area_hover>> nlist;
  406. while (c1!=ce && a1!=ae)
  407. {
  408. if ((*c1)->id()<(*a1)->id())
  409. {
  410. (*c1)->m_area->on_leave(*c1, c);
  411. ++c1;
  412. }
  413. else if ((*a1)->id()<(*c1)->id())
  414. {
  415. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  416. (*a1)->on_enter(nlist.back(),c);
  417. ++a1;
  418. }
  419. else
  420. {
  421. nlist.push_back(*c1);
  422. (*c1)->m_area->on_hover(*c1,c);
  423. ++c1,++a1;
  424. }
  425. }
  426. while(c1!=ce)
  427. {
  428. (*c1)->m_area->on_leave(*c1, c);
  429. ++c1;
  430. }
  431. while(a1!=ae)
  432. {
  433. nlist.push_back(std::make_shared<area_hover>(*a1,pt));
  434. (*a1)->on_enter(nlist.back(),c);
  435. ++a1;
  436. }
  437. m_hover_list=std::move(nlist);
  438. }
  439. void area_tool::on_leave(const std::shared_ptr<card_location_base>& c)
  440. {
  441. for(auto& t:m_hover_list)
  442. {
  443. t->m_area->on_leave(t, c);
  444. }
  445. }
  446. void area_tool::set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type)
  447. {
  448. if(0==type)
  449. {
  450. m_mapid = mapid;
  451. m_scale=scale;
  452. set_area_info(mapid,areaid,pt,t);
  453. }
  454. else if(1==type)
  455. {
  456. set_area_info(mapid,areaid,pt,t);
  457. }
  458. else if(2==type)
  459. {
  460. auto it =m_area_info.find(areaid);
  461. if(it != m_area_info.end())
  462. m_area_info.erase(it);
  463. }
  464. else
  465. {
  466. log_info("wrong type..");
  467. }
  468. }
  469. void area_tool::set_area_info(int mapid,int areaid,const point &pt,uint64_t t)
  470. {
  471. auto lm = Landmark_list::instance()->get(mapid,areaid,pt);
  472. int landmark_id = std::get<0>(lm);
  473. int landmark_dir = std::get<1>(lm);
  474. double landmark_dis = std::get<2>(lm)*m_scale;
  475. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,m_scale);
  476. std::tuple<int,int,int,double,uint64_t> tinfo=std::make_tuple(areaid,landmark_id,landmark_dir,landmark_dis,t);
  477. m_area_info[areaid].swap(tinfo);
  478. }