2023-10-26 16:41:18 +08:00
|
|
|
package message_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-11-17 17:01:12 +08:00
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-10-26 16:41:18 +08:00
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_manage"
|
2023-10-27 14:57:37 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ts/protos/graphicData"
|
2023-10-26 17:16:07 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
2023-10-26 16:41:18 +08:00
|
|
|
)
|
|
|
|
|
2023-11-17 17:01:12 +08:00
|
|
|
const (
|
2023-11-17 18:04:38 +08:00
|
|
|
SimulationTopicPrefix = "/" + config.SystemName + "/simulation/%s/"
|
|
|
|
// 仿真状态消息topic
|
|
|
|
SimulationStateTopic = SimulationTopicPrefix + "state"
|
|
|
|
// 信号布置图设备状态消息topic
|
|
|
|
SimulationSfpTopic = SimulationTopicPrefix + "sfp/%d"
|
|
|
|
// 继电器组合柜布置图设备状态消息topic
|
|
|
|
SimulationRccTopic = SimulationTopicPrefix + "rcc/%d"
|
|
|
|
// PSL设备状态消息topic
|
|
|
|
SimulationPslTopic = SimulationTopicPrefix + "psl/%d/%s"
|
|
|
|
// IBP设备状态消息topic
|
|
|
|
SimulationIbpTopic = SimulationTopicPrefix + "ibp/%d/%s"
|
2023-11-17 17:01:12 +08:00
|
|
|
)
|
|
|
|
|
2023-10-26 16:41:18 +08:00
|
|
|
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{
|
2023-10-27 14:57:37 +08:00
|
|
|
vs: vs,
|
2023-10-27 16:51:48 +08:00
|
|
|
mss: []ms_api.IMsgServer{},
|
2023-10-27 14:57:37 +08:00
|
|
|
}
|
|
|
|
for _, mapId := range vs.MapIds {
|
2023-11-07 10:49:15 +08:00
|
|
|
t := memory.QueryGiType(mapId)
|
2023-10-27 14:57:37 +08:00
|
|
|
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))
|
|
|
|
}
|
2023-10-26 16:41:18 +08:00
|
|
|
}
|
2023-10-27 16:51:48 +08:00
|
|
|
// 启动仿真状态服务
|
|
|
|
NewSimulationStateMs(vs)
|
2023-10-26 16:41:18 +08:00
|
|
|
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
|
|
|
|
}
|
2023-10-26 17:48:43 +08:00
|
|
|
|
|
|
|
func (sms *SimulationMs) OnError(err error) {
|
|
|
|
// TODO: 仿真消息错误处理
|
|
|
|
}
|