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

62 lines
1.3 KiB
Go

package apiproto
import (
"strings"
"time"
"github.com/golang/protobuf/proto"
"joylink.club/bj-rtsts-server/ats/verify/simulation"
)
type SimulationServer struct{}
// 返回通道格式
func (t *SimulationServer) getChannelName() string {
return "simulation-{sid}-devices-status"
}
// 消息运行间隔
func (t *SimulationServer) getInterval() time.Duration {
return 200 * time.Millisecond
}
// 返回所有数据
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(simulation.GetAllState())
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(v.GetAllState())
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)
}