2024-06-07 18:38:38 +08:00
|
|
|
package model
|
|
|
|
|
2024-06-27 19:59:13 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-02 18:30:12 +08:00
|
|
|
"log/slog"
|
2024-06-27 19:59:13 +08:00
|
|
|
)
|
|
|
|
|
2024-06-07 18:38:38 +08:00
|
|
|
type RtssModel interface {
|
|
|
|
// RtssModel unique id
|
|
|
|
Uid() string
|
|
|
|
}
|
2024-06-14 16:56:55 +08:00
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
// 通道端口
|
2024-06-14 16:56:55 +08:00
|
|
|
type PipePort = string
|
|
|
|
|
|
|
|
var (
|
2024-06-17 19:45:38 +08:00
|
|
|
// PipePortA 通道端口A
|
2024-06-14 16:56:55 +08:00
|
|
|
PipePortA PipePort = "A"
|
2024-06-17 19:45:38 +08:00
|
|
|
// PipePortB 通道端口B
|
2024-06-14 16:56:55 +08:00
|
|
|
PipePortB PipePort = "B"
|
2024-06-17 19:45:38 +08:00
|
|
|
// PipePortC 通道端口C
|
2024-06-14 16:56:55 +08:00
|
|
|
PipePortC PipePort = "C"
|
|
|
|
)
|
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
var (
|
|
|
|
twoPorts = []PipePort{PipePortA, PipePortB}
|
|
|
|
threePorts = []PipePort{PipePortA, PipePortB, PipePortC}
|
|
|
|
)
|
|
|
|
|
|
|
|
// 通道连接关系
|
2024-06-14 16:56:55 +08:00
|
|
|
type PipeLink struct {
|
2024-06-17 19:45:38 +08:00
|
|
|
// 通道元素
|
|
|
|
Pipe PipeElement
|
2024-06-14 16:56:55 +08:00
|
|
|
// 端口
|
|
|
|
Port PipePort
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:00:01 +08:00
|
|
|
func NewPipeLink(pipe PipeElement, port PipePort) *PipeLink {
|
|
|
|
if pipe == nil {
|
|
|
|
panic("pipe is nil")
|
|
|
|
}
|
|
|
|
return &PipeLink{
|
|
|
|
Pipe: pipe,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PipeLink) Debug() string {
|
2024-07-02 18:30:12 +08:00
|
|
|
return DebugPipeLink(pl.Pipe, pl.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DebugPipeLink(p PipeElement, port PipePort) string {
|
|
|
|
return fmt.Sprintf("{pipeUid=%s, port=%s}", p.Uid(), port)
|
2024-06-26 19:00:01 +08:00
|
|
|
}
|
|
|
|
|
2024-06-17 19:45:38 +08:00
|
|
|
// 通道元素
|
|
|
|
type PipeElement interface {
|
2024-06-26 19:00:01 +08:00
|
|
|
RtssModel
|
2024-06-17 19:45:38 +08:00
|
|
|
// 获取通道端口
|
|
|
|
Ports() []PipePort
|
2024-06-27 19:59:13 +08:00
|
|
|
IsTwoPorts() bool
|
|
|
|
IsThreePorts() bool
|
2024-06-17 19:45:38 +08:00
|
|
|
// 获取指定端口连接的通道连接关系
|
|
|
|
GetLinkedElement(port PipePort) *PipeLink
|
2024-07-02 18:30:12 +08:00
|
|
|
// 设置指定端口的通道连接关系
|
|
|
|
SetLinkedElement(port PipePort, pipeLink *PipeLink) error
|
|
|
|
// 检查通道连接关系
|
|
|
|
CheckPipeLink() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type TwoPortsPipeElement interface {
|
|
|
|
PipeElement
|
|
|
|
// 获取指定端口的对端通道连接关系
|
|
|
|
OppositePipeLink(port PipePort) *PipeLink
|
|
|
|
}
|
|
|
|
|
|
|
|
type ThreePortsPipeElement interface {
|
|
|
|
PipeElement
|
2024-06-14 16:56:55 +08:00
|
|
|
}
|
2024-06-26 19:00:01 +08:00
|
|
|
|
2024-06-27 19:59:13 +08:00
|
|
|
func checkPipePortLink(pe PipeElement, port PipePort, pipeLink *PipeLink) error {
|
|
|
|
if pipeLink == nil {
|
2024-07-10 18:09:16 +08:00
|
|
|
slog.Debug("检查通道端口连接关系", "通道端口", DebugPipeLink(pe, port), "连接关系", "无连接")
|
2024-06-27 19:59:13 +08:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-02 18:30:12 +08:00
|
|
|
slog.Debug("检查通道端口连接关系", "通道端口", DebugPipeLink(pe, port), "连接关系", pipeLink.Debug())
|
2024-06-27 19:59:13 +08:00
|
|
|
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-07-02 18:30:12 +08:00
|
|
|
var _ TwoPortsPipeElement = (*TwoPortsPipeElementImpl)(nil)
|
2024-06-27 19:59:13 +08:00
|
|
|
|
|
|
|
// 两端口的线性通道元素
|
2024-07-02 18:30:12 +08:00
|
|
|
type TwoPortsPipeElementImpl struct {
|
2024-06-26 19:00:01 +08:00
|
|
|
uid string
|
|
|
|
paPipeLink *PipeLink
|
|
|
|
pbPipeLink *PipeLink
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *TwoPortsPipeElementImpl) Uid() string {
|
2024-06-27 19:59:13 +08:00
|
|
|
return t.uid
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *TwoPortsPipeElementImpl) Ports() []PipePort {
|
2024-06-26 19:00:01 +08:00
|
|
|
return twoPorts
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *TwoPortsPipeElementImpl) IsTwoPorts() bool {
|
2024-06-27 19:59:13 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *TwoPortsPipeElementImpl) IsThreePorts() bool {
|
2024-06-27 19:59:13 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (s *TwoPortsPipeElementImpl) GetLinkedElement(port PipePort) *PipeLink {
|
2024-06-27 19:59:13 +08:00
|
|
|
if port == PipePortA {
|
|
|
|
return s.paPipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
return s.pbPipeLink
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *TwoPortsPipeElementImpl) OppositePipeLink(port PipePort) *PipeLink {
|
2024-06-26 19:00:01 +08:00
|
|
|
if port == PipePortA {
|
2024-07-01 19:49:24 +08:00
|
|
|
return t.pbPipeLink
|
2024-06-26 19:00:01 +08:00
|
|
|
}
|
2024-07-01 19:49:24 +08:00
|
|
|
return t.paPipeLink
|
2024-06-26 19:00:01 +08:00
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (s *TwoPortsPipeElementImpl) SetLinkedElement(port PipePort, pipeLink *PipeLink) error {
|
2024-06-27 19:59:13 +08:00
|
|
|
if port == PipePortA {
|
|
|
|
if s.paPipeLink != nil {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("区段{uid=%s}端口A已经设置关联元素: {uid=%s, port=%s}", s.uid, s.paPipeLink.Pipe.Uid(), s.paPipeLink.Port)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
s.paPipeLink = pipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
if s.pbPipeLink != nil {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("区段{uid=%s}端口B已经设置关联元素: {uid=%s, port=%s}", s.uid, s.pbPipeLink.Pipe.Uid(), s.pbPipeLink.Port)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
s.pbPipeLink = pipeLink
|
|
|
|
} else {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("区段{uid=%s}设置关联元素端口错误,不支持C端口", s.uid)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (s *TwoPortsPipeElementImpl) CheckPipeLink() error {
|
2024-07-08 19:47:30 +08:00
|
|
|
if s.paPipeLink == nil && s.pbPipeLink == nil {
|
|
|
|
return fmt.Errorf("区段{uid=%s}两端都没有关联通道连接关系", s.uid)
|
|
|
|
}
|
2024-06-27 19:59:13 +08:00
|
|
|
err := checkPipePortLink(s, PipePortA, s.paPipeLink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = checkPipePortLink(s, PipePortB, s.pbPipeLink)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
var _ PipeElement = (*ThreePortsPipeElementImpl)(nil)
|
2024-06-27 19:59:13 +08:00
|
|
|
|
|
|
|
// 三端口的通道元素
|
2024-07-02 18:30:12 +08:00
|
|
|
type ThreePortsPipeElementImpl struct {
|
2024-06-27 19:59:13 +08:00
|
|
|
uid string
|
|
|
|
paPipeLink *PipeLink
|
|
|
|
pbPipeLink *PipeLink
|
|
|
|
pcPipeLink *PipeLink
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) Uid() string {
|
2024-06-27 19:59:13 +08:00
|
|
|
return t.uid
|
|
|
|
}
|
2024-06-26 19:00:01 +08:00
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) Ports() []PipePort {
|
2024-06-26 19:00:01 +08:00
|
|
|
return threePorts
|
|
|
|
}
|
2024-06-27 19:59:13 +08:00
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) IsTwoPorts() bool {
|
2024-06-27 19:59:13 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) IsThreePorts() bool {
|
2024-06-27 19:59:13 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) GetLinkedElement(port PipePort) *PipeLink {
|
2024-06-27 19:59:13 +08:00
|
|
|
if port == PipePortA {
|
|
|
|
return t.paPipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
return t.pbPipeLink
|
|
|
|
} else if port == PipePortC {
|
|
|
|
return t.pcPipeLink
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) SetLinkedElement(port PipePort, pipeLink *PipeLink) error {
|
2024-06-27 19:59:13 +08:00
|
|
|
if port == PipePortA {
|
|
|
|
if t.paPipeLink != nil {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("道岔{uid=%s}端口A已经设置关联元素: {uid=%s, port=%s}", t.uid, t.paPipeLink.Pipe.Uid(), t.paPipeLink.Port)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
t.paPipeLink = pipeLink
|
|
|
|
} else if port == PipePortB {
|
|
|
|
if t.pbPipeLink != nil {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("道岔{uid=%s}端口B已经设置关联元素: {uid=%s, port=%s}", t.uid, t.pbPipeLink.Pipe.Uid(), t.pbPipeLink.Port)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
t.pbPipeLink = pipeLink
|
|
|
|
} else if port == PipePortC {
|
|
|
|
if t.pcPipeLink != nil {
|
2024-07-08 19:47:30 +08:00
|
|
|
return fmt.Errorf("道岔{uid=%s}端口C已经设置关联元素: {uid=%s, port=%s}", t.uid, t.pcPipeLink.Pipe.Uid(), t.pcPipeLink.Port)
|
2024-06-27 19:59:13 +08:00
|
|
|
}
|
|
|
|
t.pcPipeLink = pipeLink
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:30:12 +08:00
|
|
|
func (t *ThreePortsPipeElementImpl) CheckPipeLink() error {
|
2024-06-27 19:59:13 +08:00
|
|
|
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
|
|
|
|
}
|