76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
|
package mqtt
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"joylink.club/bj-rtsts-server/config"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
topicPrefix = "/" + config.SystemName + "/simulation/%s/"
|
||
|
stateTopic = topicPrefix + "state"
|
||
|
sfpTopic = topicPrefix + "sfp/%d"
|
||
|
rccTopic = topicPrefix + "rcc/%d"
|
||
|
pslTopic = topicPrefix + "psl/%d/%d"
|
||
|
ibpTopic = topicPrefix + "ibp/%d/%d"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
}
|