server.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. CameraEventServer *CameraServer
  12. State byte
  13. Devices []IDevice
  14. ReTicker *time.Ticker
  15. Main *time.Ticker
  16. }
  17. func StartIntersectionServer() {
  18. is := &IntersectionServer{
  19. Main: time.NewTicker(5 * time.Second), //主路状态回滚
  20. ReTicker: time.NewTicker(30 * time.Second), //重连
  21. }
  22. if util.Config.Server.SupportCamera {
  23. is.CameraEventServer = NewCameraEventServer()
  24. gopool.Go(is.CameraEventServer.Start)
  25. }
  26. //等事件服务先启动
  27. time.Sleep(1 * time.Second)
  28. is.Serve()
  29. }
  30. type MainNotifier struct{ s *IntersectionServer }
  31. // Notify 主路来车,通知支路设备
  32. func (m MainNotifier) Notify() {
  33. m.s.Main.Reset(5 * time.Second)
  34. if m.s.State != 1 {
  35. m.s.State = 1
  36. for _, v := range m.s.Devices {
  37. gopool.Go(v.Call)
  38. }
  39. }
  40. }
  41. func (is *IntersectionServer) Serve() {
  42. if util.Config.Server.SupportCamera {
  43. is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
  44. }
  45. //先创建响应设备
  46. for _, c := range util.Config.Screens {
  47. iDevice := &IntersectionDevice{
  48. Info: OutputDeviceInfo{
  49. Name: c.Name,
  50. Ip: c.Ip,
  51. Port: c.Port,
  52. Branch: c.Branch,
  53. },
  54. Screen: NewScreen(c.Name, c.Ip, c.Port),
  55. }
  56. is.Devices = append(is.Devices, iDevice)
  57. }
  58. for {
  59. select {
  60. case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
  61. for _, v := range is.Devices {
  62. if is.State == 1 {
  63. gopool.Go(v.Rollback)
  64. }
  65. }
  66. is.State = 0
  67. }
  68. }
  69. }