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

76 lines
2.0 KiB
Go

package mqtt
import (
"fmt"
"strings"
"joylink.club/bj-rtsts-server/config"
)
const (
topicPrefix = "/" + config.SystemName + "/simulation/%s/" // 公共部分 仿真ID
stateTopic = topicPrefix + "state" // 仿真状态topic
sfpTopic = topicPrefix + "sfp/%d" // 平面布置图设备状态topic 地图ID
rccTopic = topicPrefix + "rcc/%d" // 继电器柜继电器状态topic 地图ID
pslTopic = topicPrefix + "psl/%d/%d" // psl状态topic 地图ID/门控箱ID
ibpTopic = topicPrefix + "ibp/%d/%d" // ibp盘状态topic 地图ID/车站ID
)
var topicMap = map[string]string{
"state": stateTopic,
"sfp": sfpTopic,
"rcc": rccTopic,
"psl": pslTopic,
"ibp": ibpTopic,
}
// 检测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)
}
// 信号布置图设备状态消息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, stationId uint32) string {
return fmt.Sprintf(ibpTopic, simulationId, mapId, stationId)
}