91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package message_server
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
|
"joylink.club/bj-rtsts-server/message_server/ms_manage"
|
|
"joylink.club/bj-rtsts-server/ts/protos/graphicData"
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
|
)
|
|
|
|
const (
|
|
SimulationTopicPrefix = "/" + config.SystemName + "/simulation/"
|
|
SimulationStateTopicPrefix = SimulationTopicPrefix + "%s/state/%d"
|
|
SimulationDeviceStatesTopicPrefix = SimulationTopicPrefix + "%s/devicestates/%d"
|
|
)
|
|
|
|
var smsMap sync.Map
|
|
|
|
// 仿真消息服务
|
|
// 管理仿真消息服务,整体可以作为一个消息服务,也可以每个消息子服务各自作为一个消息服务,暂时先按整体作为一个消息服务的方式使用
|
|
type SimulationMs struct {
|
|
vs *memory.VerifySimulation
|
|
mss []ms_api.IMsgServer
|
|
}
|
|
|
|
// 启动仿真所需的消息服务
|
|
func Start(vs *memory.VerifySimulation) {
|
|
_, ok := smsMap.Load(vs.SimulationId)
|
|
if !ok {
|
|
sms := &SimulationMs{
|
|
vs: vs,
|
|
mss: []ms_api.IMsgServer{},
|
|
}
|
|
for _, mapId := range vs.MapIds {
|
|
t := memory.QueryGiType(mapId)
|
|
switch t {
|
|
case graphicData.PictureType_StationLayout: // 平面布置图
|
|
// 添加车站关联的平面布置图、IBP、PSL信息
|
|
sms.mss = append(sms.mss, NewSfpMs(vs, mapId), NewIBPMs(vs, mapId), NewPSLMs(vs, mapId))
|
|
case graphicData.PictureType_RelayCabinetLayout: // 继电器柜
|
|
sms.mss = append(sms.mss, NewRccMs(vs, mapId))
|
|
}
|
|
}
|
|
// 启动仿真状态服务
|
|
NewSimulationStateMs(vs)
|
|
ms_manage.Register(sms)
|
|
smsMap.Store(vs.SimulationId, sms)
|
|
}
|
|
}
|
|
|
|
// 关闭仿真消息服务
|
|
func Close(vs *memory.VerifySimulation) {
|
|
sms, ok := smsMap.Load(vs.SimulationId)
|
|
if ok {
|
|
ms_manage.Unregister(sms.(*SimulationMs))
|
|
smsMap.Delete(vs.SimulationId)
|
|
}
|
|
}
|
|
|
|
// 获取通道
|
|
func (sms *SimulationMs) GetChannel() string {
|
|
return sms.vs.SimulationId
|
|
}
|
|
|
|
// 发送消息间隔时间,单位ms
|
|
func (sms *SimulationMs) GetInterval() time.Duration {
|
|
return 200 * time.Millisecond
|
|
}
|
|
|
|
// 构造定时发送的消息
|
|
func (sms *SimulationMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
|
var tmList []*ms_api.TopicMsg
|
|
for _, ms := range sms.mss {
|
|
tm, err := ms.OnTick()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(tm) > 0 {
|
|
tmList = append(tmList, tm...)
|
|
}
|
|
}
|
|
return tmList, nil
|
|
}
|
|
|
|
func (sms *SimulationMs) OnError(err error) {
|
|
// TODO: 仿真消息错误处理
|
|
}
|