1
0

area.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_from_db()
  121. {
  122. std::unordered_map<int,std::shared_ptr<area>> map;
  123. const char *sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
  124. over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, over_speed_vehicle, is_attendance \
  125. FROM dat_area a,dat_map b\
  126. where a.map_id = b.map_id and area_id not in (select monkeycar_areaid from dat_monkeycar_base_info);";
  127. std::string Error;
  128. YADB::CDBResultSet DBRes;
  129. sDBConnPool.Query(sql,DBRes,Error);
  130. if(!Error.empty())
  131. log_error("monkeycar area init Error,%s",Error.c_str());
  132. int nCount = DBRes.GetRecordCount( Error );
  133. if (nCount > 0)
  134. {
  135. log_info( "init_area. The record count=%d\n", nCount );
  136. while ( DBRes.GetNextRecod(Error) )
  137. {
  138. int area_id = 0;
  139. DBRes.GetField( "area_id",area_id, Error );
  140. int map_id = 0;
  141. DBRes.GetField( "map_id",map_id, Error );
  142. unsigned int area_type_id = 0;
  143. DBRes.GetField( "area_type_id",area_type_id, Error );
  144. int over_count_person = 0;
  145. DBRes.GetField( "over_count_person",over_count_person, Error );
  146. int over_count_vehicle = 0;
  147. DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
  148. int over_time_person = 0;
  149. DBRes.GetField( "over_time_person",over_time_person, Error );
  150. int over_time_vehicle = 0;
  151. DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
  152. std::string path;
  153. DBRes.GetField( "path",path, Error );
  154. double scale = 0;
  155. DBRes.GetField( "scale",scale, Error );
  156. log_info("init_area : id:%d,path:%s",area_id, path.c_str());
  157. std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
  158. ap->m_limit_vehicle_second = over_time_vehicle;
  159. ap->m_limit_vehicle_count = over_count_vehicle;
  160. ap->m_bound=init_path(path);
  161. for(const auto &p : ap->m_bound)
  162. log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
  163. map.insert({area_id,ap});
  164. }
  165. }
  166. area_list::instance()->add(map);
  167. init_monkeycar_area();
  168. }
  169. std::vector<point> area_list::init_path(std::string &str)
  170. {
  171. if(str.empty())
  172. log_error("area path empty()...");
  173. std::vector<point> vp;
  174. std::vector<std::string> vs;
  175. std::vector<std::string> vs1;
  176. boost::split(vs,str,boost::is_any_of("ML ;"));
  177. for(auto & s:vs)
  178. {
  179. if(s.empty())
  180. continue;
  181. boost::split(vs1,s,boost::is_any_of(","));
  182. if(vs1.size()!=2)
  183. log_error("area path data Error.pls check data table.");
  184. vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
  185. }
  186. return std::move(vp);
  187. }
  188. std::shared_ptr<area> area_list::get_area(const point&pt)
  189. {
  190. std::shared_ptr<area> ret=nullptr;
  191. auto map = area_list::instance()->m_map;
  192. for(const auto &a:map)
  193. {
  194. if(a.second->in_area(pt))
  195. ret = a.second;
  196. }
  197. //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
  198. return ret;
  199. }
  200. void area_tool::on_point(uint32_t card_id,const point&pt,double speed,int16_t type)
  201. {
  202. log_info("on_point...cardid:%d,type:%d",card_id,type);
  203. //获取地标信息
  204. setLandmark(pt);
  205. if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
  206. {
  207. do_hover_biz(card_id,speed,type);
  208. }
  209. else
  210. {
  211. auto area = area_list::instance()->get_area(pt);
  212. if(area == nullptr)
  213. {
  214. //这里使用分站区域比较合理
  215. log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
  216. return;
  217. }
  218. if(m_area_hover==nullptr)
  219. {
  220. m_area_hover = std::make_shared<area_hover>(area,pt,speed);
  221. do_enter_biz(card_id,speed,type);
  222. }
  223. else
  224. {
  225. do_leave_biz(card_id,speed,type);
  226. m_area_hover.reset(new area_hover(area,pt,speed));
  227. do_enter_biz(card_id,speed,type);
  228. }
  229. }
  230. }
  231. void area_hover::setLandmark(const point &pt)
  232. {
  233. set(pt);
  234. auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
  235. landmark_id = std::get<0>(lm);
  236. landmark_dir = std::get<1>(lm);
  237. landmark_dis = std::get<2>(lm)*scale();
  238. log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,scale());
  239. }