#ifndef module_traffic_light_h #define module_traffic_light_h #include #include #include #include #include #include #include "ant.h" #include "module_traffic_light_common.h" #include "line.h" class client; // 红绿灯 struct traffic_light: point{ int m_light_id; // 红绿灯id int m_group_id; // 所属红绿灯组 std::string m_ip; // 红绿灯IP int m_site_id; // 控制分站id int m_state; // 红绿灯状态,1-初始值,2-红灯,3-绿灯,4-闪烁 int m_state_last = 0; // 状态有变化,立马更新状态,否则隔30秒再更新一次 int m_port; // 分站控制红绿灯的路数 int m_phy_light_id; // 物理灯号,红绿灯正反面共享此灯号 int m_phy_direction; // 物理灯朝向,1-正面,0-反面 uint64_t m_rec_time; // 红绿灯接收时间 int m_special; // 检查非巷道区域,0-正常,1-特殊 double m_direct_distance; // 红绿灯到灯组坐标的距离 line m_line_group; // 红绿灯与灯组坐标的连线 int m_stream; // 上下行标志,1为上行,2为下行 int m_area_id; int m_map_id; int m_device_type_id; // 红绿灯设备类型号 std::shared_ptr m_clt; traffic_light(): m_light_id(0), m_group_id(0), m_ip(""), m_state(0), m_port(0), m_phy_light_id(0), m_phy_direction(0), m_rec_time(0), m_special(0), m_direct_distance(0.0), m_stream(0), m_area_id(0), m_map_id(0) { m_device_type_id=3; m_clt = nullptr; } traffic_light(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special, int stream): point(x, y, z), m_light_id(light_id), m_group_id(group_id), m_ip(ip), m_state(state), m_port(port), m_phy_light_id(pli), m_phy_direction(pd), m_special(special), m_stream(stream) { m_device_type_id=3; m_clt = nullptr; } void update(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special, int stream) { this->x = x; this->y = y; this->z = z; this->m_light_id = light_id; this->m_group_id = group_id; this->m_ip = ip; this->m_state = state; this->m_port = port; this->m_phy_light_id = pli; this->m_phy_direction = pd; this->m_special = special; this->m_stream = stream; } void set_state(int value = light_shape::green_all_on) { m_state = value; } void set_client(const std::shared_ptr& clt) { if(m_clt != clt){ m_clt = clt; } } std::shared_ptr get_client() { return m_clt; } }; using traffic_light_ptr = std::shared_ptr; class traffic_light_group; using traffic_light_group_ptr = std::shared_ptr; //红绿灯组 struct traffic_light_group: point{ int m_group_id; // 所属红绿灯组ID double m_scope; // 控制半径 uint16_t m_manual_time; // 人工控制默认时间 uint16_t m_auto_interval; // 断网情况下,自动控制间隔 uint64_t m_card_id; // 如果优先级是1,表示小车id;优先级是2,表示打车id int m_map_id; // 地图ID int m_area_id; // 区域ID time_t m_time_stamp; // 控制时长,单位秒 time_t m_ctrl_time; // 控制时间 bool m_special; // 是否含有特殊红绿灯组 int m_green_light_id; // 灯组被控制后变绿灯的灯ID std::string m_ctrl_name; // 控制用户 std::atomic m_priority; // 优先级,0-初始值,1-路口控制,2-避让控制;3-手动控制 std::atomic m_used; // 是否使用 std::atomic_flag m_owner_flag; // 控制权 std::vector m_vt_lights; // 组下红绿灯 std::array m_path; int m_down_stream_idx; // 灯组按下行排序的索引值 move_stream m_stream_state; traffic_light_group(): point(0,0), m_group_id(0), m_scope(0.0), m_manual_time(0), m_auto_interval(0), m_card_id(0), m_map_id(0), m_area_id(0), m_time_stamp(0), m_ctrl_time(0),m_special(false), m_green_light_id(0), m_ctrl_name(""), m_priority(priority_init), m_used(false) { m_vt_lights.clear(); m_down_stream_idx = 0; m_owner_flag.clear(); m_stream_state = move_stream::unknown; } traffic_light_group(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id): point(x, y, z), m_group_id(group_id), m_scope(scope), m_auto_interval(interval), m_map_id(map_id), m_area_id(area_id) { m_priority = priority_init; m_vt_lights.clear(); m_used = false; m_card_id = 0; m_down_stream_idx = 0; m_owner_flag.clear(); m_stream_state = move_stream::unknown; } // 给红绿灯下发指令设置其颜色 void send_cmd_light(traffic_light_ptr& ptl); void set_path(const std::vector& vl); void set_path(const std::vector& vl, std::vector::const_iterator it); void set_crossing(const uint64_t& cid, const int& p) { m_card_id = cid; set_priority(p); } bool is_at_path(point p); // 设置避让 void set_avoidance(const int& ld, const int& lc, const int& lcr) { m_card_id = 0; m_time_stamp = 0; set_priority(priority_avoidance); set_light(ld, lc, lcr); } // 设置优先权 void set_priority(const int& p) { m_priority.store(p); } void set_down_stream_idx(const int& v) { m_down_stream_idx = v; } // 获取优先权 int get_priority() { return m_priority.load(); } // 设置状态 void set_status(bool s) { m_used.store(s); } // 得到状态 bool get_status() { return m_used.load(); } move_stream get_stream_state() { return m_stream_state; } void set_stream_state(const move_stream& state) { m_stream_state = state; } move_stream get_stream(const int& cid); // 控制超时检查 bool is_time_out() { if(m_time_stamp >= m_ctrl_time){ return true; } return false; } void get_turn() { while(m_owner_flag.test_and_set(std::memory_order_acquire)); } void release_turn() { m_owner_flag.clear(std::memory_order_release); } // 重置 void reset(); //设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状 void set_light(const int& lid, const int& lc, const int& lcr); // 增加红绿灯到灯组 void insert(traffic_light_ptr ptl); // 检查灯在组内是否存在 bool exist(const int& lid); //获取两个灯之间的距离 bool is_different(const int& ld, const int& lc, const int& lcr); // 手动控制 void set_manual_ctrl(const std::string& name, const int& ld, const int& lc); uint64_t get_green_light_id(); // 上行灯与下行灯之间的距离,带方向 double updown_dist(); int size() { return m_vt_lights.size(); } void update(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id) { this->x = x; this->y = y; this->z = z; this->m_group_id = group_id; this->m_scope = scope; this->m_auto_interval = interval; this->m_map_id = map_id; this->m_area_id = area_id; } }; struct traffic_light_data{ bool m_bigger_flag; //大车标识 std::list m_traffic_group; traffic_light_data(): m_bigger_flag(false){ m_traffic_group.clear(); } }; // 闯红灯 struct run_red_light{ int32_t m_group_id; int m_light_id; int m_vid; int32_t m_area_id; std::string m_occ_time; // 发生时间 run_red_light(): m_group_id(0), m_light_id(0), m_vid(0), m_area_id(0), m_occ_time(0) {} }; #endif