server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package lc
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "lc-smartX/util"
  5. "lc-smartX/util/gopool"
  6. "time"
  7. )
  8. type SmartXServer interface {
  9. Serve()
  10. }
  11. type IntersectionServer struct {
  12. RadioEventServer *RadioServer
  13. CameraEventServer *CameraServer
  14. MainState byte
  15. SubState byte
  16. MainDevices []IDevice
  17. SubDevices []IDevice
  18. ReTicker *time.Ticker
  19. Main *time.Ticker
  20. Sub *time.Ticker
  21. }
  22. func StartIntersectionServer() {
  23. is := &IntersectionServer{
  24. Main: time.NewTicker(5 * time.Second), //主路状态回滚
  25. Sub: time.NewTicker(5 * time.Second), //支路状态回滚
  26. ReTicker: time.NewTicker(30 * time.Second), //重连
  27. }
  28. if util.Config.Server.SupportCamera {
  29. is.CameraEventServer = NewCameraEventServer()
  30. gopool.Go(is.CameraEventServer.Start)
  31. }
  32. if util.Config.Server.SupportRadio {
  33. is.RadioEventServer = NewRadioEventServer()
  34. gopool.Go(is.RadioEventServer.Start)
  35. }
  36. //等事件服务先启动
  37. time.Sleep(1 * time.Second)
  38. is.Serve()
  39. }
  40. type MainNotifier struct{ s *IntersectionServer }
  41. // Notify 主路来车,通知支路设备
  42. func (m MainNotifier) Notify() {
  43. m.s.Main.Reset(5 * time.Second)
  44. if m.s.MainState != 1 {
  45. m.s.MainState = 1
  46. for _, v := range m.s.SubDevices {
  47. logrus.Debugf("SubDevices v = %+v", v)
  48. gopool.Go(v.Call)
  49. }
  50. }
  51. }
  52. type SubNotifier struct{ s *IntersectionServer }
  53. // Notify 支路来车,通知主路设备
  54. func (sub SubNotifier) Notify() {
  55. sub.s.Sub.Reset(5 * time.Second)
  56. if sub.s.SubState != 1 {
  57. sub.s.SubState = 1
  58. for _, v := range sub.s.MainDevices {
  59. gopool.Go(v.Call)
  60. }
  61. }
  62. }
  63. func (is *IntersectionServer) Serve() {
  64. if util.Config.Server.SupportCamera {
  65. is.CameraEventServer.RegisterCallback(1, &SubNotifier{is})
  66. is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
  67. }
  68. if util.Config.Server.SupportRadio {
  69. is.RadioEventServer.RegisterCallback(1, &SubNotifier{is})
  70. is.RadioEventServer.RegisterCallback(0, &MainNotifier{is})
  71. }
  72. //先创建响应设备
  73. for _, c := range util.Config.Screens {
  74. iDevice := &IntersectionDevice{
  75. Info: OutputDeviceInfo{
  76. Name: c.Name,
  77. Ip: c.Ip,
  78. Port: c.Port,
  79. Branch: c.Branch,
  80. },
  81. Screen: NewScreen(c.Name, c.Ip, c.Port),
  82. }
  83. if c.Branch == 1 {
  84. is.MainDevices = append(is.MainDevices, iDevice)
  85. } else {
  86. is.SubDevices = append(is.SubDevices, iDevice)
  87. }
  88. }
  89. for _, c := range util.Config.Speakers {
  90. iDevice := &IntersectionDevice{
  91. Info: OutputDeviceInfo{
  92. Name: c.Name,
  93. Ip: c.Ip,
  94. Branch: c.Branch,
  95. Audio: c.Audio,
  96. },
  97. Speaker: NewIpCast(c.Name, c.Ip, c.Audio, c.Speed, c.Volume),
  98. }
  99. if c.Branch == 1 {
  100. is.MainDevices = append(is.MainDevices, iDevice)
  101. } else {
  102. is.SubDevices = append(is.SubDevices, iDevice)
  103. }
  104. }
  105. for {
  106. select {
  107. case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
  108. for _, v := range is.SubDevices {
  109. if is.MainState == 1 {
  110. gopool.Go(v.Rollback)
  111. }
  112. }
  113. is.MainState = 0
  114. case <-is.Sub.C: //检查支路状态->主路输出设备作出响应
  115. for _, v := range is.MainDevices {
  116. if is.SubState == 1 {
  117. gopool.Go(v.Rollback)
  118. }
  119. }
  120. is.SubState = 0
  121. case <-is.ReTicker.C: //每19s检查并尝试重连
  122. gopool.Go(func() {
  123. for _, v := range is.MainDevices {
  124. gopool.Go(v.Reconnect)
  125. }
  126. })
  127. gopool.Go(func() {
  128. for _, v := range is.SubDevices {
  129. gopool.Go(v.Reconnect)
  130. }
  131. })
  132. }
  133. }
  134. }