123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package lc
- import (
- "lc-smartX/util"
- "lc-smartX/util/gopool"
- "time"
- )
- type SmartXServer interface {
- Serve()
- }
- type IntersectionServer struct {
- RadarEventServer *RadarServer
- CameraEventServer *CameraServer
- MainState byte
- SubState byte
- MainDevices []IDevice
- SubDevices []IDevice
- ReTicker *time.Ticker
- Main *time.Ticker
- Sub *time.Ticker
- }
- func StartIntersectionServer() {
- is := &IntersectionServer{
- Main: time.NewTicker(5 * time.Second), //主路状态回滚
- Sub: time.NewTicker(5 * time.Second), //支路状态回滚
- ReTicker: time.NewTicker(30 * time.Second), //重连
- }
- if util.Config.Server.SupportCamera {
- is.CameraEventServer = NewCameraEventServer()
- gopool.Go(is.CameraEventServer.Start)
- }
- if util.Config.Server.SupportRadar {
- is.RadarEventServer = NewRadarEventServer()
- gopool.Go(is.RadarEventServer.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.MainState != 1 {
- m.s.MainState = 1
- for _, v := range m.s.SubDevices {
- gopool.Go(v.Call)
- }
- }
- }
- type SubNotifier struct{ s *IntersectionServer }
- // Notify 支路来车,通知主路设备
- func (sub SubNotifier) Notify() {
- sub.s.Sub.Reset(5 * time.Second)
- if sub.s.SubState != 1 {
- sub.s.SubState = 1
- for _, v := range sub.s.MainDevices {
- gopool.Go(v.Call)
- }
- }
- }
- func (is *IntersectionServer) Serve() {
- if util.Config.Server.SupportCamera {
- is.CameraEventServer.RegisterCallback(1, &SubNotifier{is})
- is.CameraEventServer.RegisterCallback(0, &MainNotifier{is})
- }
- if util.Config.Server.SupportRadar {
- is.RadarEventServer.RegisterCallback(1, &SubNotifier{is})
- is.RadarEventServer.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),
- }
- if c.Branch == 1 {
- is.MainDevices = append(is.MainDevices, iDevice)
- } else {
- is.SubDevices = append(is.SubDevices, iDevice)
- }
- }
- for _, c := range util.Config.Speakers {
- iDevice := &IntersectionDevice{
- Info: OutputDeviceInfo{
- Name: c.Name,
- Ip: c.Ip,
- Branch: c.Branch,
- Audio: c.Audio,
- },
- Speaker: NewIpCast(c.Name, c.Ip, c.Audio, c.Speed, c.Volume),
- }
- if c.Branch == 1 {
- is.MainDevices = append(is.MainDevices, iDevice)
- } else {
- is.SubDevices = append(is.SubDevices, iDevice)
- }
- }
- for {
- select {
- case <-is.Main.C: //检查主路状态->支路输出设备回到初始状态
- for _, v := range is.SubDevices {
- if is.MainState == 1 {
- gopool.Go(v.Rollback)
- }
- }
- is.MainState = 0
- case <-is.Sub.C: //检查支路状态->主路输出设备作出响应
- for _, v := range is.MainDevices {
- if is.SubState == 1 {
- gopool.Go(v.Rollback)
- }
- }
- is.SubState = 0
- case <-is.ReTicker.C: //每19s检查并尝试重连
- gopool.Go(func() {
- for _, v := range is.MainDevices {
- gopool.Go(v.Reconnect)
- }
- })
- gopool.Go(func() {
- for _, v := range is.SubDevices {
- gopool.Go(v.Reconnect)
- }
- })
- }
- }
- }
|