area.cpp 16 KB

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