125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package component
|
||
|
||
import (
|
||
"fmt"
|
||
"joylink.club/ecs"
|
||
)
|
||
|
||
// TrainPositionInfo 列车当前位置信息
|
||
type TrainPositionInfo struct {
|
||
//列车头当前运行方向(true偏移量增大/false减小方向)
|
||
//link 由a->b偏移量增大
|
||
Up bool
|
||
//列车长度 mm
|
||
Len int64
|
||
//列车所在轨道link
|
||
HeadLink string
|
||
//列车所在link偏移量(mm)
|
||
HeadLinkOffset int64
|
||
//列车所在轨道link
|
||
TailLink string
|
||
//列车所在link偏移量(mm)
|
||
TailLinkOffset int64
|
||
}
|
||
|
||
func (t *TrainPositionInfo) ToString() string {
|
||
return fmt.Sprintf("Up=%t len=%d headLink=%s headOff=%d tailLink=%s tailOff=%d", t.Up, t.Len, t.HeadLink, t.HeadLinkOffset, t.TailLink, t.TailLinkOffset)
|
||
}
|
||
|
||
// TrainBaliseTelegram 应答器报文
|
||
type TrainBaliseTelegram struct {
|
||
BaliseId string //应答器ID
|
||
Telegram []byte //一个应答器同一时刻只有一条报文处于激活有效状态
|
||
sent bool //true-已经向车载ATP发送过
|
||
}
|
||
|
||
func NewTrainBaliseTelegram(baliseId string, telegram []byte) *TrainBaliseTelegram {
|
||
return &TrainBaliseTelegram{
|
||
BaliseId: baliseId,
|
||
Telegram: telegram,
|
||
sent: false,
|
||
}
|
||
}
|
||
|
||
// TrainBtm 列车应答器传输模块
|
||
type TrainBtm struct {
|
||
//应答器计数(每过一个应答器加一,在同一个应答器内不变)
|
||
BaliseCounter int
|
||
//报文计数器(每解出一个应答器报文加一)
|
||
MessageCounter int
|
||
//车载应答器天线功率放大器开关,true-开,false-关
|
||
PowerAmplifierSwitch bool
|
||
//天线此时是否在应答器上方
|
||
AboveBalise bool
|
||
//列车在运行方向顺序扫描到的应答器
|
||
ScannedBalises []*TrainBaliseTelegram
|
||
//最近经过的应答器id
|
||
viaBaliseId string
|
||
}
|
||
|
||
// 应答器计数器加1,[0,255]
|
||
func (t *TrainBtm) baliseCounterAdd1() {
|
||
t.BaliseCounter++
|
||
if t.BaliseCounter > 255 {
|
||
t.BaliseCounter = 0
|
||
}
|
||
}
|
||
|
||
// 报文计数器加1,[0,255]
|
||
func (t *TrainBtm) baliseMessageCounterAdd1() {
|
||
t.MessageCounter++
|
||
if t.MessageCounter > 255 {
|
||
t.MessageCounter = 0
|
||
}
|
||
}
|
||
|
||
// SetBaliseTelegramHadSentAndGet 获取未发送的应答器报文并标记已发送
|
||
// 通过Canet发送应答器报文时调用该方法来获取要发送的报文
|
||
func (t *TrainBtm) SetBaliseTelegramHadSentAndGet() *TrainBaliseTelegram {
|
||
for _, tbt := range t.ScannedBalises {
|
||
if !tbt.sent {
|
||
tbt.sent = true
|
||
return tbt
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
const scannedBalisesMax = 3
|
||
|
||
// Scanning BTM通过车载应答器天线接收到应答器报文
|
||
func (t *TrainBtm) Scanning(aboveBalise bool, aboveBaliseId string, telegram *TrainBaliseTelegram) {
|
||
t.AboveBalise = aboveBalise
|
||
//BTM此时在一个新的应答器上方
|
||
if t.AboveBalise && t.viaBaliseId != aboveBaliseId {
|
||
t.viaBaliseId = aboveBaliseId
|
||
t.baliseCounterAdd1()
|
||
}
|
||
//
|
||
if telegram == nil {
|
||
return
|
||
}
|
||
//过滤掉同一个应答器
|
||
for _, tbt := range t.ScannedBalises {
|
||
if tbt.BaliseId == telegram.BaliseId {
|
||
return
|
||
}
|
||
}
|
||
//一个新的应答器的报文
|
||
if len(t.ScannedBalises) < scannedBalisesMax {
|
||
t.ScannedBalises = append(t.ScannedBalises, telegram)
|
||
} else {
|
||
//move left
|
||
for i := 1; i < scannedBalisesMax; i++ {
|
||
t.ScannedBalises[i-1] = t.ScannedBalises[i]
|
||
}
|
||
t.ScannedBalises[scannedBalisesMax-1] = telegram
|
||
}
|
||
t.baliseMessageCounterAdd1()
|
||
}
|
||
|
||
var (
|
||
TrainPositionInfoType = ecs.NewComponentType[TrainPositionInfo]()
|
||
TrainBtmType = ecs.NewComponentType[TrainBtm]()
|
||
)
|