2023-09-06 16:20:36 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-16 21:19:52 +08:00
|
|
|
|
"fmt"
|
2023-09-06 16:20:36 +08:00
|
|
|
|
"joylink.club/rtsssimulation/repository/model/proto"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
|
Identity
|
|
|
|
|
|
|
|
|
|
length int64
|
|
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
|
aRelation *TurnoutPort
|
|
|
|
|
bRelation *TurnoutPort
|
2023-11-10 15:07:17 +08:00
|
|
|
|
//组成Link的非道岔物理区段(按在link上偏移量从小到大排列)
|
2023-09-20 18:20:56 +08:00
|
|
|
|
physicalSections []*PhysicalSection
|
2023-09-14 15:06:51 +08:00
|
|
|
|
|
|
|
|
|
aKm *proto.Kilometer
|
|
|
|
|
bKm *proto.Kilometer
|
2023-09-06 16:20:36 +08:00
|
|
|
|
|
2024-07-31 17:08:53 +08:00
|
|
|
|
//按偏移量小到大排序的、此Link上的模型((非区段边界)检测点、应答器、信号机)
|
|
|
|
|
devices []LinkPositionDevice
|
2023-09-20 15:14:38 +08:00
|
|
|
|
|
|
|
|
|
////Link关联的模型,包含LinkNode
|
|
|
|
|
//devicePositions []*DeviceLinkPosition
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ Identity = &Link{}
|
|
|
|
|
|
|
|
|
|
func NewLink(id string) *Link {
|
2023-09-14 15:06:51 +08:00
|
|
|
|
return &Link{
|
|
|
|
|
Identity: identity{id, proto.DeviceType_DeviceType_Link},
|
|
|
|
|
}
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 13:53:46 +08:00
|
|
|
|
// GetAllPhysicalSection 获取与该link关联的所有物理区段(包括岔区非岔区)
|
|
|
|
|
func (l *Link) GetAllPhysicalSection() []*PhysicalSection {
|
|
|
|
|
var rt []*PhysicalSection
|
|
|
|
|
if l.physicalSections != nil {
|
|
|
|
|
rt = append(rt, l.physicalSections...)
|
|
|
|
|
}
|
|
|
|
|
if l.aRelation != nil {
|
|
|
|
|
rt = append(rt, l.aRelation.turnout.section)
|
|
|
|
|
}
|
|
|
|
|
if l.bRelation != nil {
|
|
|
|
|
rt = append(rt, l.bRelation.turnout.section)
|
|
|
|
|
}
|
|
|
|
|
return rt
|
|
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
|
func (l *Link) Length() int64 {
|
|
|
|
|
return l.length
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Link) ARelation() *TurnoutPort {
|
|
|
|
|
return l.aRelation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Link) BRelation() *TurnoutPort {
|
|
|
|
|
return l.bRelation
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 17:08:53 +08:00
|
|
|
|
func (l *Link) Devices() []LinkPositionDevice {
|
2023-09-20 15:14:38 +08:00
|
|
|
|
return l.devices
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 16:20:36 +08:00
|
|
|
|
func (l *Link) PortNum() int {
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 18:20:56 +08:00
|
|
|
|
func (l *Link) PhysicalSections() []*PhysicalSection {
|
|
|
|
|
return l.physicalSections
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 14:06:00 +08:00
|
|
|
|
func (l *Link) AKm() *proto.Kilometer {
|
|
|
|
|
return l.aKm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Link) BKm() *proto.Kilometer {
|
|
|
|
|
return l.bKm
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 18:20:56 +08:00
|
|
|
|
func (l *Link) bindTurnoutPort(port proto.Port, turnoutPort *TurnoutPort) {
|
2023-09-06 16:20:36 +08:00
|
|
|
|
switch port {
|
|
|
|
|
case proto.Port_A:
|
2023-09-20 15:14:38 +08:00
|
|
|
|
l.aRelation = turnoutPort
|
2023-09-06 16:20:36 +08:00
|
|
|
|
case proto.Port_B:
|
2023-09-20 15:14:38 +08:00
|
|
|
|
l.bRelation = turnoutPort
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
|
l.bindKm(turnoutPort.turnout.km, port)
|
2023-09-20 18:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Link) bindPhysicalSections(section *PhysicalSection) {
|
|
|
|
|
l.physicalSections = append(l.physicalSections, section)
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 15:06:51 +08:00
|
|
|
|
func (l *Link) bindKm(km *proto.Kilometer, port proto.Port) {
|
|
|
|
|
switch port {
|
|
|
|
|
case proto.Port_A:
|
|
|
|
|
l.aKm = km
|
|
|
|
|
case proto.Port_B:
|
|
|
|
|
l.bKm = km
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 17:08:53 +08:00
|
|
|
|
func (l *Link) bindDevices(devices ...LinkPositionDevice) {
|
2023-09-20 15:14:38 +08:00
|
|
|
|
for _, device := range devices {
|
|
|
|
|
l.devices = append(l.devices, device)
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 15:06:51 +08:00
|
|
|
|
func (l *Link) findEndKm() *proto.Kilometer {
|
|
|
|
|
if l.aRelation == nil {
|
|
|
|
|
return l.aKm
|
|
|
|
|
} else if l.bRelation == nil {
|
|
|
|
|
return l.bKm
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找Link轨道尽头端的偏移量
|
|
|
|
|
// 如果该link未连接轨道尽头,返回false
|
|
|
|
|
func (l *Link) findEndOffset() (int64, bool) {
|
|
|
|
|
if l.aRelation == nil {
|
|
|
|
|
return 0, true
|
|
|
|
|
}
|
|
|
|
|
if l.bRelation == nil {
|
|
|
|
|
return l.length, true
|
|
|
|
|
}
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 16:20:36 +08:00
|
|
|
|
// LinkPosition link位置
|
|
|
|
|
type LinkPosition struct {
|
|
|
|
|
link *Link
|
|
|
|
|
offset int64
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 15:38:20 +08:00
|
|
|
|
func NewLinkPosition(link *Link, offset int64) *LinkPosition {
|
|
|
|
|
return &LinkPosition{link: link, offset: offset}
|
|
|
|
|
}
|
2024-07-31 17:08:53 +08:00
|
|
|
|
func (l LinkPosition) Link() *Link {
|
2023-09-20 15:14:38 +08:00
|
|
|
|
return l.link
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 17:08:53 +08:00
|
|
|
|
func (l LinkPosition) Offset() int64 {
|
2023-09-20 15:14:38 +08:00
|
|
|
|
return l.offset
|
2023-09-06 16:20:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 17:08:53 +08:00
|
|
|
|
func (l LinkPosition) String() string {
|
2024-05-16 21:19:52 +08:00
|
|
|
|
return fmt.Sprintf("[LinkPosition:{%s-%d]", l.link.Id(), l.offset)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 16:20:36 +08:00
|
|
|
|
// link端口
|
|
|
|
|
type LinkPort struct {
|
|
|
|
|
link *Link
|
|
|
|
|
port proto.Port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLinkPort(link *Link, port proto.Port) LinkPort {
|
|
|
|
|
return LinkPort{
|
|
|
|
|
link: link,
|
|
|
|
|
port: port,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkPort) Port() proto.Port {
|
|
|
|
|
return l.port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkPort) Device() PortedDevice {
|
|
|
|
|
return l.link
|
|
|
|
|
}
|
2023-11-10 16:03:33 +08:00
|
|
|
|
func (l *LinkPort) IsPortA() bool {
|
|
|
|
|
return l.port == proto.Port_A
|
|
|
|
|
}
|
2023-12-01 15:38:20 +08:00
|
|
|
|
func (l *LinkPort) Link() *Link {
|
|
|
|
|
return l.link
|
|
|
|
|
}
|
2023-09-06 16:20:36 +08:00
|
|
|
|
|
2023-11-10 11:05:07 +08:00
|
|
|
|
type LinkRange struct {
|
|
|
|
|
link *Link
|
|
|
|
|
start int64
|
|
|
|
|
end int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkRange) Link() *Link {
|
|
|
|
|
return l.link
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkRange) Start() int64 {
|
|
|
|
|
return l.start
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *LinkRange) End() int64 {
|
|
|
|
|
return l.end
|
|
|
|
|
}
|