123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- #include <memory>
- #include <write-copy.h>
- #include "db_api/CDBSingletonDefine.h"
- #include "log.h"
- #include <area.h>
- #include "point.h"
- #include "monkey_car/monkeycar_area.h"
- #include "landmark.h"
- #include "area_business.h"
- #include <boost/algorithm/string/split.hpp>
- #include <boost/algorithm/string/classification.hpp>
- #include"module_service/module_area.h"
- template<> std::shared_ptr<area_list>
- single_base<area_list, int, std::shared_ptr<area>>::m_instance=std::make_shared<area_list>();
- struct underground_area:area
- {
- underground_area(int limit_count_person, int limit_time_person,double scale,int32_t mapid)
- :area(-1,limit_count_person,limit_time_person,scale,mapid,(1<<1)|(1<<2)|(1<<3)|(1<<4))
- {
- m_area_business_list=area_business::get_instance_list(m_area_type);
- }
- void db_load_card_count()
- {
-
- }
- };
- struct ground_area:area
- {
- };
- void area::on_hover(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
- {
- for(auto i:m_area_business_list)
- {
- i->on_hover(a,c,a->get_business_data(i->business_type()));
- }
- }
- void area::on_enter(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
- {
- log_info("on_enter..%d areaId:%d",c->m_id,m_id);
- for(auto i:m_area_business_list)
- {
- i->on_enter(a,c,a->get_business_data(i->business_type()));
- }
- }
- void area::on_leave(std::shared_ptr<area_hover>&a,std::shared_ptr<card_location_base>&c)
- {
- log_info("on_leave..%d areaId:%d",c->m_id,m_id);
- for(auto i:m_area_business_list)
- {
- i->on_leave(a,c,a->get_business_data(i->business_type()));
- }
- }
- 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;
- }
- area_list::area_list()
- {
- }
- #if 0
- void area_list::init_monkeycar_area()
- {
- std::unordered_map<int,std::shared_ptr<area>> map;
- const char *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 \
- 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;";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- if(!Error.empty())
- log_error("monkeycar area init Error,%s",Error.c_str());
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_monkey area. The record count=%d", nCount );
- 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 );
-
- float monkeycar_speed = 0;
- DBRes.GetField( "monkeycar_speed",monkeycar_speed, Error );
- std::string monkeycar_coor;
- DBRes.GetField( "monkeycar_coordinate",monkeycar_coor, Error );
- float scale=0;
- DBRes.GetField( "scale",scale, Error );
- log_info("init_area : id:%d,path:%s",area_id, path.c_str());
- std::shared_ptr<db_area> da = std::make_shared<db_area>();
- da->m_default_speed = monkeycar_speed;
- da->m_point = init_path(monkeycar_coor);
-
- std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
- ap->m_bound=init_path(path);
- 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});
- }
- }
-
- area_list::instance()->add(map);
- }
- #endif
- 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 \
- 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(";");
- std_debug("基础数据 monkeycar area 增加或修改区域 sql=%s", sql.c_str());
- 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 );
- 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);
- std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person,
- over_time_person,scale,map_id,area_type_id);
- ap->m_bound=init_path(path);
- 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
- {
- std::shared_ptr<db_area> da = std::make_shared<db_area>();
- da->m_default_speed = monkeycar_speed;
- da->m_point = init_path(monkeycar_coor);
- std::shared_ptr<area> ap = std::make_shared<monkey_area>(da,area_id,over_count_person,
- over_time_person,scale,map_id,area_type_id);
- ap->m_bound=init_path(path);
- 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);
- auto tmp_ptr = area_list::instance()->get(id);
- if(tmp_ptr)
- {
- area_list::instance()->remove(id);
- }
- area_list::instance()->add(id, ap);
- 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);
- std_debug("基础数据 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);
- }
- }
- void area_list::init_from_db(int id/*=-1*/)
- {
- 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 \
- 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 s.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());
- 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 scale = 0;
- DBRes.GetField( "scale",scale, Error );
- log_info("init_area : id:%d,path:%s",area_id, path.c_str());
- if(-1 == id)
- {
- std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
- scale,map_id,area_type_id);
- ap->m_limit_vehicle_second = over_time_vehicle;
- ap->m_limit_vehicle_count = over_count_vehicle;
- ap->m_bound=init_path(path);
- 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});
- CheckAreaType(ap,area_type_id,0);
- }
- else
- {
- auto tmp_ptr = area_list::instance()->get(id);
- if(tmp_ptr)
- {
- CheckAreaType(tmp_ptr,area_type_id,tmp_ptr->m_area_type);
- tmp_ptr->update(over_count_person, over_time_person,scale,map_id,area_type_id,
- over_count_vehicle,over_time_vehicle);
- tmp_ptr->m_bound=init_path(path);
- }
- else
- {
- std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,
- scale,map_id,area_type_id);
- ap->m_limit_vehicle_second = over_time_vehicle;
- ap->m_limit_vehicle_count = over_count_vehicle;
- ap->m_bound=init_path(path);
- for(const auto &p : ap->m_bound)
- log_info("point:area_id:%d--x:%.2f,y:%.2f",area_id,p.x,p.y);
- area_list::instance()->add(id, ap);
- CheckAreaType(ap,area_type_id,0);
- }
- log_info("基础数据 增加或修改区域成功:区域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( "init_area. The record count=%d\n", nCount );
- area_list::instance()->add(map);
- init_monkeycar_area();
- }
- }
- //新画禁区功能-给禁区中的卡发送警告及呼叫
- void area_list::CheckAreaType(int area_id,int new_area_type,int old_area_type)
- {
- auto area_ptr = area_list::instance()->get(area_id);
- if (!area_ptr) {
- log_info("区域已经删除:areaid=%d", area_id);
- return;
- }
- CheckAreaType(area_ptr,new_area_type,old_area_type);
- }
- void area_list::CheckAreaType( std::shared_ptr<area> pArea,int new_area_type,int old_area_type)
- {
- if (nullptr == pArea)
- {
- return ;
- }
- if (new_area_type != AREA_TYPE::AREA_TYPE_FORBIDDEN && old_area_type != AREA_TYPE::AREA_TYPE_FORBIDDEN)
- {
- return;
- }
- struct local_visit:visitor<std::shared_ptr<card_location_base>>
- {
- std::shared_ptr<area> m_area;
- int m_old_area_type;
- bool visit(std::shared_ptr<card_location_base> c)
- {
- //处理
- point pt(c->x,c->y,c->z);
- std::shared_ptr<area> point_area = area_list::instance()->get_area(pt);
- if (point_area == nullptr)
- {
- return true;
- }
- //不在区域里
- if (m_area->m_id != point_area->m_id)
- {
- return true;
- }
- if (m_area->m_area_type == AREA_TYPE::AREA_TYPE_FORBIDDEN)
- {
- //发送进入禁区的警告
- //呼叫
- std::shared_ptr<area_hover> _area_hover = c->get_area_hover();
- if (nullptr != _area_hover)
- {
- _area_hover->m_area = m_area;
- _area_hover->m_area->on_enter(_area_hover,c);
- }
- }
- else
- {
- if (m_old_area_type == AREA_TYPE::AREA_TYPE_FORBIDDEN)
- {
- //之前是禁区,改成非禁区
- //发送一个离开禁区的警告
- //停止呼叫
- std::shared_ptr<area_hover> _area_hover = c->get_area_hover();
- if (nullptr != _area_hover)
- {
- _area_hover->m_area->on_leave(_area_hover,c);
- }
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- };
- local_visit lv;
- lv.m_area = pArea;
- lv.m_old_area_type = old_area_type;
- card_list::instance()->accept(lv);
- }
- #if 0
- void area_list::init_from_db()
- {
- std::unordered_map<int,std::shared_ptr<area>> map;
- const char *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 \
- 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);";
- std::string Error;
- YADB::CDBResultSet DBRes;
- sDBConnPool.Query(sql,DBRes,Error);
- if(!Error.empty())
- log_error("monkeycar area init Error,%s",Error.c_str());
- int nCount = DBRes.GetRecordCount( Error );
- if (nCount > 0)
- {
- log_info( "init_area. The record count=%d\n", nCount );
- 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 scale = 0;
- DBRes.GetField( "scale",scale, Error );
- log_info("init_area : id:%d,path:%s",area_id, path.c_str());
-
- std::shared_ptr<area> ap = std::make_shared<area>(area_id,over_count_person,over_time_person,scale,map_id,area_type_id);
- ap->m_limit_vehicle_second = over_time_vehicle;
- ap->m_limit_vehicle_count = over_count_vehicle;
- ap->m_bound=init_path(path);
- 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});
- }
- }
-
- area_list::instance()->add(map);
- init_monkeycar_area();
- }
- #endif
- std::vector<point> area_list::init_path(std::string &str)
- {
- if(str.empty())
- log_error("area path empty()...");
- 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.");
- vp.emplace_back(atof(vs1[0].c_str()),atof(vs1[1].c_str()));
- }
- return std::move(vp);
- }
- std::shared_ptr<area> area_list::get_area(const point&pt)
- {
- std::shared_ptr<area> ret=nullptr;
-
- auto map = area_list::instance()->m_map;
- for(const auto &a:map)
- {
- if(a.second->in_area(pt))
- ret = a.second;
- }
- //区域覆盖不完全地图,很多车辆人行驶在地图外,如何确认.
- return ret;
- }
- void area_tool::on_point(const std::shared_ptr<site>&s,std::shared_ptr<card_location_base> c,const point&pt)
- {
- log_info("on_point...cardid:%d,type:%d",c->m_id,c->m_type);
- //获取地标信息
- setLandmark(pt);
- if(m_area_hover != nullptr && m_area_hover->m_area->in_area(pt))
- {
- // do_hover_biz(card_id,speed,type);
- }
- else
- {
- auto area = area_list::instance()->get_area(pt);
- if(area == nullptr)
- {
- //这里使用分站区域比较合理
- // log_error("Cardid:%d can not find area..[x:%.2f,y:%.2f]",card_id,pt.x,pt.y);
- return;
- }
- if(m_area_hover==nullptr)
- {
- // m_area_hover = std::make_shared<area_hover>(area,pt,speed);
- // do_enter_biz(card_id,speed,type);
- }
- else
- {
- // do_leave_biz(card_id,speed,type);
- // m_area_hover.reset(new area_hover(area,pt,speed));
- // do_enter_biz(card_id,speed,type);
- }
- }
- }
- void area_hover::setLandmark(const point &pt)
- {
- set(pt);
- auto lm = Landmark_list::instance()->get(mapid(),id(),pt);
- landmark_id = std::get<0>(lm);
- landmark_dir = std::get<1>(lm);
- landmark_dis = std::get<2>(lm)*scale();
- log_info("landmark:%d %d %.2f,(%.2f,%.2f),%f",landmark_id,landmark_dir,landmark_dis,pt.x,pt.y,scale());
- }
|