server.go 3.2 KB

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