2023-10-24 10:57:04 +08:00
|
|
|
package interlock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"runtime/debug"
|
2023-10-26 15:06:26 +08:00
|
|
|
"sync"
|
2023-10-24 10:57:04 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"joylink.club/bj-rtsts-server/config"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
|
|
|
"joylink.club/bj-rtsts-server/third_party/udp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 联锁代理通信接口
|
|
|
|
type InterlockMessageManager interface {
|
2023-10-31 15:23:29 +08:00
|
|
|
CollectInterlockRelayInfo(code string) *message.InterlockSendMsgPkg
|
|
|
|
HandleInterlockDriverInfo(code string, b []byte)
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 联锁接口
|
|
|
|
type InterlockProxy interface {
|
|
|
|
// 启动联锁消息功能
|
|
|
|
Start(manager InterlockMessageManager)
|
|
|
|
// 停止联锁消息功能
|
|
|
|
Stop()
|
|
|
|
// 发送联锁采集消息
|
|
|
|
SendCollectMessage(b []byte)
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:23:29 +08:00
|
|
|
var interlockMap = make(map[string]InterlockProxy)
|
2023-10-26 15:06:26 +08:00
|
|
|
var initMutex sync.Mutex
|
2023-10-24 10:57:04 +08:00
|
|
|
|
2023-10-31 15:23:29 +08:00
|
|
|
func Default(c *config.InterlockConfig) InterlockProxy {
|
2023-10-26 15:06:26 +08:00
|
|
|
initMutex.Lock()
|
|
|
|
defer initMutex.Unlock()
|
2023-10-31 15:23:29 +08:00
|
|
|
if interlockMap[c.Code] == nil {
|
|
|
|
interlockMap[c.Code] = &interlockProxy{runConfig: c}
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
2023-10-31 15:23:29 +08:00
|
|
|
return interlockMap[c.Code]
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type interlockProxy struct {
|
|
|
|
driveInfoUdpServer udp.UdpServer
|
|
|
|
sendCollectUdpClient udp.UdpClient
|
|
|
|
|
|
|
|
manager InterlockMessageManager
|
|
|
|
collectInfoTaskCancel context.CancelFunc
|
2023-10-26 15:06:26 +08:00
|
|
|
runConfig *config.InterlockConfig
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 驱动信息进行转发
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) handleDriverInfo(b []byte) {
|
|
|
|
handler := i.manager
|
2023-10-24 10:57:04 +08:00
|
|
|
if handler != nil {
|
2023-10-31 15:23:29 +08:00
|
|
|
handler.HandleInterlockDriverInfo(i.runConfig.Code, b)
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) Start(manager InterlockMessageManager) {
|
2023-10-31 15:23:29 +08:00
|
|
|
if i.runConfig == nil || i.runConfig.Ip == "" || !i.runConfig.Open {
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 10:57:04 +08:00
|
|
|
if manager == nil {
|
|
|
|
panic("启动联锁消息服务错误: InterlockMessageManager不能为nil")
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
if i.manager != nil {
|
2023-10-24 10:57:04 +08:00
|
|
|
panic("启动联锁消息服务错误: 存在正在运行的任务")
|
|
|
|
}
|
2023-10-26 15:06:26 +08:00
|
|
|
i.manager = manager
|
|
|
|
// 初始化客户端、服务端
|
|
|
|
i.initInterlockProxy()
|
2023-10-24 10:57:04 +08:00
|
|
|
ctx, cancle := context.WithCancel(context.Background())
|
2023-10-26 15:06:26 +08:00
|
|
|
go i.collectInfoStateTask(ctx)
|
|
|
|
i.collectInfoTaskCancel = cancle
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
2023-10-26 18:00:17 +08:00
|
|
|
// 采集电路状态发送间隔,单位ms
|
|
|
|
const InterlockMessageSendInterval = 50
|
|
|
|
|
|
|
|
// 序列号
|
|
|
|
var serialNumber uint8
|
2023-10-24 10:57:04 +08:00
|
|
|
|
|
|
|
// 定时发送采集电路状态任务
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) collectInfoStateTask(ctx context.Context) {
|
2023-10-24 10:57:04 +08:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
slog.Error("定时发送道岔状态任务异常", "error", err, "stack", string(debug.Stack()))
|
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2023-10-31 15:23:29 +08:00
|
|
|
collectInfoState := i.manager.CollectInterlockRelayInfo(i.runConfig.Code)
|
|
|
|
if collectInfoState != nil {
|
2023-10-26 18:00:17 +08:00
|
|
|
serialNumber++
|
2023-11-06 15:20:45 +08:00
|
|
|
collectInfoState.SetSerialNumber(serialNumber)
|
2023-10-31 15:23:29 +08:00
|
|
|
i.sendCollectUdpClient.SendMsg(collectInfoState)
|
2023-10-26 18:00:17 +08:00
|
|
|
}
|
2023-10-24 10:57:04 +08:00
|
|
|
time.Sleep(time.Millisecond * InterlockMessageSendInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) Stop() {
|
2023-11-01 16:55:16 +08:00
|
|
|
initMutex.Lock()
|
|
|
|
defer initMutex.Unlock()
|
|
|
|
delete(interlockMap, i.runConfig.Code)
|
2023-10-26 15:06:26 +08:00
|
|
|
if i.sendCollectUdpClient != nil {
|
|
|
|
i.sendCollectUdpClient.Close()
|
|
|
|
}
|
|
|
|
if i.driveInfoUdpServer != nil {
|
|
|
|
i.driveInfoUdpServer.Close()
|
|
|
|
}
|
|
|
|
if i.collectInfoTaskCancel != nil {
|
|
|
|
i.collectInfoTaskCancel()
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
2023-10-27 17:23:44 +08:00
|
|
|
i.manager = nil
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) SendCollectMessage(b []byte) {
|
|
|
|
i.sendCollectUdpClient.Send(b)
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (i *interlockProxy) initInterlockProxy() {
|
|
|
|
i.sendCollectUdpClient = udp.NewClient(fmt.Sprintf("%v:%v", i.runConfig.Ip, i.runConfig.RemotePort))
|
|
|
|
i.driveInfoUdpServer = udp.NewServer(fmt.Sprintf(":%d", i.runConfig.LocalPort), i.handleDriverInfo)
|
|
|
|
i.driveInfoUdpServer.Listen()
|
2023-10-24 10:57:04 +08:00
|
|
|
}
|