diff --git a/component/train.go b/component/train.go index cb91b0c..22f70dc 100644 --- a/component/train.go +++ b/component/train.go @@ -3,6 +3,7 @@ package component import ( "fmt" "joylink.club/ecs" + "log/slog" ) // TrainPositionInfo 列车当前位置信息 @@ -52,7 +53,7 @@ type TrainBtm struct { //天线此时是否在应答器上方 AboveBalise bool //列车在运行方向顺序扫描到的应答器 - ScannedBalises []*TrainBaliseTelegram + ScannedBalise *TrainBaliseTelegram //最近经过的应答器id viaBaliseId string } @@ -76,11 +77,9 @@ func (t *TrainBtm) baliseMessageCounterAdd1() { // SetBaliseTelegramHadSentAndGet 获取未发送的应答器报文并标记已发送 // 通过Canet发送应答器报文时调用该方法来获取要发送的报文 func (t *TrainBtm) SetBaliseTelegramHadSentAndGet() *TrainBaliseTelegram { - for _, tbt := range t.ScannedBalises { - if !tbt.sent { - tbt.sent = true - return tbt - } + if t.ScannedBalise != nil && !t.ScannedBalise.sent { + t.ScannedBalise.sent = true + return t.ScannedBalise } return nil } @@ -94,27 +93,13 @@ func (t *TrainBtm) Scanning(aboveBalise bool, aboveBaliseId string, telegram *Tr if t.AboveBalise && t.viaBaliseId != aboveBaliseId { t.viaBaliseId = aboveBaliseId t.baliseCounterAdd1() + slog.Debug(fmt.Sprintf("列车经过应答器上方,应答器:[%s]", t.viaBaliseId)) } // 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.ScannedBalise = telegram t.baliseMessageCounterAdd1() } diff --git a/repository/repository.go b/repository/repository.go index f758aa7..b080887 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -124,6 +124,15 @@ func (repo *Repository) ResponderList() []*Transponder { } return list } +func (repo *Repository) ResponderListByLink(linkId string) []*Transponder { + var list []*Transponder + for _, model := range repo.responderMap { + if model.linkPosition.link.Id() == linkId { + list = append(list, model) + } + } + return list +} func (repo *Repository) SlopeList() []*Slope { var list []*Slope for _, model := range repo.slopeMap { diff --git a/sys/device_sys/balise_detection.go b/sys/device_sys/balise_detection.go index 69837a4..0c698db 100644 --- a/sys/device_sys/balise_detection.go +++ b/sys/device_sys/balise_detection.go @@ -8,6 +8,8 @@ import ( "joylink.club/rtsssimulation/entity" "joylink.club/rtsssimulation/repository" "log/slog" + "sort" + "strings" ) // BaliseDetectSystem 列车应答器天线探测轨旁应答器 @@ -24,13 +26,13 @@ func NewBaliseDetectSystem() *BaliseDetectSystem { } func (s *BaliseDetectSystem) Update(w ecs.World) { wd := entity.GetWorldData(w) - balises := wd.Repo.ResponderList() //所有列车 //列车速度80KM/H时,222mm/10ms s.trainQuery.Each(w, func(entry *ecs.Entry) { btm := component.TrainBtmType.Get(entry) if btm.PowerAmplifierSwitch { //车载应答器天线功率放大器开启 tp := component.TrainPositionInfoType.Get(entry) + balises := wd.Repo.ResponderListByLink(tp.HeadLink) detectedBalise := s.detect(wd, tp, balises) //列车应答器天线扫描到应答器,获取应答器报文发送给BTM tbt := s.findBaliseTelegram(wd, detectedBalise) @@ -52,6 +54,9 @@ func (s *BaliseDetectSystem) findBaliseTelegram(wd *component.WorldData, detecte baliseEntry := wd.EntityMap[detectedBalise.Id()] if baliseEntry != nil { baliseState := component.BaliseStateType.Get(baliseEntry) + //测试,设置应答器默认报文 + baliseState.ValidTelegram = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} + // if len(baliseState.ValidTelegram) > 0 { return component.NewTrainBaliseTelegram(detectedBalise.Id(), baliseState.ValidTelegram) } @@ -63,6 +68,15 @@ func (s *BaliseDetectSystem) findBaliseTelegram(wd *component.WorldData, detecte } func (s *BaliseDetectSystem) detect(wd *component.WorldData, tp *component.TrainPositionInfo, balises []*repository.Transponder) *repository.Transponder { scanRange := s.calculateScanRange(wd, tp) + if tp.Up { + sort.SliceStable(balises, func(i, j int) bool { + return balises[j].LinkPosition().Offset() < balises[i].LinkPosition().Offset() + }) + } else { + sort.SliceStable(balises, func(i, j int) bool { + return balises[i].LinkPosition().Offset() < balises[j].LinkPosition().Offset() + }) + } for _, balise := range balises { if scanRange.contains(balise.LinkPosition()) { return balise @@ -104,6 +118,18 @@ func (s *scanRange) format() { s.startOffset, s.endOffset = s.endOffset, s.startOffset } } +func (s *scanRange) str() string { + sb := strings.Builder{} + sb.WriteString("LinkRange[") + sb.WriteString("linkId:") + sb.WriteString(s.linkId) + sb.WriteString(" start:") + sb.WriteString(fmt.Sprintf("%d", s.startOffset)) + sb.WriteString(" end:") + sb.WriteString(fmt.Sprintf("%d", s.endOffset)) + sb.WriteString("]") + return sb.String() +} // true-应答器balisePosition在该scanRange内 func (s *scanRange) contains(balisePosition *repository.LinkPosition) bool {