area.h 8.3 KB

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