area.cpp 17 KB

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