server.go 2.9 KB

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