rts-sim-testing-service/third_party/interlock/beijing11/interlock.go
thesai cac309f4b1
All checks were successful
local-test分支打包构建docker并发布运行 / Docker-Build (push) Successful in 1m31s
[新增]11号线联锁通信所属线路配置;11号线联锁通信原始数据及解析后的json数据日志
2024-05-29 17:20:51 +08:00

64 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package beijing11 北京11号线联锁通信
package beijing11
import (
"encoding/json"
"fmt"
"joylink.club/bj-rtsts-server/config"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
"sync"
)
var (
initMutex = sync.Mutex{}
logTag = "[北京11号线联锁通信]"
running = false
server udp.UdpServer
)
func Start(interlockConfig *config.InterlockConfig) {
if interlockConfig == nil || interlockConfig.Ip == "" || !interlockConfig.Open {
return
}
if running {
return
}
initMutex.Lock()
defer initMutex.Unlock()
if running {
return
}
//UDP通信设施
server = udp.NewServer(fmt.Sprintf(":%d", interlockConfig.LocalPort), func(b []byte) {
slog.Info(fmt.Sprintf("%s收到消息%x", logTag, b))
frame := &FromInterlockFrame{}
err := frame.Decode(b)
if err != nil {
slog.Error(fmt.Sprintf("%s解析数据出错%s", logTag, err))
} else {
marshal, err := json.Marshal(frame)
if err != nil {
slog.Error(fmt.Sprintf("%s解析为json出错%s", logTag, err))
} else {
slog.Info(fmt.Sprintf("%s解析为json%s", logTag, string(marshal)))
}
}
})
err := server.Listen()
if err != nil {
panic(fmt.Sprintf("%s启动UDP服务失败%s", logTag, err))
}
running = true
}
func Stop() {
initMutex.Lock()
defer initMutex.Unlock()
running = false
if server != nil {
server.Close()
server = nil
}
}