122 lines
3.7 KiB
Go
122 lines
3.7 KiB
Go
package can_btm
|
||
|
||
import (
|
||
"fmt"
|
||
"joylink.club/bj-rtsts-server/const/balise_const"
|
||
"joylink.club/bj-rtsts-server/dto/state_proto"
|
||
"joylink.club/rtsssimulation/repository"
|
||
"strings"
|
||
"sync"
|
||
)
|
||
|
||
var baliseLock = &sync.Mutex{}
|
||
|
||
func IsLine12(train *state_proto.TrainState) bool {
|
||
if strings.Contains(train.ProjectCode, "12") {
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
func AddNewExpectedBalise(balise *repository.Transponder, btmCache *state_proto.TrainBtmCache, telegram, userTelegram []byte, isLine12 bool) bool {
|
||
baliseLock.Lock()
|
||
defer baliseLock.Unlock()
|
||
bl := btmCache.BaliseList
|
||
for _, tt := range bl {
|
||
if tt != nil && tt.BaliseId == balise.Id() {
|
||
return false
|
||
}
|
||
}
|
||
for i := 1; i < len(bl); i++ {
|
||
bl[i-1] = bl[i]
|
||
}
|
||
unpack := false
|
||
bc := BaliseCounterAdd(btmCache.BaliseCount, isLine12)
|
||
mc := btmCache.MessageCounter
|
||
if userTelegram != nil && len(userTelegram) > 0 {
|
||
mc = uint32(BaliseCounterAdd(mc, isLine12))
|
||
unpack = true
|
||
}
|
||
|
||
btmCache.BaliseCount = uint32(bc)
|
||
btmCache.MessageCounter = mc
|
||
btmS := &state_proto.BTMState{BaliseId: balise.Id(),
|
||
Telegram: fmt.Sprintf("%x", userTelegram),
|
||
Telegram128: fmt.Sprintf("%X", telegram),
|
||
Unpack: unpack,
|
||
BaliseType: int32(balise.BaliseType().Number()),
|
||
AboveBalise: true, HasData: true}
|
||
|
||
if userTelegram == nil || len(userTelegram) == 0 {
|
||
btmS.Telegram = strings.Repeat("00", balise_const.UserTelegramByteLen)
|
||
btmS.Telegram128 = strings.Repeat("00", balise_const.TelegramByteLen)
|
||
btmS.HasData = false
|
||
}
|
||
//存入队尾
|
||
bl[len(bl)-1] = btmS
|
||
return true
|
||
}
|
||
|
||
// HandleTrainHeadPositionInfoForTrain 处理列车位置信息
|
||
// 参数1: 参数2:发送序列号,参数3:应答器计数(每过一个应答器加一,在同一个应答器内不变),参数4:报文计数器 (每解出一个报文加一)(0~255)
|
||
func FindBaliseResend(train *state_proto.TrainState) (*state_proto.BTMState, byte, byte, byte) {
|
||
baliseLock.Lock()
|
||
defer baliseLock.Unlock()
|
||
cache := train.BtmBaliseCache
|
||
for _, balise := range cache.BaliseList {
|
||
if balise != nil && balise.BaliseId == cache.ResendBaliseId && balise.ResendCount < 3 {
|
||
balise.ResendCount++
|
||
ndsn := BaliseCounterAdd(cache.Dsn, IsLine12(train))
|
||
cache.Dsn = uint32(ndsn)
|
||
return balise, ndsn, byte(cache.BaliseCount), byte(cache.MessageCounter)
|
||
}
|
||
}
|
||
ndsn := BaliseCounterAdd(cache.Dsn, IsLine12(train))
|
||
return nil, ndsn, 0, 0
|
||
}
|
||
func FindBaliseByNotSend(train *state_proto.TrainState) (*state_proto.BTMState, byte, byte, byte) {
|
||
baliseLock.Lock()
|
||
defer baliseLock.Unlock()
|
||
cache := train.BtmBaliseCache
|
||
for _, btmCache := range cache.BaliseList {
|
||
if btmCache != nil && !btmCache.IsSend {
|
||
ndsn := BaliseCounterAdd(cache.Dsn, IsLine12(train))
|
||
cache.Dsn = uint32(ndsn)
|
||
cache.ResendBaliseId = btmCache.BaliseId
|
||
return btmCache, ndsn, byte(cache.BaliseCount), byte(cache.MessageCounter)
|
||
}
|
||
}
|
||
ndsn := BaliseCounterAdd(cache.Dsn, IsLine12(train))
|
||
return nil, ndsn, 0, 0
|
||
}
|
||
func ClearBalise(train *state_proto.TrainState) {
|
||
baliseLock.Lock()
|
||
defer baliseLock.Unlock()
|
||
train.BtmBaliseCache.BaliseList = make([]*state_proto.BTMState, 3)
|
||
}
|
||
|
||
// 11号线根据序列号查询
|
||
func FindBaliseByMessageSerial(train *state_proto.TrainState, ms byte) (*state_proto.BTMState, byte, bool) {
|
||
baliseLock.Lock()
|
||
defer baliseLock.Unlock()
|
||
cache := train.BtmBaliseCache
|
||
|
||
for _, btmCache := range cache.BaliseList {
|
||
if btmCache != nil {
|
||
if btmCache.BaliseId == cache.ResendBaliseId {
|
||
if byte(btmCache.PackageDataSN) == ms {
|
||
bt, dsn, _, _ := FindBaliseByNotSend(train)
|
||
return bt, dsn, true
|
||
} else {
|
||
ndsn := BaliseCounterAdd(cache.Dsn, IsLine12(train))
|
||
cache.Dsn = uint32(ndsn)
|
||
return btmCache, ndsn, false
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
bt, dsn, _, _ := FindBaliseByNotSend(train)
|
||
return bt, dsn, true
|
||
|
||
}
|