area.cpp 7.9 KB

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