2023-12-20 10:37:54 +08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-23 09:37:31 +08:00
|
|
|
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
|
2024-02-20 14:18:42 +08:00
|
|
|
pslTopic = topicPrefix + "psl/%d/%d" // psl状态topic 地图ID/PSL盘ID
|
|
|
|
ibpTopic = topicPrefix + "ibp/%d/%d" // ibp盘状态topic 地图ID/IBP盘ID
|
2024-03-19 08:28:50 +08:00
|
|
|
trainControlTopic = topicPrefix + "train/control/%s" // 列车控制台状态topic 列车id
|
2023-12-20 10:37:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var topicMap = map[string]string{
|
|
|
|
"state": stateTopic,
|
2024-01-23 09:37:31 +08:00
|
|
|
"tpis": tpapiServiceTopic,
|
2023-12-20 10:37:54 +08:00
|
|
|
"sfp": sfpTopic,
|
|
|
|
"rcc": rccTopic,
|
|
|
|
"psl": pslTopic,
|
|
|
|
"ibp": ibpTopic,
|
2024-03-19 08:28:50 +08:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2024-01-23 09:37:31 +08:00
|
|
|
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
|
2024-02-20 14:18:42 +08:00
|
|
|
func GetIbpTopic(simulationId string, mapId int32, ibpId uint32) string {
|
|
|
|
return fmt.Sprintf(ibpTopic, simulationId, mapId, ibpId)
|
2023-12-20 10:37:54 +08:00
|
|
|
}
|
2024-03-19 08:28:50 +08:00
|
|
|
|
|
|
|
// 列车控制消息topic
|
|
|
|
func GetTrainControlTopic(simulationId string, trainId string) string {
|
|
|
|
return fmt.Sprintf(trainControlTopic, simulationId, trainId)
|
|
|
|
}
|