module_traffic_light.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef module_traffic_light_h
  2. #define module_traffic_light_h
  3. #include <memory>
  4. #include <ctime>
  5. #include <atomic>
  6. #include <vector>
  7. #include <list>
  8. #include <array>
  9. #include "ant.h"
  10. #include "module_traffic_light_common.h"
  11. #include "line.h"
  12. class client;
  13. // 红绿灯
  14. struct traffic_light: point{
  15. int m_light_id; // 红绿灯id
  16. int m_group_id; // 所属红绿灯组
  17. std::string m_ip; // 红绿灯IP
  18. int m_site_id; // 控制分站id
  19. int m_state; // 红绿灯状态,1-初始值,2-红灯,3-绿灯,4-闪烁
  20. int m_state_last = 0; // 状态有变化,立马更新状态,否则隔30秒再更新一次
  21. int m_port; // 分站控制红绿灯的路数
  22. int m_phy_light_id; // 物理灯号,红绿灯正反面共享此灯号
  23. int m_phy_direction; // 物理灯朝向,1-正面,0-反面
  24. uint64_t m_rec_time; // 红绿灯接收时间
  25. int m_special; // 检查非巷道区域,0-正常,1-特殊
  26. double m_direct_distance; // 红绿灯到灯组坐标的距离
  27. line m_line_group; // 红绿灯与灯组坐标的连线
  28. int m_stream; // 上下行标志,1为上行,2为下行
  29. int m_area_id;
  30. int m_map_id;
  31. int m_device_type_id; // 红绿灯设备类型号
  32. std::shared_ptr<client> m_clt;
  33. traffic_light(): m_light_id(0), m_group_id(0), m_ip(""), m_state(0), m_port(0), m_phy_light_id(0), m_phy_direction(0), m_rec_time(0), m_special(0), m_direct_distance(0.0), m_stream(0), m_area_id(0), m_map_id(0)
  34. {
  35. m_device_type_id=3;
  36. m_clt = nullptr;
  37. }
  38. traffic_light(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special, int stream): point(x, y, z), m_light_id(light_id), m_group_id(group_id), m_ip(ip), m_state(state), m_port(port), m_phy_light_id(pli), m_phy_direction(pd), m_special(special), m_stream(stream)
  39. {
  40. m_device_type_id=3;
  41. m_clt = nullptr;
  42. }
  43. void update(double x, double y, double z, int light_id, int group_id, std::string ip, int state, int port, int pli, int pd, int special, int stream)
  44. {
  45. this->x = x;
  46. this->y = y;
  47. this->z = z;
  48. this->m_light_id = light_id;
  49. this->m_group_id = group_id;
  50. this->m_ip = ip;
  51. this->m_state = state;
  52. this->m_port = port;
  53. this->m_phy_light_id = pli;
  54. this->m_phy_direction = pd;
  55. this->m_special = special;
  56. this->m_stream = stream;
  57. }
  58. void set_state(int value = light_shape::green_all_on)
  59. {
  60. m_state = value;
  61. }
  62. void set_client(const std::shared_ptr<client>& clt)
  63. {
  64. if(m_clt != clt){
  65. m_clt = clt;
  66. }
  67. }
  68. std::shared_ptr<client> get_client()
  69. {
  70. return m_clt;
  71. }
  72. };
  73. using traffic_light_ptr = std::shared_ptr<traffic_light>;
  74. class traffic_light_group;
  75. using traffic_light_group_ptr = std::shared_ptr<traffic_light_group>;
  76. //红绿灯组
  77. struct traffic_light_group: point{
  78. int m_group_id; // 所属红绿灯组ID
  79. double m_scope; // 控制半径
  80. uint16_t m_manual_time; // 人工控制默认时间
  81. uint16_t m_auto_interval; // 断网情况下,自动控制间隔
  82. uint64_t m_card_id; // 如果优先级是1,表示小车id;优先级是2,表示打车id
  83. int m_map_id; // 地图ID
  84. int m_area_id; // 区域ID
  85. time_t m_time_stamp; // 控制时长,单位秒
  86. time_t m_ctrl_time; // 控制时间
  87. bool m_special; // 是否含有特殊红绿灯组
  88. int m_green_light_id; // 灯组被控制后变绿灯的灯ID
  89. std::string m_ctrl_name; // 控制用户
  90. std::atomic<int> m_priority; // 优先级,0-初始值,1-路口控制,2-避让控制;3-手动控制
  91. std::atomic<bool> m_used; // 是否使用
  92. std::atomic_flag m_owner_flag; // 控制权
  93. std::vector<traffic_light_ptr> m_vt_lights; // 组下红绿灯
  94. std::array<path, 2> m_path;
  95. int m_down_stream_idx; // 灯组按下行排序的索引值
  96. move_stream m_stream_state;
  97. traffic_light_group(): point(0,0), m_group_id(0), m_scope(0.0), m_manual_time(0), m_auto_interval(0), m_card_id(0), m_map_id(0), m_area_id(0), m_time_stamp(0), m_ctrl_time(0),m_special(false), m_green_light_id(0), m_ctrl_name(""), m_priority(priority_init), m_used(false)
  98. {
  99. m_vt_lights.clear();
  100. m_down_stream_idx = 0;
  101. m_owner_flag.clear();
  102. m_stream_state = move_stream::unknown;
  103. }
  104. traffic_light_group(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id): point(x, y, z), m_group_id(group_id), m_scope(scope), m_auto_interval(interval), m_map_id(map_id), m_area_id(area_id)
  105. {
  106. m_priority = priority_init;
  107. m_vt_lights.clear();
  108. m_used = false;
  109. m_card_id = 0;
  110. m_down_stream_idx = 0;
  111. m_owner_flag.clear();
  112. m_stream_state = move_stream::unknown;
  113. }
  114. // 给红绿灯下发指令设置其颜色
  115. void send_cmd_light(traffic_light_ptr& ptl);
  116. void set_path(const std::vector<line_v>& vl);
  117. void set_path(const std::vector<line_v>& vl, std::vector<line_v>::const_iterator it);
  118. void set_crossing(const uint64_t& cid, const int& p)
  119. {
  120. m_card_id = cid;
  121. set_priority(p);
  122. }
  123. bool is_at_path(point p);
  124. // 设置避让
  125. void set_avoidance(const int& ld, const int& lc, const int& lcr)
  126. {
  127. m_card_id = 0;
  128. m_time_stamp = 0;
  129. set_priority(priority_avoidance);
  130. set_light(ld, lc, lcr);
  131. }
  132. // 设置优先权
  133. void set_priority(const int& p)
  134. {
  135. m_priority.store(p);
  136. }
  137. void set_down_stream_idx(const int& v)
  138. {
  139. m_down_stream_idx = v;
  140. }
  141. // 获取优先权
  142. int get_priority()
  143. {
  144. return m_priority.load();
  145. }
  146. // 设置状态
  147. void set_status(bool s)
  148. {
  149. m_used.store(s);
  150. }
  151. // 得到状态
  152. bool get_status()
  153. {
  154. return m_used.load();
  155. }
  156. move_stream get_stream_state()
  157. {
  158. return m_stream_state;
  159. }
  160. void set_stream_state(const move_stream& state)
  161. {
  162. m_stream_state = state;
  163. }
  164. move_stream get_stream(const int& cid);
  165. // 控制超时检查
  166. bool is_time_out()
  167. {
  168. if(m_time_stamp >= m_ctrl_time){
  169. return true;
  170. }
  171. return false;
  172. }
  173. void get_turn()
  174. {
  175. while(m_owner_flag.test_and_set(std::memory_order_acquire));
  176. }
  177. void release_turn()
  178. {
  179. m_owner_flag.clear(std::memory_order_release);
  180. }
  181. // 重置
  182. void reset();
  183. //设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状
  184. void set_light(const int& lid, const int& lc, const int& lcr);
  185. // 增加红绿灯到灯组
  186. void insert(traffic_light_ptr ptl);
  187. // 检查灯在组内是否存在
  188. bool exist(const int& lid);
  189. //获取两个灯之间的距离
  190. bool is_different(const int& ld, const int& lc, const int& lcr);
  191. // 手动控制
  192. void set_manual_ctrl(const std::string& name, const int& ld, const int& lc);
  193. uint64_t get_green_light_id();
  194. // 上行灯与下行灯之间的距离,带方向
  195. double updown_dist();
  196. int size()
  197. {
  198. return m_vt_lights.size();
  199. }
  200. void update(double x, double y, double z, int group_id, double scope, int interval, int map_id, int area_id)
  201. {
  202. this->x = x;
  203. this->y = y;
  204. this->z = z;
  205. this->m_group_id = group_id;
  206. this->m_scope = scope;
  207. this->m_auto_interval = interval;
  208. this->m_map_id = map_id;
  209. this->m_area_id = area_id;
  210. }
  211. };
  212. struct traffic_light_data{
  213. bool m_bigger_flag; //大车标识
  214. std::list<traffic_light_ptr> m_traffic_group;
  215. traffic_light_data(): m_bigger_flag(false){
  216. m_traffic_group.clear();
  217. }
  218. };
  219. // 闯红灯
  220. struct run_red_light{
  221. int32_t m_group_id;
  222. int m_light_id;
  223. int m_vid;
  224. int32_t m_area_id;
  225. std::string m_occ_time; // 发生时间
  226. run_red_light(): m_group_id(0), m_light_id(0), m_vid(0), m_area_id(0), m_occ_time(0)
  227. {}
  228. };
  229. #endif