|
@@ -29,24 +29,37 @@ void traffic_light_manager::init_light_from_db(int lid)
|
|
}
|
|
}
|
|
|
|
|
|
while(res.GetNextRecod(err)){
|
|
while(res.GetNextRecod(err)){
|
|
- traffic_light_ptr pl = std::make_shared<traffic_light>();
|
|
|
|
|
|
+ int light_id = 0;
|
|
|
|
+ res.GetField("light_id", light_id, err);
|
|
|
|
+ int group_id = 0;
|
|
|
|
+ res.GetField("lights_group_id", group_id, err);
|
|
|
|
+ std::string ip = "";
|
|
|
|
+ res.GetField("ip", ip, err);
|
|
|
|
+ double x = 0.0, y = 0.0, z = 0.0;
|
|
|
|
+ res.GetField("x", x, err);
|
|
|
|
+ res.GetField("y", y, err);
|
|
|
|
+ res.GetField("z", z, err);
|
|
|
|
+ int site_id = 0, state = 0, port = 0, phy_light_id = 0, phy_direction = 0, special = 0;
|
|
|
|
+ res.GetField("reader_id", site_id, err);
|
|
|
|
+ res.GetField("state", state, err);
|
|
|
|
+ res.GetField("port", port, err);
|
|
|
|
+ res.GetField("physics_light_id", phy_light_id, err);
|
|
|
|
+ res.GetField("physics_light_direction", phy_direction, err);
|
|
|
|
+ res.GetField("special_flag", special, err);
|
|
|
|
+ traffic_light_ptr pl = find_light(light_id);
|
|
if(nullptr == pl){
|
|
if(nullptr == pl){
|
|
- return;
|
|
|
|
|
|
+ pl = std::make_shared<traffic_light>(x, y, z, light_id, group_id, ip, state, port, phy_light_id, phy_direction, special);
|
|
|
|
+ m_unmap_lights.insert(std::make_pair(light_id, pl));
|
|
|
|
+ }else{
|
|
|
|
+ pl->update(x, y , z, light_id, group_id, ip, state, port, phy_light_id, phy_direction, special);
|
|
}
|
|
}
|
|
|
|
|
|
- res.GetField("light_id", pl->m_light_id, err);
|
|
|
|
- res.GetField("lights_group_id", pl->m_group_id, err);
|
|
|
|
- res.GetField("ip", pl->m_ip, err);
|
|
|
|
- res.GetField("x", pl->x, err);
|
|
|
|
- res.GetField("y", pl->y, err);
|
|
|
|
- res.GetField("z", pl->z, err);
|
|
|
|
- res.GetField("reader_id", pl->m_site_id, err);
|
|
|
|
- res.GetField("state", pl->m_state, err);
|
|
|
|
- res.GetField("port", pl->m_port, err);
|
|
|
|
- res.GetField("physics_light_id", pl->m_phy_light_id, err);
|
|
|
|
- res.GetField("physics_light_direction", pl->m_phy_direction, err);
|
|
|
|
- res.GetField("special_flag", pl->m_special, err);
|
|
|
|
- m_unmap_lights[pl->m_light_id] = pl;
|
|
|
|
|
|
+ // 将红绿灯分配到对应组中
|
|
|
|
+ auto it_group = m_unmap_groups.find(pl->m_group_id);
|
|
|
|
+ if(it_group != m_unmap_groups.end())
|
|
|
|
+ {
|
|
|
|
+ it_group->second->m_vt_lights.push_back(pl);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
logn_info(2, "sql:%s",sql.c_str());
|
|
logn_info(2, "sql:%s",sql.c_str());
|
|
@@ -71,22 +84,25 @@ void traffic_light_manager::init_light_group_from_db(int gid)
|
|
}
|
|
}
|
|
|
|
|
|
while(res.GetNextRecod(err)){
|
|
while(res.GetNextRecod(err)){
|
|
- traffic_light_group_ptr pg = std::make_shared<traffic_light_group>();
|
|
|
|
- if(nullptr == pg){
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- res.GetField("lights_group_id", pg->m_group_id, err);
|
|
|
|
- res.GetField("x", pg->x, err);
|
|
|
|
- res.GetField("y", pg->y, err);
|
|
|
|
- res.GetField("z", pg->z, err);
|
|
|
|
- res.GetField("scope", pg->m_scope, err);
|
|
|
|
- res.GetField("map_id", pg->m_map_id, err);
|
|
|
|
- res.GetField("area_id", pg->m_area_id, err);
|
|
|
|
|
|
+ int group_id = 0;
|
|
|
|
+ res.GetField("lights_group_id", group_id, err);
|
|
|
|
+ double x = 0.0, y = 0.0, z = 0.0, scope = 0.0;
|
|
|
|
+ res.GetField("x", x, err);
|
|
|
|
+ res.GetField("y", y, err);
|
|
|
|
+ res.GetField("z", z, err);
|
|
|
|
+ res.GetField("scope", scope, err);
|
|
|
|
+ int map_id = 0, area_id = 0;
|
|
|
|
+ res.GetField("map_id", map_id, err);
|
|
|
|
+ res.GetField("area_id", area_id, err);
|
|
int ai = 0;
|
|
int ai = 0;
|
|
res.GetField("light_auto_interval", ai, err);
|
|
res.GetField("light_auto_interval", ai, err);
|
|
- pg->m_auto_interval = ai;
|
|
|
|
- m_unmap_groups[pg->m_group_id] = pg;
|
|
|
|
|
|
+ traffic_light_group_ptr pg = find_group(group_id);
|
|
|
|
+ if(nullptr == pg){
|
|
|
|
+ pg = std::make_shared<traffic_light_group>(x, y, z, group_id, scope, ai, map_id, area_id);
|
|
|
|
+ m_unmap_groups.insert(std::make_pair(group_id, pg));
|
|
|
|
+ }else{
|
|
|
|
+ pg->update(x, y, z, group_id, scope, ai, map_id, area_id);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
logn_info(2, "sql:%s",sql.c_str());
|
|
logn_info(2, "sql:%s",sql.c_str());
|
|
@@ -94,8 +110,10 @@ void traffic_light_manager::init_light_group_from_db(int gid)
|
|
|
|
|
|
void traffic_light_manager::init(const traffic_send_callback& tcb)
|
|
void traffic_light_manager::init(const traffic_send_callback& tcb)
|
|
{
|
|
{
|
|
- init_light_from_db();
|
|
|
|
|
|
+ // 1.先获得红绿灯组数据
|
|
init_light_group_from_db();
|
|
init_light_group_from_db();
|
|
|
|
+ // 2.再获得红绿灯数据
|
|
|
|
+ init_light_from_db();
|
|
|
|
|
|
for(hashmap_light::iterator it_light = m_unmap_lights.begin(); it_light != m_unmap_lights.end(); ++it_light){
|
|
for(hashmap_light::iterator it_light = m_unmap_lights.begin(); it_light != m_unmap_lights.end(); ++it_light){
|
|
if(it_light->second->m_group_id >0){
|
|
if(it_light->second->m_group_id >0){
|
|
@@ -111,20 +129,33 @@ void traffic_light_manager::init(const traffic_send_callback& tcb)
|
|
|
|
|
|
void traffic_light_manager::start()
|
|
void traffic_light_manager::start()
|
|
{
|
|
{
|
|
- log_info("[traffic_light] start traffic light service");
|
|
|
|
|
|
+ log_info("[traffic_light] start traffic light service, light_group's size=%d", m_unmap_groups.size());
|
|
m_stop = false;
|
|
m_stop = false;
|
|
m_crossing_rule = std::unique_ptr<crossing_rule>(new crossing_rule);
|
|
m_crossing_rule = std::unique_ptr<crossing_rule>(new crossing_rule);
|
|
if(nullptr == m_crossing_rule){
|
|
if(nullptr == m_crossing_rule){
|
|
log_info("[traffic_light] create crossing rule failed");
|
|
log_info("[traffic_light] create crossing rule failed");
|
|
return;
|
|
return;
|
|
|
|
+ }else{
|
|
|
|
+ for(auto it = m_unmap_groups.begin(); it != m_unmap_groups.end(); ++it)
|
|
|
|
+ {
|
|
|
|
+ m_crossing_rule->put(it->second);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
m_avoidance_rule = std::unique_ptr<avoidance_rule>(new avoidance_rule);
|
|
m_avoidance_rule = std::unique_ptr<avoidance_rule>(new avoidance_rule);
|
|
if(nullptr == m_avoidance_rule){
|
|
if(nullptr == m_avoidance_rule){
|
|
log_info("[traffic_light] create avoidance rule failed");
|
|
log_info("[traffic_light] create avoidance rule failed");
|
|
return;
|
|
return;
|
|
|
|
+ }else{
|
|
|
|
+ for(auto it = m_unmap_groups.begin(); it != m_unmap_groups.end(); ++it)
|
|
|
|
+ {
|
|
|
|
+ log_info("[traffic_light] insert light group into area, area_id=%d", it->second->m_area_id);
|
|
|
|
+ m_avoidance_rule->put(it->second);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ log_info("[traffic_light] rule's map size, crossing=%d, avoidance=%d", m_crossing_rule->size(), m_avoidance_rule->size());
|
|
|
|
+
|
|
m_thread_light.reset(new std::thread(std::bind(&traffic_light_manager::run, this)));
|
|
m_thread_light.reset(new std::thread(std::bind(&traffic_light_manager::run, this)));
|
|
}
|
|
}
|
|
|
|
|
|
@@ -139,7 +170,8 @@ void traffic_light_manager::put(const light_message& msg)
|
|
{
|
|
{
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
m_list_data.push_back(msg);
|
|
m_list_data.push_back(msg);
|
|
- m_condition.notify_one();
|
|
|
|
|
|
+ lock.unlock();
|
|
|
|
+ m_condition.notify_all();
|
|
}
|
|
}
|
|
|
|
|
|
void traffic_light_manager::run()
|
|
void traffic_light_manager::run()
|
|
@@ -147,7 +179,7 @@ void traffic_light_manager::run()
|
|
while(!m_stop){
|
|
while(!m_stop){
|
|
std::list<light_message> tmp_data;
|
|
std::list<light_message> tmp_data;
|
|
tmp_data.clear();
|
|
tmp_data.clear();
|
|
-
|
|
|
|
|
|
+ //log_info("[traffic_light] working... m_stop=%d", m_stop);
|
|
{
|
|
{
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
while(m_list_data.empty()){
|
|
while(m_list_data.empty()){
|
|
@@ -157,6 +189,8 @@ void traffic_light_manager::run()
|
|
if(m_list_data.size() > 0){
|
|
if(m_list_data.size() > 0){
|
|
m_list_data.swap(tmp_data);
|
|
m_list_data.swap(tmp_data);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ lock.unlock();
|
|
}
|
|
}
|
|
|
|
|
|
for(std::list<light_message>::iterator it = tmp_data.begin(); it != tmp_data.end(); ++it){
|
|
for(std::list<light_message>::iterator it = tmp_data.begin(); it != tmp_data.end(); ++it){
|
|
@@ -170,16 +204,18 @@ void traffic_light_manager::run()
|
|
handle_manual(it->m_group_id,it->m_light_id, it->m_ctrl_name, it->m_light_status);
|
|
handle_manual(it->m_group_id,it->m_light_id, it->m_ctrl_name, it->m_light_status);
|
|
break;
|
|
break;
|
|
case cmd_card_data:
|
|
case cmd_card_data:
|
|
- log_info("[traffic_light] cmd=%d, cid=%d, bigger=%d, aid=%d, speed=%.2f", it->m_cmd, it->m_pos.m_card_id, it->m_pos.m_bigger, it->m_pos.m_area_id, it->m_pos.m_speed);
|
|
|
|
|
|
+ log_info("[traffic_light] cmd=%d, cid=%d, ctype=%d, bigger=%d, aid=%d, speed=%.2f", it->m_cmd, it->m_pos.m_card_id, it->m_pos.m_type, it->m_pos.m_bigger, it->m_pos.m_area_id, it->m_pos.m_speed);
|
|
handle_position(it->m_pos);
|
|
handle_position(it->m_pos);
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
log_warn("[traffic_light] message cmd error, cmd=%d", it->m_cmd);
|
|
log_warn("[traffic_light] message cmd error, cmd=%d", it->m_cmd);
|
|
break;
|
|
break;
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ //log_info("[traffic_light] working thread one loop, m_stop=%d", m_stop);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ log_info("[traffic_light] working thread is exit. m_stop=%d", m_stop);
|
|
}
|
|
}
|
|
|
|
|
|
void traffic_light_manager::handle_reload(const int& gid, const int& lid)
|
|
void traffic_light_manager::handle_reload(const int& gid, const int& lid)
|
|
@@ -214,17 +250,19 @@ void traffic_light_manager::handle_manual(const int& gid, const int& ld, const s
|
|
|
|
|
|
void traffic_light_manager::handle_position(pos_data& p)
|
|
void traffic_light_manager::handle_position(pos_data& p)
|
|
{
|
|
{
|
|
- if(p.m_type == 2){
|
|
|
|
|
|
+ if(p.m_type == 3 || p.m_type == 1){
|
|
std::map<uint64_t, pos_data>::iterator it = m_map_card.find(p.m_card_id);
|
|
std::map<uint64_t, pos_data>::iterator it = m_map_card.find(p.m_card_id);
|
|
if(it != m_map_card.end()){
|
|
if(it != m_map_card.end()){
|
|
m_map_card.erase(it);
|
|
m_map_card.erase(it);
|
|
}
|
|
}
|
|
- }else if(p.m_type == 3 || p.m_type == 1){
|
|
|
|
- m_geo_list.update(p.x, p.y, p.m_card_id);
|
|
|
|
|
|
+ }else if(p.m_type == 2){
|
|
|
|
+ //log_info("[traffic_light] call handle rule");
|
|
|
|
+ update(p.x, p.y, p.m_card_id);
|
|
m_map_card[p.m_card_id] = p;
|
|
m_map_card[p.m_card_id] = p;
|
|
// 路口规则
|
|
// 路口规则
|
|
if(nullptr != m_crossing_rule){
|
|
if(nullptr != m_crossing_rule){
|
|
- m_crossing_rule->handle_rule(p);
|
|
|
|
|
|
+ // maybe error
|
|
|
|
+ //m_crossing_rule->handle_rule(p);
|
|
}
|
|
}
|
|
|
|
|
|
if(!p.m_bigger && nullptr != m_avoidance_rule){
|
|
if(!p.m_bigger && nullptr != m_avoidance_rule){
|