rts-sim-testing-service/third_party/can_btm/balise_btm.go

291 lines
8.1 KiB
Go
Raw Normal View History

2023-11-21 18:13:50 +08:00
package can_btm
import (
"fmt"
2023-11-24 15:20:05 +08:00
"joylink.club/bj-rtsts-server/config"
2023-11-21 18:13:50 +08:00
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
2023-11-24 15:20:05 +08:00
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
2023-11-23 16:30:48 +08:00
"joylink.club/rtsssimulation/fi"
2023-11-21 18:13:50 +08:00
"log/slog"
2023-11-22 11:22:37 +08:00
"sort"
2023-11-24 15:20:05 +08:00
"sync"
"time"
2023-11-21 18:13:50 +08:00
)
2023-11-24 15:20:05 +08:00
//使用当有多个虚拟车时同一时刻只有一个虚拟车与CANET关联
// BtmCanetManager BTM CANET 管理器
type BtmCanetManager interface {
memory.VerifyEvn
//GetBtmCanetConfig 获取CANET配置信息
GetBtmCanetConfig() config.BtmCanetConfig
}
2023-11-21 18:13:50 +08:00
// btm与canet(网口-CAN口转换器)
type btmCanetClient struct {
2023-11-24 15:20:05 +08:00
bcm BtmCanetManager
2023-11-23 16:30:48 +08:00
//udp server
2023-11-21 18:13:50 +08:00
udpServer udp.UdpServer
2023-11-23 16:30:48 +08:00
//udp client
udpClient udp.UdpClient
//udp 本地侦听端口
localUdpPort int
//udp 远程端口
2023-11-21 18:13:50 +08:00
remoteUdpPort int
2023-11-23 16:30:48 +08:00
//udp 远程ip
remoteIp string
//最近一次车载ATP系统查询帧序号
atpReqSn byte
2023-11-24 15:20:05 +08:00
//最近一次车载ATP系统查询帧CRC16校验结果,true-校验通过
2023-11-23 16:30:48 +08:00
atpReqCrc16Check bool
2023-11-24 15:20:05 +08:00
//btm系统时间,每次接收到ATP查询请求帧时同步一次时间
btmTime btmClock
//数据流水号
dsn byte
2023-11-24 16:01:28 +08:00
//重发的数据
resendData *resendData
2023-11-24 15:20:05 +08:00
}
type btmClock struct {
btmTk uint32 //与ATP系统同步的时间ms
sysTk time.Time //本地系统时间
2023-11-22 11:22:37 +08:00
}
2023-11-24 15:20:05 +08:00
// 获取以btmTk为基准的当前时间ms
func (c *btmClock) tkNow() uint32 {
return c.btmTk + uint32(time.Now().UnixMilli()-c.sysTk.UnixMilli())
}
2023-11-22 11:22:37 +08:00
type BtmCanetClient interface {
2023-11-24 15:20:05 +08:00
Start(bcm BtmCanetManager)
2023-11-22 11:22:37 +08:00
Stop()
}
2023-11-21 18:13:50 +08:00
2023-11-24 15:20:05 +08:00
var (
btmClientLocker sync.Mutex
btmClient BtmCanetClient
)
func Default() BtmCanetClient {
btmClientLocker.Lock()
defer btmClientLocker.Unlock()
if btmClient == nil {
btmClient = &btmCanetClient{}
}
return btmClient
2023-11-22 11:22:37 +08:00
}
2023-11-24 15:20:05 +08:00
func (s *btmCanetClient) Start(bcm BtmCanetManager) {
s.bcm = bcm
cfg := s.bcm.GetBtmCanetConfig()
2023-11-27 09:14:20 +08:00
//测试用
cfg.Open = true
cfg.RemoteUdpPort = 5555
cfg.RemoteIp = "192.168.3.5"
cfg.LocalUdpPort = 6666
//
2023-11-24 15:20:05 +08:00
if !cfg.Open {
return
}
s.localUdpPort = cfg.LocalUdpPort
s.remoteIp = cfg.RemoteIp
s.remoteUdpPort = cfg.RemoteUdpPort
2023-11-21 18:13:50 +08:00
//
2023-11-22 11:22:37 +08:00
s.udpServer = udp.NewServer(fmt.Sprintf(":%d", s.localUdpPort), s.handleCanetFrames)
2023-11-21 18:13:50 +08:00
s.udpServer.Listen()
//
s.udpClient = udp.NewClient(fmt.Sprintf("%s:%d", s.remoteIp, s.remoteUdpPort))
}
func (s *btmCanetClient) Stop() {
if s.udpServer != nil {
s.udpServer.Close()
2023-11-24 15:20:05 +08:00
s.udpServer = nil
2023-11-21 18:13:50 +08:00
}
if s.udpClient != nil {
s.udpClient.Close()
2023-11-24 15:20:05 +08:00
s.udpClient = nil
2023-11-21 18:13:50 +08:00
}
}
2023-11-22 11:22:37 +08:00
func (s *btmCanetClient) handleCanetFrames(cfs []byte) {
2023-11-21 18:13:50 +08:00
//一个cannet 帧 13字节
if len(cfs) > 0 && len(cfs)%13 == 0 {
cfSum := len(cfs) / 13
2023-11-22 11:22:37 +08:00
dms := make([]*message.CanetFrame, 0, 16) //13个应答器报文数据帧+TimeA帧+TimeB帧+结束帧
2023-11-21 18:13:50 +08:00
for cfi := 0; cfi < cfSum; cfi++ {
cfStart := cfi * 13
cf := message.NewCanetFrame(cfs[cfStart : cfStart+13])
2023-11-22 11:22:37 +08:00
//
switch cf.CanFrameType() {
case message.CfReq:
s.dealWithAptReq(cf)
case message.CfStatusRsp:
s.dealWithBtmStatusRsp(cf)
case message.CfTimeSync:
s.dealWithBtmTimeSyncRsp(cf)
case message.CfMsg:
fallthrough
case message.CfMsgTimeA:
fallthrough
case message.CfMsgTimeB:
fallthrough
case message.CfMsgEnd:
dms = append(dms, cf)
default:
slog.Warn("CanetFrame帧没有具体对应的应用帧", "CannetFrame", cf.String())
} //switch
} //for
//将数据包按ID3即0x80+offset由小到大排序
sort.SliceStable(dms, func(i, j int) bool {
return dms[i].CanId.ID3 < dms[j].CanId.ID3
})
//有数据帧但是不足16帧
if len(dms) > 0 {
if len(dms) != 16 {
slog.Warn("接收到数据帧但数据帧数量不足16帧")
} else {
s.dealWithBtmDataFrames(dms)
}
2023-11-21 18:13:50 +08:00
}
2023-11-22 11:22:37 +08:00
2023-11-21 18:13:50 +08:00
} else {
slog.Warn("从cannet接收数据未满足条件len(cfs) > 0 && len(cfs)%13 == 0", "len(cfs)", len(cfs))
}
}
2023-11-22 11:22:37 +08:00
// 处理接收的ATP查询请求帧
func (s *btmCanetClient) dealWithAptReq(f *message.CanetFrame) {
atpReq := &message.AtpRequestFrame{}
if !atpReq.Decode(f) {
slog.Warn("CanetFrame解码成AtpRequestFrame失败", "CanetFrame", f.String())
2023-11-23 16:30:48 +08:00
return
2023-11-22 11:22:37 +08:00
}
//处理查询请求
slog.Debug(fmt.Sprintf("处理查询请求:%s", atpReq.String()))
2023-11-23 16:30:48 +08:00
//
2023-11-24 15:20:05 +08:00
s.btmTime.btmTk = atpReq.Time
s.btmTime.sysTk = time.Now()
2023-11-23 16:30:48 +08:00
s.atpReqSn = atpReq.FId.ID4
s.atpReqCrc16Check = atpReq.Crc16CheckOk
2023-11-24 15:20:05 +08:00
se := fi.TrainBalisePowerAmplifierSwitch(s.bcm.EvnWorld(), atpReq.PowerAmplifierTurnOn)
2023-11-23 16:30:48 +08:00
if se != nil {
2023-11-24 15:20:05 +08:00
slog.Warn(fmt.Sprintf("列车车载BTM功率放大器开关控制异常[%s]", se.Error()))
2023-11-23 16:30:48 +08:00
}
2023-11-24 15:20:05 +08:00
//ATP 是否要求BTM 重发上一应答器报文
isResendRequest := atpReq.ResendRequest == 2 //0b10
s.rspToAtp(isResendRequest)
2023-11-22 11:22:37 +08:00
}
2023-11-23 18:14:01 +08:00
// BTM发送响应给ATP
// 当收到应答器报文时响应:时间同步帧、状态应答帧、数据帧
// 当未收到应答器报文时响应:时间同步帧、状态应答帧
2023-11-24 15:20:05 +08:00
func (s *btmCanetClient) rspToAtp(isResendRequest bool) {
2023-11-24 16:01:28 +08:00
//重发上一报文处理
if isResendRequest && s.resendData != nil && s.resendData.canResend() {
s.resendData.countAdd1()
s.sendCanetFrame(s.resendData.data)
return
} else {
s.resendData = nil
}
2023-11-24 15:20:05 +08:00
//BTM状态
2023-11-23 18:14:01 +08:00
statusF := message.NewBtmStatusRspFrame(s.atpReqSn)
2023-11-24 15:20:05 +08:00
btmStatus, btmStatusErr := fi.FindTrainBaliseBtmStatus(s.bcm.EvnWorld())
if btmStatusErr != nil {
slog.Debug(fmt.Sprintf("从仿真获取BTM状态失败%s", btmStatusErr.Error()))
return
}
statusF.AntennaFault = btmStatus.AntennaFault
statusF.BaliseCounter = byte(btmStatus.BaliseCounter)
statusF.MessageCounter = byte(btmStatus.MessageCounter)
statusF.PowerAmplifierOn = btmStatus.PowerAmplifierOn
statusF.TkTimeA = s.btmTime.tkNow()
statusF.PowerAmplifierFailure = btmStatus.PowerAmplifierFault
statusF.DetailedCode = 0
if btmStatus.AboveBalise {
statusF.DetailedCode = 0x07
}
statusF.AtpReqCrcCheckWrong = !s.atpReqCrc16Check
statusF.Dsn = s.dsn
s.dsnAdd1()
//
baliseTelegram, btSendErr := fi.BaliseTelegramForSend(s.bcm.EvnWorld())
if btSendErr != nil {
//slog.Debug(btSendErr.Error())
}
2023-11-23 18:14:01 +08:00
//true-收到应答器报文
2023-11-24 15:20:05 +08:00
isRcvTelegram := baliseTelegram != nil
2023-11-23 18:14:01 +08:00
if isRcvTelegram { //当收到应答器报文时响应:时间同步帧、状态应答帧、数据帧
2023-11-24 15:20:05 +08:00
statusDataCf, statusDataCfOk := message.CreateBtmRspFramesData(statusF, baliseTelegram.Telegram, false, s.btmTime.tkNow(), s.btmTime.tkNow(), s.btmTime.tkNow())
if statusDataCfOk {
timeSyncF := message.NewBtmTimeSyncCheckFrame(s.atpReqSn)
timeSyncF.T2 = s.btmTime.btmTk
timeSyncF.T3 = s.btmTime.tkNow()
s.sendCanetFrame(timeSyncF.Encode().Encode())
//
2023-11-24 16:01:28 +08:00
s.resendData = newResendData(statusDataCf)
2023-11-24 15:20:05 +08:00
s.sendCanetFrame(statusDataCf)
} else {
slog.Warn("BtmCanetClient应答帧、数据帧编码失败")
}
2023-11-23 18:14:01 +08:00
} else { //当未收到应答器报文时响应:时间同步帧、状态应答帧
2023-11-24 15:20:05 +08:00
timeSyncF := message.NewBtmTimeSyncCheckFrame(s.atpReqSn)
timeSyncF.T2 = s.btmTime.btmTk
timeSyncF.T3 = s.btmTime.tkNow()
s.sendCanetFrame(timeSyncF.Encode().Encode())
//
2023-11-23 18:14:01 +08:00
statusCf := statusF.Encode().Encode()
s.sendCanetFrame(statusCf)
}
}
// 发送Canet帧一帧13字节
func (s *btmCanetClient) sendCanetFrame(cf []byte) {
s.udpClient.Send(cf)
}
2023-11-24 15:20:05 +08:00
func (s *btmCanetClient) dsnAdd1() {
if s.dsn >= 255 {
s.dsn = 0
} else {
s.dsn++
}
}
2023-11-23 18:14:01 +08:00
2023-11-24 16:01:28 +08:00
// 准备重发的状态应答帧和报文数据帧
type resendData struct {
data []byte //重发的数据
count int //重发次数
}
func newResendData(data []byte) *resendData {
return &resendData{
data: data,
count: 0,
}
}
func (r *resendData) canResend() bool {
return r.count <= 3
}
func (r *resendData) countAdd1() {
r.count++
}
2023-11-22 11:22:37 +08:00
//////////////////////////////////////////////////////////////////////////////////////////
// 处理接收的状态应答帧
func (s *btmCanetClient) dealWithBtmStatusRsp(f *message.CanetFrame) {
slog.Debug(fmt.Sprintf("接收到状态应答帧%s", f.String()))
}
// 处理接收的时间同步校验应答帧
func (s *btmCanetClient) dealWithBtmTimeSyncRsp(f *message.CanetFrame) {
slog.Debug(fmt.Sprintf("接收到时间同步校验应答帧%s", f.String()))
}
// 处理接收到的16帧数据帧
func (s *btmCanetClient) dealWithBtmDataFrames(dms []*message.CanetFrame) {
for _, dm := range dms {
slog.Debug(fmt.Sprintf("接收到数据帧%s", dm.String()))
}
}