rtss-core/model/section.go

123 lines
2.8 KiB
Go
Raw Normal View History

2024-06-07 18:38:38 +08:00
package model
import (
"fmt"
"log/slog"
)
2024-06-17 19:45:38 +08:00
// 区段
2024-06-07 18:38:38 +08:00
type Section interface {
RtssModel
}
// 物理区段(非道岔区段)
// 用于和道岔连接构成轨道网络
// 然后使用轨道网络构建link和linkNode形成新的以道岔岔心为节点的网络用于路径计算等
type PhysicalSection interface {
TwoPortsPipeElement
// 获取A端和B端的公里标
GetPaKm() *KilometerMark
GetPbKm() *KilometerMark
// 计算各个端口到岔心的距离
CalculateDistance(calculateKmDistance func(km1, km2 *KilometerMark) (int64, error)) error
GetLength() int64
2024-06-17 19:45:38 +08:00
}
// 道岔区段(道岔物理区段)
// 道岔处的由至少3个及以上的计轴或绝缘节所确定的区段
type TurnoutSection interface {
Section
// 获取关联的道岔列表
GetTurnouts() []Turnout
// 添加关联道岔
AddTurnout(turnout Turnout)
}
// 逻辑区段
// 是在物理区段基础上进一步细分的区段,为了相对更精细的列车追踪和进路触发等控制
// 最终映射到link上做相应的逻辑处理
type LogicalSection interface {
Section
// 所属物理区段(可能是一般物理区段也可能是道岔区段)
BelongSection() Section
// 获取A端和B端的公里标
GetPaKm() *KilometerMark
GetPbKm() *KilometerMark
}
type PhysicalSectionImpl struct {
*TwoPortsPipeElementImpl
PaKm *KilometerMark
PbKm *KilometerMark
len int64
2024-06-17 19:45:38 +08:00
}
var _ PhysicalSection = (*PhysicalSectionImpl)(nil)
2024-06-24 18:20:11 +08:00
func NewPhysicalSection(uid string) *PhysicalSectionImpl {
return &PhysicalSectionImpl{
TwoPortsPipeElementImpl: &TwoPortsPipeElementImpl{
2024-06-27 19:59:13 +08:00
uid: uid,
},
2024-06-17 19:45:38 +08:00
}
2024-06-07 18:38:38 +08:00
}
func (s *PhysicalSectionImpl) GetLength() int64 {
return s.len
}
func (s *PhysicalSectionImpl) CalculateDistance(calculateKmDistance func(km1, km2 *KilometerMark) (int64, error)) error {
if s.PaKm == nil || s.PbKm == nil {
return fmt.Errorf("区段公里标不能为空")
}
var err error
s.len, err = calculateKmDistance(s.PaKm, s.PbKm)
slog.Debug("计算物理区段公里标距离", "uid", s.Uid(), "length", s.len)
return err
}
func (s *PhysicalSectionImpl) GetPaKm() *KilometerMark {
return s.PaKm
}
func (s *PhysicalSectionImpl) GetPbKm() *KilometerMark {
return s.PbKm
}
type SectionImpl struct {
uid string
}
func (s *SectionImpl) Uid() string {
return s.uid
}
var _ TurnoutSection = (*TurnoutSectionImpl)(nil)
type TurnoutSectionImpl struct {
*SectionImpl
Turnouts []Turnout
}
func NewTurnoutSection(uid string) *TurnoutSectionImpl {
return &TurnoutSectionImpl{
SectionImpl: &SectionImpl{
uid: uid,
},
Turnouts: make([]Turnout, 0),
}
}
func (t *TurnoutSectionImpl) AddTurnout(turnout Turnout) {
for _, exist := range t.Turnouts {
if exist.Uid() == turnout.Uid() {
return
}
}
t.Turnouts = append(t.Turnouts, turnout)
}
func (t *TurnoutSectionImpl) GetTurnouts() []Turnout {
return t.Turnouts
}