area.cpp 17 KB

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