area.h 6.3 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_person_second(limit_time_person)
  31. ,m_limit_person_count(limit_count_person)
  32. ,m_scale(scale)
  33. ,m_mapid(mapid)
  34. ,m_person_count(0)
  35. ,m_vehicle_count(0)
  36. {
  37. }
  38. virtual void on_hover(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  39. virtual void on_enter(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  40. virtual void on_leave(uint32_t card_id,std::shared_ptr<area_hover>&c,double speed,int32_t type);
  41. bool in_area(const point & p);
  42. int id()const
  43. {
  44. return m_id;
  45. }
  46. int mapid()const
  47. {
  48. return m_mapid;
  49. }
  50. double scale()const
  51. {
  52. return m_scale;
  53. }
  54. bool special()const
  55. {
  56. return m_area_type == AREA_TYPE_NO_COVER;
  57. }
  58. virtual ~area()
  59. {}
  60. ///区域卡数
  61. int card_count()const
  62. {
  63. return m_person_count + m_vehicle_count;
  64. }
  65. std::vector<point> m_bound;
  66. public:
  67. //std::atomic<int> m_card_count;
  68. int m_id;
  69. ///区域类型 AREA_TYPE
  70. int m_area_type;
  71. int m_limit_person_second;
  72. int m_limit_person_count;
  73. int m_limit_vehicle_second;
  74. int m_limit_vehicle_count;
  75. double m_scale;
  76. int32_t m_mapid;
  77. ///区域人卡数
  78. std::atomic<int> m_person_count;
  79. ///区域车卡数
  80. std::atomic<int> m_vehicle_count;
  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 landmark_id;
  96. int landmark_dir;
  97. double landmark_dis;
  98. area_hover()=default;
  99. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  100. :m_area(area)
  101. {
  102. m_enter_time=m_last_time=time(0);
  103. m_enter_point=m_last_point=pt;
  104. landmark_id=0;
  105. landmark_dir=0;
  106. landmark_dis=0;
  107. }
  108. int id()const
  109. {
  110. return m_area->id();
  111. }
  112. int mapid()const
  113. {
  114. return m_area->mapid();
  115. }
  116. double scale()const
  117. {
  118. return m_area->scale();
  119. }
  120. bool operator == (const area_hover&o)const
  121. {
  122. return m_area->id()==o.m_area->id();
  123. }
  124. bool operator < (const area_hover&o)const
  125. {
  126. return m_area->id()<o.m_area->id();
  127. }
  128. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  129. {
  130. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  131. }
  132. void setLandmark(const point &pt);
  133. void set(const point&pt)
  134. {
  135. m_last_time=time(0);
  136. m_last_point = pt;
  137. }
  138. };
  139. //每张卡包含一个对象
  140. //在解析出数据点时,调用on_point
  141. struct area_tool
  142. {
  143. std::shared_ptr<area_hover> m_area_hover=nullptr;
  144. void on_point(uint32_t card_id,const point&pt,double speed,int16_t type);
  145. void setLandmark(const point &pt)
  146. {
  147. if(m_area_hover)
  148. {
  149. m_area_hover->setLandmark(pt);
  150. }
  151. }
  152. //special area or no area att.,return true;else return false.
  153. bool special_area()
  154. {
  155. if(m_area_hover==nullptr)
  156. return true;
  157. return m_area_hover->m_area->special();
  158. }
  159. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  160. {
  161. if(m_area_hover)
  162. return m_area_hover->getLandmark();
  163. else
  164. return std::make_tuple(0,0,0,0,0,0,0);
  165. }
  166. //检测是否超时
  167. void on_timer(int64_t card_id)
  168. {
  169. }
  170. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  171. {
  172. m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  173. }
  174. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  175. {
  176. m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  177. }
  178. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  179. {
  180. m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  181. }
  182. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  183. {
  184. do_leave_biz(card_id,speed,type);
  185. auto area = area_list::instance()->get(new_areaid);
  186. point pt;
  187. m_area_hover.reset(new area_hover(area,pt,speed));
  188. do_enter_biz(card_id,speed,type);
  189. }
  190. };
  191. #endif