area.h 7.2 KB

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