area.h 5.7 KB

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