area.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. template<> std::shared_ptr<area_list>
  14. single_base<area_list, int, std::shared_ptr<area>>::m_instance=std::make_shared<area_list>();
  15. void area::on_hover(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
  16. {
  17. for(auto i:m_area_business_list)
  18. i->on_hover(a,c);
  19. }
  20. void area::on_enter(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
  21. {
  22. log_info("on_enter..%d areaId:%d",c->m_id,m_id);
  23. for(auto i:m_area_business_list)
  24. i->on_enter(a,c);
  25. }
  26. void area::on_leave(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
  27. {
  28. log_info("on_leave..%d areaId:%d",c->m_id,m_id);
  29. for(auto i:m_area_business_list)
  30. i->on_leave(a,c);
  31. }
  32. bool area::in_area(const point & p)
  33. {
  34. if(m_bound.empty())
  35. return false;
  36. int counter = 0;
  37. double xinters;
  38. point p1,p2;
  39. p1 = m_bound[0];
  40. int size = m_bound.size();
  41. for (int i=1;i<= size;i++) {
  42. p2 = m_bound[i%size];
  43. if (p.y > std::min(p1.y,p2.y)) {
  44. if (p.y <= std::max(p1.y,p2.y)) {
  45. if (p.x <= std::max(p1.x,p2.x)) {
  46. if (p1.y != p2.y) {
  47. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  48. if (p1.x == p2.x || p.x <= xinters)
  49. counter++;
  50. }
  51. }
  52. }
  53. }
  54. p1 = p2;
  55. }
  56. return (counter % 2 == 0) ? false : true;
  57. }
  58. area_list::area_list()
  59. {
  60. }
  61. #if 0
  62. void area_list::init_monkeycar_area()
  63. {
  64. std::unordered_map<int,std::shared_ptr<area>> map;
  65. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  66. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed \
  67. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  68. where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id;";
  69. std::string Error;
  70. YADB::CDBResultSet DBRes;
  71. sDBConnPool.Query(sql,DBRes,Error);
  72. if(!Error.empty())
  73. log_error("monkeycar area init Error,%s",Error.c_str());
  74. int nCount = DBRes.GetRecordCount( Error );
  75. if (nCount > 0)
  76. {
  77. log_info( "init_monkey area. The record count=%d", nCount );
  78. while ( DBRes.GetNextRecod(Error) )
  79. {
  80. int area_id = 0;
  81. DBRes.GetField( "area_id",area_id, Error );
  82. int map_id = 0;
  83. DBRes.GetField( "map_id",map_id, Error );
  84. unsigned int area_type_id = 0;
  85. DBRes.GetField( "area_type_id",area_type_id, Error );
  86. int over_count_person = 0;
  87. DBRes.GetField( "over_count_person",over_count_person, Error );
  88. int over_count_vehicle = 0;
  89. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  90. int over_time_person = 0;
  91. DBRes.GetField( "over_time_person",over_time_person, Error );
  92. int over_time_vehicle = 0;
  93. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  94. std::string path;
  95. DBRes.GetField( "path",path, Error );
  96. float monkeycar_speed = 0;
  97. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  98. std::string monkeycar_coor;
  99. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  100. float scale=0;
  101. DBRes.GetField( "scale",scale, Error );
  102. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  103. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  104. da->m_default_speed = monkeycar_speed;
  105. da->m_point = init_path(monkeycar_coor);
  106. 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);
  107. ap->m_bound=init_path(path);
  108. for(const auto &p : ap->m_bound)
  109. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  110. for(const auto &p : da->m_point)
  111. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  112. map.insert({area_id,ap});
  113. }
  114. }
  115. area_list::instance()->add(map);
  116. }
  117. #endif
  118. void area_list::init_monkeycar_area(int id)
  119. {
  120. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  121. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle,\
  122. over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed \
  123. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  124. where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id";
  125. if(-1 == id)
  126. {
  127. sql.append(";");
  128. }
  129. else
  130. {
  131. sql.append(" AND a.area_id=");
  132. sql.append(std::to_string(id));
  133. sql.append(";");
  134. std_debug("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
  135. log_info("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
  136. }
  137. std::string Error;
  138. YADB::CDBResultSet DBRes;
  139. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  140. int nCount = DBRes.GetRecordCount( Error );
  141. if (nCount < 1)
  142. {
  143. log_error("基础数据 monkeycar area 增加或修改失败,数据库中找不到: area_id=%d", id);
  144. return ;
  145. }
  146. std::unordered_map<int,std::shared_ptr<area>> map;
  147. while ( DBRes.GetNextRecod(Error) )
  148. {
  149. int area_id = 0;
  150. DBRes.GetField( "area_id",area_id, Error );
  151. int map_id = 0;
  152. DBRes.GetField( "map_id",map_id, Error );
  153. unsigned int area_type_id = 0;
  154. DBRes.GetField( "area_type_id",area_type_id, Error );
  155. int over_count_person = 0;
  156. DBRes.GetField( "over_count_person",over_count_person, Error );
  157. int over_count_vehicle = 0;
  158. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  159. int over_time_person = 0;
  160. DBRes.GetField( "over_time_person",over_time_person, Error );
  161. int over_time_vehicle = 0;
  162. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  163. std::string path;
  164. DBRes.GetField( "path",path, Error );
  165. double monkeycar_speed = 0;
  166. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  167. std::string monkeycar_coor;
  168. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  169. double scale=0;
  170. DBRes.GetField( "scale",scale, Error );
  171. log_info("monkeycar area init_area : id:%d,path:%s",area_id, path.c_str());
  172. if(-1 == id)
  173. {
  174. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  175. da->m_default_speed = monkeycar_speed;
  176. da->m_point = init_path(monkeycar_coor);
  177. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person,
  178. over_time_person,scale,map_id,area_type_id);
  179. ap->m_bound=init_path(path);
  180. for(const auto &p : ap->m_bound)
  181. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  182. for(const auto &p : da->m_point)
  183. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  184. map.insert({area_id,ap});
  185. }
  186. else
  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,
  192. over_time_person,scale,map_id,area_type_id);
  193. ap->m_bound=init_path(path);
  194. for(const auto &p : ap->m_bound)
  195. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  196. for(const auto &p : da->m_point)
  197. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  198. auto tmp_ptr = area_list::instance()->get(id);
  199. if(tmp_ptr)
  200. {
  201. area_list::instance()->remove(id);
  202. }
  203. area_list::instance()->add(id, ap);
  204. log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  205. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  206. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  207. over_count_vehicle,over_time_vehicle);
  208. std_debug("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  209. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  210. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  211. over_count_vehicle,over_time_vehicle);
  212. }
  213. }
  214. if(-1 == id)
  215. {
  216. log_info( "monkeycar area init_area. The record count=%d\n", nCount );
  217. area_list::instance()->add(map);
  218. }
  219. }
  220. void area_list::init_from_db(int id/*=-1*/)
  221. {
  222. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  223. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
  224. over_speed_vehicle, is_attendance \
  225. FROM dat_area a,dat_map b\
  226. where a.map_id = b.map_id and area_id not in \
  227. (select monkeycar_base_info_id from dat_monkeycar_base_info)";
  228. if(-1 == id)
  229. {
  230. sql.append(";");
  231. }
  232. else
  233. {
  234. sql.append(" AND s.area_id=");
  235. sql.append(std::to_string(id));
  236. sql.append(";");
  237. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  238. }
  239. std::string Error;
  240. YADB::CDBResultSet DBRes;
  241. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  242. int nCount = DBRes.GetRecordCount( Error );
  243. if (nCount < 1)
  244. {
  245. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
  246. return ;
  247. }
  248. std::unordered_map<int,std::shared_ptr<area>> map;
  249. while ( DBRes.GetNextRecod(Error) )
  250. {
  251. int area_id = 0;
  252. DBRes.GetField( "area_id",area_id, Error );
  253. int map_id = 0;
  254. DBRes.GetField( "map_id",map_id, Error );
  255. unsigned int area_type_id = 0;
  256. DBRes.GetField( "area_type_id",area_type_id, Error );
  257. int over_count_person = 0;
  258. DBRes.GetField( "over_count_person",over_count_person, Error );
  259. int over_count_vehicle = 0;
  260. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  261. int over_time_person = 0;
  262. DBRes.GetField( "over_time_person",over_time_person, Error );
  263. int over_time_vehicle = 0;
  264. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  265. std::string path;
  266. DBRes.GetField( "path",path, Error );
  267. double scale = 0;
  268. DBRes.GetField( "scale",scale, Error );
  269. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  270. if(-1 == id)
  271. {
  272. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  273. scale,map_id,area_type_id);
  274. ap->m_limit_vehicle_second = over_time_vehicle;
  275. ap->m_limit_vehicle_count = over_count_vehicle;
  276. ap->m_bound=init_path(path);
  277. for(const auto &p : ap->m_bound)
  278. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  279. map.insert({area_id,ap});
  280. }
  281. else
  282. {
  283. auto tmp_ptr = area_list::instance()->get(id);
  284. if(tmp_ptr)
  285. {
  286. tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id,
  287. over_count_vehicle,over_time_vehicle);
  288. tmp_ptr->m_bound=init_path(path);
  289. }
  290. else
  291. {
  292. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  293. scale,map_id,area_type_id);
  294. ap->m_limit_vehicle_second = over_time_vehicle;
  295. ap->m_limit_vehicle_count = over_count_vehicle;
  296. ap->m_bound=init_path(path);
  297. for(const auto &p : ap->m_bound)
  298. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  299. area_list::instance()->add(id, ap);
  300. }
  301. log_info("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  302. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  303. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  304. over_count_vehicle,over_time_vehicle);
  305. }
  306. }
  307. if(-1 == id)
  308. {
  309. log_info( "init_area. The record count=%d\n", nCount );
  310. area_list::instance()->add(map);
  311. init_monkeycar_area();
  312. }
  313. }
  314. #if 0
  315. void area_list::init_from_db()
  316. {
  317. std::unordered_map<int,std::shared_ptr<area>> map;
  318. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  319. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance \
  320. FROM dat_area a,dat_map b\
  321. where a.map_id = b.map_id and area_id not in (select monkeycar_base_info_id from dat_monkeycar_base_info);";
  322. std::string Error;
  323. YADB::CDBResultSet DBRes;
  324. sDBConnPool.Query(sql,DBRes,Error);
  325. if(!Error.empty())
  326. log_error("monkeycar area init Error,%s",Error.c_str());
  327. int nCount = DBRes.GetRecordCount( Error );
  328. if (nCount > 0)
  329. {
  330. log_info( "init_area. The record count=%d\n", nCount );
  331. while ( DBRes.GetNextRecod(Error) )
  332. {
  333. int area_id = 0;
  334. DBRes.GetField( "area_id",area_id, Error );
  335. int map_id = 0;
  336. DBRes.GetField( "map_id",map_id, Error );
  337. unsigned int area_type_id = 0;
  338. DBRes.GetField( "area_type_id",area_type_id, Error );
  339. int over_count_person = 0;
  340. DBRes.GetField( "over_count_person",over_count_person, Error );
  341. int over_count_vehicle = 0;
  342. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  343. int over_time_person = 0;
  344. DBRes.GetField( "over_time_person",over_time_person, Error );
  345. int over_time_vehicle = 0;
  346. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  347. std::string path;
  348. DBRes.GetField( "path",path, Error );
  349. double scale = 0;
  350. DBRes.GetField( "scale",scale, Error );
  351. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  352. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
  353. ap->m_limit_vehicle_second = over_time_vehicle;
  354. ap->m_limit_vehicle_count = over_count_vehicle;
  355. ap->m_bound=init_path(path);
  356. for(const auto &p : ap->m_bound)
  357. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  358. map.insert({area_id,ap});
  359. }
  360. }
  361. area_list::instance()->add(map);
  362. init_monkeycar_area();
  363. }
  364. #endif
  365. std::vector<point> area_list::init_path(std::string &str)
  366. {
  367. if(str.empty())
  368. log_error("area path empty()...");
  369. std::vector<point> vp;
  370. std::vector<std::string> vs;
  371. std::vector<std::string> vs1;
  372. boost::split(vs,str,boost::is_any_of("ML ;"));
  373. for(auto & s:vs)
  374. {
  375. if(s.empty())
  376. continue;
  377. boost::split(vs1,s,boost::is_any_of(","));
  378. if(vs1.size()!=2)
  379. log_error("area path data Error.pls check data table.");
  380. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  381. }
  382. return std::move(vp);
  383. }
  384. std::shared_ptr<area> area_list::get_area(const point&pt)
  385. {
  386. std::shared_ptr<area> ret=nullptr;
  387. auto map = area_list::instance()->m_map;
  388. for(const auto &a:map)
  389. {
  390. if(a.second->in_area(pt))
  391. ret = a.second;
  392. }
  393. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  394. return ret;
  395. }
  396. void area_tool::on_point(const std::shared_ptr<site>&s,std::shared_ptr<card_location_base> c,const point&pt)
  397. {
  398. log_info("on_point...cardid:%d,type:%d",c->m_id,c->m_type);
  399. //获取地标信息
  400. setLandmark(pt);
  401. if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
  402. {
  403. // do_hover_biz(card_id,speed,type);
  404. }
  405. else
  406. {
  407. auto area = area_list::instance()->get_area(pt);
  408. if(area == nullptr)
  409. {
  410. //这里使用分站区域比较合理
  411. // log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
  412. return;
  413. }
  414. if(m_area_hover==nullptr)
  415. {
  416. // m_area_hover = std::make_shared<area_hover>(area,pt,speed);
  417. // do_enter_biz(card_id,speed,type);
  418. }
  419. else
  420. {
  421. // do_leave_biz(card_id,speed,type);
  422. // m_area_hover.reset(new area_hover(area,pt,speed));
  423. // do_enter_biz(card_id,speed,type);
  424. }
  425. }
  426. }
  427. void area_hover::setLandmark(const point &pt)
  428. {
  429. set(pt);
  430. auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
  431. landmark_id = std::get<0>(lm);
  432. landmark_dir = std::get<1>(lm);
  433. landmark_dis = std::get<2>(lm)*scale();
  434. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,scale());
  435. }