server.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. RadarEventServer *RadarServer
  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 = NewCameraEventServer()
  29. gopool.Go(is.CameraEventServer.Start)
  30. }
  31. if util.Config.Server.SupportRadar {
  32. is.RadarEventServer = NewRadarEventServer()
  33. gopool.Go(is.RadarEventServer.Start)
  34. }
  35. //等事件服务先启动
  36. time.Sleep(1 * time.Second)
  37. is.Serve()
  38. }
  39. type MainNotifier struct{ s *IntersectionServer }
  40. // Notify 主路来车,通知支路设备
  41. func (m MainNotifier) Notify() {
  42. m.s.Main.Reset(5 * time.Second)
  43. if m.s.MainState != 1 {
  44. m.s.MainState = 1
  45. for _, v := range m.s.SubDevices {
  46. gopool.Go(v.Call)
  47. }
  48. }
  49. }
  50. type SubNotifier struct{ s *IntersectionServer }
  51. // Notify 支路来车,通知主路设备
  52. func (sub SubNotifier) Notify() {
  53. sub.s.Sub.Reset(5 * time.Second)
  54. if sub.s.SubState != 1 {
  55. sub.s.SubState = 1
  56. for _, v := range sub.s.MainDevices {
  57. gopool.Go(v.Call)
  58. }
  59. }
  60. }
  61. func (is *IntersectionServer) Serve() {
  62. if util.Config.Server.SupportCamera {
  63. is.CameraEventServer.RegisterCallback(1, &SubNotifier{is})
  64. is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
  65. }
  66. if util.Config.Server.SupportRadar {
  67. is.RadarEventServer.RegisterCallback(1, &SubNotifier{is})
  68. is.RadarEventServer.RegisterCallback(0, &MainNotifier{is})
  69. }
  70. //先创建响应设备
  71. for _, c := range util.Config.Screens {
  72. iDevice := &IntersectionDevice{
  73. Info: OutputDeviceInfo{
  74. Name: c.Name,
  75. Ip: c.Ip,
  76. Port: c.Port,
  77. Branch: c.Branch,
  78. },
  79. Screen: NewScreen(c.Name, c.Ip, c.Port),
  80. }
  81. if c.Branch == 1 {
  82. is.MainDevices = append(is.MainDevices, iDevice)
  83. } else {
  84. is.SubDevices = append(is.SubDevices, iDevice)
  85. }
  86. }
  87. for _, c := range util.Config.Speakers {
  88. iDevice := &IntersectionDevice{
  89. Info: OutputDeviceInfo{
  90. Name: c.Name,
  91. Ip: c.Ip,
  92. Branch: c.Branch,
  93. Audio: c.Audio,
  94. },
  95. Speaker: NewIpCast(c.Name, c.Ip, c.Audio, c.Speed, c.Volume),
  96. }
  97. if c.Branch == 1 {
  98. is.MainDevices = append(is.MainDevices, iDevice)
  99. } else {
  100. is.SubDevices = append(is.SubDevices, iDevice)
  101. }
  102. }
  103. for {
  104. select {
  105. case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
  106. for _, v := range is.SubDevices {
  107. if is.MainState == 1 {
  108. gopool.Go(v.Rollback)
  109. }
  110. }
  111. is.MainState = 0
  112. case <-is.Sub.C: //检查支路状态->主路输出设备作出响应
  113. for _, v := range is.MainDevices {
  114. if is.SubState == 1 {
  115. gopool.Go(v.Rollback)
  116. }
  117. }
  118. is.SubState = 0
  119. case <-is.ReTicker.C: //每19s检查并尝试重连
  120. gopool.Go(func() {
  121. for _, v := range is.MainDevices {
  122. gopool.Go(v.Reconnect)
  123. }
  124. })
  125. gopool.Go(func() {
  126. for _, v := range is.SubDevices {
  127. gopool.Go(v.Reconnect)
  128. }
  129. })
  130. }
  131. }
  132. }