rts-sim-testing-service/grpcproto/simulation_server.go

85 lines
1.8 KiB
Go
Raw Normal View History

2023-07-31 08:41:42 +08:00
package apiproto
import (
"strconv"
"strings"
"time"
"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
}
2023-07-31 09:04:31 +08:00
return []byte(generateTestState(simulation).String())
2023-07-31 08:41:42 +08:00
}
// 定时发送数据
func (t *SimulationServer) onTick() []TopicMsg {
msgArr := make([]TopicMsg, len(simulation.SimulationMap))
i := 0
for k, v := range simulation.SimulationMap {
channelName := handlerChannelName(k, t.getChannelName())
data := generateTestState(v)
msgArr[i] = TopicMsg{
channalName: channelName,
data: []byte(data.String()),
}
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),
Normal: true,
Reverse: false,
}
}
} else {
i := 0
for _, v := range switchMap {
switchArr[i] = v
i++
}
}
return &state.PushedDevicesStatus{
All: true,
AllStatus: &state.AllDevicesStatus{
SwitchState: switchArr,
},
}
}