2024-06-07 18:38:38 +08:00
|
|
|
package model
|
|
|
|
|
2024-06-26 19:00:01 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2024-06-17 19:45:38 +08:00
|
|
|
|
|
|
|
// 道岔
|
2024-06-07 18:38:38 +08:00
|
|
|
type Turnout interface {
|
2024-06-17 19:45:38 +08:00
|
|
|
PipeElement
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:20:11 +08:00
|
|
|
var _ Turnout = (*TurnoutImpl)(nil)
|
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
type TurnoutImpl struct {
|
|
|
|
uid string
|
|
|
|
paPipeLink *PipeLink
|
|
|
|
pbPipeLink *PipeLink
|
|
|
|
pcPipeLink *PipeLink
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
uid: uid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:00:01 +08:00
|
|
|
func (t *TurnoutImpl) SetLinkedElement(port PipePort, pipeLink *PipeLink) error {
|
|
|
|
if port == PipePortA {
|
|
|
|
if t.paPipeLink != nil {
|
|
|
|
return fmt.Errorf("道岔uid=%s端口A已经设置关联元素: {uid=%s, port=%s}", t.uid, t.paPipeLink.Pipe.Uid(), t.paPipeLink.Port)
|
|
|
|
}
|
|
|
|
t.paPipeLink = pipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
if t.pbPipeLink != nil {
|
|
|
|
return fmt.Errorf("道岔uid=%s端口B已经设置关联元素: {uid=%s, port=%s}", t.uid, t.pbPipeLink.Pipe.Uid(), t.pbPipeLink.Port)
|
|
|
|
}
|
|
|
|
t.pbPipeLink = pipeLink
|
|
|
|
} else if port == PipePortC {
|
|
|
|
if t.pcPipeLink != nil {
|
|
|
|
return fmt.Errorf("道岔uid=%s端口C已经设置关联元素: {uid=%s, port=%s}", t.uid, t.pcPipeLink.Pipe.Uid(), t.pcPipeLink.Port)
|
|
|
|
}
|
|
|
|
t.pcPipeLink = pipeLink
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TurnoutImpl) CheckPipeLink() error {
|
|
|
|
err := checkPipePortLink(t, PipePortA, t.paPipeLink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = checkPipePortLink(t, PipePortB, t.pbPipeLink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = checkPipePortLink(t, PipePortC, t.pcPipeLink)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPipePortLink(pe PipeElement, port PipePort, pipeLink *PipeLink) error {
|
|
|
|
if pipeLink == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
plink := pipeLink.Pipe.GetLinkedElement(pipeLink.Port)
|
|
|
|
if plink == nil {
|
|
|
|
return fmt.Errorf("通道端口{uid=%s, port=%s}关联通道端口{uid=%s, port=%s},但关联通道的对应端口却未关联通道端口", pe.Uid(), port, pipeLink.Pipe.Uid(), pipeLink.Port)
|
|
|
|
} else if plink.Pipe.Uid() != pe.Uid() || plink.Port != port {
|
|
|
|
return fmt.Errorf("通道端口{uid=%s, port=%s}关联通道端口{uid=%s, port=%s},但关联通道的对应端口却关联了其他通道端口: {uid=%s, port=%s}", pe.Uid(), port, pipeLink.Pipe.Uid(), pipeLink.Port,
|
|
|
|
plink.Pipe.Uid(), plink.Port)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
func (t *TurnoutImpl) Uid() string {
|
|
|
|
return t.uid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TurnoutImpl) Ports() []PipePort {
|
|
|
|
return threePorts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TurnoutImpl) GetLinkedElement(port PipePort) *PipeLink {
|
|
|
|
if port == PipePortA {
|
|
|
|
return t.paPipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
return t.pbPipeLink
|
|
|
|
} else if port == PipePortC {
|
|
|
|
return t.pcPipeLink
|
|
|
|
}
|
|
|
|
return nil
|
2024-06-07 18:38:38 +08:00
|
|
|
}
|