rts-sim-testing-service/mqtt/topic.go

89 lines
2.6 KiB
Go
Raw Normal View History

2023-12-20 10:37:54 +08:00
package mqtt
import (
"fmt"
"strings"
"joylink.club/bj-rtsts-server/config"
)
const (
topicPrefix = "/" + config.SystemName + "/simulation/%s/" // 公共部分 仿真ID
stateTopic = topicPrefix + "state" // 仿真状态topic
tpapiServiceTopic = topicPrefix + "tpis" // 第三方API服务状态topic
sfpTopic = topicPrefix + "sfp/%d" // 平面布置图设备状态topic 地图ID
rccTopic = topicPrefix + "rcc/%d" // 继电器柜继电器状态topic 地图ID
pslTopic = topicPrefix + "psl/%d/%d" // psl状态topic 地图ID/PSL盘ID
ibpTopic = topicPrefix + "ibp/%d/%d" // ibp盘状态topic 地图ID/IBP盘ID
trainControlTopic = topicPrefix + "train/control/%s" // 列车控制台状态topic 列车id
2023-12-20 10:37:54 +08:00
)
var topicMap = map[string]string{
"state": stateTopic,
"tpis": tpapiServiceTopic,
2023-12-20 10:37:54 +08:00
"sfp": sfpTopic,
"rcc": rccTopic,
"psl": pslTopic,
"ibp": ibpTopic,
"train": trainControlTopic,
2023-12-20 10:37:54 +08:00
}
// 检测topic是否合法
func MatchTopic(topic string) bool {
topicArr := strings.Split(topic, "/")
for k, v := range topicMap {
result := strings.Contains(topic, "/"+k)
if result {
fmtArr := strings.Split(v, "/")
for i, f := range fmtArr {
if f == "%s" || f == "%d" {
continue
} else {
result = topicArr[i] == f
}
if !result {
break
}
}
}
if result {
return true
}
}
return false
}
// 仿真状态消息topic
func GetStateTopic(simulationId string) string {
return fmt.Sprintf(stateTopic, simulationId)
}
func GetTpapiServiceTopic(simulation string) string {
return fmt.Sprintf(tpapiServiceTopic, simulation)
}
2023-12-20 10:37:54 +08:00
// 信号布置图设备状态消息topic
func GetSfpTopic(simulationId string, mapId int32) string {
return fmt.Sprintf(sfpTopic, simulationId, mapId)
}
// 继电器组合柜布置图设备状态消息topic
func GetRccTopic(simulationId string, mapId int32) string {
return fmt.Sprintf(rccTopic, simulationId, mapId)
}
// PSL设备状态消息topic
func GetPslTopic(simulationId string, mapId int32, boxId uint32) string {
return fmt.Sprintf(pslTopic, simulationId, mapId, boxId)
}
// IBP设备状态消息topic
func GetIbpTopic(simulationId string, mapId int32, ibpId uint32) string {
return fmt.Sprintf(ibpTopic, simulationId, mapId, ibpId)
2023-12-20 10:37:54 +08:00
}
// 列车控制消息topic
func GetTrainControlTopic(simulationId string, trainId string) string {
return fmt.Sprintf(trainControlTopic, simulationId, trainId)
}