160 lines
3.6 KiB
Go
160 lines
3.6 KiB
Go
package repository
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"joylink.club/rtsssimulation/repository/model/proto"
|
||
)
|
||
|
||
// 物理区段
|
||
type PhysicalSection struct {
|
||
Identity
|
||
|
||
len int32 //长度 mm
|
||
checkPoints []*CheckPoint //将此区段分隔出来的所有(非区段边界)检测点
|
||
|
||
// A/B端关联的设备端口(非道岔物理区段/道岔)
|
||
aRelation DevicePort
|
||
bRelation DevicePort
|
||
|
||
// 非道岔物理区段A/B端的公里标
|
||
aKm *proto.Kilometer
|
||
bKm *proto.Kilometer
|
||
|
||
turnouts []*Turnout //道岔物理区段关联的道岔
|
||
|
||
// 关联的设备(目前有信号机、应答器、(非区段边界)检测点)
|
||
devices []Identity
|
||
|
||
//在Link上的区间(根据aKm和bKm计算出的,start的offset一定小于end的offset)
|
||
startLinkPosition *LinkPosition
|
||
endLinkPosition *LinkPosition
|
||
}
|
||
|
||
func NewPhysicalSection(id string) *PhysicalSection {
|
||
return &PhysicalSection{
|
||
Identity: identity{id, proto.DeviceType_DeviceType_PhysicalSection},
|
||
}
|
||
}
|
||
|
||
func (s *PhysicalSection) PortNum() int {
|
||
return 2
|
||
}
|
||
|
||
func (s *PhysicalSection) bindDevicePort(port proto.Port, devicePort DevicePort) error {
|
||
_, isSectionPort := devicePort.(*PhysicalSectionPort)
|
||
_, isTurnoutPort := devicePort.(*TurnoutPort)
|
||
if !isSectionPort && !isTurnoutPort {
|
||
return errors.New(fmt.Sprintf("物理区段不能与[%s]类型的设备端口关联", devicePort.Device().Type()))
|
||
}
|
||
switch port {
|
||
case proto.Port_A:
|
||
s.aRelation = devicePort
|
||
case proto.Port_B:
|
||
s.bRelation = devicePort
|
||
default:
|
||
return errors.New(fmt.Sprintf("物理区段无端口[%s]", port))
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// 绑定区段边界公里标。(仅限非道岔物理区段调用)
|
||
func (s *PhysicalSection) bindBoundaryKm(km *proto.Kilometer, port proto.Port) error {
|
||
switch port {
|
||
case proto.Port_A:
|
||
s.aKm = km
|
||
case proto.Port_B:
|
||
s.bKm = km
|
||
default:
|
||
return errors.New(fmt.Sprintf("区段无端口[%s]", port))
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// 道岔物理区段绑定道岔
|
||
func (s *PhysicalSection) bindTurnouts(turnouts ...*Turnout) {
|
||
for _, turnout := range turnouts {
|
||
s.turnouts = append(s.turnouts, turnout)
|
||
for _, cp := range turnout.checkPoints() {
|
||
s.bindDevices(cp)
|
||
}
|
||
}
|
||
}
|
||
|
||
func (s *PhysicalSection) bindDevices(devices ...Identity) {
|
||
for _, device := range devices {
|
||
s.devices = append(s.devices, device)
|
||
cp, ok := device.(*CheckPoint)
|
||
if ok {
|
||
s.checkPoints = append(s.checkPoints, cp)
|
||
}
|
||
}
|
||
}
|
||
|
||
func (s *PhysicalSection) bindStartLinkPosition(position *LinkPosition) {
|
||
s.startLinkPosition = position
|
||
}
|
||
|
||
func (s *PhysicalSection) bindEndLinkPosition(position *LinkPosition) {
|
||
s.endLinkPosition = position
|
||
}
|
||
|
||
func (s *PhysicalSection) ARelation() DevicePort {
|
||
return s.aRelation
|
||
}
|
||
|
||
func (s *PhysicalSection) BRelation() DevicePort {
|
||
return s.bRelation
|
||
}
|
||
|
||
func (s *PhysicalSection) findOtherDevicePort(port proto.Port) DevicePort {
|
||
switch port {
|
||
case proto.Port_A:
|
||
return s.bRelation
|
||
case proto.Port_B:
|
||
return s.aRelation
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *PhysicalSection) findOtherBoundaryKmByPort(port proto.Port) *proto.Kilometer {
|
||
switch port {
|
||
case proto.Port_A:
|
||
return s.bKm
|
||
case proto.Port_B:
|
||
return s.aKm
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *PhysicalSection) findBoundaryKmByPort(port proto.Port) *proto.Kilometer {
|
||
switch port {
|
||
case proto.Port_A:
|
||
return s.aKm
|
||
case proto.Port_B:
|
||
return s.bKm
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *PhysicalSection) StartLinkPosition() *LinkPosition {
|
||
return s.startLinkPosition
|
||
}
|
||
|
||
func (s *PhysicalSection) EndLinkPosition() *LinkPosition {
|
||
return s.endLinkPosition
|
||
}
|
||
|
||
type PhysicalSectionPort struct {
|
||
section *PhysicalSection
|
||
port proto.Port
|
||
}
|
||
|
||
func (p *PhysicalSectionPort) Port() proto.Port {
|
||
return p.port
|
||
}
|
||
|
||
func (p *PhysicalSectionPort) Device() PortedDevice {
|
||
return p.section
|
||
}
|