area.cpp 17 KB

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