package lc import ( "lc-smartX/util" "lc-smartX/util/gopool" "time" ) type SmartXServer interface { Serve() } type IntersectionServer struct { CameraEventServer *CameraServer State byte Devices []IDevice ReTicker *time.Ticker Main *time.Ticker } func StartIntersectionServer() { is := &IntersectionServer{ Main: time.NewTicker(5 * time.Second), //主路状态回滚 ReTicker: time.NewTicker(30 * time.Second), //重连 } if util.Config.Server.SupportCamera { is.CameraEventServer = NewCameraEventServer() gopool.Go(is.CameraEventServer.Start) } //等事件服务先启动 time.Sleep(1 * time.Second) is.Serve() } type MainNotifier struct{ s *IntersectionServer } // Notify 主路来车,通知支路设备 func (m MainNotifier) Notify() { m.s.Main.Reset(5 * time.Second) if m.s.State != 1 { m.s.State = 1 for _, v := range m.s.Devices { gopool.Go(v.Call) } } } func (is *IntersectionServer) Serve() { if util.Config.Server.SupportCamera { is.CameraEventServer.RegisterCallback(0, &MainNotifier{is}) } //先创建响应设备 for _, c := range util.Config.Screens { iDevice := &IntersectionDevice{ Info: OutputDeviceInfo{ Name: c.Name, Ip: c.Ip, Port: c.Port, Branch: c.Branch, }, Screen: NewScreen(c.Name, c.Ip, c.Port), } is.Devices = append(is.Devices, iDevice) } for { select { case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态 for _, v := range is.Devices { if is.State == 1 { gopool.Go(v.Rollback) } } is.State = 0 } } }