rts-sim-testing-service/third_party/axle_device/rssp_axle.go

130 lines
3.3 KiB
Go
Raw Normal View History

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-09 14:04:37 +08:00
//接收方每个安全通信会话对应的发送周期值,单位ms
sendingPeriod uint32
2023-11-06 18:10:15 +08:00
//主安全通道
2023-11-09 17:38:17 +08:00
rsspChannel *RsspChannel
2023-11-06 18:10:15 +08:00
//收到应用层消息回调
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
2023-11-09 17:38:17 +08:00
ra.sendingPeriod = cfg.RsspCfg.SendingPeriod
2023-11-09 13:05:29 +08:00
//
2023-11-09 17:38:17 +08:00
mrc := &RsspChannel{}
ra.rsspChannel = mrc.Init(&cfg.RsspCfg)
2023-11-09 13:05:29 +08:00
//
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
//设置安全通道层
2023-11-09 17:38:17 +08:00
if s.rsspChannel != nil {
s.rsspChannel.handleUserData = s.rcvCmdMsg
s.rsspChannel.Start()
2023-11-06 18:10:15 +08:00
}
//
2023-11-09 09:46:48 +08:00
sendContext, sendCancel := context.WithCancel(context.Background())
2023-11-13 17:35:53 +08:00
go s.periodRun(sendContext)
2023-11-09 09:46:48 +08:00
s.cancelSendStatus = sendCancel
//
2023-11-06 18:10:15 +08:00
return nil
}
func (s *rsspAxle) Stop() {
2023-11-09 17:38:17 +08:00
if s.rsspChannel != nil {
s.rsspChannel.Stop()
2023-11-06 18:10:15 +08:00
}
2023-11-09 09:46:48 +08:00
//
if s.cancelSendStatus != nil {
s.cancelSendStatus()
}
s.messageManager = nil
}
2023-11-13 17:35:53 +08:00
func (s *rsspAxle) periodRun(runContext context.Context) {
2023-11-14 17:57:54 +08:00
time.Sleep(2 * time.Second)
2023-11-09 09:46:48 +08:00
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 {
2023-11-13 17:35:53 +08:00
case <-runContext.Done():
2023-11-09 09:46:48 +08:00
return
default:
}
2023-11-14 17:57:54 +08:00
//slog.Debug("计轴设备periodRun")
2023-11-09 09:46:48 +08:00
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 {
2023-11-14 17:57:54 +08:00
slog.Warn(e.Error())
2023-11-09 09:46:48 +08:00
}
2023-11-13 17:35:53 +08:00
//
2023-11-09 14:04:37 +08:00
time.Sleep(time.Duration(s.sendingPeriod) * time.Millisecond)
2023-11-13 17:35:53 +08:00
//更新周期性参数
2023-11-14 17:57:54 +08:00
s.rsspChannel.NextPeriod()
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()
//向主通道发送
2023-11-09 17:38:17 +08:00
if s.rsspChannel != nil {
2023-11-14 17:57:54 +08:00
//slog.Debug("计轴设备发送SectionStatusMsgPack", "区段状态个数", len(msg.Sms), "packLen", len(data))
s.rsspChannel.SendUserData(data)
2023-11-06 18:10:15 +08:00
}
}