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

50 lines
1.2 KiB
Go
Raw Normal View History

2023-07-31 08:41:42 +08:00
package apiproto
import (
2023-09-21 16:22:55 +08:00
"strconv"
2023-07-31 08:41:42 +08:00
"strings"
"time"
"google.golang.org/protobuf/proto"
2023-07-31 08:41:42 +08:00
"joylink.club/bj-rtsts-server/ats/verify/simulation"
2023-08-30 18:05:11 +08:00
"joylink.club/bj-rtsts-server/dto"
2023-07-31 08:41:42 +08:00
)
type SimulationServer struct{}
// 返回通道格式
func (t *SimulationServer) getChannelName() string {
2023-09-21 16:22:55 +08:00
return "simulation-{sid}_{mid}-devices-status"
2023-07-31 08:41:42 +08:00
}
// 消息运行间隔
func (t *SimulationServer) getInterval() time.Duration {
return 200 * time.Millisecond
2023-07-31 08:41:42 +08:00
}
// 定时发送数据
func (t *SimulationServer) onTick() []TopicMsg {
2023-08-01 14:54:11 +08:00
simArr := simulation.GetSimulationArr()
2023-09-21 16:22:55 +08:00
var msgArr []TopicMsg
for _, v := range simArr {
for _, mapId := range v.MapIds {
idStr := strconv.Itoa(int(mapId))
channelName := handlerChannelName(v.SimulationId, idStr, t.getChannelName())
b, err := proto.Marshal(v.GetAllState(mapId))
if err != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
}
msgArr = append(msgArr, TopicMsg{channalName: channelName, data: b})
}
2023-07-31 08:41:42 +08:00
}
return msgArr
}
// 处理订阅通道名称
2023-09-21 16:22:55 +08:00
func handlerChannelName(sid, mapId string, format string) string {
var channelName string
channelName = strings.Replace(format, "{sid}", sid, 1)
channelName = strings.Replace(channelName, "{mid}", mapId, 1)
return channelName
2023-07-31 08:41:42 +08:00
}