1
0

area.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_business;
  12. struct business_data;
  13. struct card_location_base;
  14. /*
  15. 每个区域对应一个area对象。
  16. */
  17. struct area
  18. {
  19. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  20. :m_id(id)
  21. ,m_area_type(type)
  22. ,m_limit_person_second(limit_time_person)
  23. ,m_limit_person_count(limit_count_person)
  24. ,m_scale(scale)
  25. ,m_mapid(mapid)
  26. ,m_person_count(0)
  27. ,m_vehicle_count(0)
  28. {
  29. }
  30. virtual void on_hover(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  31. virtual void on_enter(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  32. virtual void on_leave(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  33. bool in_area(const point & p);
  34. int id()const
  35. {
  36. return m_id;
  37. }
  38. int mapid()const
  39. {
  40. return m_mapid;
  41. }
  42. double scale()const
  43. {
  44. return m_scale;
  45. }
  46. bool special()const
  47. {
  48. return m_area_type == AREA_TYPE_NO_COVER;
  49. }
  50. virtual ~area()
  51. {}
  52. ///区域卡数
  53. int card_count()const
  54. {
  55. return m_person_count + m_vehicle_count;
  56. }
  57. void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid,
  58. int32_t type,int limit_count_vehicle, int limit_time_vehicle)
  59. {
  60. m_area_type=type;
  61. m_limit_person_second=limit_time_person;
  62. m_limit_person_count=limit_count_person;
  63. m_scale=scale;
  64. m_mapid=mapid;
  65. m_limit_vehicle_count=limit_count_vehicle;
  66. m_limit_vehicle_second=limit_time_vehicle;
  67. }
  68. public:
  69. std::vector<std::shared_ptr<area_business>> m_area_business_list;
  70. public:
  71. std::vector<point> m_bound;
  72. //std::atomic<int> m_card_count;
  73. //数据库唯一ID
  74. int m_id;
  75. ///区域类型 AREA_TYPE
  76. //用户定义的业务类型,BIT集合
  77. /*
  78. 1:位置[优先级]
  79. 2:超时[超时时间分钟数]
  80. 3:超员[人员数量、车辆数量]
  81. 4:超速[超速值、判断策略N/M]
  82. 5:人员考勤
  83. 6:车辆考勤[离开最小,离开最小距离]
  84. 7:禁区[进入时长]
  85. 8:猴车区域
  86. */
  87. int m_area_type;
  88. //人卡超时及超员数量
  89. int m_limit_person_second;
  90. int m_limit_person_count;
  91. //人卡超时及超员数量
  92. int m_limit_vehicle_second;
  93. int m_limit_vehicle_count;
  94. double m_scale;
  95. int32_t m_mapid;
  96. ///区域人卡数
  97. std::atomic<int> m_person_count;
  98. ///区域车卡数
  99. std::atomic<int> m_vehicle_count;
  100. };
  101. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  102. {
  103. area_list();
  104. std::shared_ptr<area> get_area(const point&pt);
  105. std::vector<point> init_path(std::string &str);
  106. //void init_from_db();
  107. //void init_monkeycar_area();
  108. ///id=-1为初始化所有
  109. void init_from_db(int id=-1);
  110. ///id=-1为初始化所有
  111. void init_monkeycar_area(int id=-1);
  112. };
  113. struct area_hover
  114. {
  115. std::shared_ptr<area> m_area;
  116. time_t m_enter_time,m_last_time;
  117. point m_enter_point,m_last_point;
  118. int landmark_id;
  119. int landmark_dir;
  120. double landmark_dis;
  121. /*
  122. 记录该业务所关心的需持续使用的数据,每个业务一个指针
  123. 建议该数据项在on_enter时初始化,on_leave时清除
  124. */
  125. std::vector<std::shared_ptr<business_data>> m_data;
  126. area_hover()=default;
  127. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  128. :m_area(area)
  129. {
  130. m_enter_time=m_last_time=time(0);
  131. m_enter_point=m_last_point=pt;
  132. landmark_id=0;
  133. landmark_dir=0;
  134. landmark_dis=0;
  135. }
  136. std::shared_ptr<business_data>*get_business_data(int type)
  137. {
  138. if(type>=(int)m_data.size())
  139. {
  140. m_data.resize(type+1);
  141. }
  142. return &m_data[type];
  143. }
  144. int id()const
  145. {
  146. return m_area->id();
  147. }
  148. int mapid()const
  149. {
  150. return m_area->mapid();
  151. }
  152. double scale()const
  153. {
  154. return m_area->scale();
  155. }
  156. bool operator == (const area_hover&o)const
  157. {
  158. return m_area->id()==o.m_area->id();
  159. }
  160. bool operator < (const area_hover&o)const
  161. {
  162. return m_area->id()<o.m_area->id();
  163. }
  164. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  165. {
  166. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  167. }
  168. void setLandmark(const point &pt);
  169. void set(const point&pt)
  170. {
  171. m_last_time=time(0);
  172. m_last_point = pt;
  173. }
  174. };
  175. //每张卡包含一个对象
  176. //在解析出数据点时,调用on_point
  177. struct site;
  178. struct area_tool
  179. {
  180. std::shared_ptr<area_hover> m_area_hover=nullptr;
  181. void on_point(const std::shared_ptr<site>&s,std::shared_ptr<card_location_base> c,const point&pt);
  182. void setLandmark(const point &pt)
  183. {
  184. if(m_area_hover)
  185. {
  186. m_area_hover->setLandmark(pt);
  187. }
  188. }
  189. //special area or no area att.,return true;else return false.
  190. bool special_area()
  191. {
  192. if(m_area_hover==nullptr)
  193. return true;
  194. return m_area_hover->m_area->special();
  195. }
  196. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  197. {
  198. if(m_area_hover)
  199. return m_area_hover->getLandmark();
  200. else
  201. return std::make_tuple(0,0,0,0,0,0,0);
  202. }
  203. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  204. {
  205. // m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  206. }
  207. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  208. {
  209. // m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  210. }
  211. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  212. {
  213. // m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  214. }
  215. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  216. {
  217. #if 0
  218. do_leave_biz(card_id,speed,type);
  219. auto area = area_list::instance()->get(new_areaid);
  220. point pt;
  221. m_area_hover.reset(new area_hover(area,pt,speed));
  222. do_enter_biz(card_id,speed,type);
  223. #endif
  224. }
  225. };
  226. #endif