2024-06-07 18:38:38 +08:00
|
|
|
package model
|
|
|
|
|
2024-06-26 19:00:01 +08:00
|
|
|
import (
|
2024-07-10 18:09:16 +08:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
2024-06-26 19:00:01 +08:00
|
|
|
"strings"
|
|
|
|
)
|
2024-06-17 19:45:38 +08:00
|
|
|
|
|
|
|
// 道岔
|
2024-06-07 18:38:38 +08:00
|
|
|
type Turnout interface {
|
2024-07-02 18:30:12 +08:00
|
|
|
ThreePortsPipeElement
|
2024-07-04 20:16:10 +08:00
|
|
|
// 获取道岔岔心公里标
|
2024-07-05 13:22:01 +08:00
|
|
|
GetKm() *KilometerMark
|
2024-07-04 20:16:10 +08:00
|
|
|
// 获取道岔A端公里标
|
2024-07-05 13:22:01 +08:00
|
|
|
GetPaKm() *KilometerMark
|
2024-07-04 20:16:10 +08:00
|
|
|
// 获取道岔B端公里标
|
2024-07-05 13:22:01 +08:00
|
|
|
GetPbKm() *KilometerMark
|
2024-07-04 20:16:10 +08:00
|
|
|
// 获取道岔C端公里标
|
2024-07-05 13:22:01 +08:00
|
|
|
GetPcKm() *KilometerMark
|
2024-07-10 18:09:16 +08:00
|
|
|
// 计算各个端口到岔心的距离
|
|
|
|
CalculateDistances(calculateKmDistance func(km1, km2 *KilometerMark) (int64, error)) error
|
|
|
|
// 获取到指定端口的长度
|
|
|
|
GetLength(port PipePort) int64
|
|
|
|
// 获取A端到B端的距离
|
|
|
|
GetPaPbLength() int64
|
|
|
|
// 获取A端到C端的距离
|
|
|
|
GetPaPcLength() int64
|
2024-06-17 19:45:38 +08:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:20:11 +08:00
|
|
|
var _ Turnout = (*TurnoutImpl)(nil)
|
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
type TurnoutImpl struct {
|
2024-07-02 18:30:12 +08:00
|
|
|
*ThreePortsPipeElementImpl
|
2024-07-10 18:09:16 +08:00
|
|
|
Km *KilometerMark
|
|
|
|
PaKm *KilometerMark
|
|
|
|
PbKm *KilometerMark
|
|
|
|
PcKm *KilometerMark
|
|
|
|
paLen int64
|
|
|
|
pbLen int64
|
|
|
|
pcLen int64
|
2024-06-17 19:45:38 +08:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:20:11 +08:00
|
|
|
func NewTurnout(uid string) *TurnoutImpl {
|
2024-06-17 19:45:38 +08:00
|
|
|
if strings.Trim(uid, " ") == "" {
|
|
|
|
panic("Turnout uid is empty")
|
|
|
|
}
|
|
|
|
return &TurnoutImpl{
|
2024-07-02 18:30:12 +08:00
|
|
|
ThreePortsPipeElementImpl: &ThreePortsPipeElementImpl{
|
2024-06-27 19:59:13 +08:00
|
|
|
uid: uid,
|
|
|
|
},
|
2024-06-17 19:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-04 20:16:10 +08:00
|
|
|
|
2024-07-10 18:09:16 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-05 13:22:01 +08:00
|
|
|
func (t *TurnoutImpl) GetKm() *KilometerMark {
|
2024-07-04 20:16:10 +08:00
|
|
|
return t.Km
|
|
|
|
}
|
|
|
|
|
2024-07-05 13:22:01 +08:00
|
|
|
func (t *TurnoutImpl) GetPaKm() *KilometerMark {
|
2024-07-04 20:16:10 +08:00
|
|
|
return t.PaKm
|
|
|
|
}
|
|
|
|
|
2024-07-05 13:22:01 +08:00
|
|
|
func (t *TurnoutImpl) GetPbKm() *KilometerMark {
|
2024-07-04 20:16:10 +08:00
|
|
|
return t.PbKm
|
|
|
|
}
|
|
|
|
|
2024-07-05 13:22:01 +08:00
|
|
|
func (t *TurnoutImpl) GetPcKm() *KilometerMark {
|
2024-07-04 20:16:10 +08:00
|
|
|
return t.PcKm
|
|
|
|
}
|