rts-sim-module/repository/repository.go

161 lines
5.4 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 (
"errors"
"fmt"
"joylink.club/rtsssimulation/repository/model/proto"
)
type Repository struct {
id string
version string
physicalSectionMap map[string]*PhysicalSection
checkPointMap map[string]*CheckPoint
turnoutMap map[string]*Turnout
signalMap map[string]*Signal
responderMap map[string]*Transponder
slopeMap map[string]*Slope
sectionalCurvatureMap map[string]*SectionalCurvature
linkMap map[string]*Link
//devicePositionMap map[string]*DeviceLinkPosition //key为device的id
//linkNodeMap map[string]*LinkNode //LinkNode的id应该没什么意义所以此处key用LinkNode中Turnout的id
//slopeLinkSegmentMap map[string]*SlopeLinkSegment
//sectionalCurvatureLinkSegmentMap map[string]*SectionalCurvatureLinkSegment
kilometerConvertMap map[string]*proto.KilometerConvert
}
func newRepository(id string, version string) *Repository {
return &Repository{
id: id,
version: version,
//modelMap: make(map[string]model.Identity),
physicalSectionMap: make(map[string]*PhysicalSection),
checkPointMap: make(map[string]*CheckPoint),
turnoutMap: make(map[string]*Turnout),
signalMap: make(map[string]*Signal),
responderMap: make(map[string]*Transponder),
slopeMap: make(map[string]*Slope),
sectionalCurvatureMap: make(map[string]*SectionalCurvature),
linkMap: make(map[string]*Link),
//devicePositionMap: make(map[string]*DeviceLinkPosition),
//linkNodeMap: make(map[string]*LinkNode),
//slopeLinkSegmentMap: make(map[string]*SlopeLinkSegment),
//sectionalCurvatureLinkSegmentMap: make(map[string]*SectionalCurvatureLinkSegment),
kilometerConvertMap: make(map[string]*proto.KilometerConvert),
}
}
func (repo *Repository) PhysicalSectionList() []*PhysicalSection {
var list []*PhysicalSection
for _, model := range repo.physicalSectionMap {
list = append(list, model)
}
return list
}
func (repo *Repository) CheckPointList() []*CheckPoint {
var list []*CheckPoint
for _, model := range repo.checkPointMap {
list = append(list, model)
}
return list
}
func (repo *Repository) TurnoutList() []*Turnout {
var list []*Turnout
for _, model := range repo.turnoutMap {
list = append(list, model)
}
return list
}
func (repo *Repository) SignalList() []*Signal {
var list []*Signal
for _, model := range repo.signalMap {
list = append(list, model)
}
return list
}
func (repo *Repository) ResponderList() []*Transponder {
var list []*Transponder
for _, model := range repo.responderMap {
list = append(list, model)
}
return list
}
func (repo *Repository) SlopeList() []*Slope {
var list []*Slope
for _, model := range repo.slopeMap {
list = append(list, model)
}
return list
}
func (repo *Repository) SectionalCurvatureList() []*SectionalCurvature {
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)
}
return list
}
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))
}
}
func (repo *Repository) FindLink(id string) *Link {
return repo.linkMap[id]
}
func (repo *Repository) AddPhysicalSection(section *PhysicalSection) {
repo.physicalSectionMap[section.Id()] = section
}
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
}
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
}
func buildKilometerConvertKey(cs1 string, cs2 string, dir1 proto.Direction, dir2 proto.Direction) string {
return cs1 + dir1.String() + cs2 + dir2.String()
}