2023-07-31 08:41:42 +08:00
|
|
|
package apiproto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-07-31 15:44:08 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2023-07-31 08:41:42 +08:00
|
|
|
"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 {
|
2023-08-03 09:23:46 +08:00
|
|
|
return 200 * time.Millisecond
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返回所有数据
|
|
|
|
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-08-01 17:08:45 +08:00
|
|
|
data, err := proto.Marshal(simulation.GetAllState())
|
2023-08-01 14:54:11 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 定时发送数据
|
|
|
|
func (t *SimulationServer) onTick() []TopicMsg {
|
2023-08-01 14:54:11 +08:00
|
|
|
simArr := simulation.GetSimulationArr()
|
|
|
|
msgArr := make([]TopicMsg, len(simArr))
|
|
|
|
for i, v := range simArr {
|
|
|
|
channelName := handlerChannelName(v.SimulationId, t.getChannelName())
|
2023-08-01 17:08:45 +08:00
|
|
|
b, err := proto.Marshal(v.GetAllState())
|
2023-07-31 15:44:08 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-07-31 08:41:42 +08:00
|
|
|
msgArr[i] = TopicMsg{
|
|
|
|
channalName: channelName,
|
2023-07-31 15:44:08 +08:00
|
|
|
data: b,
|
2023-07-31 08:41:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return msgArr
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理订阅通道名称
|
|
|
|
func handlerChannelName(sid string, format string) string {
|
|
|
|
return strings.Replace(format, "{sid}", sid, 1)
|
|
|
|
}
|