1
0

area.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. auto tmp_ptr = area_list::instance()->get(id);
  191. if(tmp_ptr)
  192. {
  193. //待补充
  194. }
  195. else
  196. {
  197. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  198. da->m_default_speed = monkeycar_speed;
  199. da->m_point = init_path(monkeycar_coor);
  200. std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person,
  201. over_time_person,scale,map_id,area_type_id);
  202. ap->m_bound=init_path(path);
  203. for(const auto &p : ap->m_bound)
  204. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  205. for(const auto &p : da->m_point)
  206. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  207. area_list::instance()->add(id, ap);
  208. }
  209. log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  210. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  211. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  212. over_count_vehicle,over_time_vehicle);
  213. std_debug("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
  214. scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  215. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  216. over_count_vehicle,over_time_vehicle);
  217. }
  218. }
  219. if(-1 == id)
  220. {
  221. log_info( "monkeycar area init_area. The record count=%d\n", nCount );
  222. area_list::instance()->add(map);
  223. }
  224. }
  225. void area_list::init_from_db(int id)
  226. {
  227. std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  228. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
  229. over_speed_vehicle, is_attendance \
  230. FROM dat_area a,dat_map b\
  231. where a.map_id = b.map_id and area_id not in \
  232. (select monkeycar_areaid from dat_monkeycar_base_info)";
  233. if(-1 == id)
  234. {
  235. sql.append(";");
  236. }
  237. else
  238. {
  239. sql.append(" AND s.area_id=");
  240. sql.append(std::to_string(id));
  241. sql.append(";");
  242. std_debug("基础数据 增加或修改区域 sql=%s", sql.c_str());
  243. log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
  244. }
  245. std::string Error;
  246. YADB::CDBResultSet DBRes;
  247. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  248. int nCount = DBRes.GetRecordCount( Error );
  249. if (nCount < 1)
  250. {
  251. log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d", id);
  252. return ;
  253. }
  254. std::unordered_map<int,std::shared_ptr<area>> map;
  255. while ( DBRes.GetNextRecod(Error) )
  256. {
  257. int area_id = 0;
  258. DBRes.GetField( "area_id",area_id, Error );
  259. int map_id = 0;
  260. DBRes.GetField( "map_id",map_id, Error );
  261. unsigned int area_type_id = 0;
  262. DBRes.GetField( "area_type_id",area_type_id, Error );
  263. int over_count_person = 0;
  264. DBRes.GetField( "over_count_person",over_count_person, Error );
  265. int over_count_vehicle = 0;
  266. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  267. int over_time_person = 0;
  268. DBRes.GetField( "over_time_person",over_time_person, Error );
  269. int over_time_vehicle = 0;
  270. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  271. std::string path;
  272. DBRes.GetField( "path",path, Error );
  273. double scale = 0;
  274. DBRes.GetField( "scale",scale, Error );
  275. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  276. if(-1 == id)
  277. {
  278. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  279. scale,map_id,area_type_id);
  280. ap->m_limit_vehicle_second = over_time_vehicle;
  281. ap->m_limit_vehicle_count = over_count_vehicle;
  282. ap->m_bound=init_path(path);
  283. for(const auto &p : ap->m_bound)
  284. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  285. map.insert({area_id,ap});
  286. }
  287. else
  288. {
  289. auto tmp_ptr = area_list::instance()->get(id);
  290. if(tmp_ptr)
  291. {
  292. tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id,
  293. over_count_vehicle,over_time_vehicle);
  294. tmp_ptr->m_bound=init_path(path);
  295. }
  296. else
  297. {
  298. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
  299. scale,map_id,area_type_id);
  300. ap->m_limit_vehicle_second = over_time_vehicle;
  301. ap->m_limit_vehicle_count = over_count_vehicle;
  302. ap->m_bound=init_path(path);
  303. for(const auto &p : ap->m_bound)
  304. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  305. area_list::instance()->add(id, ap);
  306. }
  307. log_info("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  308. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  309. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  310. over_count_vehicle,over_time_vehicle);
  311. std_debug("基础数据 增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
  312. ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
  313. id,over_count_person, over_time_person,scale,map_id,area_type_id,
  314. over_count_vehicle,over_time_vehicle);
  315. }
  316. }
  317. if(-1 == id)
  318. {
  319. log_info( "init_area. The record count=%d\n", nCount );
  320. area_list::instance()->add(map);
  321. init_monkeycar_area(-1);
  322. }
  323. }
  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_areaid 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. std::vector<point> area_list::init_path(std::string &str)
  374. {
  375. if(str.empty())
  376. log_error("area path empty()...");
  377. std::vector<point> vp;
  378. std::vector<std::string> vs;
  379. std::vector<std::string> vs1;
  380. boost::split(vs,str,boost::is_any_of("ML ;"));
  381. for(auto & s:vs)
  382. {
  383. if(s.empty())
  384. continue;
  385. boost::split(vs1,s,boost::is_any_of(","));
  386. if(vs1.size()!=2)
  387. log_error("area path data Error.pls check data table.");
  388. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  389. }
  390. return std::move(vp);
  391. }
  392. std::shared_ptr<area> area_list::get_area(const point&pt)
  393. {
  394. std::shared_ptr<area> ret=nullptr;
  395. auto map = area_list::instance()->m_map;
  396. for(const auto &a:map)
  397. {
  398. if(a.second->in_area(pt))
  399. ret = a.second;
  400. }
  401. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  402. return ret;
  403. }
  404. void area_tool::on_point(uint32_t card_id,const point&pt,double speed,int16_t type)
  405. {
  406. log_info("on_point...cardid:%d,type:%d",card_id,type);
  407. //获取地标信息
  408. setLandmark(pt);
  409. if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
  410. {
  411. do_hover_biz(card_id,speed,type);
  412. }
  413. else
  414. {
  415. auto area = area_list::instance()->get_area(pt);
  416. if(area == nullptr)
  417. {
  418. //这里使用分站区域比较合理
  419. log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
  420. return;
  421. }
  422. if(m_area_hover==nullptr)
  423. {
  424. m_area_hover = std::make_shared<area_hover>(area,pt,speed);
  425. do_enter_biz(card_id,speed,type);
  426. }
  427. else
  428. {
  429. do_leave_biz(card_id,speed,type);
  430. m_area_hover.reset(new area_hover(area,pt,speed));
  431. do_enter_biz(card_id,speed,type);
  432. }
  433. }
  434. }
  435. void area_hover::setLandmark(const point &pt)
  436. {
  437. set(pt);
  438. auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
  439. landmark_id = std::get<0>(lm);
  440. landmark_dir = std::get<1>(lm);
  441. landmark_dis = std::get<2>(lm)*scale();
  442. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,scale());
  443. }