module_traffic_light.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. for(auto it = m_vt_lights.begin(); it != m_vt_lights.end(); ++it){
  31. auto ptl = traffic_light_manager::instance()->find_light((*it)->m_light_id);
  32. if(ptl != nullptr){
  33. if(ptl->m_light_id == ld){
  34. ptl->set_state(lc);
  35. send_cmd_light(ptl);
  36. }else{
  37. ptl->set_state(lcr);
  38. send_cmd_light(ptl);
  39. }
  40. break;
  41. }
  42. }
  43. }
  44. bool traffic_light_group::is_different(const int& ld, const int& lc, const int& lcr)
  45. {
  46. bool ret = false;
  47. get_turn();
  48. if(get_priority() <= priority_avoidance)
  49. {
  50. for(auto it = m_vt_lights.begin(); it != m_vt_lights.end(); ++it)
  51. {
  52. if((*it)->m_light_id == ld)
  53. {
  54. if((*it)->m_state != lc)
  55. {
  56. ret = true;
  57. break;
  58. }
  59. }else{
  60. if((*it)->m_state != lcr)
  61. {
  62. ret = true;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. if(ret){
  69. set_avoidance(ld, lc, lcr);
  70. }
  71. release_turn();
  72. return ret;
  73. }
  74. void traffic_light_group::send_cmd_light(traffic_light_ptr& ptl)
  75. {
  76. if(nullptr != ptl){
  77. // 控制码
  78. int shape = 0;
  79. if(red == ptl->m_state){
  80. shape = red_circle_solid;
  81. }else if(green == ptl->m_state){
  82. shape = green_down;
  83. }else{
  84. shape = green_spark;
  85. }
  86. traffic_light_manager::instance()->send_light_ctrl(ptl->m_light_id, DT_LIGHT, shape);
  87. }
  88. }
  89. void traffic_light_group::set_manual_ctrl(const std::string& name, const int& ld, const int& lc)
  90. {
  91. get_turn();
  92. m_ctrl_name = name;
  93. m_time_stamp = time(NULL);
  94. set_priority(priority_manual_ctrl);
  95. m_card_id = 0;
  96. auto ptl = std::find_if(m_vt_lights.begin(), m_vt_lights.end(),[=](traffic_light_ptr lp){
  97. if(lp->m_light_id == ld)
  98. {
  99. return true;
  100. }else{
  101. return false;
  102. }
  103. });
  104. if(ptl != m_vt_lights.end()){
  105. (*ptl)->set_state(lc);
  106. }
  107. release_turn();
  108. }