module_traffic_light_manager.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef module_traffic_light_manager_h
  2. #define module_traffic_light_manager_h
  3. #include <thread>
  4. #include <functional>
  5. #include "module_traffic_light.h"
  6. #include "module_traffic_light_rule.h"
  7. #include "db_api/CDBCommon.h"
  8. #include "geo_hash.h"
  9. #include "log.h"
  10. using traffic_send_callback = std::function<void(int, int, int)>;
  11. struct traffic_light_callback{
  12. traffic_send_callback m_traffic_send;
  13. traffic_light_callback()
  14. {
  15. m_traffic_send = nullptr;
  16. }
  17. void set_callback(const traffic_send_callback& cb)
  18. {
  19. m_traffic_send = std::move(cb);
  20. }
  21. };
  22. // 红绿灯管理
  23. struct traffic_light_manager{
  24. static traffic_light_manager* instance();
  25. // 从数据库加载数据
  26. void init_light_from_db(int lid = 0);
  27. void init_light_group_from_db(int gid = 0);
  28. // 初始化回调信息
  29. void init(const traffic_send_callback& cb);
  30. // 开启
  31. void start();
  32. // 关闭
  33. void stop();
  34. // 运行
  35. void run();
  36. // 信息处理
  37. void put(const light_message& msg);
  38. // 重新加载红绿灯信息
  39. void handle_reload(const int& gid, const int& lid);
  40. int reload_light(const int& lid);
  41. int reload_group(const int& gid);
  42. // 手动控制红绿灯信息
  43. void handle_manual(const int& gid, const int& ld, const std::string& name, const int& lc);
  44. // 卡数据
  45. void handle_position(pos_data& p);
  46. //查找红绿灯
  47. traffic_light_ptr find_light(const int k)
  48. {
  49. hashmap_light::iterator it = m_unmap_lights.find(k);
  50. if(it != m_unmap_lights.end())
  51. {
  52. return it->second;
  53. }
  54. return nullptr;
  55. }
  56. // 查找红绿灯组
  57. traffic_light_group_ptr find_group(const int k)
  58. {
  59. hashmap_group::iterator it = m_unmap_groups.find(k);
  60. if(it != m_unmap_groups.end())
  61. {
  62. return it->second;
  63. }
  64. return nullptr;
  65. }
  66. // 更新红绿灯数据
  67. void update_light(const int& key, traffic_light_ptr ptl);
  68. // 更新红绿灯组数据
  69. void update_group(const int& key, traffic_light_group_ptr ptlg);
  70. // 查找附近的车
  71. std::vector<uint64_t> find_nearby_vehicle(const point& p,const int& dist, const uint64_t& card_id)
  72. {
  73. return m_geo_list.find_near(p.x, p.y, dist, card_id);
  74. }
  75. pos_data* get_position(const uint64_t& cid)
  76. {
  77. std::map<uint64_t, pos_data>::iterator it = m_map_card.find(cid);
  78. if(it == m_map_card.end())
  79. {
  80. log_error("[traffic_light] 找不到卡,card_id=%lld", cid);
  81. return nullptr;
  82. }
  83. return &(it->second);
  84. }
  85. void update(int x, int y, uint64_t cid)
  86. {
  87. m_geo_list.update(x, y, cid);
  88. }
  89. bool send_light_ctrl(const int& light_id, const int& type, const int& shape){
  90. if(light_id > 0 && m_send_callback){
  91. m_send_callback(light_id, type, shape);
  92. }
  93. return true;
  94. }
  95. void set_send_callback(const traffic_send_callback& cb)
  96. {
  97. m_send_callback = std::move(cb);
  98. }
  99. private:
  100. bool m_stop;
  101. std::list<light_message> m_list_data;
  102. std::mutex m_mutex;
  103. std::condition_variable m_condition;
  104. std::unique_ptr<std::thread> m_thread_light;
  105. std::unique_ptr<crossing_rule> m_crossing_rule; // 路口规则
  106. std::unique_ptr<avoidance_rule> m_avoidance_rule; // 避让规则
  107. traffic_send_callback m_send_callback;
  108. geo_list m_geo_list;
  109. hashmap_light m_unmap_lights;
  110. hashmap_group m_unmap_groups;
  111. std::map<uint64_t, pos_data> m_map_card;
  112. };
  113. #endif