area.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. struct area_hover;
  10. struct point;
  11. struct area_business;
  12. struct card_location_base;
  13. /*
  14. 每个区域对应一个area对象。
  15. */
  16. struct area
  17. {
  18. area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t type)
  19. :m_id(id)
  20. ,m_area_type(type)
  21. ,m_limit_person_second(limit_time_person)
  22. ,m_limit_person_count(limit_count_person)
  23. ,m_scale(scale)
  24. ,m_mapid(mapid)
  25. ,m_person_count(0)
  26. ,m_vehicle_count(0)
  27. {
  28. }
  29. virtual void on_hover(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  30. virtual void on_enter(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  31. virtual void on_leave(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c);
  32. bool in_area(const point & p);
  33. int id()const
  34. {
  35. return m_id;
  36. }
  37. int mapid()const
  38. {
  39. return m_mapid;
  40. }
  41. double scale()const
  42. {
  43. return m_scale;
  44. }
  45. bool special()const
  46. {
  47. return m_area_type == AREA_TYPE_NO_COVER;
  48. }
  49. virtual ~area()
  50. {}
  51. ///区域卡数
  52. int card_count()const
  53. {
  54. return m_person_count + m_vehicle_count;
  55. }
  56. void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid,
  57. int32_t type,int limit_count_vehicle, int limit_time_vehicle)
  58. {
  59. m_area_type=type;
  60. m_limit_person_second=limit_time_person;
  61. m_limit_person_count=limit_count_person;
  62. m_scale=scale;
  63. m_mapid=mapid;
  64. m_limit_vehicle_count=limit_count_vehicle;
  65. m_limit_vehicle_second=limit_time_vehicle;
  66. }
  67. public:
  68. std::vector<std::shared_ptr<area_business>> m_area_business_list;
  69. public:
  70. std::vector<point> m_bound;
  71. //std::atomic<int> m_card_count;
  72. //数据库唯一ID
  73. int m_id;
  74. ///区域类型 AREA_TYPE
  75. //用户定义的业务类型,BIT集合
  76. /*
  77. 1:位置[优先级]
  78. 2:超时[超时时间分钟数]
  79. 3:超员[人员数量、车辆数量]
  80. 4:超速[超速值、判断策略N/M]
  81. 5:人员考勤
  82. 6:车辆考勤[离开最小,离开最小距离]
  83. 7:禁区[进入时长]
  84. 8:猴车区域
  85. */
  86. int m_area_type;
  87. //人卡超时及超员数量
  88. int m_limit_person_second;
  89. int m_limit_person_count;
  90. //人卡超时及超员数量
  91. int m_limit_vehicle_second;
  92. int m_limit_vehicle_count;
  93. double m_scale;
  94. int32_t m_mapid;
  95. ///区域人卡数
  96. std::atomic<int> m_person_count;
  97. ///区域车卡数
  98. std::atomic<int> m_vehicle_count;
  99. };
  100. struct area_list:single_base<area_list,int,std::shared_ptr<area>>
  101. {
  102. area_list();
  103. std::shared_ptr<area> get_area(const point&pt);
  104. std::vector<point> init_path(std::string &str);
  105. //void init_from_db();
  106. //void init_monkeycar_area();
  107. ///id=-1为初始化所有
  108. void init_from_db(int id=-1);
  109. ///id=-1为初始化所有
  110. void init_monkeycar_area(int id=-1);
  111. };
  112. struct area_hover
  113. {
  114. std::shared_ptr<area> m_area;
  115. time_t m_enter_time,m_last_time;
  116. point m_enter_point,m_last_point;
  117. int landmark_id;
  118. int landmark_dir;
  119. double landmark_dis;
  120. /*
  121. 记录该业务所关心的需持续使用的数据,每个业务一个指针
  122. 建议该数据项在on_enter时初始化,on_leave时清除
  123. */
  124. std::vector<void*> m_hover_data;
  125. area_hover()=default;
  126. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  127. :m_area(area)
  128. {
  129. m_enter_time=m_last_time=time(0);
  130. m_enter_point=m_last_point=pt;
  131. landmark_id=0;
  132. landmark_dir=0;
  133. landmark_dis=0;
  134. }
  135. int id()const
  136. {
  137. return m_area->id();
  138. }
  139. int mapid()const
  140. {
  141. return m_area->mapid();
  142. }
  143. double scale()const
  144. {
  145. return m_area->scale();
  146. }
  147. bool operator == (const area_hover&o)const
  148. {
  149. return m_area->id()==o.m_area->id();
  150. }
  151. bool operator < (const area_hover&o)const
  152. {
  153. return m_area->id()<o.m_area->id();
  154. }
  155. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  156. {
  157. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  158. }
  159. void setLandmark(const point &pt);
  160. void set(const point&pt)
  161. {
  162. m_last_time=time(0);
  163. m_last_point = pt;
  164. }
  165. };
  166. //每张卡包含一个对象
  167. //在解析出数据点时,调用on_point
  168. struct site;
  169. struct area_tool
  170. {
  171. std::shared_ptr<area_hover> m_area_hover=nullptr;
  172. void on_point(const std::shared_ptr<site>&s,std::shared_ptr<card_location_base> c,const point&pt);
  173. void setLandmark(const point &pt)
  174. {
  175. if(m_area_hover)
  176. {
  177. m_area_hover->setLandmark(pt);
  178. }
  179. }
  180. //special area or no area att.,return true;else return false.
  181. bool special_area()
  182. {
  183. if(m_area_hover==nullptr)
  184. return true;
  185. return m_area_hover->m_area->special();
  186. }
  187. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  188. {
  189. if(m_area_hover)
  190. return m_area_hover->getLandmark();
  191. else
  192. return std::make_tuple(0,0,0,0,0,0,0);
  193. }
  194. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  195. {
  196. // m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  197. }
  198. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  199. {
  200. // m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  201. }
  202. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  203. {
  204. // m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  205. }
  206. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  207. {
  208. #if 0
  209. do_leave_biz(card_id,speed,type);
  210. auto area = area_list::instance()->get(new_areaid);
  211. point pt;
  212. m_area_hover.reset(new area_hover(area,pt,speed));
  213. do_enter_biz(card_id,speed,type);
  214. #endif
  215. }
  216. };
  217. #endif