area.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. bool m_event_person_show_count;
  83. //人卡超时及超员数量(阀值)
  84. int m_limit_vehicle_second;
  85. int m_limit_vehicle_count;
  86. //是否人卡超员已有告警
  87. bool m_event_vehicle_count;
  88. bool m_event_vehicle_show_count;
  89. double m_scale;
  90. int32_t m_mapid;
  91. ///区域人卡数
  92. std::atomic<int> m_person_count;
  93. ///区域车卡数
  94. std::atomic<int> m_vehicle_count;
  95. ///区域显示人卡数
  96. std::atomic<int> m_person_show_count;
  97. ///区域显示车卡数
  98. std::atomic<int> m_vehicle_show_count;
  99. //区域速度门限
  100. std::map<int,double> m_speed;
  101. //是否是工作区域(0:不是、1:是)
  102. int m_is_work_area=0;
  103. };
  104. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  105. {
  106. area_list();
  107. //根据分站、所在点找出所在区域列表
  108. 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 &);
  109. std::vector<point> init_path(std::string &str,int area_id);
  110. ///id=-1为初始化所有
  111. void init_from_db(int id=-1);
  112. void init_monkeycar_area(int id=-1);
  113. private:
  114. 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);
  115. };
  116. struct area_hover
  117. {
  118. std::shared_ptr<area> m_area;
  119. uint64_t m_enter_time,m_last_time;
  120. point m_enter_point,m_last_point;
  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(const std::shared_ptr<area>&area,const point&pt);
  128. std::shared_ptr<business_data>&get_business_data(int type)
  129. {
  130. if(type>=(int)m_data.size())
  131. {
  132. m_data.resize(type+1);
  133. }
  134. return m_data[type];
  135. }
  136. int id()const
  137. {
  138. return m_area->id();
  139. }
  140. int mapid()const
  141. {
  142. return m_area->mapid();
  143. }
  144. double scale()const
  145. {
  146. return m_area->scale();
  147. }
  148. bool operator == (const area_hover&o)const
  149. {
  150. return m_area->id()==o.m_area->id();
  151. }
  152. bool operator < (const area_hover&o)const
  153. {
  154. return m_area->id()<o.m_area->id();
  155. }
  156. void set(const point&pt)
  157. {
  158. m_last_time=time(0);
  159. m_last_point = pt;
  160. }
  161. };
  162. //每张卡包含一个对象
  163. //在解析出数据点时,调用on_point
  164. struct site;
  165. struct area_tool
  166. {
  167. //卡所在的所有area的列表,以id排序小->大
  168. std::vector<std::shared_ptr<area_hover>> m_hover_list;
  169. //推送卡位置时需要推送的所在区域id列表
  170. std::map<int,std::tuple<int,int,int,double,uint64_t>> m_area_info;
  171. int m_mapid=-1;
  172. double m_scale=2.0;
  173. std::shared_ptr<site> m_site=nullptr;
  174. void set(const std::shared_ptr<site>& s)
  175. {
  176. if(m_site != s)
  177. m_site=s;
  178. }
  179. void on_point(const std::shared_ptr<card_location_base>& c,const point&pt);
  180. void on_leave(const std::shared_ptr<card_location_base>& c);
  181. void set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type);
  182. void set_area_info(int mapid,int areaid,const point &pt,uint64_t t);
  183. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  184. {
  185. #if 0
  186. do_leave_biz(card_id,speed,type);
  187. auto area = area_list::instance()->get(new_areaid);
  188. point pt;
  189. m_area_hover.reset(new area_hover(area,pt,speed));
  190. do_enter_biz(card_id,speed,type);
  191. #endif
  192. }
  193. };
  194. #endif