package apiproto import ( "strconv" "strings" "time" "github.com/golang/protobuf/proto" "joylink.club/bj-rtsts-server/ats/verify/protos/state" "joylink.club/bj-rtsts-server/ats/verify/simulation" "joylink.club/bj-rtsts-server/ats/verify/simulation/wayside" ) type SimulationServer struct{} // 返回通道格式 func (t *SimulationServer) getChannelName() string { return "simulation-{sid}-devices-status" } // 消息运行间隔 func (t *SimulationServer) getInterval() time.Duration { return time.Second } // 返回所有数据 func (t *SimulationServer) allMsgData(params map[string]string) []byte { simId := params["SID"] if simId == "" { return nil } simulation := simulation.FindSimulation(simId) if simulation == nil { return nil } return []byte(generateTestState(simulation).String()) } // 定时发送数据 func (t *SimulationServer) onTick() []TopicMsg { msgArr := make([]TopicMsg, len(simulation.SimulationMap)) i := 0 for k, v := range simulation.SimulationMap { channelName := handlerChannelName(k, t.getChannelName()) b, err := proto.Marshal(generateTestState(v)) if err != nil { panic(err) } msgArr[i] = TopicMsg{ channalName: channelName, data: b, } i++ } return msgArr } // 处理订阅通道名称 func handlerChannelName(sid string, format string) string { return strings.Replace(format, "{sid}", sid, 1) } // 生成道岔测试状态数据 func generateTestState(v *wayside.VerifyMemory) *state.PushedDevicesStatus { switchMap := v.Status.SwitchStateMap switchArr := make([]*state.SwitchState, 9) if len(switchMap) == 0 { for i := 0; i < 9; i++ { switchArr[i] = &state.SwitchState{ Id: strconv.Itoa(i + 1), Normal: true, Reverse: false, } v.Status.SwitchStateMap[strconv.Itoa(i+1)] = switchArr[i] } } else { i := 0 for _, v := range switchMap { switchArr[i] = v i++ } } return &state.PushedDevicesStatus{ All: true, AllStatus: &state.AllDevicesStatus{ SwitchState: switchArr, }, } }