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

90 lines
2.0 KiB
Go
Raw Normal View History

2023-07-31 08:41:42 +08:00
package apiproto
import (
"strconv"
"strings"
"time"
"github.com/golang/protobuf/proto"
2023-07-31 08:41:42 +08:00
"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())
b, err := proto.Marshal(generateTestState(v))
if err != nil {
panic(err)
}
2023-07-31 08:41:42 +08:00
msgArr[i] = TopicMsg{
channalName: channelName,
data: b,
2023-07-31 08:41:42 +08:00
}
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{
2023-07-31 17:27:05 +08:00
Id: strconv.Itoa(i + 1),
2023-07-31 08:41:42 +08:00
Normal: true,
Reverse: false,
}
2023-07-31 17:27:05 +08:00
v.Status.SwitchStateMap[strconv.Itoa(i+1)] = switchArr[i]
2023-07-31 08:41:42 +08:00
}
} else {
i := 0
for _, v := range switchMap {
switchArr[i] = v
i++
}
}
return &state.PushedDevicesStatus{
All: true,
AllStatus: &state.AllDevicesStatus{
SwitchState: switchArr,
},
}
}