89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
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/%d" // 列车控制台状态topic 列车id,地图id
|
|
)
|
|
|
|
var topicMap = map[string]string{
|
|
"state": stateTopic,
|
|
"tpis": tpapiServiceTopic,
|
|
"sfp": sfpTopic,
|
|
"rcc": rccTopic,
|
|
"psl": pslTopic,
|
|
"ibp": ibpTopic,
|
|
"train": trainControlTopic,
|
|
}
|
|
|
|
// 检测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)
|
|
}
|
|
|
|
// 信号布置图设备状态消息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)
|
|
}
|
|
|
|
// 列车控制消息topic
|
|
func GetTrainControlTopic(simulationId string, trainId string, mapId int32) string {
|
|
return fmt.Sprintf(trainControlTopic, simulationId, trainId, mapId)
|
|
}
|