area.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef _AREA_HPP_
  2. #define _AREA_HPP_
  3. #include <atomic>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <point.h>
  7. #include "common.h"
  8. #include <write-copy.h>
  9. struct area_hover;
  10. struct point;
  11. struct area
  12. {
  13. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  14. :m_id(id)
  15. ,m_area_type(type)
  16. ,m_limit_person_second(limit_time_person)
  17. ,m_limit_person_count(limit_count_person)
  18. ,m_scale(scale)
  19. ,m_mapid(mapid)
  20. ,m_person_count(0)
  21. ,m_vehicle_count(0)
  22. {
  23. }
  24. virtual void on_hover(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  25. virtual void on_enter(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  26. virtual void on_leave(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  27. bool in_area(const point & p);
  28. int id()const
  29. {
  30. return m_id;
  31. }
  32. int mapid()const
  33. {
  34. return m_mapid;
  35. }
  36. double scale()const
  37. {
  38. return m_scale;
  39. }
  40. bool special()const
  41. {
  42. return m_area_type == AREA_TYPE_NO_COVER;
  43. }
  44. virtual ~area()
  45. {}
  46. ///区域卡数
  47. int card_count()const
  48. {
  49. return m_person_count + m_vehicle_count;
  50. }
  51. void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid,
  52. int32_t type,int limit_count_vehicle, int limit_time_vehicle)
  53. {
  54. m_area_type=type;
  55. m_limit_person_second=limit_time_person;
  56. m_limit_person_count=limit_count_person;
  57. m_scale=scale;
  58. m_mapid=mapid;
  59. m_limit_vehicle_count=limit_count_vehicle;
  60. m_limit_vehicle_second=limit_time_vehicle;
  61. }
  62. std::vector<point> m_bound;
  63. public:
  64. //std::atomic<int> m_card_count;
  65. int m_id;
  66. ///区域类型 AREA_TYPE
  67. int m_area_type;
  68. int m_limit_person_second;
  69. int m_limit_person_count;
  70. int m_limit_vehicle_second;
  71. int m_limit_vehicle_count;
  72. double m_scale;
  73. int32_t m_mapid;
  74. ///区域人卡数
  75. std::atomic<int> m_person_count;
  76. ///区域车卡数
  77. std::atomic<int> m_vehicle_count;
  78. };
  79. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  80. {
  81. area_list();
  82. std::shared_ptr<area> get_area(const point&pt);
  83. std::vector<point> init_path(std::string &str);
  84. ///id=-1为初始化所有
  85. void init_from_db(int id=-1);
  86. void init_monkeycar_area(int id=-1);
  87. };
  88. struct area_hover
  89. {
  90. std::shared_ptr<area> m_area;
  91. time_t m_enter_time,m_last_time;
  92. point m_enter_point,m_last_point;
  93. int landmark_id;
  94. int landmark_dir;
  95. double landmark_dis;
  96. area_hover()=default;
  97. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  98. :m_area(area)
  99. {
  100. m_enter_time=m_last_time=time(0);
  101. m_enter_point=m_last_point=pt;
  102. landmark_id=0;
  103. landmark_dir=0;
  104. landmark_dis=0;
  105. }
  106. int id()const
  107. {
  108. return m_area->id();
  109. }
  110. int mapid()const
  111. {
  112. return m_area->mapid();
  113. }
  114. double scale()const
  115. {
  116. return m_area->scale();
  117. }
  118. bool operator == (const area_hover&o)const
  119. {
  120. return m_area->id()==o.m_area->id();
  121. }
  122. bool operator < (const area_hover&o)const
  123. {
  124. return m_area->id()<o.m_area->id();
  125. }
  126. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  127. {
  128. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  129. }
  130. void setLandmark(const point &pt);
  131. void set(const point&pt)
  132. {
  133. m_last_time=time(0);
  134. m_last_point = pt;
  135. }
  136. };
  137. //每张卡包含一个对象
  138. //在解析出数据点时,调用on_point
  139. struct area_tool
  140. {
  141. std::shared_ptr<area_hover> m_area_hover=nullptr;
  142. void on_point(uint32_t card_id,const point&pt,double speed,int16_t type);
  143. void setLandmark(const point &pt)
  144. {
  145. if(m_area_hover)
  146. {
  147. m_area_hover->setLandmark(pt);
  148. }
  149. }
  150. //special area or no area att.,return true;else return false.
  151. bool special_area()
  152. {
  153. if(m_area_hover==nullptr)
  154. return true;
  155. return m_area_hover->m_area->special();
  156. }
  157. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  158. {
  159. if(m_area_hover)
  160. return m_area_hover->getLandmark();
  161. else
  162. return std::make_tuple(0,0,0,0,0,0,0);
  163. }
  164. //检测是否超时
  165. void on_timer(int64_t card_id)
  166. {
  167. }
  168. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  169. {
  170. m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  171. }
  172. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  173. {
  174. m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  175. }
  176. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  177. {
  178. m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  179. }
  180. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  181. {
  182. do_leave_biz(card_id,speed,type);
  183. auto area = area_list::instance()->get(new_areaid);
  184. point pt;
  185. m_area_hover.reset(new area_hover(area,pt,speed));
  186. do_enter_biz(card_id,speed,type);
  187. }
  188. };
  189. #endif