area.h 6.3 KB

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