area.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. //void init_from_db();
  107. //void init_monkeycar_area();
  108. ///id=-1为初始化所有
  109. void init_from_db(int id=-1);
  110. ///id=-1为初始化所有
  111. void init_monkeycar_area(int id=-1);
  112. private:
  113. //禁区功能-给禁区中的卡发送警告及呼叫
  114. void CheckAreaType(int area_id,int new_area_type,int old_area_type);
  115. void CheckAreaType( std::shared_ptr<area> pArea,int new_area_type,int old_area_type);
  116. };
  117. struct area_hover
  118. {
  119. std::shared_ptr<area> m_area;
  120. time_t m_enter_time,m_last_time;
  121. point m_enter_point,m_last_point;
  122. int landmark_id;
  123. int landmark_dir;
  124. double landmark_dis;
  125. /*
  126. 记录该业务所关心的需持续使用的数据,每个业务一个指针
  127. 建议该数据项在on_enter时初始化,on_leave时清除
  128. */
  129. std::vector<std::shared_ptr<business_data>> m_data;
  130. area_hover()=default;
  131. area_hover(std::shared_ptr<area>&area,const point&pt,double speed)
  132. :m_area(area)
  133. {
  134. m_enter_time=m_last_time=time(0);
  135. m_enter_point=m_last_point=pt;
  136. landmark_id=0;
  137. landmark_dir=0;
  138. landmark_dis=0;
  139. }
  140. std::shared_ptr<business_data> get_business_data(int type)
  141. {
  142. if(type>=(int)m_data.size())
  143. {
  144. m_data.resize(type+1);
  145. }
  146. return m_data[type];
  147. }
  148. int id()const
  149. {
  150. return m_area->id();
  151. }
  152. int mapid()const
  153. {
  154. return m_area->mapid();
  155. }
  156. double scale()const
  157. {
  158. return m_area->scale();
  159. }
  160. bool operator == (const area_hover&o)const
  161. {
  162. return m_area->id()==o.m_area->id();
  163. }
  164. bool operator < (const area_hover&o)const
  165. {
  166. return m_area->id()<o.m_area->id();
  167. }
  168. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  169. {
  170. return std::make_tuple(m_enter_time,m_last_time,mapid(),id(),landmark_id,landmark_dir,landmark_dis);
  171. }
  172. void setLandmark(const point &pt);
  173. void set(const point&pt)
  174. {
  175. m_last_time=time(0);
  176. m_last_point = pt;
  177. }
  178. };
  179. //每张卡包含一个对象
  180. //在解析出数据点时,调用on_point
  181. struct site;
  182. struct area_tool
  183. {
  184. std::shared_ptr<area_hover> m_area_hover=nullptr;
  185. void on_point(const std::shared_ptr<site>&s,std::shared_ptr<card_location_base> c,const point&pt);
  186. void setLandmark(const point &pt)
  187. {
  188. if(m_area_hover)
  189. {
  190. m_area_hover->setLandmark(pt);
  191. }
  192. }
  193. //special area or no area att.,return true;else return false.
  194. bool special_area()
  195. {
  196. if(m_area_hover==nullptr)
  197. return true;
  198. return m_area_hover->m_area->special();
  199. }
  200. std::tuple<time_t,time_t,int,int,int,int,double> getLandmark()
  201. {
  202. if(m_area_hover)
  203. return m_area_hover->getLandmark();
  204. else
  205. return std::make_tuple(0,0,0,0,0,0,0);
  206. }
  207. void do_hover_biz(uint32_t card_id,double speed,int16_t type)
  208. {
  209. // m_area_hover->m_area->on_hover(card_id,m_area_hover,speed,type);
  210. }
  211. void do_enter_biz(uint32_t card_id,double speed,int16_t type)
  212. {
  213. // m_area_hover->m_area->on_enter(card_id,m_area_hover,speed,type);
  214. }
  215. void do_leave_biz(uint32_t card_id,double speed,int16_t type)
  216. {
  217. // m_area_hover->m_area->on_leave(card_id,m_area_hover,speed,type);
  218. }
  219. void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
  220. {
  221. #if 0
  222. do_leave_biz(card_id,speed,type);
  223. auto area = area_list::instance()->get(new_areaid);
  224. point pt;
  225. m_area_hover.reset(new area_hover(area,pt,speed));
  226. do_enter_biz(card_id,speed,type);
  227. #endif
  228. }
  229. };
  230. #endif