#ifndef module_traffic_light_manager_h #define module_traffic_light_manager_h #include #include #include "module_traffic_light.h" #include "module_traffic_light_rule.h" #include "db_api/CDBCommon.h" #include "geo_hash.h" #include "log.h" using traffic_send_callback = std::function; struct traffic_light_callback{ traffic_send_callback m_traffic_send; traffic_light_callback() { m_traffic_send = nullptr; } void set_callback(const traffic_send_callback& cb) { m_traffic_send = std::move(cb); } }; // 红绿灯管理 struct traffic_light_manager{ static traffic_light_manager* instance(); // 从数据库加载数据 void init_light_from_db(int lid = 0); void init_light_group_from_db(int gid = 0); // 初始化回调信息 void init(const traffic_send_callback& cb); // 开启 void start(); // 关闭 void stop(); // 运行 void run(); // 信息处理 void put(const light_message& msg); // 重新加载红绿灯信息 void handle_reload(const int& gid, const int& lid); int reload_light(const int& lid); int reload_group(const int& gid); // 手动控制红绿灯信息 void handle_manual(const int& gid, const int& ld, const std::string& name, const int& lc); // 卡数据 void handle_position(pos_data& p); //查找红绿灯 traffic_light_ptr find_light(const int k) { hashmap_light::iterator it = m_unmap_lights.find(k); if(it != m_unmap_lights.end()) { return it->second; } return nullptr; } // 查找红绿灯组 traffic_light_group_ptr find_group(const int k) { hashmap_group::iterator it = m_unmap_groups.find(k); if(it != m_unmap_groups.end()) { return it->second; } return nullptr; } // 更新红绿灯数据 void update_light(const int& key, traffic_light_ptr ptl); // 更新红绿灯组数据 void update_group(const int& key, traffic_light_group_ptr ptlg); // 查找附近的车 std::vector find_nearby_vehicle(const point& p,const int& dist, const uint64_t& card_id) { return m_geo_list.find_near(p.x, p.y, dist, card_id); } pos_data* get_position(const uint64_t& cid) { std::map::iterator it = m_map_card.find(cid); if(it == m_map_card.end()) { log_error("[traffic_light] 找不到卡,card_id=%lld", cid); return nullptr; } return &(it->second); } void update(int x, int y, uint64_t cid) { m_geo_list.update(x, y, cid); } bool send_light_ctrl(const int& light_id, const int& type, const int& shape){ if(light_id > 0 && m_send_callback){ m_send_callback(light_id, type, shape); } return true; } void set_send_callback(const traffic_send_callback& cb) { m_send_callback = std::move(cb); } private: bool m_stop; std::list m_list_data; std::mutex m_mutex; std::condition_variable m_condition; std::unique_ptr m_thread_light; std::unique_ptr m_crossing_rule; // 路口规则 std::unique_ptr m_avoidance_rule; // 避让规则 traffic_send_callback m_send_callback; geo_list m_geo_list; hashmap_light m_unmap_lights; hashmap_group m_unmap_groups; std::map m_map_card; }; #endif