This commit is contained in:
xzb 2023-12-27 18:12:21 +08:00
commit 556ed6e2d6
23 changed files with 383 additions and 169 deletions

View File

@ -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,

33
cgrepo/manage.go Normal file
View File

@ -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
// }

34
cgrepo/model/common.go Normal file
View File

@ -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
}

141
cgrepo/model/link.go Normal file
View File

@ -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
}

6
cgrepo/model/psd.go Normal file
View File

@ -0,0 +1,6 @@
package model
// 站台屏蔽门
type PSD interface {
Model
}

11
cgrepo/model/relay.go Normal file
View File

@ -0,0 +1,11 @@
package model
// 继电器位置
type RelayPosition int
const (
// 继电器位置-后(表示落下/反位)
RelayPosition_H RelayPosition = 0
// 继电器位置-前(表示吸起/定位)
RelayPosition_Q RelayPosition = 1
)

9
cgrepo/model/section.go Normal file
View File

@ -0,0 +1,9 @@
package model
// 物理区段(实际检测区段)
type PhysicalSection interface {
Model
Code() string
// 所在Link范围
LinkRanges() []*LinkRange
}

6
cgrepo/model/signal.go Normal file
View File

@ -0,0 +1,6 @@
package model
// 信号机
type Signal interface {
Model
}

44
cgrepo/model/station.go Normal file
View File

@ -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
}

32
cgrepo/model/turnout.go Normal file
View File

@ -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
}

View File

@ -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偏移

View File

@ -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 {

View File

@ -1,6 +1,6 @@
package impl
import "joylink.club/rtsssimulation/repo/model"
import "joylink.club/rtsssimulation/cgrepo/model"
type Turnout struct {
uid string

56
cgrepo/repo.go Normal file
View File

@ -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
// }

View File

@ -463,9 +463,9 @@ message Lamp {
//
message DeviceEcc {
//
//
Model.Type deviceType = 1;
//
//
string deviceCode = 2;
//
repeated Ecc ecc = 3;

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}