module_traffic_light.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <algorithm>
  2. #include "module_traffic_light.h"
  3. #include "module_traffic_light_manager.h"
  4. void traffic_light_group::reset()
  5. {
  6. m_time_stamp = 0;
  7. m_ctrl_name = "";
  8. m_card_id = 0;
  9. set_status(false);
  10. set_priority(priority_init);
  11. for(auto it = m_vt_lights.begin();it != m_vt_lights.end(); ++it){
  12. auto tl = traffic_light_manager::instance()->find_light((*it)->m_phy_light_id);
  13. if(tl != nullptr){
  14. tl->set_state();
  15. }
  16. }
  17. }
  18. void traffic_light_group::insert(traffic_light_ptr ptl)
  19. {
  20. ptl->m_direct_distance = this->dist_direct(*ptl);
  21. line tmp(*this, *ptl);
  22. ptl->m_line_group = tmp;
  23. m_vt_lights.push_back(ptl);
  24. if(1 == ptl->m_special){
  25. m_special = 1;
  26. }
  27. }
  28. void traffic_light_group::set_light(const int& ld, const int& lc, const int& lcr)
  29. {
  30. log_info("[traffic_light] set light group status, light_id=%d, status=%d, reverse_status=%d",ld, lc, lcr);
  31. for(auto it = m_vt_lights.begin(); it != m_vt_lights.end(); ++it){
  32. auto ptl = traffic_light_manager::instance()->find_light((*it)->m_light_id);
  33. if(ptl != nullptr){
  34. if(ptl->m_light_id == ld){
  35. ptl->set_state(lc);
  36. send_cmd_light(ptl);
  37. }else{
  38. ptl->set_state(lcr);
  39. send_cmd_light(ptl);
  40. }
  41. break;
  42. }
  43. }
  44. }
  45. bool traffic_light_group::is_different(const int& ld, const int& lc, const int& lcr)
  46. {
  47. bool ret = false;
  48. get_turn();
  49. if(get_priority() <= priority_avoidance)
  50. {
  51. for(auto it = m_vt_lights.begin(); it != m_vt_lights.end(); ++it)
  52. {
  53. if((*it)->m_light_id == ld)
  54. {
  55. if((*it)->m_state != lc)
  56. {
  57. ret = true;
  58. break;
  59. }
  60. }else{
  61. if((*it)->m_state != lcr)
  62. {
  63. ret = true;
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. if(ret){
  70. set_avoidance(ld, lc, lcr);
  71. }
  72. release_turn();
  73. return ret;
  74. }
  75. void traffic_light_group::send_cmd_light(traffic_light_ptr& ptl)
  76. {
  77. if(nullptr != ptl){
  78. // 控制码
  79. int shape = 0;
  80. if(red == ptl->m_state){
  81. shape = red_circle_solid;
  82. }else if(green == ptl->m_state){
  83. shape = green_down;
  84. }else{
  85. shape = green_spark;
  86. }
  87. traffic_light_manager::instance()->send_light_ctrl(ptl->m_light_id, DT_LIGHT, shape);
  88. }
  89. }
  90. void traffic_light_group::set_manual_ctrl(const std::string& name, const int& ld, const int& lc)
  91. {
  92. get_turn();
  93. m_ctrl_name = name;
  94. m_time_stamp = time(NULL);
  95. set_priority(priority_manual_ctrl);
  96. m_card_id = 0;
  97. auto ptl = std::find_if(m_vt_lights.begin(), m_vt_lights.end(),[=](traffic_light_ptr lp){
  98. if(lp->m_light_id == ld)
  99. {
  100. return true;
  101. }else{
  102. return false;
  103. }
  104. });
  105. if(ptl != m_vt_lights.end()){
  106. (*ptl)->set_state(lc);
  107. }
  108. release_turn();
  109. }