123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #ifndef _AREA_HPP_
- #define _AREA_HPP_
- #include <atomic>
- #include <algorithm>
- #include <cfloat>
- #include <iterator>
- #include <point.h>
- #include "common.h"
- #include <write-copy.h>
- #include <ant.h>
- #include <set>
- #include <map>
- struct area_hover;
- struct point;
- struct area_business;
- struct business_data;
- struct card_location_base;
- struct site;
- struct area_persons_thre_time;
- /*
- 每个区域对应一个area对象。
- */
- struct area
- {
- area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type);
- virtual void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
- virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
- virtual void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
- virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c);
- virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &);
- bool in_area(const point &p);
- int id()const
- {
- return m_id;
- }
- int mapid()const
- {
- return m_mapid;
- }
- double scale()const
- {
- return m_scale;
- }
- bool is_mine()//是矿井区域
- {
- return AREA_TYPE_DOWNMINE==m_area_type;
- //return 0 == m_id;
- }
- //分站创建的区域 --- 分站创建的区域id = 0-分站id
- bool is_reader_area()
- {
- return m_id < 0;
- }
- virtual ~area()
- {}
- double get_speed(int vehicle_category_id) {return m_speed[vehicle_category_id];}
- void update(int limit_count_person, int limit_time_person,double scale,int32_t mapid, int limit_count_vehicle, int limit_time_vehicle);
- public:
- void set_business_list(std::vector<area_business*>&&business_list)
- {
- m_area_business_list=business_list;
- }
- int get_frozen_count()
- {
- return m_frozen_count.load(std::memory_order_acquire);
- }
- int add_frozen_count(int deta=1)
- {
- return m_frozen_count.fetch_add(deta,std::memory_order_release);
- }
- int sub_frozen_count(int deta=1)
- {
- return add_frozen_count(-deta);
- }
- void change_business(uint32_t new_bits);
- void clear();
- public:
- //数据库唯一ID
- int m_id;
- //用户定义的业务类型,BIT集合
- /*
- 1:位置[优先级]
- 2:超时[超时时间分钟数]
- 3:超员[人员数量、车辆数量]
- 4:超速[超速值、判断策略N/M]
- 5:人员考勤
- 6:车辆考勤[离开最小,离开最小距离]
- 7:禁区[进入时长]
- 8:猴车区域
- */
- int m_biz_type=0;
- int m_area_type=0;
- // 人卡超时及超员数量(阀值)
- int m_limit_person_min;
- // 区域表中人员超员的(阀值)
- int m_default_limit_person_count;
- // 当前人员超员的(阀值)
- std::atomic<int> m_limit_person_count;
- //是否人卡超员已有告警
- bool m_event_person_count;
- bool m_event_person_show_count;
- //是否人卡超员已有告警
- bool m_event_vehicle_count;
- bool m_event_vehicle_show_count;
- //人卡超时及超员数量(阀值)
- int m_limit_vehicle_min;
- int m_limit_vehicle_count;
- int32_t m_mapid;
- double m_scale;
- ///区域人卡数
- std::atomic<int> m_person_count;
- ///区域车卡数
- std::atomic<int> m_vehicle_count;
- ///区域显示人卡数
- std::atomic<int> m_person_show_count;
- ///区域显示车卡数
- std::atomic<int> m_vehicle_show_count;
- std::atomic<int> m_frozen_count;
- //是否是工作区域(0:不是、1:是)
- int m_is_work_area=0;
- //区域速度门限
- std::map<int,double> m_speed;
- std::vector<point> m_bound;
- std::vector<area_business*> m_area_business_list;
- //区域时间段超员限制
- std::shared_ptr<area_persons_thre_time> m_persons_thre_time;
- // 判断区域当前超员的阈值,是否变化 true:有变化
- bool get_limit_person_count();
- };
- struct area_list:single_base<area_list,int,std::shared_ptr<area>>
- {
- area_list();
- //根据分站、所在点找出所在区域列表
- std::vector<std::shared_ptr<area>> get_area(const std::shared_ptr<site>& s,const std::shared_ptr<card_location_base> &c,const point&pt,int &);
- std::vector<point> init_path(std::string &str,int area_id);
- ///id=-1为初始化所有
- void init_from_db(int id=-1);
- void init_monkeycar_area(int id=-1);
- // 区域时间段超员设置
- void init_area_persons_dynamic_thre_from_db(int area_id = -1);
- private:
- std::shared_ptr<area> create(int type,int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type);
- };
- struct area_hover
- {
- std::shared_ptr<area> m_area;
- uint64_t m_enter_time,m_last_time;
- point m_enter_point,m_last_point;
- /*
- 记录该业务所关心的需持续使用的数据,每个业务一个指针
- 建议该数据项在on_enter时初始化,on_leave时清除
- */
- std::vector<std::shared_ptr<business_data>> m_data;
- area_hover()=default;
- area_hover(const std::shared_ptr<area>&area,const point&pt);
- std::shared_ptr<business_data>&get_business_data(int type)
- {
- if(type>=(int)m_data.size())
- {
- m_data.resize(type+1);
- }
- return m_data[type];
- }
- int id()const
- {
- return m_area->id();
- }
- int mapid()const
- {
- return m_area->mapid();
- }
- double scale()const
- {
- return m_area->scale();
- }
- bool operator == (const area_hover&o)const
- {
- return m_area->id()==o.m_area->id();
- }
- bool operator < (const area_hover&o)const
- {
- return m_area->id()<o.m_area->id();
- }
- void set(const point&pt)
- {
- m_last_time=time(0);
- m_last_point = pt;
- }
- };
- //每张卡包含一个对象
- //在解析出数据点时,调用on_point
- struct site;
- struct task;
- struct area_tool
- {
- private:
- int m_mapid=-1;
- public:
- int get_mapid()const
- {
- return m_mapid;
- }
- double m_scale=2.0;
- //卡所在的所有area的列表,以id排序小->大
- std::vector<std::shared_ptr<area_hover>> m_hover_list;
- //推送卡位置时需要推送的所在区域id列表
- std::map<int,std::tuple<int,int,int,double,uint64_t>> m_area_info;
- std::shared_ptr<site> m_site=nullptr;
- void clear()
- {
- m_area_info.clear();
- }
- const int get_site_id() const
- {
- int sid=-1;
- if(m_site)
- sid=m_site->m_id;
- return sid;
- }
- void set_site(const std::shared_ptr<site>& s)
- {
- if(s == nullptr || m_site == s)
- return;
- m_site = s;
- m_mapid = m_site->m_map_id;
- m_scale = m_site->m_scale;
- }
- void on_change_business(const std::shared_ptr<card_location_base>& c, const task&t);
- void on_point(const std::shared_ptr<card_location_base>& c,const point&pt);
- void on_leave(const std::shared_ptr<card_location_base>& c);
- void set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type);
- void set_area_info(int mapid,int areaid,const point &pt,uint64_t t);
- void change_area(uint32_t card_id,double speed,int16_t type,int32_t new_areaid)
- {
- #if 0
- do_leave_biz(card_id,speed,type);
- auto area = area_list::instance()->get(new_areaid);
- point pt;
- m_area_hover.reset(new area_hover(area,pt,speed));
- do_enter_biz(card_id,speed,type);
- #endif
- }
- };
- #endif
|