area.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //void init_from_db();
  85. //void init_monkeycar_area();
  86. ///id=-1为初始化所有
  87. void init_from_db(int id=-1);
  88. ///id=-1为初始化所有
  89. void init_monkeycar_area(int id=-1);
  90. };
  91. struct area_hover
  92. {
  93. std::shared_ptr<area> m_area;
  94. time_t m_enter_time,m_last_time;
  95. point m_enter_point,m_last_point;
  96. int landmark_id;
  97. int landmark_dir;
  98. double landmark_dis;
  99. area_hover()=default;
  100. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  101. :m_area(area)
  102. {
  103. m_enter_time=m_last_time=time(0);
  104. m_enter_point=m_last_point=pt;
  105. landmark_id=0;
  106. landmark_dir=0;
  107. landmark_dis=0;
  108. }
  109. int id()const
  110. {
  111. return m_area->id();
  112. }
  113. int mapid()const
  114. {
  115. return m_area->mapid();
  116. }
  117. double scale()const
  118. {
  119. return m_area->scale();
  120. }
  121. bool operator == (const area_hover&o)const
  122. {
  123. return m_area->id()==o.m_area->id();
  124. }
  125. bool operator < (const area_hover&o)const
  126. {
  127. return m_area->id()<o.m_area->id();
  128. }
  129. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  130. {
  131. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  132. }
  133. void setLandmark(const point &pt);
  134. void set(const point&pt)
  135. {
  136. m_last_time=time(0);
  137. m_last_point = pt;
  138. }
  139. };
  140. //每张卡包含一个对象
  141. //在解析出数据点时,调用on_point
  142. struct area_tool
  143. {
  144. std::shared_ptr<area_hover> m_area_hover=nullptr;
  145. void on_point(uint32_t card_id,const point&pt,double speed,int16_t type);
  146. void setLandmark(const point &pt)
  147. {
  148. if(m_area_hover)
  149. {
  150. m_area_hover->setLandmark(pt);
  151. }
  152. }
  153. //special area or no area att.,return true;else return false.
  154. bool special_area()
  155. {
  156. if(m_area_hover==nullptr)
  157. return true;
  158. return m_area_hover->m_area->special();
  159. }
  160. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  161. {
  162. if(m_area_hover)
  163. return m_area_hover->getLandmark();
  164. else
  165. return std::make_tuple(0,0,0,0,0,0,0);
  166. }
  167. //检测是否超时
  168. void on_timer(int64_t card_id)
  169. {
  170. }
  171. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  172. {
  173. m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  174. }
  175. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  176. {
  177. m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  178. }
  179. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  180. {
  181. m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  182. }
  183. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  184. {
  185. do_leave_biz(card_id,speed,type);
  186. auto area = area_list::instance()->get(new_areaid);
  187. point pt;
  188. m_area_hover.reset(new area_hover(area,pt,speed));
  189. do_enter_biz(card_id,speed,type);
  190. }
  191. };
  192. #endif