server.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. func StartSmartXServer(s SmartXServer) {
  11. s.Serve()
  12. }
  13. type IntersectionServer struct {
  14. MainState byte
  15. SubState byte
  16. MainDevices []IDevice
  17. SubDevices []IDevice
  18. ReTicker *time.Ticker
  19. Main *time.Ticker
  20. Sub *time.Ticker
  21. }
  22. type MainNotifier struct{ s *IntersectionServer }
  23. // Notify 主路来车,通知支路设备
  24. func (m MainNotifier) Notify() {
  25. m.s.Main.Reset(5 * time.Second)
  26. if m.s.MainState != 1 {
  27. m.s.MainState = 1
  28. for _, v := range m.s.SubDevices {
  29. gopool.Go(v.Call)
  30. }
  31. }
  32. }
  33. type SubNotifier struct{ s *IntersectionServer }
  34. // Notify 支路来车,通知主路设备
  35. func (sub SubNotifier) Notify() {
  36. sub.s.Sub.Reset(5 * time.Second)
  37. if sub.s.SubState != 1 {
  38. sub.s.SubState = 1
  39. for _, v := range sub.s.MainDevices {
  40. gopool.Go(v.Call)
  41. }
  42. }
  43. }
  44. func (is *IntersectionServer) Serve() {
  45. RegisterCallback(1, &SubNotifier{is})
  46. RegisterCallback(0, &MainNotifier{is})
  47. //先创建响应设备
  48. for _, v := range util.Config.OutputDevices {
  49. iDevice := &IntersectionDevice{
  50. Info: v,
  51. S: NewScreen(v.Name, v.ScreenIp, v.ScreenPort),
  52. L: NewIpCast(),
  53. }
  54. if iDevice.Info.Branch == 1 {
  55. is.MainDevices = append(is.MainDevices, iDevice)
  56. } else {
  57. is.SubDevices = append(is.SubDevices, iDevice)
  58. }
  59. }
  60. for {
  61. select {
  62. case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
  63. for _, v := range is.SubDevices {
  64. if is.MainState == 1 {
  65. gopool.Go(v.Rollback)
  66. }
  67. }
  68. is.MainState = 0
  69. case <-is.Sub.C: //检查支路状态->主路输出设备作出响应
  70. for _, v := range is.MainDevices {
  71. if is.SubState == 1 {
  72. gopool.Go(v.Rollback)
  73. }
  74. }
  75. is.SubState = 0
  76. case <-is.ReTicker.C: //每19s检查并尝试重连
  77. gopool.Go(func() {
  78. for _, v := range is.MainDevices {
  79. gopool.Go(v.Reconnect)
  80. }
  81. })
  82. gopool.Go(func() {
  83. for _, v := range is.SubDevices {
  84. gopool.Go(v.Reconnect)
  85. }
  86. })
  87. }
  88. }
  89. }
  90. // LServer ###
  91. // ===
  92. // ===
  93. // ===
  94. // === todo 服务器模式没测通,屏没有连接服务器