2023-11-06 18:10:15 +08:00
|
|
|
package axle_device
|
|
|
|
|
2023-11-09 09:46:48 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2023-11-09 13:43:18 +08:00
|
|
|
"fmt"
|
2023-11-09 13:05:29 +08:00
|
|
|
"joylink.club/bj-rtsts-server/config"
|
2023-11-09 09:46:48 +08:00
|
|
|
"joylink.club/bj-rtsts-server/third_party/message"
|
|
|
|
"log/slog"
|
|
|
|
"runtime/debug"
|
2023-11-09 13:23:26 +08:00
|
|
|
"time"
|
2023-11-09 09:46:48 +08:00
|
|
|
)
|
2023-11-06 18:10:15 +08:00
|
|
|
|
|
|
|
//计轴设备与联锁系统安全通信应用层实现
|
|
|
|
|
|
|
|
type RsspAxle interface {
|
|
|
|
//Start 启动计轴设备与联锁系统安全通信服务
|
|
|
|
Start(amm AxleMessageManager) error
|
|
|
|
//Stop 停止计轴设备与联锁系统安全通信服务
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type rsspAxle struct {
|
2023-11-07 17:47:47 +08:00
|
|
|
//所属城市
|
|
|
|
city string
|
|
|
|
//所属线路
|
|
|
|
lineId string
|
2023-11-07 13:22:47 +08:00
|
|
|
//所属集中站
|
|
|
|
centralizedStation string
|
2023-11-06 18:10:15 +08:00
|
|
|
//主安全通道
|
|
|
|
masterRssp *RsspChannel
|
|
|
|
//备安全通道
|
|
|
|
slaveRssp *RsspChannel
|
|
|
|
//收到应用层消息回调
|
|
|
|
messageManager AxleMessageManager
|
2023-11-09 10:14:56 +08:00
|
|
|
//发送区段状态任务
|
2023-11-09 09:46:48 +08:00
|
|
|
cancelSendStatus context.CancelFunc
|
2023-11-06 18:10:15 +08:00
|
|
|
}
|
|
|
|
|
2023-11-09 13:05:29 +08:00
|
|
|
func InitRsspAxle(cfg *config.RsspAxleConfig) RsspAxle {
|
|
|
|
ra := &rsspAxle{}
|
|
|
|
//
|
|
|
|
ra.city = cfg.City
|
|
|
|
ra.lineId = cfg.LineId
|
|
|
|
ra.centralizedStation = cfg.CentralizedStation
|
|
|
|
//
|
|
|
|
cfgLen := len(cfg.RsspCfgs)
|
|
|
|
var masterConfig, slaveConfig *config.RsspConfig
|
|
|
|
if cfgLen >= 1 {
|
|
|
|
masterConfig = &cfg.RsspCfgs[0]
|
|
|
|
}
|
|
|
|
if cfgLen >= 2 {
|
|
|
|
slaveConfig = &cfg.RsspCfgs[1]
|
|
|
|
}
|
|
|
|
if masterConfig != nil {
|
|
|
|
mrc := &RsspChannel{}
|
|
|
|
ra.masterRssp = mrc.Init(masterConfig)
|
|
|
|
}
|
|
|
|
if slaveConfig != nil {
|
|
|
|
src := &RsspChannel{}
|
|
|
|
ra.slaveRssp = src.Init(slaveConfig)
|
|
|
|
}
|
|
|
|
//
|
|
|
|
return ra
|
2023-11-06 18:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// rssp 安全层执行
|
|
|
|
func (s *rsspAxle) rcvCmdMsg(data []byte) {
|
|
|
|
msg := &message.SectionCmdMsgPack{}
|
|
|
|
msg.Decode(data)
|
2023-11-07 17:47:47 +08:00
|
|
|
s.messageManager.HandleSectionCmdMsg(s.city, s.lineId, s.centralizedStation, msg)
|
2023-11-06 18:10:15 +08:00
|
|
|
}
|
|
|
|
func (s *rsspAxle) Start(amm AxleMessageManager) error {
|
|
|
|
s.messageManager = amm
|
|
|
|
//设置安全通道层
|
|
|
|
if s.masterRssp != nil {
|
|
|
|
s.masterRssp.handleUserData = s.rcvCmdMsg
|
|
|
|
s.masterRssp.Start()
|
|
|
|
}
|
|
|
|
if s.slaveRssp != nil {
|
|
|
|
s.slaveRssp.handleUserData = s.rcvCmdMsg
|
|
|
|
s.slaveRssp.Start()
|
|
|
|
}
|
|
|
|
//
|
2023-11-09 09:46:48 +08:00
|
|
|
sendContext, sendCancel := context.WithCancel(context.Background())
|
|
|
|
go s.doTaskSendStatus(sendContext)
|
|
|
|
s.cancelSendStatus = sendCancel
|
|
|
|
//
|
2023-11-06 18:10:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *rsspAxle) Stop() {
|
|
|
|
if s.masterRssp != nil {
|
|
|
|
s.masterRssp.Stop()
|
|
|
|
}
|
|
|
|
if s.slaveRssp != nil {
|
|
|
|
s.slaveRssp.Stop()
|
|
|
|
}
|
2023-11-09 09:46:48 +08:00
|
|
|
//
|
|
|
|
if s.cancelSendStatus != nil {
|
|
|
|
s.cancelSendStatus()
|
|
|
|
}
|
2023-11-09 13:05:29 +08:00
|
|
|
if s.cancelSendStatus != nil {
|
|
|
|
s.cancelSendStatus()
|
|
|
|
}
|
2023-11-09 09:46:48 +08:00
|
|
|
s.messageManager = nil
|
|
|
|
}
|
|
|
|
func (s *rsspAxle) doTaskSendStatus(sendContext context.Context) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
2023-11-09 13:43:18 +08:00
|
|
|
slog.Error(fmt.Sprintf("[%s-%s-%s]定时发送计轴区段状态任务异常", s.city, s.lineId, s.centralizedStation), "error", e, "stack", string(debug.Stack()))
|
2023-11-09 09:46:48 +08:00
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sendContext.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if s.messageManager == nil {
|
2023-11-09 13:43:18 +08:00
|
|
|
slog.Warn(fmt.Sprintf("[%s-%s-%s]定时发送计轴区段状态任务因messageManager不存在退出", s.city, s.lineId, s.centralizedStation))
|
2023-11-09 09:46:48 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
//收集区段状态
|
|
|
|
sectionStatusMsg, e := s.messageManager.CollectSectionStatus(s.city, s.lineId, s.centralizedStation)
|
|
|
|
if e == nil {
|
|
|
|
if sectionStatusMsg != nil {
|
|
|
|
msgPack := &message.SectionStatusMsgPack{}
|
|
|
|
msgPack.Ck = 0 //暂时无用
|
|
|
|
msgPack.Sms = sectionStatusMsg
|
|
|
|
s.sendStatusMsg(msgPack)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slog.Debug(e.Error())
|
|
|
|
}
|
2023-11-09 13:23:26 +08:00
|
|
|
//todo
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
2023-11-09 09:46:48 +08:00
|
|
|
}
|
2023-11-06 18:10:15 +08:00
|
|
|
}
|
2023-11-07 13:22:47 +08:00
|
|
|
|
|
|
|
// 发送计轴区段状态给联锁
|
|
|
|
func (s *rsspAxle) sendStatusMsg(msg *message.SectionStatusMsgPack) {
|
2023-11-06 18:10:15 +08:00
|
|
|
data := msg.Encode()
|
|
|
|
//向备通道发送
|
|
|
|
if s.slaveRssp != nil {
|
|
|
|
data_s := make([]byte, len(data))
|
|
|
|
copy(data_s, data)
|
|
|
|
s.slaveRssp.sendUserData(data_s)
|
|
|
|
}
|
|
|
|
//向主通道发送
|
|
|
|
if s.masterRssp != nil {
|
|
|
|
s.masterRssp.sendUserData(data)
|
|
|
|
}
|
|
|
|
}
|