123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870 |
- #include <memory>
- #include <write-copy.h>
- #include "db_api/CDBSingletonDefine.h"
- #include "log.h"
- #include <area.h>
- #include "worker.h"
- #include "point.h"
- #include "tool_time.h"
- #include "common_tool.h"
- #include "db_tool.h"
- #include "monkey_car/monkeycar_area.h"
- #include "landmark.h"
- #include "area_business.h"
- #include "area_persons_thre_time.h"
- #include <boost/algorithm/string/split.hpp>
- #include <boost/algorithm/string/classification.hpp>
- #include "area_business.h"
- #include "card.h"
- #include "tool_time.h"
- #include "common_tool.h"
- #include "websocket/ws_common.h"
- #include "websocket/wsClientMgr.h"
- #include "ant.h"
- #include "event.h"
- #include "load_raw.h"
- struct underground_area:area
- {
- underground_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t biz_type)
- :area(id,limit_count_person,limit_time_person,scale,mapid,biz_type)
- {
- }
- virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
- {
- return s&&!s->is_up_site();
- }
- };
- struct special_area:area
- {
- special_area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t biz_type)
- :area(id,limit_count_person,limit_time_person,scale,mapid,biz_type)
- {}
- virtual bool in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int &sarid)
- {
- bool f=false;
- int id = c->get_area();
- if(id == m_id)
- {
- f=true;
- sarid=m_id;
- }
- return f;
- }
- //推入特殊区域的是否需要其他信息
- virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
- {
- area::on_enter(a, c);
- sys::_CARD_POS_ cp;
- cp.area_id = m_id;
- swsClientMgr.SendSpecialAreaProcess(cp);
- }
- };
- area::area(int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type)
- :m_id(id)
- ,m_biz_type(b_type)
- ,m_limit_person_min(limit_time_person)
- ,m_limit_person_count(limit_count_person)
- ,m_event_person_count(false)
- ,m_event_person_show_count(false)
- ,m_event_vehicle_count(false)
- ,m_event_vehicle_show_count(false)
- ,m_mapid(mapid)
- ,m_scale(scale)
- ,m_person_count(0)
- ,m_vehicle_count(0)
- ,m_person_show_count(0)
- ,m_vehicle_show_count(0)
- ,m_frozen_count(0)
- ,m_is_work_area(0)
- {
- m_default_limit_person_count = limit_count_person;
- m_area_business_list=area_business::get_instance_list(m_biz_type,id);
- m_persons_thre_time = std::make_shared<area_persons_thre_time>();
- m_persons_thre_time->set_default_person_count_limit(limit_count_person);
- }
- void area::update(int limit_count_person, int limit_time_person,double scale,int32_t mapid, int limit_count_vehicle, int limit_time_vehicle)
- {
- log_info("scale_test: o_scale:%.2f, n_scale:%.2f", m_scale, scale);
- m_limit_person_min=limit_time_person;
- m_default_limit_person_count=limit_count_person;
- m_persons_thre_time->set_default_person_count_limit(limit_count_person);
- m_scale=scale;
- m_mapid=mapid;
- m_limit_vehicle_count=limit_count_vehicle;
- m_limit_vehicle_min=limit_time_vehicle;
- }
- void area::clear()
- {
- EVENT_TYPE ev = is_mine()?EVENT_TYPE::ET_OVER_COUNT_PERSON : EVENT_TYPE::ET_AREA_OVER_COUNT_PERSON ;
- EVENT_TYPE ev_ = is_mine()?EVENT_TYPE::ET_OVER_COUNT_VEHICLE : EVENT_TYPE::ET_AREA_OVER_COUNT_VEHICLE ;
- if(m_event_person_count)
- event_tool::instance()->handle_event(OT_AREA,ev,m_id,m_limit_person_count,0,false,DT_NORMAL);
- if(m_event_person_show_count)
- event_tool::instance()->handle_event(OT_AREA,ev,m_id,m_limit_person_count,0,false,DT_SPECIAL);
- if(m_event_vehicle_count)
- event_tool::instance()->handle_event(OT_AREA,ev_,m_id,m_limit_vehicle_count,0,false,DT_NORMAL);
- if(m_event_vehicle_show_count)
- event_tool::instance()->handle_event(OT_AREA,ev_,m_id,m_limit_vehicle_count,0,false,DT_SPECIAL);
- }
- void area::change_business(uint32_t new_bits)
- {
- worker*w=worker::instance();
- uint32_t del=((m_biz_type^new_bits)|m_biz_type)^m_biz_type;
- uint32_t add=((m_biz_type^new_bits)|new_bits)^new_bits;
- log_info("change_area_business:area_id:%d,b_type:%d,area_type:%d [del:%d, add:%d]",m_id,new_bits,m_biz_type,del,add);
- if(del==0 && add==0)
- return;
- task*t =task::alloc<message_change_business>();
- t->m_cmd_code=0x10001; //区域业务类型修改的编码
- t->m_hash_id=0; //这个不用
- auto&mcb=t->body<message_change_business>();
- mcb.area_id=m_id;
- if(del!=0)
- {
- mcb.del_list=area_business::get_instance_list(del,m_id);
- }
- if(add!=0)
- {
- mcb.add_list=area_business::get_instance_list(add,m_id);
- }
- mcb.new_list=area_business::get_instance_list(new_bits,m_id);
- mcb.ref_count.store(w->num_thread());
- add_frozen_count(w->num_thread()+1);
- this->m_biz_type=new_bits;
- w->broadcast(t);
- }
- void area::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
- {
- a->m_last_time=tool_time::now_to_ms();
- a->m_last_point=*c;
- for(const auto &i:m_area_business_list)
- {
- auto &x=a->get_business_data(i->area_business_type());
- i->on_hover(a,c,x);
- }
- }
- /*
- * 处理所有区域业务逻辑
- * */
- void area::on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
- {
- log_info("on_enter..%d areaId:%d", c->m_id, m_id);
- // 更新进入区域的时间
- a->m_enter_time = tool_time::now_to_ms();
- a->m_enter_point = *c;
- for(const auto &i:m_area_business_list)
- {
- auto &x=a->get_business_data(i->area_business_type());
- i->on_enter(a,c,x);
- }
- }
- void area::on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
- {
- log_info("on_load_his..%d areaId:%d",c->m_id,m_id);
- //a->m_last_time=tool_time::now_to_ms();
- a->m_last_point=*c;
- for(const auto &i:m_area_business_list)
- {
- auto &x=a->get_business_data(i->area_business_type());
- i->on_load_his(a,c,x);
- }
- }
- void area::on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c)
- {
- log_info("on_leave..card=%d areaId=%d",c->m_id,m_id);
- a->m_last_time=tool_time::now_to_ms();
- a->m_last_point=*c;
- for(const auto &i:m_area_business_list)
- {
- auto &x=a->get_business_data(i->area_business_type());
- i->on_leave(a,c,x);
- }
- }
- bool area::in_area(const point &p)
- {
- if(m_bound.empty())
- return false;
- int counter = 0;
- double xinters;
- point p1,p2;
- p1 = m_bound[0];
- int size = m_bound.size();
- for (int i=1;i<= size;i++) {
- p2 = m_bound[i%size];
- if (p.y > std::min(p1.y,p2.y)) {
- if (p.y <= std::max(p1.y,p2.y)) {
- if (p.x <= std::max(p1.x,p2.x)) {
- if (p1.y != p2.y) {
- xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
- if (p1.x == p2.x || p.x <= xinters)
- counter++;
- }
- }
- }
- }
- p1 = p2;
- }
- return (counter % 2 == 0) ? false : true;
- }
- bool area::in_area(const std::shared_ptr<site>&s,const std::shared_ptr<card_location_base>&c, const point & p,int & sarid)
- {
- switch(m_area_type){
- case AREA_TYPE_MONKEY:
- case AREA_TYPE_ATTENDANCE:
- case AREA_TYPE_FORBIDDEN:
- return in_area(p);
- default:
- return false;
- }
- }
- //
- bool area::get_limit_person_count()
- {
- int count = m_persons_thre_time->get_limit_person_count();
- if (count != m_limit_person_count.load())
- {
- m_limit_person_count = count;
- return true;
- }
- return false;
- }
- /////////////////// area_list //////////////////////////////////////////////////
- area_list::area_list()
- {
- }
- void area_list::init_monkeycar_area(int id)
- {
- std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path, c.scale,\
- over_count_person, over_count_vehicle, over_time_person, over_time_vehicle,\
- over_speed_vehicle, is_attendance ,b.monkeycar_coordinate,b.monkeycar_speed,a.business_type \
- FROM dat_area a ,dat_monkeycar_base_info b ,dat_map c\
- where a.area_id = b.monkeycar_base_info_id and a.map_id = c.map_id";
- if(-1 == id)
- {
- sql.append(";");
- }
- else
- {
- sql.append(" AND a.area_id=");
- sql.append(std::to_string(id));
- sql.append(";");
- log_info("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- log_error("基础数据 monkeycar area 增加或修改失败,数据库中找不到: area_id=%d", id);
- return ;
- }
- std::unordered_map<int,std::shared_ptr<area>> map;
- while ( DBRes.GetNextRecod(Error) )
- {
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- unsigned int area_type_id = 0;
- DBRes.GetField( "area_type_id",area_type_id, Error );
- int over_count_person = 0;
- DBRes.GetField( "over_count_person",over_count_person, Error );
- int over_count_vehicle = 0;
- DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
- int over_time_person = 0;
- DBRes.GetField( "over_time_person",over_time_person, Error );
- int over_time_vehicle = 0;
- DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
- std::string path;
- DBRes.GetField( "path",path, Error );
- double monkeycar_speed = 0;
- DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
- std::string monkeycar_coor;
- DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
- double scale=0;
- DBRes.GetField( "scale",scale, Error );
- uint32_t b_type =0;
- DBRes.GetField( "business_type",b_type, Error );
- log_info("monkeycar area init_area : id:%d,path:%s",area_id, path.c_str());
- if(-1 == id)
- {
- std::shared_ptr<db_area> da = std::make_shared<db_area>();
- da->m_default_speed = monkeycar_speed;
- da->m_point = init_path(monkeycar_coor,area_id);
- std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
- ap->update(over_count_person, over_time_person,scale,map_id,over_count_vehicle,over_time_vehicle);
- ap->m_area_type=area_type_id;
- ap->m_bound=init_path(path,area_id);
- for(const auto &p : ap->m_bound)
- log_info("point:monkey:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
- for(const auto &p : da->m_point)
- log_info("point:monkey_coor:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
- map.insert({area_id,ap});
- }
- else
- {
- auto tmp_ptr = area_list::instance()->get(id);
- bool newobj=false;
- if(!tmp_ptr)
- {
- std::shared_ptr<db_area> da = std::make_shared<db_area>();
- da->m_default_speed = monkeycar_speed;
- da->m_point = init_path(monkeycar_coor,area_id);
- tmp_ptr=std::make_shared<monkey_area>(da,area_id,over_count_person, over_time_person,scale,map_id,b_type);
- newobj=true;
- }
- tmp_ptr->m_bound=init_path(path,area_id);
- tmp_ptr->update(over_count_person, over_time_person,scale,map_id, over_count_vehicle,over_time_vehicle);
- tmp_ptr->m_area_type=area_type_id;
- if(newobj)
- {
- area_list::instance()->add(id, tmp_ptr);
- }
- else
- {
- tmp_ptr->change_business(b_type);
- }
- log_info("基础数据 monkeycar area增加或修改区域成功:区域id:%d,over_count_person:%d over_time_person:%d,\
- scale:%.2f,map_id:%d,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
- id,over_count_person, over_time_person,scale,map_id,area_type_id,
- over_count_vehicle,over_time_vehicle);
- }
- }
- if(-1 == id)
- {
- log_info( "monkeycar area init_area. The record count=%d\n", nCount );
- area_list::instance()->add(map);
- }
- }
- std::shared_ptr<area> area_list::create(int type,int id,int limit_count_person, int limit_time_person,double scale,int32_t mapid,int32_t b_type)
- {
- if(type==AREA_TYPE_NO_COVER)
- {
- return std::make_shared<special_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
- }
- else if(type == AREA_TYPE_DOWNMINE)
- {
- return std::make_shared<underground_area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
- }
- else
- {
- return std::make_shared<area>(id,limit_count_person,limit_time_person,scale,mapid,b_type);
- }
- }
- #define PERSON_ATT_MASK 0X00000010
- #define CAR_ATT_MASK 0X00000020
- void area_list::init_from_db(int id/*=-1*/)
- {
- do{
- std::string sql = "SELECT a.area_id, a.name, a.map_id, a.area_type_id, a.path,b.scale, \
- over_count_person, over_count_vehicle, over_time_person, over_time_vehicle, \
- over_speed_vehicle, is_attendance,business_type, a.is_work_area\
- FROM dat_area a,dat_map b\
- where a.map_id = b.map_id and area_id not in \
- (select monkeycar_base_info_id from dat_monkeycar_base_info)";
- if(-1 == id)
- {
- sql.append(";");
- }
- else
- {
- sql.append(" AND a.area_id=");
- sql.append(std::to_string(id));
- sql.append(";");
- log_info("基础数据 增加或修改区域 sql=%s", sql.c_str());
- }
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- log_error("基础数据 增加或修改失败,数据库中找不到: area_id=%d:%s", id,sql.c_str());
- break;
- }
- int downmine_index,patt_index,catt_index;
- downmine_index = patt_index = catt_index = 0;
- std::unordered_map<int,std::shared_ptr<area>> map;
- while ( DBRes.GetNextRecod(Error) )
- {
- int area_id = 0;
- DBRes.GetField( "area_id",area_id, Error );
- std::string area_name = "";
- DBRes.GetField( "name",area_name, Error );
- int map_id = 0;
- DBRes.GetField( "map_id",map_id, Error );
- unsigned int area_type_id = 0;
- DBRes.GetField( "area_type_id",area_type_id, Error );
- int over_count_person = 0;
- DBRes.GetField( "over_count_person",over_count_person, Error );
- int over_count_vehicle = 0;
- DBRes.GetField( "over_count_vehicle",over_count_vehicle, Error );
- int over_time_person = 0;
- DBRes.GetField( "over_time_person",over_time_person, Error );
- int over_time_vehicle = 0;
- DBRes.GetField( "over_time_vehicle",over_time_vehicle, Error );
- std::string over_speed_vehicle;
- DBRes.GetField( "over_speed_vehicle",over_speed_vehicle, Error );
- std::string path;
- DBRes.GetField( "path",path, Error );
- double scale = 0;
- DBRes.GetField( "scale",scale, Error );
- uint32_t b_type =0;
- DBRes.GetField( "business_type",b_type, Error );
- int is_work_area = 0;
- DBRes.GetField( "is_work_area",is_work_area, Error );
- log_info("init_area : id:%d,path:%s..speed:%s",area_id, path.c_str(),over_speed_vehicle.c_str());
- std::map<int,double> map_;
- auto vp=init_path(over_speed_vehicle,area_id);
- for(const auto &v:vp)
- map_.insert({v.x,v.y});
- if(-1 == id)
- {
- std::shared_ptr<area> ap = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
- ap->m_limit_vehicle_min = over_time_vehicle;
- ap->m_limit_vehicle_count = over_count_vehicle;
- ap->m_speed=std::move(map_);
- ap->m_is_work_area = is_work_area;
- ap->m_area_type=area_type_id;
- if(area_type_id == AREA_TYPE_DOWNMINE)
- downmine_index++;
- if(b_type&PERSON_ATT_MASK)
- patt_index++;
- if(b_type&CAR_ATT_MASK)
- catt_index++;
- ap->m_bound = init_path(path,area_id);
- for(const auto &p : ap->m_bound)
- log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
- map.insert({area_id,ap});
- }
- else
- {
- auto tmp_ptr = area_list::instance()->get(id);
- bool newobj=false;
- if(!tmp_ptr)
- {
- tmp_ptr = create(area_type_id,area_id,over_count_person,over_time_person, scale,map_id,b_type);
- newobj=true;
- }
- tmp_ptr->update(over_count_person, over_time_person,scale,map_id, over_count_vehicle,over_time_vehicle);
- tmp_ptr->m_speed=std::move(map_);
- tmp_ptr->m_bound=init_path(path,area_id);
- tmp_ptr->m_is_work_area = is_work_area;
- tmp_ptr->m_area_type=area_type_id;
- for(const auto &p : tmp_ptr->m_bound)
- log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
- log_info("基础数据 增加或修改区域成功:区域id:%d-%s,over_count_person:%d over_time_person:%d,scale:%.2f,map_id:%d\
- ,area_type_id:%d,over_count_vehicle:%d,over_time_vehicle:%d",
- id,area_name.c_str(),over_count_person, over_time_person,scale,map_id,area_type_id,
- over_count_vehicle,over_time_vehicle);
- if(newobj)
- {
- area_list::instance()->add(id, tmp_ptr);
- }
- else
- {
- tmp_ptr->change_business(b_type);
- }
- }
- }
- if(-1 == id)
- {
- assert(downmine_index>0);
- assert(patt_index==1);
- assert(catt_index>0);
- log_info( "init_area. The record count=%d\n", nCount );
- area_list::instance()->add(map);
- }
- }while(0);
- init_monkeycar_area(id);
- }
- std::vector<point> area_list::init_path(std::string &str_0,int area_id)
- {
- const std::string str=str_0;
- if(str.empty())
- {
- log_error("area path is empty,area_id=%d.",area_id);
- return std::vector<point>();
- }
- std::vector<point> vp;
- std::vector<std::string> vs;
- std::vector<std::string> vs1;
- boost::split(vs,str,boost::is_any_of("ML ;"));
- for(auto & s:vs)
- {
- if(s.empty())
- continue;
- boost::split(vs1,s,boost::is_any_of(","));
- if(vs1.size()!=2)
- {
- log_error("area path data Error.pls check data table,area_id=%d",area_id);
- continue;
- }
- vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
- }
- return std::move(vp);
- }
- std::vector<std::shared_ptr<area>> area_list::get_area(const std::shared_ptr<site>& s,const std::shared_ptr<card_location_base> &c,const point&pt,int & sarid)
- {
- std::vector<std::shared_ptr<area>> ret;
- // 人卡且是井上分站,获得井上分站区域
- if(tool_other::is_person(c->type_()) && s && s->is_up_site())
- {
- ret.push_back(s->get_area());//地面分站的区域
- return std::move(ret);
- }
- // 如果卡在区域内,获得区域
- auto&map = area_list::instance()->m_map;
- for(const auto &a:map)
- {
- if(a.second->in_area(s, c, pt, sarid))
- ret.push_back(a.second);
- }
- // 分站存在,获得分站所在区域
- if(s){
- ret.push_back(s->get_area());
- int area_id = s->m_area_id;
- if(auto area_= area_list::instance()->get(area_id))
- ret.push_back(area_);
- }
- //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
- return std::move(ret);
- }
- // 区域时间段超员设置
- void area_list::init_area_persons_dynamic_thre_from_db(int area_id/* = -1*/)
- {
- std::string sql = "SELECT area_id,adpt_id,start_time,end_time,thre_value from dat_area_persons_dynamic_thre ";
- if (area_id > -1 )
- {
- sql.append(" where area_id=");
- sql.append(std::to_string(area_id));
- }
- sql.append(" order by area_id asc,start_time asc ;");
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql.c_str(),DBRes,Error);
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount < 1)
- {
- return;
- }
- std::map<int, std::shared_ptr<area_persons_thre_time>> map_thre;
- while ( DBRes.GetNextRecod(Error) )
- {
- SArea_Persons_Thre td;
- DBRes.GetField("area_id", td.area_id, Error);
- DBRes.GetField("adpt_id", td.db_id, Error);
- DBRes.GetField("start_time", td.sz_start_time, Error);
- DBRes.GetField("end_time", td.sz_end_time, Error);
- DBRes.GetField("thre_value", td.thre_value, Error);
- td.start_time = tool_time::time_to_day_seconds(td.sz_start_time);
- td.end_time = tool_time::time_to_day_seconds(td.sz_end_time);
- auto it = map_thre.find(td.area_id);
- if (it == map_thre.end())
- {
- std::shared_ptr<area_persons_thre_time> p = std::make_shared<area_persons_thre_time>();
- p->add_persons_thre(td);
- map_thre[td.area_id] = p;
- }
- else
- {
- it->second->add_persons_thre(td);
- }
- }
- for( auto &it : map_thre)
- {
- std::shared_ptr<area> a = area_list::instance()->get(it.first);
- if (nullptr != a)
- {
- it.second->set_default_person_count_limit(a->m_default_limit_person_count);
- a->m_persons_thre_time = it.second;
- }
- }
- log_info("加载区域:%d 时间段人员超员设置sql: %s",area_id, sql.c_str());
- }
- /////////////////////////////////////////////////////////////////////
- area_hover::area_hover(const std::shared_ptr<area>&area,const point&pt)
- :m_area(area)
- {
- m_enter_time = m_last_time = tool_time::now_to_ms();
- m_enter_point = m_last_point = pt;
- }
- void area_tool::on_point(const std::shared_ptr<card_location_base>& c,const point&pt)
- {
- return;
- log_info("on_point...cardid:%d,type:%d,x:%.2f,y:%.2f",c->m_id,c->m_type,pt.x,pt.y);
- if(pt.empty()) return;
- int special_area=-1;
- std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(m_site, c, pt,special_area);
- if(special_area != -1)
- {
- areas.clear();
- auto area_=area_list::instance()->get(special_area);
- areas.push_back(area_);
- }
- else
- {
- std::sort(areas.begin(),areas.end(),[](const std::shared_ptr<area>&l,const std::shared_ptr<area>&r){
- return l->id()<r->id();
- });
- }
- auto c1 = m_hover_list.begin(), ce = m_hover_list.end();
- auto a1 = areas.begin() , ae = areas.end();
- std::vector<std::shared_ptr<area_hover>> nlist;
- while (c1!=ce && a1!=ae)
- {
- if ((*c1)->id()<(*a1)->id()) //离开区域
- {
- if((*c1)->m_area->get_frozen_count()==0)//如果该区域未在修改中,调用on_leave
- {
- (*c1)->m_area->on_leave(*c1, c);
- }
- else//否则,该区域持续保存在卡的区域列表,等待修改完成
- {
- nlist.push_back(*c1);
- log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
- }
- ++c1;
- }
- else if ((*a1)->id()<(*c1)->id()) //进入新区域
- {
- if((*a1)->get_frozen_count()==0)////如果该区域未在修改中,执行正常逻辑
- {
- nlist.push_back(std::make_shared<area_hover>(*a1,pt));
- (*a1)->on_enter(nlist.back(),c);
- }
- else//丢弃本次进入的点,等待修改完成
- {
- log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
- }
- ++a1;
- }
- else //需要持续的区域
- {
- nlist.push_back(*c1);
- if((*c1)->m_area->get_frozen_count()==0)//正常情况
- {
- (*c1)->m_area->on_hover(*c1,c);
- }
- else//丢弃本次on_hover时间
- {
- log_warn("丢弃区域行走事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
- }
- ++c1,++a1;
- }
- }
- while(c1!=ce)
- {
- if((*c1)->m_area->get_frozen_count()==0)
- {
- (*c1)->m_area->on_leave(*c1, c);
- }
- else
- {
- log_warn("丢弃离开区域事件,cardid=%d,area_id=%d",c->m_id,(*c1)->id());
- }
- ++c1;
- }
- while(a1!=ae)
- {
- if((*a1)->get_frozen_count()==0)
- {
- nlist.push_back(std::make_shared<area_hover>(*a1,pt));
- (*a1)->on_enter(nlist.back(),c);
- }
- else
- {
- log_warn("丢弃进入区域事件,cardid=%d,area_id=%d",c->m_id,(*a1)->id());
- }
- ++a1;
- }
- m_hover_list=std::move(nlist);
- //更新到数据库
- if(m_site && !m_hover_list.empty())
- {
- std::string strAreaInfo = "";
- for (std::vector<std::shared_ptr<area_hover>>::iterator itvec = m_hover_list.begin(); itvec != m_hover_list.end(); ++itvec)
- {
- if ((*itvec)->id() >= 0)
- {
- char tmpArea[128] = {0};
- snprintf(tmpArea, 128, "%d,%lu|", (*itvec)->id(), (*itvec)->m_enter_time);
- strAreaInfo += tmpArea;
- }
- }
- if(!strAreaInfo.empty())
- {
- char sql[1024] = {0};
- std::string _time=tool_time::to_str_ex(c->time_());
- snprintf(sql, 1024, "REPLACE INTO rt_location (card_id, site_id,cur_time, x, y, z, state, power_status, area_info) VALUES (%s, %d,'%s',%lf, %lf, %lf, 0, '%d', '%s');",
- tool_other::type_id_to_str(c->m_type, c->m_id).c_str(), m_site->id(),_time.c_str(), pt.x, pt.y, pt.z, c->m_battery_value, strAreaInfo.c_str());
- db_tool::PushAsync(sql);
- log_info("insert rt_location: card=%d site_id=%d,point=%.2f-%.2f, power_status=%d, area_info=%s", c->m_id, m_site->id(), pt.x, pt.y, c->m_battery_value, strAreaInfo.c_str());
- }
- }
- }
- void area_tool::on_leave(const std::shared_ptr<card_location_base>& c)
- {
- for(const auto& t:m_hover_list)
- {
- t->m_area->on_leave(t, c);
- }
- m_hover_list.clear(); //手动升井、自动升井清除该卡区域列表
- }
- void area_tool::set_area_info(int mapid,double scale,int areaid,const point &pt,uint64_t t,int type)
- {
- if(0==type)
- {
- m_mapid = mapid;
- m_scale=scale;
- set_area_info(mapid,areaid,pt,t);
- }
- else if(1==type)
- {
- if(m_mapid != mapid || m_scale!=scale){
- m_mapid=mapid;m_scale=scale;
- }
- set_area_info(mapid,areaid,pt,t);
- }
- else if(2==type)
- {
- auto it =m_area_info.find(areaid);
- if(it != m_area_info.end())
- m_area_info.erase(it);
- }
- else
- {
- log_info("wrong type..");
- }
- }
- void area_tool::set_area_info(int mapid,int areaid,const point &pt,uint64_t t)
- {
- auto lm = Landmark_list::instance()->get_landmark(mapid,areaid,pt);
- int landmark_id = std::get<0>(lm);
- int landmark_dir = std::get<1>(lm);
- double landmark_dis = std::get<2>(lm)*m_scale;
- std::tuple<int,int,int,double,uint64_t> tinfo=std::make_tuple(areaid,landmark_id,landmark_dir,landmark_dis,t);
- m_area_info[areaid].swap(tinfo);
- }
- void area_tool::on_change_business(const std::shared_ptr<card_location_base>& c, const task&t)
- {
- auto&mcb=t.body<message_change_business>();
- auto i=std::lower_bound(m_hover_list.begin(),m_hover_list.end(),mcb.area_id ,[&mcb](std::shared_ptr<area_hover>&ah,int id){
- return ah->m_area->m_id<id;
- });
- if(i==m_hover_list.end())
- return ;
-
- if((*i)->m_area->m_id!=mcb.area_id)
- return;
-
- log_info("调用调整的区域业务,card_id=%d,area_id=%d", c->m_id, mcb.area_id);
- for(auto&biz:mcb.add_list)
- {
- biz->on_enter(*i,c,(*i)->get_business_data(biz->area_business_type()));
- }
- for(auto&biz:mcb.del_list)
- {
- biz->on_leave(*i,c,(*i)->get_business_data(biz->area_business_type()));
- }
- }
|