module_traffic_light_manager.h 9.1 KB

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