thesai
cac309f4b1
All checks were successful
local-test分支打包构建docker并发布运行 / Docker-Build (push) Successful in 1m31s
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
// 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
|
||
}
|
||
}
|