diff --git a/repo/check_test.go b/cgrepo/check_test.go similarity index 100% rename from repo/check_test.go rename to cgrepo/check_test.go diff --git a/repo/dto/cg_repo.pb.go b/cgrepo/dto/cg_repo.pb.go similarity index 100% rename from repo/dto/cg_repo.pb.go rename to cgrepo/dto/cg_repo.pb.go diff --git a/repo/error_record.go b/cgrepo/error_record.go similarity index 100% rename from repo/error_record.go rename to cgrepo/error_record.go diff --git a/repo/idmapping_build.go b/cgrepo/idmapping_build.go similarity index 98% rename from repo/idmapping_build.go rename to cgrepo/idmapping_build.go index 9980f40..9f07b16 100644 --- a/repo/idmapping_build.go +++ b/cgrepo/idmapping_build.go @@ -6,8 +6,7 @@ import ( "strconv" "strings" - "joylink.club/rtsssimulation/repo/dto" - "joylink.club/rtsssimulation/repo/model" + "joylink.club/rtsssimulation/cgrepo/dto" ) // 城轨uid @@ -58,10 +57,10 @@ type idMap struct { // 元素id Eid uint32 // 唯一id - Uid model.Uid + Uid *CgUid } -func NewIdMap(did string, eid uint32, uid model.Uid) *idMap { +func NewIdMap(did string, eid uint32, uid *CgUid) *idMap { return &idMap{ Did: did, Eid: eid, diff --git a/cgrepo/manage.go b/cgrepo/manage.go new file mode 100644 index 0000000..15f8f68 --- /dev/null +++ b/cgrepo/manage.go @@ -0,0 +1,33 @@ +package repo + +// type repoManager struct { +// repoMap map[string]CgRepo +// lock sync.Mutex +// } + +// var defaultManager = &repoManager{ +// repoMap: make(map[string]CgRepo), +// } + +// // 获取或构建模型仓库 +// func GetOrBuildRepo(id string, dc func(errRecord *ErrorRecord) *dto.CgRepo) (CgRepo, *ErrorRecord) { +// manager := defaultManager +// manager.lock.Lock() +// defer manager.lock.Unlock() +// r, ok := manager.repoMap[id] +// errRecord := NewErrorRecord() +// if !ok { +// // 所需protobuf数据转换 +// msgs := dc(errRecord) +// // 数据转换出错直接返回 +// if errRecord.HasError() { +// return nil, errRecord +// } +// // 构建模型Repo +// r, errRecord = BuildFrom(msgs) +// if r != nil { +// manager.repoMap[id] = r +// } +// } +// return r, errRecord +// } diff --git a/cgrepo/model/common.go b/cgrepo/model/common.go new file mode 100644 index 0000000..a766c75 --- /dev/null +++ b/cgrepo/model/common.go @@ -0,0 +1,34 @@ +package model + +type ModelType string + +const ( + // 车站 + ModelType_Station ModelType = "Station" + // 站台 + ModelType_Stand ModelType = "Stand" + // 屏蔽门 + ModelType_PSD ModelType = "PSD" + // Link + ModelType_Link ModelType = "Link" + // 区段 + ModelType_Section ModelType = "Section" + // 道岔 + ModelType_Turnout ModelType = "Turnout" + // 信号机 + ModelType_Signal ModelType = "Signal" + // 应答器 + ModelType_Balise ModelType = "Balise" +) + +type Uid interface { + Id() string +} + +// 模型接口 +type Model interface { + // Unique id,唯一id + Uid() Uid + // 模型类型 + Type() ModelType +} diff --git a/cgrepo/model/link.go b/cgrepo/model/link.go new file mode 100644 index 0000000..9e84047 --- /dev/null +++ b/cgrepo/model/link.go @@ -0,0 +1,141 @@ +package model + +import "log/slog" + +// link端口 +type Link_Port int + +const ( + LinkPort_A Link_Port = -1 + LinkPort_B Link_Port = 1 +) + +// Link +type Link interface { + Model + // Link总长度 + Length() int64 + // 获取端口A关联的道岔 + GetPaTurnout() Turnout + // 获取端口B关联的道岔 + GetPbTurnout() Turnout + // 下一个link及端口 + // port - 当前link端口 + // tpos - 当前道岔位置 + Next(port Link_Port, tpos TurnoutPosition) *LinkPort +} + +type LinkPort struct { + link Link + port Link_Port +} + +func NewLinkPort(link Link, port Link_Port) *LinkPort { + return &LinkPort{ + link: link, + port: port, + } +} + +// link偏移位置 +// 默认linkA端口偏移为0,从A到B为增大,从B到A为减小 +type LinkOffset struct { + link Link + offset int64 // 偏移坐标,link相当于一个一维坐标系,以端口A为原点 +} + +// 创建link偏移位置,若offset超出link长度则返回nil +func NewLinkOffset(link Link, offset int64) *LinkOffset { + if offset < 0 || offset > link.Length() { + return nil + } + return &LinkOffset{ + link: link, + offset: offset, + } +} + +func (l *LinkOffset) Link() Link { + return l.link +} +func (l *LinkOffset) Offset() int64 { + return l.offset +} + +// link偏移范围 +type LinkRange struct { + link Link + start int64 + end int64 +} + +func NewLinkRange(link Link, a int64, b int64) *LinkRange { + if a < 0 || a > link.Length() { + slog.Error("创建link偏移范围失败: 超出link长度范围", slog.Int64("a", a)) + return nil + } else if b < 0 || b > link.Length() { + slog.Error("创建link偏移范围失败: 超出link长度范围", slog.Int64("b", b)) + return nil + } + var start int64 + var end int64 + if a > b { + start = b + end = a + } else { + start = a + end = b + } + return &LinkRange{ + link: link, + start: start, + end: end, + } +} + +// 是否相交 +func (l *LinkRange) IsIntersect(other *LinkRange) bool { + if l.link != other.link { + return false + } + if l.start > other.end || other.start > l.end { + return false + } + return true +} + +// 是否相交 +func (l *LinkRange) IsIntersect2(a, b int64) bool { + var start int64 + var end int64 + if a > b { + start = b + end = a + } else { + start = a + end = b + } + if start > l.end || end < l.start { + return false + } + return true +} + +// 是否在范围内 +func (l *LinkRange) IsInRange(lo *LinkOffset) bool { + if lo.link != l.link { + return false + } + if lo.offset < l.start || lo.offset > l.end { + return false + } + return true +} + +// 偏移坐标是否在范围内 +func (l *LinkRange) IsInRange2(offset int64) bool { + if offset < l.start || offset > l.end { + return false + } + return true +} diff --git a/cgrepo/model/psd.go b/cgrepo/model/psd.go new file mode 100644 index 0000000..2fd5b11 --- /dev/null +++ b/cgrepo/model/psd.go @@ -0,0 +1,6 @@ +package model + +// 站台屏蔽门 +type PSD interface { + Model +} diff --git a/cgrepo/model/relay.go b/cgrepo/model/relay.go new file mode 100644 index 0000000..6233b3c --- /dev/null +++ b/cgrepo/model/relay.go @@ -0,0 +1,11 @@ +package model + +// 继电器位置 +type RelayPosition int + +const ( + // 继电器位置-后(表示落下/反位) + RelayPosition_H RelayPosition = 0 + // 继电器位置-前(表示吸起/定位) + RelayPosition_Q RelayPosition = 1 +) diff --git a/cgrepo/model/section.go b/cgrepo/model/section.go new file mode 100644 index 0000000..93ca2fc --- /dev/null +++ b/cgrepo/model/section.go @@ -0,0 +1,9 @@ +package model + +// 物理区段(实际检测区段) +type PhysicalSection interface { + Model + Code() string + // 所在Link范围 + LinkRanges() []*LinkRange +} diff --git a/cgrepo/model/signal.go b/cgrepo/model/signal.go new file mode 100644 index 0000000..5fa1d8b --- /dev/null +++ b/cgrepo/model/signal.go @@ -0,0 +1,6 @@ +package model + +// 信号机 +type Signal interface { + Model +} diff --git a/cgrepo/model/station.go b/cgrepo/model/station.go new file mode 100644 index 0000000..38599ee --- /dev/null +++ b/cgrepo/model/station.go @@ -0,0 +1,44 @@ +package model + +// 车站 +type Station interface { + Model + // 车站名 + Name() string + // 是否设备集中站 + IsEcs() bool +} + +// 设备集中站 +type Ecs interface { + Station + // 获取所有道岔 + Turnouts() []Turnout + // 获取所有信号机 + Signals() []Signal + // 获取所有站台屏蔽门 + PSDs() []PSD + // 获取联锁驱采表 + CiQCTable() +} + +// 联锁驱采表 +type CiQCTable interface { + // 驱动码位表(每一位所驱动的继电器uid) + QD() []string + // 采集码位表(每一位所采集的继电器位置) + CJ() []CiCJ +} + +// 联锁采集 +type CiCJ interface { + CjPos() []CiCJPos +} + +// 联锁采集继电器位置 +type CiCJPos interface { + // 继电器uid + RelayId() string + // 继电器位置 + Pos() RelayPosition +} diff --git a/cgrepo/model/turnout.go b/cgrepo/model/turnout.go new file mode 100644 index 0000000..10a4dd8 --- /dev/null +++ b/cgrepo/model/turnout.go @@ -0,0 +1,32 @@ +package model + +// 道岔位置 +type TurnoutPosition int + +const ( + // 失表 + TPos_Lost TurnoutPosition = 0 + // 定位 + TPos_DW TurnoutPosition = 1 + // 反位 + TPos_FW TurnoutPosition = 2 +) + +type Turnout_Port int + +const ( + TurnoutPort_A Turnout_Port = 0 + TurnoutPort_B Turnout_Port = 1 + TurnoutPort_C Turnout_Port = 2 +) + +// 道岔 +type Turnout interface { + Model + // 获取A方向连接的link端口 + GetALinkPort() *LinkPort + // 获取B方向连接的link端口 + GetBLinkPort() *LinkPort + // 获取C方向连接的link端口 + GetCLinkPort() *LinkPort +} diff --git a/repo/model/impl/link.go b/cgrepo/model_impl/link.go similarity index 89% rename from repo/model/impl/link.go rename to cgrepo/model_impl/link.go index 872d6fa..be889c9 100644 --- a/repo/model/impl/link.go +++ b/cgrepo/model_impl/link.go @@ -4,8 +4,8 @@ import ( "fmt" "sync/atomic" - "joylink.club/rtsssimulation/repo/dto" - "joylink.club/rtsssimulation/repo/model" + "joylink.club/rtsssimulation/cgrepo/dto" + "joylink.club/rtsssimulation/cgrepo/model" ) // link生成uid基础值 @@ -35,7 +35,7 @@ func (l *Link) Uid() string { } func (l *Link) Type() model.ModelType { - return model.MT_Link + return model.ModelType_Link } // link偏移 diff --git a/repo/model/impl/physical_section.go b/cgrepo/model_impl/physical_section.go similarity index 88% rename from repo/model/impl/physical_section.go rename to cgrepo/model_impl/physical_section.go index cbc2d97..008f365 100644 --- a/repo/model/impl/physical_section.go +++ b/cgrepo/model_impl/physical_section.go @@ -1,7 +1,7 @@ package impl import ( - "joylink.club/rtsssimulation/repo/model" + "joylink.club/rtsssimulation/cgrepo/model" ) type PhysicalSection struct { @@ -25,7 +25,7 @@ func (s *PhysicalSection) Uid() string { } func (s *PhysicalSection) Type() model.ModelType { - return model.MT_Section + return model.ModelType_Section } func (s *PhysicalSection) Code() string { diff --git a/repo/model/impl/section_check_device.go b/cgrepo/model_impl/section_check_device.go similarity index 100% rename from repo/model/impl/section_check_device.go rename to cgrepo/model_impl/section_check_device.go diff --git a/repo/model/impl/turnout.go b/cgrepo/model_impl/turnout.go similarity index 89% rename from repo/model/impl/turnout.go rename to cgrepo/model_impl/turnout.go index d2d41b1..a465f0e 100644 --- a/repo/model/impl/turnout.go +++ b/cgrepo/model_impl/turnout.go @@ -1,6 +1,6 @@ package impl -import "joylink.club/rtsssimulation/repo/model" +import "joylink.club/rtsssimulation/cgrepo/model" type Turnout struct { uid string diff --git a/cgrepo/repo.go b/cgrepo/repo.go new file mode 100644 index 0000000..4053076 --- /dev/null +++ b/cgrepo/repo.go @@ -0,0 +1,56 @@ +package repo + +import "joylink.club/rtsssimulation/cgrepo/model" + +type CgRepo interface { + // 模型仓库id + Id() string + // 获取所有道岔 + Turnouts() []model.Turnout + // 通过uid查询模型对象 + FindByUid(uid string) model.Model +} + +type IdMap interface { + // 获取数据元素id + DeId() string + // 获取uid + Uid() string +} + +// type repo struct { +// id string +// idMapping *IdMapping // id映射 +// modelMap map[string]model.Model // 模型map,key为uid +// linkMap map[string]*impl.Link // 链路map,key为uid +// physicalSectionMap map[string]*impl.PhysicalSection // 物理区段map,key为uid +// turnoutMap map[string]*impl.Turnout // 道岔map,key为uid +// } + +// func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) { +// errRecord := NewErrorRecord() +// idMapping := BuildIdMapping(msgs, errRecord) +// if errRecord.HasError() { +// return nil, errRecord +// } +// repo := &repo{ +// id: msgs.Id, +// idMapping: idMapping, +// modelMap: make(map[string]model.Model, 1024), +// linkMap: make(map[string]*impl.Link, 256), +// physicalSectionMap: make(map[string]*impl.PhysicalSection, 256), +// turnoutMap: make(map[string]*impl.Turnout, 128), +// } + +// return repo, errRecord +// } + +// // 模型仓库id +// func (r *repo) Id() string { +// return r.id +// } + +// // 通过uid查询模型对象 +// func (r *repo) FindByUid(uid string) model.Model { +// return nil +// } diff --git a/proto/src/cg_repo.proto b/proto/src/cg_repo.proto index 6c76f68..647bdbc 100644 --- a/proto/src/cg_repo.proto +++ b/proto/src/cg_repo.proto @@ -463,9 +463,9 @@ message Lamp { // 设备电子元件组合 message DeviceEcc { - // 设备类型 + // 模型类型 Model.Type deviceType = 1; - // 设备编号 + // 模型编号 string deviceCode = 2; // 电子元件组合 repeated Ecc ecc = 3; diff --git a/repo/api.go b/repo/api.go deleted file mode 100644 index 0281e87..0000000 --- a/repo/api.go +++ /dev/null @@ -1,17 +0,0 @@ -package repo - -import "joylink.club/rtsssimulation/repo/model" - -type CgRepo interface { - // 模型仓库id - Id() string - // 通过uid查询模型对象 - FindByUid(uid string) model.Model -} - -type IdMap interface { - // 获取数据元素id - DeId() string - // 获取uid - Uid() string -} diff --git a/repo/manage.go b/repo/manage.go deleted file mode 100644 index 4b36369..0000000 --- a/repo/manage.go +++ /dev/null @@ -1,39 +0,0 @@ -package repo - -import ( - "sync" - - "joylink.club/rtsssimulation/repo/dto" -) - -type repoManager struct { - repoMap map[string]CgRepo - lock sync.Mutex -} - -var defaultManager = &repoManager{ - repoMap: make(map[string]CgRepo), -} - -// 获取或构建模型仓库 -func GetOrBuildRepo(id string, dc func(errRecord *ErrorRecord) *dto.CgRepo) (CgRepo, *ErrorRecord) { - manager := defaultManager - manager.lock.Lock() - defer manager.lock.Unlock() - r, ok := manager.repoMap[id] - errRecord := NewErrorRecord() - if !ok { - // 所需protobuf数据转换 - msgs := dc(errRecord) - // 数据转换出错直接返回 - if errRecord.HasError() { - return nil, errRecord - } - // 构建模型Repo - r, errRecord = BuildFrom(msgs) - if r != nil { - manager.repoMap[id] = r - } - } - return r, errRecord -} diff --git a/repo/model/api.go b/repo/model/api.go deleted file mode 100644 index d564aaa..0000000 --- a/repo/model/api.go +++ /dev/null @@ -1,57 +0,0 @@ -package model - -type ModelType string - -const ( - // 车站 - MT_Station ModelType = "Station" - // 站台 - MT_Stand ModelType = "Stand" - // 屏蔽门 - MT_PSD ModelType = "PSD" - // Link - MT_Link ModelType = "Link" - // 区段 - MT_Section ModelType = "Section" - // 道岔 - MT_Turnout ModelType = "Turnout" - // 信号机 - MT_Signal ModelType = "Signal" - // 应答器 - MT_Balise ModelType = "Balise" -) - -type Uid interface { - Id() string - // 设备原始编号 - Code() string -} - -// 模型接口 -type Model interface { - // Unique id,唯一id - Uid() Uid - // 模型类型 - Type() ModelType -} - -// Link -type Link interface { - Model -} - -// 物理区段(实际检测区段,一般区段两端点,道岔区段为3-4个端点) -type PhysicalSection interface { - Model - Code() string -} - -// 道岔 -type Turnout interface { - Model -} - -// 关联的端口关系 -type AssociatedPort interface { - Port() string -} diff --git a/repo/repo.go b/repo/repo.go deleted file mode 100644 index 62a91d1..0000000 --- a/repo/repo.go +++ /dev/null @@ -1,44 +0,0 @@ -package repo - -import ( - "joylink.club/rtsssimulation/repo/dto" - "joylink.club/rtsssimulation/repo/model" - "joylink.club/rtsssimulation/repo/model/impl" -) - -type repo struct { - id string - idMapping *IdMapping // id映射 - modelMap map[string]model.Model // 模型map,key为uid - linkMap map[string]*impl.Link // 链路map,key为uid - physicalSectionMap map[string]*impl.PhysicalSection // 物理区段map,key为uid - turnoutMap map[string]*impl.Turnout // 道岔map,key为uid -} - -func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) { - errRecord := NewErrorRecord() - idMapping := BuildIdMapping(msgs, errRecord) - if errRecord.HasError() { - return nil, errRecord - } - repo := &repo{ - id: msgs.Id, - idMapping: idMapping, - modelMap: make(map[string]model.Model, 1024), - linkMap: make(map[string]*impl.Link, 256), - physicalSectionMap: make(map[string]*impl.PhysicalSection, 256), - turnoutMap: make(map[string]*impl.Turnout, 128), - } - - return repo, errRecord -} - -// 模型仓库id -func (r *repo) Id() string { - return r.id -} - -// 通过uid查询模型对象 -func (r *repo) FindByUid(uid string) model.Model { - return nil -}