server.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. RadarEventServer *RadarServer
  13. CameraEventServer *CameraServer
  14. State 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. ReTicker: time.NewTicker(30 * time.Second), //重连
  25. }
  26. if util.Config.Server.SupportCamera {
  27. is.CameraEventServer = NewCameraEventServer()
  28. gopool.Go(is.CameraEventServer.Start)
  29. }
  30. if util.Config.Server.SupportRadar {
  31. is.RadarEventServer = NewRadarEventServer()
  32. gopool.Go(is.RadarEventServer.Start)
  33. }
  34. //等事件服务先启动
  35. time.Sleep(1 * time.Second)
  36. is.Serve()
  37. }
  38. type MainNotifier struct{ s *IntersectionServer }
  39. // Notify 主路来车,通知支路设备
  40. func (m MainNotifier) Notify(id string) {
  41. logrus.Errorf("Noitfy m.s.State = %d", m.s.State)
  42. logrus.Errorf("Noitfy dev = %v", m.s.SubDevices)
  43. logrus.Errorf("Noitfy dev = %v", m.s.MainDevices)
  44. m.s.Main.Reset(5 * time.Second)
  45. if m.s.State != 1 {
  46. m.s.State = 1
  47. if id == "1" || id == "2" {
  48. for _, v := range m.s.SubDevices {
  49. gopool.Go(v.Call)
  50. }
  51. } else if id == "3" || id == "4" {
  52. for _, v := range m.s.MainDevices {
  53. gopool.Go(v.Call)
  54. }
  55. }
  56. }
  57. }
  58. func (is *IntersectionServer) Serve() {
  59. if util.Config.Server.SupportCamera {
  60. is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
  61. }
  62. //先创建响应设备
  63. for _, c := range util.Config.Screens {
  64. iDevice := &IntersectionDevice{
  65. Info: OutputDeviceInfo{
  66. Name: c.Name,
  67. Ip: c.Ip,
  68. Port: c.Port,
  69. Branch: c.Branch,
  70. },
  71. Screen: NewScreen(c.Name, c.Ip, c.Port, c.Branch),
  72. }
  73. if c.Branch == 1 {
  74. is.MainDevices = append(is.MainDevices, iDevice)
  75. } else {
  76. is.SubDevices = append(is.SubDevices, iDevice)
  77. }
  78. }
  79. for _, c := range util.Config.Speakers {
  80. iDevice := &IntersectionDevice{
  81. Info: OutputDeviceInfo{
  82. Name: c.Name,
  83. Ip: c.Ip,
  84. Branch: c.Branch,
  85. Audio: c.Audio,
  86. },
  87. Speaker: NewIpCast(c.Name, c.Ip, c.Audio, c.Speed, c.Volume),
  88. }
  89. if c.Branch == 1 {
  90. is.MainDevices = append(is.MainDevices, iDevice)
  91. } else {
  92. is.SubDevices = append(is.SubDevices, iDevice)
  93. }
  94. }
  95. for {
  96. select {
  97. case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
  98. for _, v := range is.SubDevices {
  99. if is.State == 1 {
  100. gopool.Go(v.Rollback)
  101. }
  102. }
  103. for _, v := range is.MainDevices {
  104. if is.State == 1 {
  105. gopool.Go(v.Rollback)
  106. }
  107. }
  108. is.State = 0
  109. }
  110. }
  111. }