server.go 3.6 KB

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