1
0

area.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <set>
  11. #include <map>
  12. struct area_hover;
  13. struct point;
  14. struct area_business;
  15. struct business_data;
  16. struct card_location_base;
  17. struct site;
  18. /*
  19. 每个区域对应一个area对象。
  20. */
  21. struct area
  22. {
  23. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type);
  24. virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  25. virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  26. virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  27. virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
  28. virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &);
  29. int id()const
  30. {
  31. return m_id;
  32. }
  33. int mapid()const
  34. {
  35. return m_mapid;
  36. }
  37. double scale()const
  38. {
  39. return m_scale;
  40. }
  41. bool is_mine()//是矿井区域
  42. {
  43. return 0 == m_id;
  44. }
  45. virtual ~area()
  46. {}
  47. double get_speed(int vehicle_category_id) {return m_speed[vehicle_category_id];}
  48. void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid,
  49. int32_t type,int limit_count_vehicle, int limit_time_vehicle)
  50. {
  51. m_area_type=type;
  52. m_limit_person_second=limit_time_person;
  53. m_limit_person_count=limit_count_person;
  54. m_scale=scale;
  55. m_mapid=mapid;
  56. m_limit_vehicle_count=limit_count_vehicle;
  57. m_limit_vehicle_second=limit_time_vehicle;
  58. }
  59. public:
  60. std::vector<area_business*> m_area_business_list;
  61. public:
  62. std::vector<point> m_bound;
  63. //数据库唯一ID
  64. int m_id;
  65. //用户定义的业务类型,BIT集合
  66. /*
  67. 1:位置[优先级]
  68. 2:超时[超时时间分钟数]
  69. 3:超员[人员数量、车辆数量]
  70. 4:超速[超速值、判断策略N/M]
  71. 5:人员考勤
  72. 6:车辆考勤[离开最小,离开最小距离]
  73. 7:禁区[进入时长]
  74. 8:猴车区域
  75. */
  76. int m_area_type;
  77. //人卡超时及超员数量(阀值)
  78. int m_limit_person_second;
  79. int m_limit_person_count;
  80. //是否人卡超员已有告警
  81. bool m_event_person_count;
  82. //人卡超时及超员数量(阀值)
  83. int m_limit_vehicle_second;
  84. int m_limit_vehicle_count;
  85. //是否人卡超员已有告警
  86. bool m_event_vehicle_count;
  87. double m_scale;
  88. int32_t m_mapid;
  89. ///区域人卡数
  90. std::atomic<int> m_person_count;
  91. ///区域车卡数
  92. std::atomic<int> m_vehicle_count;
  93. //区域速度门限
  94. std::map<int,double> m_speed;
  95. //是否是工作区域(0:不是、1:是)
  96. int m_is_work_area=0;
  97. };
  98. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  99. {
  100. area_list();
  101. //根据分站、所在点找出所在区域列表
  102. 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 &);
  103. std::vector<point> init_path(std::string &str,int area_id);
  104. ///id=-1为初始化所有
  105. void init_from_db(int id=-1);
  106. void init_monkeycar_area(int id=-1);
  107. private:
  108. 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);
  109. };
  110. struct area_hover
  111. {
  112. std::shared_ptr<area> m_area;
  113. uint64_t m_enter_time,m_last_time;
  114. point m_enter_point,m_last_point;
  115. /*
  116. 记录该业务所关心的需持续使用的数据,每个业务一个指针
  117. 建议该数据项在on_enter时初始化,on_leave时清除
  118. */
  119. std::vector<std::shared_ptr<business_data>> m_data;
  120. area_hover()=default;
  121. area_hover(const std::shared_ptr<area>&area,const point&pt);
  122. std::shared_ptr<business_data>&get_business_data(int type)
  123. {
  124. if(type>=(int)m_data.size())
  125. {
  126. m_data.resize(type+1);
  127. }
  128. return m_data[type];
  129. }
  130. int id()const
  131. {
  132. return m_area->id();
  133. }
  134. int mapid()const
  135. {
  136. return m_area->mapid();
  137. }
  138. double scale()const
  139. {
  140. return m_area->scale();
  141. }
  142. bool operator == (const area_hover&o)const
  143. {
  144. return m_area->id()==o.m_area->id();
  145. }
  146. bool operator < (const area_hover&o)const
  147. {
  148. return m_area->id()<o.m_area->id();
  149. }
  150. void set(const point&pt)
  151. {
  152. m_last_time=time(0);
  153. m_last_point = pt;
  154. }
  155. };
  156. //每张卡包含一个对象
  157. //在解析出数据点时,调用on_point
  158. struct site;
  159. struct area_tool
  160. {
  161. //卡所在的所有area的列表,以id排序小->大
  162. std::vector<std::shared_ptr<area_hover>> m_hover_list;
  163. //推送卡位置时需要推送的所在区域id列表
  164. std::map<int,std::tuple<int,int,int,double,uint64_t>> m_area_info;
  165. int m_mapid=-1;
  166. double m_scale=2.0;
  167. std::shared_ptr<site> m_site=nullptr;
  168. void set(const std::shared_ptr<site>& s)
  169. {
  170. if(m_site != s)
  171. m_site=s;
  172. }
  173. void on_point(const std::shared_ptr<card_location_base>& c,const point&pt);
  174. void on_leave(const std::shared_ptr<card_location_base>& c);
  175. void set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type);
  176. void set_area_info(int mapid,int areaid,const point &pt,uint64_t t);
  177. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  178. {
  179. #if 0
  180. do_leave_biz(card_id,speed,type);
  181. auto area = area_list::instance()->get(new_areaid);
  182. point pt;
  183. m_area_hover.reset(new area_hover(area,pt,speed));
  184. do_enter_biz(card_id,speed,type);
  185. #endif
  186. }
  187. };
  188. #endif