area.cpp 17 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 <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. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  242. }
  243. std::string Error;
  244. YADB::CDBResultSet DBRes;
  245. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  246. int nCount = DBRes.GetRecordCount( Error );
  247. if (nCount < 1)
  248. {
  249. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
  250. return ;
  251. }
  252. std::unordered_map<int,std::shared_ptr<area>> map;
  253. while ( DBRes.GetNextRecod(Error) )
  254. {
  255. int area_id = 0;
  256. DBRes.GetField( "area_id",area_id, Error );
  257. int map_id = 0;
  258. DBRes.GetField( "map_id",map_id, Error );
  259. unsigned int area_type_id = 0;
  260. DBRes.GetField( "area_type_id",area_type_id, Error );
  261. int over_count_person = 0;
  262. DBRes.GetField( "over_count_person",over_count_person, Error );
  263. int over_count_vehicle = 0;
  264. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  265. int over_time_person = 0;
  266. DBRes.GetField( "over_time_person",over_time_person, Error );
  267. int over_time_vehicle = 0;
  268. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  269. std::string path;
  270. DBRes.GetField( "path",path, Error );
  271. double scale = 0;
  272. DBRes.GetField( "scale",scale, Error );
  273. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  274. if(-1 == id)
  275. {
  276. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  277. scale,map_id,area_type_id);
  278. ap->m_limit_vehicle_second = over_time_vehicle;
  279. ap->m_limit_vehicle_count = over_count_vehicle;
  280. ap->m_bound=init_path(path);
  281. for(const auto &p : ap->m_bound)
  282. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  283. map.insert({area_id,ap});
  284. }
  285. else
  286. {
  287. auto tmp_ptr = area_list::instance()->get(id);
  288. if(tmp_ptr)
  289. {
  290. tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id,
  291. over_count_vehicle,over_time_vehicle);
  292. tmp_ptr->m_bound=init_path(path);
  293. }
  294. else
  295. {
  296. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  297. scale,map_id,area_type_id);
  298. ap->m_limit_vehicle_second = over_time_vehicle;
  299. ap->m_limit_vehicle_count = over_count_vehicle;
  300. ap->m_bound=init_path(path);
  301. for(const auto &p : ap->m_bound)
  302. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  303. area_list::instance()->add(id, ap);
  304. }
  305. log_info("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  306. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  307. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  308. over_count_vehicle,over_time_vehicle);
  309. }
  310. }
  311. if(-1 == id)
  312. {
  313. log_info( "init_area. The record count=%d\n", nCount );
  314. area_list::instance()->add(map);
  315. init_monkeycar_area();
  316. }
  317. }
  318. #if 0
  319. void area_list::init_from_db()
  320. {
  321. std::unordered_map<int,std::shared_ptr<area>> map;
  322. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  323. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance \
  324. FROM dat_area a,dat_map b\
  325. where a.map_id = b.map_id and area_id not in (select monkeycar_base_info_id from dat_monkeycar_base_info);";
  326. std::string Error;
  327. YADB::CDBResultSet DBRes;
  328. sDBConnPool.Query(sql,DBRes,Error);
  329. if(!Error.empty())
  330. log_error("monkeycar area init Error,%s",Error.c_str());
  331. int nCount = DBRes.GetRecordCount( Error );
  332. if (nCount > 0)
  333. {
  334. log_info( "init_area. The record count=%d\n", nCount );
  335. while ( DBRes.GetNextRecod(Error) )
  336. {
  337. int area_id = 0;
  338. DBRes.GetField( "area_id",area_id, Error );
  339. int map_id = 0;
  340. DBRes.GetField( "map_id",map_id, Error );
  341. unsigned int area_type_id = 0;
  342. DBRes.GetField( "area_type_id",area_type_id, Error );
  343. int over_count_person = 0;
  344. DBRes.GetField( "over_count_person",over_count_person, Error );
  345. int over_count_vehicle = 0;
  346. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  347. int over_time_person = 0;
  348. DBRes.GetField( "over_time_person",over_time_person, Error );
  349. int over_time_vehicle = 0;
  350. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  351. std::string path;
  352. DBRes.GetField( "path",path, Error );
  353. double scale = 0;
  354. DBRes.GetField( "scale",scale, Error );
  355. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  356. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
  357. ap->m_limit_vehicle_second = over_time_vehicle;
  358. ap->m_limit_vehicle_count = over_count_vehicle;
  359. ap->m_bound=init_path(path);
  360. for(const auto &p : ap->m_bound)
  361. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  362. map.insert({area_id,ap});
  363. }
  364. }
  365. area_list::instance()->add(map);
  366. init_monkeycar_area();
  367. }
  368. #endif
  369. std::vector<point> area_list::init_path(std::string &str)
  370. {
  371. if(str.empty())
  372. log_error("area path empty()...");
  373. std::vector<point> vp;
  374. std::vector<std::string> vs;
  375. std::vector<std::string> vs1;
  376. boost::split(vs,str,boost::is_any_of("ML ;"));
  377. for(auto & s:vs)
  378. {
  379. if(s.empty())
  380. continue;
  381. boost::split(vs1,s,boost::is_any_of(","));
  382. if(vs1.size()!=2)
  383. log_error("area path data Error.pls check data table.");
  384. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  385. }
  386. return std::move(vp);
  387. }
  388. std::shared_ptr<area> area_list::get_area(const point&pt)
  389. {
  390. std::shared_ptr<area> ret=nullptr;
  391. auto map = area_list::instance()->m_map;
  392. for(const auto &a:map)
  393. {
  394. if(a.second->in_area(pt))
  395. ret = a.second;
  396. }
  397. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  398. return ret;
  399. }
  400. void area_tool::on_point(uint32_t card_id,const point&pt,double speed,int16_t type)
  401. {
  402. log_info("on_point...cardid:%d,type:%d",card_id,type);
  403. //获取地标信息
  404. setLandmark(pt);
  405. if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
  406. {
  407. do_hover_biz(card_id,speed,type);
  408. }
  409. else
  410. {
  411. auto area = area_list::instance()->get_area(pt);
  412. if(area == nullptr)
  413. {
  414. //这里使用分站区域比较合理
  415. log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
  416. return;
  417. }
  418. if(m_area_hover==nullptr)
  419. {
  420. m_area_hover = std::make_shared<area_hover>(area,pt,speed);
  421. do_enter_biz(card_id,speed,type);
  422. }
  423. else
  424. {
  425. do_leave_biz(card_id,speed,type);
  426. m_area_hover.reset(new area_hover(area,pt,speed));
  427. do_enter_biz(card_id,speed,type);
  428. }
  429. }
  430. }
  431. void area_hover::setLandmark(const point &pt)
  432. {
  433. set(pt);
  434. auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
  435. landmark_id = std::get<0>(lm);
  436. landmark_dir = std::get<1>(lm);
  437. landmark_dis = std::get<2>(lm)*scale();
  438. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,scale());
  439. }