area.cpp 8.0 KB

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