area.cpp 18 KB

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