area.cpp 17 KB

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