rts-sim-testing-service/grpcproto/simulation_server.go
2023-08-01 14:54:11 +08:00

90 lines
2.0 KiB
Go

package apiproto
import (
"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/memory"
)
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
}
data, err := proto.Marshal(generateTestState(simulation))
if err != nil {
panic(err)
}
return data
}
// 定时发送数据
func (t *SimulationServer) onTick() []TopicMsg {
simArr := simulation.GetSimulationArr()
msgArr := make([]TopicMsg, len(simArr))
for i, v := range simArr {
channelName := handlerChannelName(v.SimulationId, t.getChannelName())
b, err := proto.Marshal(generateTestState(v))
if err != nil {
panic(err)
}
msgArr[i] = TopicMsg{
channalName: channelName,
data: b,
}
}
return msgArr
}
// 处理订阅通道名称
func handlerChannelName(sid string, format string) string {
return strings.Replace(format, "{sid}", sid, 1)
}
// 生成道岔测试状态数据
func generateTestState(v *memory.VerifySimulation) *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{},
}
}