rts-sim-module/repository/turnout.go

166 lines
3.3 KiB
Go
Raw Normal View History

package repository
import (
"errors"
"fmt"
"joylink.club/rtsssimulation/repository/model/proto"
)
type Turnout struct {
Identity
// 岔心的公里标
km *proto.Kilometer
// 道岔关联的道岔物理区段
section *PhysicalSection
// A/B/C端口关联的设备端口
aDevicePort DevicePort
bDevicePort DevicePort
cDevicePort DevicePort
// A/B/C端口的区段边界的公里标
aKm *proto.Kilometer
bKm *proto.Kilometer
cKm *proto.Kilometer
// A/B/C方向关联的设备目前有信号机、应答器、(非区段边界)检测点)
aDevices []Identity
bDevices []Identity
cDevices []Identity
}
func NewTurnout(id string, km *proto.Kilometer) *Turnout {
return &Turnout{
Identity: identity{id, proto.DeviceType_DeviceType_Turnout},
km: km,
}
}
func (t *Turnout) PortNum() int {
return 3
}
func (t *Turnout) bindPhysicalSection(section *PhysicalSection) {
t.section = section
}
func (t *Turnout) 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:
t.aDevicePort = devicePort
case proto.Port_B:
t.bDevicePort = devicePort
case proto.Port_C:
t.cDevicePort = devicePort
default:
return errors.New(fmt.Sprintf("道岔无端口[%s]", port))
}
return nil
}
func (t *Turnout) bindBoundaryKm(km *proto.Kilometer, port proto.Port) error {
switch port {
case proto.Port_A:
t.aKm = km
case proto.Port_B:
t.bKm = km
case proto.Port_C:
t.cKm = km
default:
return errors.New(fmt.Sprintf("道岔无端口[%s]", port))
}
return nil
}
func (t *Turnout) bindDevice(device Identity, port proto.Port) {
switch port {
case proto.Port_A:
t.aDevices = append(t.aDevices, device)
case proto.Port_B:
t.bDevices = append(t.bDevices, device)
case proto.Port_C:
t.cDevices = append(t.cDevices, device)
}
}
func (t *Turnout) checkPoints() []*CheckPoint {
var cps []*CheckPoint
for _, device := range t.aDevices {
cp, ok := device.(*CheckPoint)
if ok {
cps = append(cps, cp)
}
}
for _, device := range t.bDevices {
cp, ok := device.(*CheckPoint)
if ok {
cps = append(cps, cp)
}
}
for _, device := range t.cDevices {
cp, ok := device.(*CheckPoint)
if ok {
cps = append(cps, cp)
}
}
return cps
}
// 获取指定端口连接的设备端口
func (t *Turnout) getDevicePortByPort(port proto.Port) DevicePort {
switch port {
case proto.Port_A:
return t.aDevicePort
case proto.Port_B:
return t.bDevicePort
case proto.Port_C:
return t.cDevicePort
default:
return nil
}
}
func (t *Turnout) findBoundaryKmByPort(port proto.Port) *proto.Kilometer {
switch port {
case proto.Port_A:
return t.aKm
case proto.Port_B:
return t.bKm
case proto.Port_C:
return t.cKm
}
return nil
}
func (t *Turnout) getDevicesByPort(port proto.Port) []Identity {
switch port {
case proto.Port_A:
return t.aDevices
case proto.Port_B:
return t.bDevices
case proto.Port_C:
return t.cDevices
}
return nil
}
type TurnoutPort struct {
turnout *Turnout
port proto.Port
}
func (t *TurnoutPort) Port() proto.Port {
return t.port
}
func (t *TurnoutPort) Device() PortedDevice {
return t.turnout
}