module_traffic_light_manager.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include "websocket/sio/sio_client.h"
  11. #include "common_tool.h"
  12. #include "crc.h"
  13. #include "protocol.h"
  14. #include "websocket/wsTimerThread.h"
  15. // 红绿灯管理
  16. struct traffic_light_manager{
  17. static traffic_light_manager* instance();
  18. // 从数据库加载数据
  19. void init_light_from_db(int lid = 0);
  20. void init_light_group_from_db(int gid = 0);
  21. void init_light_group_path();
  22. void init_map(int id = 0);
  23. // 初始化回调信息
  24. void init(const int& lgc);
  25. // 开启
  26. void start();
  27. // 关闭
  28. void stop();
  29. // 运行
  30. void run();
  31. // 信息处理
  32. void put(const light_message& msg);
  33. // 重新加载红绿灯信息
  34. void handle_reload(const int& gid, const int& lid);
  35. int reload_light(const int& lid);
  36. int reload_group(const int& gid);
  37. // 手动控制红绿灯信息
  38. void manual_ctrl(sio::message::ptr const& data);
  39. void set_manual(light_message& msg);
  40. void cancel_manual(light_message& msg);
  41. void handle_manual(const int& gid, const int& ld, const std::string& name, const int& lc);
  42. // 卡数据
  43. void handle_position(pos_data& p);
  44. traffic_light_ptr get(const int& id)
  45. {
  46. return find_light(id);
  47. }
  48. //查找红绿灯
  49. traffic_light_ptr find_light(const int k)
  50. {
  51. hashmap_light::iterator it = m_unmap_lights.find(k);
  52. if(it != m_unmap_lights.end())
  53. {
  54. return it->second;
  55. }
  56. return nullptr;
  57. }
  58. // 查找红绿灯组
  59. traffic_light_group_ptr find_group(const int k)
  60. {
  61. hashmap_group::iterator it = m_unmap_groups.find(k);
  62. if(it != m_unmap_groups.end())
  63. {
  64. return it->second;
  65. }
  66. return nullptr;
  67. }
  68. std::vector<traffic_light_group_ptr> find_stream_groups(traffic_light_group_ptr, const move_stream& stream);
  69. // 更新红绿灯数据
  70. void update_light(const int& key, traffic_light_ptr ptl);
  71. // 更新红绿灯组数据
  72. void update_group(const int& key, traffic_light_group_ptr ptlg);
  73. // 查找附近的车
  74. std::vector<uint64_t> find_nearby_vehicle(const point& p,const int& dist, const uint64_t& card_id)
  75. {
  76. return m_geo_list.find_near(p.x, p.y, dist, card_id);
  77. }
  78. pos_data* get_position(const uint64_t& cid)
  79. {
  80. std::map<uint64_t, pos_data>::iterator it = m_map_card.find(cid);
  81. if(it == m_map_card.end())
  82. {
  83. log_error("[traffic_light] 找不到卡,card_id=%lld", cid);
  84. return nullptr;
  85. }
  86. return &(it->second);
  87. }
  88. void update(int x, int y, uint64_t cid)
  89. {
  90. m_geo_list.update(x, y, cid);
  91. }
  92. void vtlg_insert(const uint64_t& cid, traffic_light_group_ptr& group)
  93. {
  94. std::lock_guard<std::mutex> lg(m_vtlg_mutex);
  95. auto it = m_vehicle_traffic_groups.find(cid);
  96. if(it == m_vehicle_traffic_groups.end()){
  97. m_vehicle_traffic_groups.insert(std::make_pair(cid, std::list<traffic_light_group_ptr>()));
  98. it = m_vehicle_traffic_groups.find(cid);
  99. }
  100. if(it->second.size() >= m_light_group_ctrl_num){
  101. auto itg = it->second.front();
  102. itg->reset();
  103. it->second.pop_front();
  104. }
  105. it->second.push_back(group);
  106. }
  107. void vtlg_erase(const uint64_t& cid, traffic_light_group_ptr& group)
  108. {
  109. std::lock_guard<std::mutex> lg(m_vtlg_mutex);
  110. auto it = m_vehicle_traffic_groups.find(cid);
  111. if(it != m_vehicle_traffic_groups.end()){
  112. for(auto it_g = it->second.begin(); it_g != it->second.end();++it_g){
  113. if((*it_g)->m_group_id == group->m_group_id){
  114. group->reset();
  115. it->second.erase(it_g);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. bool vtlg_exist(const uint64_t& cid, const int& gid)
  122. {
  123. auto it = m_vehicle_traffic_groups.find(cid);
  124. if(it == m_vehicle_traffic_groups.end()){
  125. return false;
  126. }
  127. for(auto it_g = it->second.begin(); it_g != it->second.end();++it_g){
  128. if((*it_g)->m_group_id == gid){
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. bool vtlg_exist(const int& gid)
  135. {
  136. bool ret = false;
  137. for(auto it : m_vehicle_traffic_groups)
  138. {
  139. if(vtlg_exist(it.first, gid)){
  140. ret = true;
  141. break;
  142. }
  143. }
  144. return ret;
  145. }
  146. int vtlg_size(const uint64_t& cid)
  147. {
  148. auto it = m_vehicle_traffic_groups.find(cid);
  149. if(it == m_vehicle_traffic_groups.end()){
  150. return 0;
  151. }
  152. return it->second.size();
  153. }
  154. bool send_light_data(const int& light_id, const int& type, const int& shape){
  155. std::vector<char> msg;
  156. uint16_t cmd = THIRD_PARTY_CHAR_LIGHT_SETUP_STATE;
  157. tool_other::memcpy_int<uint16_t>(msg, cmd);
  158. tool_other::memcpy_int<uint32_t>(msg, light_id);
  159. msg.push_back(0x05);
  160. msg.push_back(static_cast<char>(shape));
  161. int16_t len = msg.size() + 2;
  162. int16_t crc = do_crc_1(msg, 0);
  163. auto it = msg.begin();
  164. msg.insert(it, len&0xff);
  165. it = msg.begin();
  166. msg.insert(it, (len>>8)&0xff);
  167. tool_other::memcpy_int<int16_t>(msg, crc);
  168. auto light_ptr = get(light_id);
  169. log_info("[light_info] ctrl light, light_id=%d, message's length=%d, light_ptr exist=%s", light_id, msg.size(), (light_ptr==nullptr)?"empty":"yes");
  170. std::vector<char> m = msg;
  171. output_message(std::move(m), light_id);
  172. if(light_ptr && light_ptr->m_clt){
  173. light_ptr->m_clt->send(std::move(msg));
  174. }
  175. return true;
  176. }
  177. void output_message(std::vector<char>&& msg, int light_id)
  178. {
  179. std::string s("红绿灯控制的数据帧,灯号=");
  180. s.append(std::to_string(light_id));
  181. s.append(":");
  182. char a[4] = {0};
  183. for(std::vector<char>::size_type i = 0;i < msg.size(); ++i){
  184. sprintf(a, "%02X ", static_cast<unsigned char>(msg[i]));
  185. s.append(std::string(a));
  186. }
  187. log_info("%s", s.c_str());
  188. }
  189. void output_upstreamidx()
  190. {
  191. std::string msg = "红绿灯上行排序顺序(灯组id, 排序索引):";
  192. for(auto it : m_map_groups){
  193. char buf[20] = {0};
  194. sprintf(buf, "(%d,%d)", it.second->m_group_id, it.first);
  195. msg.append(std::string(buf));
  196. }
  197. log_info("%s", msg.c_str());
  198. }
  199. int remove_light(const int& id);
  200. int remove_light_group(const int& id);
  201. void set_scale(const double& scale)
  202. {
  203. m_map_scale = scale;
  204. }
  205. double get_scale()
  206. {
  207. return m_map_scale;
  208. }
  209. std::string get_light_state();
  210. bool on_path(const std::vector<pos_data>& vtp, const point& p);
  211. void visit_light_status();
  212. private:
  213. bool m_stop;
  214. double m_map_scale = 0.0;
  215. unsigned int m_light_group_ctrl_num{0}; // 控制红绿灯组数
  216. std::list<light_message> m_list_data;
  217. std::mutex m_mutex;
  218. std::mutex m_vtlg_mutex;
  219. std::condition_variable m_condition;
  220. std::unique_ptr<std::thread> m_thread_light;
  221. std::unique_ptr<crossing_rule> m_crossing_rule; // 路口规则
  222. std::unique_ptr<avoidance_rule> m_avoidance_rule; // 避让规则
  223. geo_list m_geo_list;
  224. hashmap_light m_unmap_lights;
  225. hashmap_group m_unmap_groups;
  226. map_group m_map_groups;
  227. std::map<uint64_t, pos_data> m_map_card;
  228. std::map<int, std::vector<line_v>> m_map_path;
  229. std::map<uint64_t, std::list<traffic_light_group_ptr>> m_vehicle_traffic_groups;
  230. sys::jsonBuilder m_jsBuilder;//json构造器类
  231. };
  232. #endif