2023-08-29 14:59:31 +08:00
|
|
|
package repository
|
|
|
|
|
2023-09-06 16:20:36 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"joylink.club/rtsssimulation/repository/model/proto"
|
|
|
|
)
|
2023-08-29 14:59:31 +08:00
|
|
|
|
|
|
|
type Repository struct {
|
2023-09-14 15:06:51 +08:00
|
|
|
id string
|
|
|
|
version string
|
|
|
|
physicalSectionMap map[string]*PhysicalSection
|
2023-09-06 16:20:36 +08:00
|
|
|
checkPointMap map[string]*CheckPoint
|
|
|
|
turnoutMap map[string]*Turnout
|
|
|
|
signalMap map[string]*Signal
|
2023-09-14 15:06:51 +08:00
|
|
|
responderMap map[string]*Transponder
|
2023-09-06 16:20:36 +08:00
|
|
|
slopeMap map[string]*Slope
|
|
|
|
sectionalCurvatureMap map[string]*SectionalCurvature
|
2023-09-22 15:13:24 +08:00
|
|
|
kilometerConvertMap map[string]*proto.KilometerConvert
|
|
|
|
relayMap map[string]*Relay
|
2023-09-06 16:20:36 +08:00
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
linkMap map[string]*Link
|
2023-08-29 14:59:31 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
func newRepository(id string, version string) *Repository {
|
2023-08-29 18:01:32 +08:00
|
|
|
return &Repository{
|
2023-09-22 15:13:24 +08:00
|
|
|
id: id,
|
|
|
|
version: version,
|
2023-09-06 16:20:36 +08:00
|
|
|
physicalSectionMap: make(map[string]*PhysicalSection),
|
2023-09-14 15:06:51 +08:00
|
|
|
checkPointMap: make(map[string]*CheckPoint),
|
2023-09-06 16:20:36 +08:00
|
|
|
turnoutMap: make(map[string]*Turnout),
|
|
|
|
signalMap: make(map[string]*Signal),
|
2023-09-14 15:06:51 +08:00
|
|
|
responderMap: make(map[string]*Transponder),
|
2023-09-06 16:20:36 +08:00
|
|
|
slopeMap: make(map[string]*Slope),
|
|
|
|
sectionalCurvatureMap: make(map[string]*SectionalCurvature),
|
2023-09-22 15:13:24 +08:00
|
|
|
kilometerConvertMap: make(map[string]*proto.KilometerConvert),
|
|
|
|
relayMap: make(map[string]*Relay),
|
2023-09-20 15:14:38 +08:00
|
|
|
linkMap: make(map[string]*Link),
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
func (repo *Repository) PhysicalSectionList() []*PhysicalSection {
|
|
|
|
var list []*PhysicalSection
|
|
|
|
for _, model := range repo.physicalSectionMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
return list
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
func (repo *Repository) CheckPointList() []*CheckPoint {
|
|
|
|
var list []*CheckPoint
|
|
|
|
for _, model := range repo.checkPointMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
func (repo *Repository) TurnoutList() []*Turnout {
|
2023-09-20 15:14:38 +08:00
|
|
|
var list []*Turnout
|
|
|
|
for _, model := range repo.turnoutMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
func (repo *Repository) SignalList() []*Signal {
|
2023-09-20 15:14:38 +08:00
|
|
|
var list []*Signal
|
|
|
|
for _, model := range repo.signalMap {
|
|
|
|
list = append(list, model)
|
2023-08-29 18:01:32 +08:00
|
|
|
}
|
2023-09-06 16:20:36 +08:00
|
|
|
return list
|
2023-08-29 18:01:32 +08:00
|
|
|
}
|
2023-09-14 15:06:51 +08:00
|
|
|
func (repo *Repository) ResponderList() []*Transponder {
|
2023-09-20 15:14:38 +08:00
|
|
|
var list []*Transponder
|
|
|
|
for _, model := range repo.responderMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
return list
|
2023-08-29 18:01:32 +08:00
|
|
|
}
|
2023-09-06 16:20:36 +08:00
|
|
|
func (repo *Repository) SlopeList() []*Slope {
|
2023-09-20 15:14:38 +08:00
|
|
|
var list []*Slope
|
|
|
|
for _, model := range repo.slopeMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
return list
|
2023-08-29 18:01:32 +08:00
|
|
|
}
|
2023-09-06 16:20:36 +08:00
|
|
|
func (repo *Repository) SectionalCurvatureList() []*SectionalCurvature {
|
2023-09-20 15:14:38 +08:00
|
|
|
var list []*SectionalCurvature
|
|
|
|
for _, model := range repo.sectionalCurvatureMap {
|
|
|
|
list = append(list, model)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
func (repo *Repository) LinkList() []*Link {
|
|
|
|
var list []*Link
|
|
|
|
for _, model := range repo.linkMap {
|
|
|
|
list = append(list, model)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
func (repo *Repository) KilometerConvertList() []*proto.KilometerConvert {
|
|
|
|
var list []*proto.KilometerConvert
|
|
|
|
for _, model := range repo.kilometerConvertMap {
|
|
|
|
list = append(list, model)
|
2023-09-22 15:13:24 +08:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
func (repo *Repository) RelayList() []*Relay {
|
|
|
|
var list []*Relay
|
|
|
|
for _, model := range repo.relayMap {
|
|
|
|
list = append(list, model)
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
return list
|
2023-08-29 18:01:32 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
func (repo *Repository) getModel(deviceId string, deviceType proto.DeviceType) (Identity, error) {
|
|
|
|
switch deviceType {
|
|
|
|
case proto.DeviceType_DeviceType_PhysicalSection:
|
|
|
|
return repo.physicalSectionMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_CheckPoint:
|
|
|
|
return repo.checkPointMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_Turnout:
|
|
|
|
return repo.turnoutMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_Signal:
|
|
|
|
return repo.signalMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_Transponder:
|
|
|
|
return repo.responderMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_Slope:
|
|
|
|
return repo.slopeMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_SectionalCurvature:
|
|
|
|
return repo.sectionalCurvatureMap[deviceId], nil
|
|
|
|
case proto.DeviceType_DeviceType_Link:
|
|
|
|
return repo.linkMap[deviceId], nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New(fmt.Sprintf("仓库中不存在[%s]类型的模型", deviceType))
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 14:59:31 +08:00
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
func (repo *Repository) FindLink(id string) *Link {
|
|
|
|
return repo.linkMap[id]
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
2023-08-29 14:59:31 +08:00
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
func (repo *Repository) AddPhysicalSection(section *PhysicalSection) {
|
|
|
|
repo.physicalSectionMap[section.Id()] = section
|
2023-09-06 16:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) addKilometerConvert(kc *proto.KilometerConvert) {
|
|
|
|
repo.kilometerConvertMap[buildKilometerConvertKey(kc.KmA.CoordinateSystem, kc.KmB.CoordinateSystem, kc.KmA.Direction, kc.KmB.Direction)] = kc
|
|
|
|
repo.kilometerConvertMap[buildKilometerConvertKey(kc.KmB.CoordinateSystem, kc.KmA.CoordinateSystem, kc.KmB.Direction, kc.KmA.Direction)] = kc
|
|
|
|
}
|
2023-08-29 14:59:31 +08:00
|
|
|
|
2023-09-06 16:20:36 +08:00
|
|
|
func (repo *Repository) getKilometerConvert(cs1, cs2 string, dir1, dir2 proto.Direction) (*proto.KilometerConvert, error) {
|
|
|
|
convert := repo.kilometerConvertMap[buildKilometerConvertKey(cs1, cs2, dir1, dir2)]
|
|
|
|
if convert == nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("没有[%s-%s]的公里标转换数据", cs1, cs2))
|
|
|
|
}
|
|
|
|
return convert, nil
|
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
|
|
|
|
func buildKilometerConvertKey(cs1 string, cs2 string, dir1 proto.Direction, dir2 proto.Direction) string {
|
|
|
|
return cs1 + dir1.String() + cs2 + dir2.String()
|
|
|
|
}
|