area.cpp 17 KB

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