area.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. // //入库 : 进入区域
  30. // int cu = m_card_count.load();
  31. // cu++;
  32. // m_card_count.store(cu);
  33. // //check超员
  34. module_area::on_enter(card_id,c,type);
  35. }
  36. void area::on_leave(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type)
  37. {
  38. log_info("on_leave..%d areaId:%d",card_id,m_id);
  39. //入库 : 出 区域
  40. // //入库 : 出 区域
  41. // int cu = m_card_count.load();
  42. // m_card_count.store(cu--);
  43. // //check 超员
  44. // if(c->m_is_over_time)
  45. // {
  46. // //取消告警
  47. // }
  48. module_area::on_leave(card_id,c,type);
  49. }
  50. bool area::in_area(const point & p)
  51. {
  52. if(m_bound.empty())
  53. return false;
  54. int counter = 0;
  55. double xinters;
  56. point p1,p2;
  57. p1 = m_bound[0];
  58. int size = m_bound.size();
  59. for (int i=1;i<= size;i++) {
  60. p2 = m_bound[i%size];
  61. if (p.y > std::min(p1.y,p2.y)) {
  62. if (p.y <= std::max(p1.y,p2.y)) {
  63. if (p.x <= std::max(p1.x,p2.x)) {
  64. if (p1.y != p2.y) {
  65. xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  66. if (p1.x == p2.x || p.x <= xinters)
  67. counter++;
  68. }
  69. }
  70. }
  71. }
  72. p1 = p2;
  73. }
  74. return (counter % 2 == 0) ? false : true;
  75. }
  76. area_list::area_list()
  77. {
  78. }
  79. void area_list::init_monkeycar_area()
  80. {
  81. std::unordered_map<int,std::shared_ptr<area>> map;
  82. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
  83. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed \
  84. FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
  85. where a.area_id = b.monkeycar_areaid and a.map_id = c.map_id;";
  86. std::string Error;
  87. YADB::CDBResultSet DBRes;
  88. sDBConnPool.Query(sql,DBRes,Error);
  89. if(!Error.empty())
  90. log_error("monkeycar area init Error,%s",Error.c_str());
  91. int nCount = DBRes.GetRecordCount( Error );
  92. if (nCount > 0)
  93. {
  94. log_info( "init_monkey area. The record count=%d", nCount );
  95. while ( DBRes.GetNextRecod(Error) )
  96. {
  97. int area_id = 0;
  98. DBRes.GetField( "area_id",area_id, Error );
  99. int map_id = 0;
  100. DBRes.GetField( "map_id",map_id, Error );
  101. unsigned int area_type_id = 0;
  102. DBRes.GetField( "area_type_id",area_type_id, Error );
  103. int over_count_person = 0;
  104. DBRes.GetField( "over_count_person",over_count_person, Error );
  105. int over_count_vehicle = 0;
  106. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  107. int over_time_person = 0;
  108. DBRes.GetField( "over_time_person",over_time_person, Error );
  109. int over_time_vehicle = 0;
  110. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  111. std::string path;
  112. DBRes.GetField( "path",path, Error );
  113. float monkeycar_speed = 0;
  114. DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
  115. std::string monkeycar_coor;
  116. DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
  117. float scale=0;
  118. DBRes.GetField( "scale",scale, Error );
  119. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  120. std::shared_ptr<db_area> da = std::make_shared<db_area>();
  121. da->m_default_speed = monkeycar_speed;
  122. da->m_point = init_path(monkeycar_coor);
  123. 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);
  124. ap->m_bound=init_path(path);
  125. for(const auto &p : ap->m_bound)
  126. log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  127. for(const auto &p : da->m_point)
  128. log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  129. map.insert({area_id,ap});
  130. }
  131. }
  132. area_list::instance()->add(map);
  133. }
  134. void area_list::init_from_db()
  135. {
  136. std::unordered_map<int,std::shared_ptr<area>> map;
  137. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  138. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance \
  139. FROM dat_area a,dat_map b\
  140. where a.map_id = b.map_id and area_id not in (select monkeycar_areaid from dat_monkeycar_base_info);";
  141. std::string Error;
  142. YADB::CDBResultSet DBRes;
  143. sDBConnPool.Query(sql,DBRes,Error);
  144. if(!Error.empty())
  145. log_error("monkeycar area init Error,%s",Error.c_str());
  146. int nCount = DBRes.GetRecordCount( Error );
  147. if (nCount > 0)
  148. {
  149. log_info( "init_area. The record count=%d\n", nCount );
  150. while ( DBRes.GetNextRecod(Error) )
  151. {
  152. int area_id = 0;
  153. DBRes.GetField( "area_id",area_id, Error );
  154. int map_id = 0;
  155. DBRes.GetField( "map_id",map_id, Error );
  156. unsigned int area_type_id = 0;
  157. DBRes.GetField( "area_type_id",area_type_id, Error );
  158. int over_count_person = 0;
  159. DBRes.GetField( "over_count_person",over_count_person, Error );
  160. int over_count_vehicle = 0;
  161. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  162. int over_time_person = 0;
  163. DBRes.GetField( "over_time_person",over_time_person, Error );
  164. int over_time_vehicle = 0;
  165. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  166. std::string path;
  167. DBRes.GetField( "path",path, Error );
  168. double scale = 0;
  169. DBRes.GetField( "scale",scale, Error );
  170. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  171. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
  172. ap->m_limit_vehicle_second = over_time_vehicle;
  173. ap->m_limit_vehicle_count = over_count_vehicle;
  174. ap->m_bound=init_path(path);
  175. for(const auto &p : ap->m_bound)
  176. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  177. map.insert({area_id,ap});
  178. }
  179. }
  180. area_list::instance()->add(map);
  181. init_monkeycar_area();
  182. }
  183. std::vector<point> area_list::init_path(std::string &str)
  184. {
  185. if(str.empty())
  186. log_error("area path empty()...");
  187. std::vector<point> vp;
  188. std::vector<std::string> vs;
  189. std::vector<std::string> vs1;
  190. boost::split(vs,str,boost::is_any_of("ML ;"));
  191. for(auto & s:vs)
  192. {
  193. if(s.empty())
  194. continue;
  195. boost::split(vs1,s,boost::is_any_of(","));
  196. if(vs1.size()!=2)
  197. log_error("area path data Error.pls check data table.");
  198. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  199. }
  200. return std::move(vp);
  201. }
  202. std::shared_ptr<area> area_list::get_area(const point&pt)
  203. {
  204. std::shared_ptr<area> ret=nullptr;
  205. auto map = area_list::instance()->m_map;
  206. for(const auto &a:map)
  207. {
  208. if(a.second->in_area(pt))
  209. ret = a.second;
  210. }
  211. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  212. return ret;
  213. }
  214. void area_tool::on_point(uint32_t card_id,const point&pt,double speed,int16_t type)
  215. {
  216. log_info("on_point...cardid:%d,type:%d",card_id,type);
  217. //获取地标信息
  218. setLandmark(pt);
  219. if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
  220. {
  221. do_hover_biz(card_id,speed,type);
  222. }
  223. else
  224. {
  225. auto area = area_list::instance()->get_area(pt);
  226. if(area == nullptr)
  227. {
  228. //这里使用分站区域比较合理
  229. log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
  230. return;
  231. }
  232. if(m_area_hover==nullptr)
  233. {
  234. m_area_hover = std::make_shared<area_hover>(area,pt,speed);
  235. do_enter_biz(card_id,speed,type);
  236. }
  237. else
  238. {
  239. do_leave_biz(card_id,speed,type);
  240. m_area_hover.reset(new area_hover(area,pt,speed));
  241. do_enter_biz(card_id,speed,type);
  242. }
  243. }
  244. }
  245. void area_hover::setLandmark(const point &pt)
  246. {
  247. set(pt);
  248. auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
  249. landmark_id = std::get<0>(lm);
  250. landmark_dir = std::get<1>(lm);
  251. landmark_dis = std::get<2>(lm)*scale();
  252. log_info("landmark:%d %d %.2f",landmark_id,landmark_dir,landmark_dis);
  253. }