rts-sim-module/repository/physical_section.go

217 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package repository
import (
"fmt"
"joylink.club/rtsssimulation/repository/model/proto"
"joylink.club/rtsssimulation/util/number"
)
// PhysicalSection 物理区段
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
//aKm/bKm对应的LinkPosition
aLinkPosition *LinkPosition
bLinkPosition *LinkPosition
//在Link上的区间start小于end
linkRanges []*LinkRange
//物理区段所属集中站编号code
centralizedStation string
// 所属站台
platform *Platform
//电子元件
componentGroups []*ElectronicComponentGroup
}
func (p *PhysicalSection) ComponentGroups() []*ElectronicComponentGroup {
return p.componentGroups
}
func NewPhysicalSection(id string) *PhysicalSection {
return &PhysicalSection{
Identity: identity{id, proto.DeviceType_DeviceType_PhysicalSection},
}
}
func (p *PhysicalSection) bindLinkRange(link *Link, one int64, two int64) {
p.linkRanges = append(p.linkRanges, &LinkRange{
link: link,
start: number.Min(one, two),
end: number.Max(one, two),
})
}
func (p *PhysicalSection) PortNum() int {
return 2
}
func (p *PhysicalSection) ALinkPosition() *LinkPosition {
return p.aLinkPosition
}
func (p *PhysicalSection) BLinkPosition() *LinkPosition {
return p.bLinkPosition
}
func (p *PhysicalSection) ARelation() DevicePort {
return p.aRelation
}
func (p *PhysicalSection) BRelation() DevicePort {
return p.bRelation
}
func (p *PhysicalSection) AKilometer() *proto.Kilometer {
return p.aKm
}
func (p *PhysicalSection) BKilometer() *proto.Kilometer {
return p.bKm
}
// IsAxleSection 判断是否为计轴区段
func (p *PhysicalSection) IsAxleSection() (bool, error) {
if len(p.checkPoints) > 0 {
axleCount := 0
for _, cp := range p.checkPoints {
if cp.pointType == proto.CheckPointType_AxleCounter {
axleCount++
}
}
if axleCount == len(p.checkPoints) {
return true, nil
} else if axleCount == 0 {
return false, nil
} else { //axleCount>0&&axleCount<len(s.checkPoints)
return false, fmt.Errorf("物理区段中检测点不纯")
}
} else {
return false, fmt.Errorf("物理区段没有检测点")
}
}
func (p *PhysicalSection) CentralizedStation() string {
return p.centralizedStation
}
func (p *PhysicalSection) bindDevicePort(port proto.Port, devicePort DevicePort) error {
_, isSectionPort := devicePort.(*PhysicalSectionPort)
_, isTurnoutPort := devicePort.(*TurnoutPort)
if !isSectionPort && !isTurnoutPort {
return fmt.Errorf("物理区段不能与[%s]类型的设备端口关联", devicePort.Device().Type())
}
switch port {
case proto.Port_A:
p.aRelation = devicePort
case proto.Port_B:
p.bRelation = devicePort
default:
return fmt.Errorf("物理区段无端口[%s]", port)
}
return nil
}
// 绑定区段边界公里标。(仅限非道岔物理区段调用)
func (p *PhysicalSection) bindBoundaryKm(km *proto.Kilometer, port proto.Port) error {
if km == nil || (km.CoordinateSystem == "" && km.Value == 0) {
return nil
}
switch port {
case proto.Port_A:
p.aKm = km
case proto.Port_B:
p.bKm = km
default:
return fmt.Errorf("区段无端口[%s]", port)
}
return nil
}
// 道岔物理区段绑定道岔
func (p *PhysicalSection) bindTurnouts(turnouts ...*Turnout) {
for _, turnout := range turnouts {
p.turnouts = append(p.turnouts, turnout)
for _, cp := range turnout.checkPoints() {
p.bindDevices(cp)
}
}
}
func (p *PhysicalSection) bindDevices(devices ...Identity) {
for _, device := range devices {
p.devices = append(p.devices, device)
cp, ok := device.(*CheckPoint)
if ok {
p.checkPoints = append(p.checkPoints, cp)
}
}
}
func (p *PhysicalSection) findOtherDevicePort(port proto.Port) DevicePort {
switch port {
case proto.Port_A:
return p.bRelation
case proto.Port_B:
return p.aRelation
}
return nil
}
func (p *PhysicalSection) findOtherBoundaryKmByPort(port proto.Port) *proto.Kilometer {
switch port {
case proto.Port_A:
return p.bKm
case proto.Port_B:
return p.aKm
}
return nil
}
func (p *PhysicalSection) findBoundaryKmByPort(port proto.Port) *proto.Kilometer {
switch port {
case proto.Port_A:
return p.aKm
case proto.Port_B:
return p.bKm
}
return nil
}
func (p *PhysicalSection) LinkRanges() []*LinkRange {
return p.linkRanges
}
func (p *PhysicalSection) Platform() *Platform {
return p.platform
}
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
}