area.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef _AREA_HPP_
  2. #define _AREA_HPP_
  3. #include <atomic>
  4. #include <algorithm>
  5. #include <cfloat>
  6. #include <iterator>
  7. #include <point.h>
  8. #include "common.h"
  9. #include <write-copy.h>
  10. #include <ant.h>
  11. #include <set>
  12. #include <map>
  13. struct area_hover;
  14. struct point;
  15. struct area_business;
  16. struct business_data;
  17. struct card_location_base;
  18. struct site;
  19. struct area_persons_thre_time;
  20. /*
  21. 每个区域对应一个area对象。
  22. */
  23. struct area
  24. {
  25. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type);
  26. virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  27. virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  28. virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  29. virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  30. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &);
  31. bool in_area(const point &p);
  32. int id()const
  33. {
  34. return m_id;
  35. }
  36. int mapid()const
  37. {
  38. return m_mapid;
  39. }
  40. double scale()const
  41. {
  42. return m_scale;
  43. }
  44. bool is_mine()//是矿井区域
  45. {
  46. return 0 == m_id;
  47. }
  48. //分站创建的区域 --- 分站创建的区域id = 0-分站id
  49. bool is_reader_area()
  50. {
  51. return m_id < 0;
  52. }
  53. virtual ~area()
  54. {}
  55. double get_speed(int vehicle_category_id) {return m_speed[vehicle_category_id];}
  56. void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid, int limit_count_vehicle, int limit_time_vehicle);
  57. public:
  58. void set_business_list(std::vector<area_business*>&&business_list)
  59. {
  60. m_area_business_list=business_list;
  61. }
  62. int get_frozen_count()
  63. {
  64. return m_frozen_count.load(std::memory_order_acquire);
  65. }
  66. int add_frozen_count(int deta=1)
  67. {
  68. return m_frozen_count.fetch_add(deta,std::memory_order_release);
  69. }
  70. int sub_frozen_count(int deta=1)
  71. {
  72. return add_frozen_count(-deta);
  73. }
  74. void change_business(uint32_t new_bits);
  75. void clear();
  76. public:
  77. //数据库唯一ID
  78. int m_id;
  79. //用户定义的业务类型,BIT集合
  80. /*
  81. 1:位置[优先级]
  82. 2:超时[超时时间分钟数]
  83. 3:超员[人员数量、车辆数量]
  84. 4:超速[超速值、判断策略N/M]
  85. 5:人员考勤
  86. 6:车辆考勤[离开最小,离开最小距离]
  87. 7:禁区[进入时长]
  88. 8:猴车区域
  89. */
  90. int m_biz_type=0;
  91. int m_area_type=0;
  92. // 人卡超时及超员数量(阀值)
  93. int m_limit_person_min;
  94. // 区域表中人员超员的(阀值)
  95. int m_default_limit_person_count;
  96. // 当前人员超员的(阀值)
  97. std::atomic<int> m_limit_person_count;
  98. //是否人卡超员已有告警
  99. bool m_event_person_count;
  100. bool m_event_person_show_count;
  101. //是否人卡超员已有告警
  102. bool m_event_vehicle_count;
  103. bool m_event_vehicle_show_count;
  104. //人卡超时及超员数量(阀值)
  105. int m_limit_vehicle_min;
  106. int m_limit_vehicle_count;
  107. int32_t m_mapid;
  108. double m_scale;
  109. ///区域人卡数
  110. std::atomic<int> m_person_count;
  111. ///区域车卡数
  112. std::atomic<int> m_vehicle_count;
  113. ///区域显示人卡数
  114. std::atomic<int> m_person_show_count;
  115. ///区域显示车卡数
  116. std::atomic<int> m_vehicle_show_count;
  117. std::atomic<int> m_frozen_count;
  118. //是否是工作区域(0:不是、1:是)
  119. int m_is_work_area=0;
  120. //区域速度门限
  121. std::map<int,double> m_speed;
  122. std::vector<point> m_bound;
  123. std::vector<area_business*> m_area_business_list;
  124. //区域时间段超员限制
  125. std::shared_ptr<area_persons_thre_time> m_persons_thre_time;
  126. // 判断区域当前超员的阈值,是否变化 true:有变化
  127. bool get_limit_person_count();
  128. };
  129. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  130. {
  131. area_list();
  132. //根据分站、所在点找出所在区域列表
  133. std::vector<std::shared_ptr<area>> get_area(const std::shared_ptr<site>& s,const std::shared_ptr<card_location_base> &c,const point&pt,int &);
  134. std::vector<point> init_path(std::string &str,int area_id);
  135. ///id=-1为初始化所有
  136. void init_from_db(int id=-1);
  137. void init_monkeycar_area(int id=-1);
  138. // 区域时间段超员设置
  139. void init_area_persons_dynamic_thre_from_db(int area_id = -1);
  140. private:
  141. std::shared_ptr<area> create(int type,int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type);
  142. };
  143. struct area_hover
  144. {
  145. std::shared_ptr<area> m_area;
  146. uint64_t m_enter_time,m_last_time;
  147. point m_enter_point,m_last_point;
  148. /*
  149. 记录该业务所关心的需持续使用的数据,每个业务一个指针
  150. 建议该数据项在on_enter时初始化,on_leave时清除
  151. */
  152. std::vector<std::shared_ptr<business_data>> m_data;
  153. area_hover()=default;
  154. area_hover(const std::shared_ptr<area>&area,const point&pt);
  155. std::shared_ptr<business_data>&get_business_data(int type)
  156. {
  157. if(type>=(int)m_data.size())
  158. {
  159. m_data.resize(type+1);
  160. }
  161. return m_data[type];
  162. }
  163. int id()const
  164. {
  165. return m_area->id();
  166. }
  167. int mapid()const
  168. {
  169. return m_area->mapid();
  170. }
  171. double scale()const
  172. {
  173. return m_area->scale();
  174. }
  175. bool operator == (const area_hover&o)const
  176. {
  177. return m_area->id()==o.m_area->id();
  178. }
  179. bool operator < (const area_hover&o)const
  180. {
  181. return m_area->id()<o.m_area->id();
  182. }
  183. void set(const point&pt)
  184. {
  185. m_last_time=time(0);
  186. m_last_point = pt;
  187. }
  188. };
  189. //每张卡包含一个对象
  190. //在解析出数据点时,调用on_point
  191. struct site;
  192. struct task;
  193. struct area_tool
  194. {
  195. private:
  196. int m_mapid=-1;
  197. public:
  198. int get_mapid()const
  199. {
  200. return m_mapid;
  201. }
  202. double m_scale=2.0;
  203. //卡所在的所有area的列表,以id排序小->大
  204. std::vector<std::shared_ptr<area_hover>> m_hover_list;
  205. //推送卡位置时需要推送的所在区域id列表
  206. std::map<int,std::tuple<int,int,int,double,uint64_t>> m_area_info;
  207. std::shared_ptr<site> m_site=nullptr;
  208. void clear()
  209. {
  210. m_area_info.clear();
  211. }
  212. const int get_site_id() const
  213. {
  214. int sid=-1;
  215. if(m_site)
  216. sid=m_site->m_id;
  217. return sid;
  218. }
  219. void set_site(const std::shared_ptr<site>& s)
  220. {
  221. if(s == nullptr || m_site == s)
  222. return;
  223. m_site=s;
  224. m_mapid=m_site->m_map_id;
  225. }
  226. void on_change_business(const std::shared_ptr<card_location_base>& c, const task&t);
  227. void on_point(const std::shared_ptr<card_location_base>& c,const point&pt);
  228. void on_leave(const std::shared_ptr<card_location_base>& c);
  229. void set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type);
  230. void set_area_info(int mapid,int areaid,const point &pt,uint64_t t);
  231. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  232. {
  233. #if 0
  234. do_leave_biz(card_id,speed,type);
  235. auto area = area_list::instance()->get(new_areaid);
  236. point pt;
  237. m_area_hover.reset(new area_hover(area,pt,speed));
  238. do_enter_biz(card_id,speed,type);
  239. #endif
  240. }
  241. };
  242. #endif