rtss-core/model/turnout.go

110 lines
2.4 KiB
Go

package model
import (
"fmt"
"log/slog"
"strings"
)
// 道岔
type Turnout interface {
ThreePortsPipeElement
// 获取道岔岔心公里标
GetKm() *KilometerMark
// 获取道岔A端公里标
GetPaKm() *KilometerMark
// 获取道岔B端公里标
GetPbKm() *KilometerMark
// 获取道岔C端公里标
GetPcKm() *KilometerMark
// 计算各个端口到岔心的距离
CalculateDistances(calculateKmDistance func(km1, km2 *KilometerMark) (int64, error)) error
// 获取到指定端口的长度
GetLength(port PipePort) int64
// 获取A端到B端的距离
GetPaPbLength() int64
// 获取A端到C端的距离
GetPaPcLength() int64
}
var _ Turnout = (*TurnoutImpl)(nil)
type TurnoutImpl struct {
*ThreePortsPipeElementImpl
Km *KilometerMark
PaKm *KilometerMark
PbKm *KilometerMark
PcKm *KilometerMark
paLen int64
pbLen int64
pcLen int64
}
func NewTurnout(uid string) *TurnoutImpl {
if strings.Trim(uid, " ") == "" {
panic("Turnout uid is empty")
}
return &TurnoutImpl{
ThreePortsPipeElementImpl: &ThreePortsPipeElementImpl{
uid: uid,
},
}
}
func (t *TurnoutImpl) GetLength(port PipePort) int64 {
switch port {
case PipePortA:
return t.paLen
case PipePortB:
return t.pbLen
case PipePortC:
return t.pcLen
}
panic(fmt.Sprintf("获取道岔指定端口长度异常:错误的端口参数'%s'", port))
}
func (t *TurnoutImpl) GetPaPbLength() int64 {
return t.pbLen + t.paLen
}
func (t *TurnoutImpl) GetPaPcLength() int64 {
return t.pcLen + t.paLen
}
func (t *TurnoutImpl) CalculateDistances(calculateKmDistance func(km1, km2 *KilometerMark) (int64, error)) error {
if t.Km == nil || t.PaKm == nil || t.PbKm == nil || t.PcKm == nil {
return fmt.Errorf("道岔公里标不能为空")
}
var err error
t.paLen, err = calculateKmDistance(t.Km, t.PaKm)
if err != nil {
return err
}
t.pbLen, err = calculateKmDistance(t.Km, t.PbKm)
if err != nil {
return err
}
t.pcLen, err = calculateKmDistance(t.Km, t.PcKm)
if err != nil {
return err
}
slog.Debug("计算道岔公里标距离", "uid", t.Uid(), "A端距离", t.paLen, "B端距离", t.pbLen, "C端距离", t.pcLen)
return nil
}
func (t *TurnoutImpl) GetKm() *KilometerMark {
return t.Km
}
func (t *TurnoutImpl) GetPaKm() *KilometerMark {
return t.PaKm
}
func (t *TurnoutImpl) GetPbKm() *KilometerMark {
return t.PbKm
}
func (t *TurnoutImpl) GetPcKm() *KilometerMark {
return t.PcKm
}