area.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef _AREA_HPP_
  2. #define _AREA_HPP_
  3. #include <atomic>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <point.h>
  7. #include <write-copy.h>
  8. //下午查看代码,整理了一下思路,如下
  9. //普通区域和考勤区域就不分开了,使用同一个具现类,至于里面的操作,是否区域告警之类的可以通过数据库配置进行控制,比如超员可以查看超员配置是否为0,0则不会进行告警
  10. //其他区域 井上和井下 考勤区域则不在区域模块进行考虑
  11. //猴车区域拥有一般区域的行为,所以打算通过继承方式
  12. //区域超速win版本有实现,没有应用,统一是井下超速,这里待讨论
  13. //区域超员win版本是通过一个线程循环来进行判断的。这里我觉着可以不采用
  14. //区域超时win版本的确是当有点过来的时候才会进行判断。这里需讨论是否满足需求,即丢失信号,是否进行告警判断
  15. //告警对象:分井下 卡 分站 区域 ,不同的告警对象下分不同的告警类型,告警类型分人和车(win版本设计)。
  16. //考虑到业务需要,以及重叠区域,之前(志军哥)的代码设计可能不使用。至于以后是否考虑重叠区域,后续可以有需求再处理。
  17. //代码中有对通过坐标点找不到区域的逻辑,使用的是分站注册时候的区域id,这块是否沿用之前思路。
  18. //益俊那边的json组装需尽快提供
  19. //月腾那边的区域代码逻辑尽快完善,这边可能需要在你代码实现的基础上进行操作,框架可以先给我
  20. //区域超员,超时,人车阈值win版本不同,这里需讨论
  21. //区域进出,插入的数据库表,人车分离。
  22. struct area_hover;
  23. struct point;
  24. /**
  25. * @brief 区域类型
  26. */
  27. enum AREA_TYPE
  28. {
  29. ///非考勤区域
  30. AT_NOT_ATTENDANCE,
  31. ///与考勤,卡数统计,超员,超时无关的区域
  32. AT_OTHER,
  33. };
  34. struct area
  35. {
  36. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid)
  37. :m_id(id)
  38. ,m_limit_time_second(limit_time_person)
  39. ,m_limit_person_count(limit_count_person)
  40. ,m_scale(scale)
  41. ,m_mapid(mapid)
  42. {
  43. }
  44. virtual void on_hover(int64_t card_id,std::shared_ptr<area_hover>&c,double speed,uint64_t type);
  45. virtual void on_enter(int64_t card_id,std::shared_ptr<area_hover>&c,double speed,uint64_t type);
  46. virtual void on_leave(int64_t card_id,std::shared_ptr<area_hover>&c,double speed,uint64_t type);
  47. bool in_area(const point & p);
  48. int id()const
  49. {
  50. return m_id;
  51. }
  52. int mapid()const
  53. {
  54. return m_mapid;
  55. }
  56. double scale()const
  57. {
  58. return m_scale;
  59. }
  60. virtual ~area()
  61. {}
  62. std::vector<point> m_bound;
  63. private:
  64. std::atomic<int> m_card_count;
  65. int m_id;
  66. ///区域类型 AREA_TYPE
  67. int m_area_type;
  68. ///区域人卡数
  69. std::atomic<int> m_person_count;
  70. ///区域车卡数
  71. std::atomic<int> m_vehicle_count;
  72. std::atomic<bool> m_is_over_person;
  73. ///区域超时门限
  74. int m_limit_time_second;
  75. ///人数门限
  76. int m_limit_person_count;
  77. ///速度门限
  78. double m_limit_speed;
  79. double m_scale;
  80. int32_t m_mapid;
  81. };
  82. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  83. {
  84. area_list();
  85. std::shared_ptr<area> get_area(const point&pt);
  86. std::vector<point> init_path(std::string &str);
  87. void init_from_db();
  88. void init_monkeycar_area();
  89. };
  90. struct area_hover
  91. {
  92. std::shared_ptr<area> m_area;
  93. time_t m_enter_time,m_last_time;
  94. point m_enter_point,m_last_point;
  95. int m_num_speeding;
  96. bool m_is_over_time;
  97. int landmark_id;
  98. int landmark_dir;
  99. double landmark_dis;
  100. area_hover()=default;
  101. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  102. :m_area(area)
  103. {
  104. m_enter_time=m_last_time=time(0);
  105. m_enter_point=m_last_point=pt;
  106. m_num_speeding=0;
  107. landmark_id=0;
  108. landmark_dir=0;
  109. landmark_dis=0;
  110. }
  111. int id()const
  112. {
  113. return m_area->id();
  114. }
  115. int mapid()const
  116. {
  117. return m_area->mapid();
  118. }
  119. double scale()const
  120. {
  121. return m_area->scale();
  122. }
  123. bool operator == (const area_hover&o)const
  124. {
  125. return m_area->id()==o.m_area->id();
  126. }
  127. bool operator < (const area_hover&o)const
  128. {
  129. return m_area->id()<o.m_area->id();
  130. }
  131. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  132. {
  133. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  134. }
  135. void setLandmark(const point &pt);
  136. void set(const point&pt)
  137. {
  138. m_last_time=time(0);
  139. m_last_point = pt;
  140. }
  141. };
  142. //每张卡包含一个对象
  143. //在解析出数据点时,调用on_point
  144. struct area_tool
  145. {
  146. std::shared_ptr<area_hover> m_area_hover=nullptr;
  147. void on_point(int64_t card_id,const point&pt,double speed,int16_t type);
  148. void setLandmark(const point &pt)
  149. {
  150. if(m_area_hover)
  151. {
  152. m_area_hover->setLandmark(pt);
  153. }
  154. }
  155. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  156. {
  157. if(m_area_hover)
  158. return m_area_hover->getLandmark();
  159. else
  160. return std::make_tuple(0,0,0,0,0,0,0);
  161. }
  162. //检测是否超时
  163. void on_timer(int64_t card_id)
  164. {
  165. }
  166. void do_hover_biz(int64_t card_id,double speed,int16_t type)
  167. {
  168. m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  169. }
  170. void do_enter_biz(int64_t card_id,double speed,int16_t type)
  171. {
  172. m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  173. }
  174. void do_leave_biz(int64_t card_id,double speed,int16_t type)
  175. {
  176. m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  177. }
  178. };
  179. #endif