123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #ifndef module_traffic_light_manager_h
- #define module_traffic_light_manager_h
- #include <thread>
- #include <functional>
- #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<void(int, int, int)>;
- 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<uint64_t> 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<uint64_t, pos_data>::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<light_message> m_list_data;
- std::mutex m_mutex;
- std::condition_variable m_condition;
- std::unique_ptr<std::thread> m_thread_light;
- std::unique_ptr<crossing_rule> m_crossing_rule; // 路口规则
- std::unique_ptr<avoidance_rule> 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<uint64_t, pos_data> m_map_card;
- };
- #endif
|