模型数据管理

This commit is contained in:
xzb 2023-08-21 15:12:57 +08:00
parent d09b89b7fe
commit 4587da57f2
11 changed files with 245 additions and 50 deletions

View File

@ -7,10 +7,21 @@ import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/memory"
"joylink.club/rtsssimulation/model"
"joylink.club/rtsssimulation/state"
"joylink.club/rtsssimulation/system"
)
func testModel() {
link1 := model.NewLinkModel("link1")
link2 := model.NewLinkModel("link2")
memory.AddModel(link1)
memory.AddModel(link2)
memory.ForEachModelsByType(state.DeviceType_Link, func(md memory.ModelData) {
link := md.(*model.LinkModel)
fmt.Println("==>>", link.Id)
})
}
func main() {
world := ecs.NewWorld(300)
worlTime := time.Now()

View File

@ -48,7 +48,7 @@ func CreatePsdEntity(w ecs.World) *ecs.Entry {
return e
}
// 创建单个屏蔽门实体
// 创建屏蔽门cell实体
func CreatePsdCellEntity(w ecs.World, psdEntry *ecs.Entry) *ecs.Entry {
return w.Create(components.DeviceIdentityComponent, components.PercentageDeviceComponent, components.EntityTagHandlerComponent.Get(psdEntry).Tag)
}

View File

@ -1,5 +1,78 @@
package memory
//模型存储、管理仓库
type ModelStorage struct {
import (
"fmt"
"joylink.club/rtsssimulation/model"
"joylink.club/rtsssimulation/state"
)
type ModelData = model.IDeviceModel
// 模型数据仓库
var storage *modelStorage = &modelStorage{
idModelMap: make(map[string]ModelData, 2048),
typeModelMap: make(map[state.DeviceType][]ModelData, 128),
}
// 模型存储、管理仓库
type modelStorage struct {
//key-模型id,value-模型指针
idModelMap map[string]ModelData
//key-设备类型value-对应设备类型的所有模型数据的指针列表
typeModelMap map[state.DeviceType][]ModelData
}
// 添加模型数据
// m 为具体模型数据的指针
func AddModel(m ModelData) error {
_, ok := storage.idModelMap[m.GetId()]
if ok {
return fmt.Errorf("模型[%s]已经存在", m.GetId())
} else {
storage.idModelMap[m.GetId()] = m
//
_, mdOk := storage.typeModelMap[m.GetType()]
if !mdOk {
storage.typeModelMap[m.GetType()] = make([]ModelData, 0, 512)
}
storage.typeModelMap[m.GetType()] = append(storage.typeModelMap[m.GetType()], m)
//
return nil
}
}
// 根据设备类型获取该类型的所有设备数据
func FindModelsByType(deviceType state.DeviceType) []ModelData {
models, ok := storage.typeModelMap[deviceType]
if ok {
return models
} else {
return []ModelData{}
}
}
// 根据设备id获取对应模型数据
// 如果不存在返回nil
func FindModelById(id string) ModelData {
md, ok := storage.idModelMap[id]
if ok {
return md
} else {
return nil
}
}
// 遍历某个类型的所有设备
func ForEachModelsByType(deviceType state.DeviceType, callback func(md ModelData)) {
mds := FindModelsByType(deviceType)
for _, modelData := range mds {
callback(modelData)
}
}
// 根据id检测设备模型数据是否存在
func HasModelById(id string) bool {
_, ok := storage.idModelMap[id]
return ok
}

View File

@ -1,8 +1,14 @@
package model
import "joylink.club/rtsssimulation/state"
//计轴检测点模型
type AxlePointModel struct {
DeviceModel
//计轴检测点所在轨道
LinkOffset *LinkOffsetRef
}
func NewAxlePointModel(id string) *AxlePointModel {
return &AxlePointModel{DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_AxlePoint}}
}

View File

@ -1,13 +1,19 @@
package model
import "joylink.club/rtsssimulation/state"
type IDeviceModel interface {
GetId() string
IsSame(other IDeviceModel) bool
GetType() state.DeviceType
}
// 设备模型基础信息
type DeviceModel struct {
// 设备id
Id string
// 设备类型
Type state.DeviceType
}
func (me *DeviceModel) GetId() string {
@ -16,6 +22,9 @@ func (me *DeviceModel) GetId() string {
func (me *DeviceModel) IsSame(other IDeviceModel) bool {
return me.Id == other.GetId()
}
func (me *DeviceModel) GetType() state.DeviceType {
return me.Type
}
// 端口定义,如轨道、区段、道岔的端口
type PortEnum = int8

View File

@ -1,5 +1,7 @@
package model
import "joylink.club/rtsssimulation/state"
//长轨道,道岔端点间的轨道
type LinkModel struct {
DeviceModel
@ -9,6 +11,10 @@ type LinkModel struct {
PortB *SwitchPortRef
}
func NewLinkModel(id string) *LinkModel {
return &LinkModel{DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_Link}}
}
//轨道端口引用
type LinkPortRef struct {
Link *LinkModel

View File

@ -1,5 +1,7 @@
package model
import "joylink.club/rtsssimulation/state"
//站台一侧屏蔽门模型
type PsdModel struct {
DeviceModel
@ -8,7 +10,7 @@ type PsdModel struct {
}
func NewPsdModel(id string) *PsdModel {
return &PsdModel{DeviceModel: DeviceModel{Id: id}, Cells: make([]*PsdCellModel, 0)}
return &PsdModel{DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_Psd}, Cells: make([]*PsdCellModel, 0)}
}
func (me *PsdModel) AddCell(cell *PsdCellModel) {
me.Cells = append(me.Cells, cell)
@ -25,7 +27,7 @@ type PsdCellModel struct {
func NewPsdCellModel(id string, psdModel *PsdModel) *PsdCellModel {
return &PsdCellModel{
DeviceModel: DeviceModel{Id: id},
DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_PsdCell},
psd: psdModel,
}
}

View File

@ -12,3 +12,7 @@ type SignalModel struct {
//默认显示信号
DefaultDisplay state.SignalAspect
}
func NewSignalModel(id string) *SignalModel {
return &SignalModel{DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_Signal}}
}

View File

@ -1,5 +1,7 @@
package model
import "joylink.club/rtsssimulation/state"
//道岔
type SwitchModel struct {
DeviceModel
@ -19,3 +21,7 @@ type SwitchPortRef struct {
//引用道岔的端口
Port PortEnum
}
func NewSwitchModel(id string) *SwitchModel {
return &SwitchModel{DeviceModel: DeviceModel{Id: id, Type: state.DeviceType_Switch}}
}

View File

@ -7,7 +7,16 @@ message DeviceIdentity{
//id
string id = 1;
}
//
enum DeviceType{
Any = 0;
Link = 1;
Switch = 2;
Signal = 3;
Psd = 4;
PsdCell = 5;
AxlePoint = 6;
}
/////////////////////////////////////////////////////////
//

View File

@ -20,6 +20,68 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// 设备类型枚举
type DeviceType int32
const (
DeviceType_Any DeviceType = 0
DeviceType_Link DeviceType = 1
DeviceType_Switch DeviceType = 2
DeviceType_Signal DeviceType = 3
DeviceType_Psd DeviceType = 4
DeviceType_PsdCell DeviceType = 5
DeviceType_AxlePoint DeviceType = 6
)
// Enum value maps for DeviceType.
var (
DeviceType_name = map[int32]string{
0: "Any",
1: "Link",
2: "Switch",
3: "Signal",
4: "Psd",
5: "PsdCell",
6: "AxlePoint",
}
DeviceType_value = map[string]int32{
"Any": 0,
"Link": 1,
"Switch": 2,
"Signal": 3,
"Psd": 4,
"PsdCell": 5,
"AxlePoint": 6,
}
)
func (x DeviceType) Enum() *DeviceType {
p := new(DeviceType)
*p = x
return p
}
func (x DeviceType) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (DeviceType) Descriptor() protoreflect.EnumDescriptor {
return file_status_proto_enumTypes[0].Descriptor()
}
func (DeviceType) Type() protoreflect.EnumType {
return &file_status_proto_enumTypes[0]
}
func (x DeviceType) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Use DeviceType.Descriptor instead.
func (DeviceType) EnumDescriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{0}
}
// 信号机显示枚举
type SignalAspect int32
@ -105,11 +167,11 @@ func (x SignalAspect) String() string {
}
func (SignalAspect) Descriptor() protoreflect.EnumDescriptor {
return file_status_proto_enumTypes[0].Descriptor()
return file_status_proto_enumTypes[1].Descriptor()
}
func (SignalAspect) Type() protoreflect.EnumType {
return &file_status_proto_enumTypes[0]
return &file_status_proto_enumTypes[1]
}
func (x SignalAspect) Number() protoreflect.EnumNumber {
@ -118,7 +180,7 @@ func (x SignalAspect) Number() protoreflect.EnumNumber {
// Deprecated: Use SignalAspect.Descriptor instead.
func (SignalAspect) EnumDescriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{0}
return file_status_proto_rawDescGZIP(), []int{1}
}
// 列车激活的枚举
@ -158,11 +220,11 @@ func (x TrainActiveEnum) String() string {
}
func (TrainActiveEnum) Descriptor() protoreflect.EnumDescriptor {
return file_status_proto_enumTypes[1].Descriptor()
return file_status_proto_enumTypes[2].Descriptor()
}
func (TrainActiveEnum) Type() protoreflect.EnumType {
return &file_status_proto_enumTypes[1]
return &file_status_proto_enumTypes[2]
}
func (x TrainActiveEnum) Number() protoreflect.EnumNumber {
@ -171,7 +233,7 @@ func (x TrainActiveEnum) Number() protoreflect.EnumNumber {
// Deprecated: Use TrainActiveEnum.Descriptor instead.
func (TrainActiveEnum) EnumDescriptor() ([]byte, []int) {
return file_status_proto_rawDescGZIP(), []int{1}
return file_status_proto_rawDescGZIP(), []int{2}
}
// 设备身份信息
@ -1185,20 +1247,26 @@ var file_status_proto_rawDesc = []byte{
0x69, 0x72, 0x6d, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x0a, 0x07,
0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x63,
0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c,
0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x2a, 0x81,
0x01, 0x0a, 0x0c, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x41, 0x73, 0x70, 0x65, 0x63, 0x74, 0x12,
0x06, 0x0a, 0x02, 0x4e, 0x6f, 0x10, 0x00, 0x12, 0x05, 0x0a, 0x01, 0x52, 0x10, 0x01, 0x12, 0x05,
0x0a, 0x01, 0x47, 0x10, 0x02, 0x12, 0x05, 0x0a, 0x01, 0x59, 0x10, 0x03, 0x12, 0x05, 0x0a, 0x01,
0x57, 0x10, 0x04, 0x12, 0x05, 0x0a, 0x01, 0x42, 0x10, 0x05, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x59,
0x10, 0x06, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x57, 0x10, 0x07, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x47,
0x10, 0x08, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x59, 0x10, 0x09, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x59,
0x10, 0x0a, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x46, 0x10, 0x0b, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x46,
0x10, 0x0c, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x46, 0x10, 0x0d, 0x12, 0x06, 0x0a, 0x02, 0x57, 0x46,
0x10, 0x0e, 0x2a, 0x35, 0x0a, 0x0f, 0x54, 0x72, 0x61, 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76,
0x65, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x4e, 0x10,
0x00, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x41, 0x10, 0x01, 0x12, 0x0a, 0x0a,
0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x42, 0x10, 0x02, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x73,
0x74, 0x61, 0x74, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x2a, 0x5c,
0x0a, 0x0a, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x07, 0x0a, 0x03,
0x41, 0x6e, 0x79, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x4c, 0x69, 0x6e, 0x6b, 0x10, 0x01, 0x12,
0x0a, 0x0a, 0x06, 0x53, 0x77, 0x69, 0x74, 0x63, 0x68, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x53,
0x69, 0x67, 0x6e, 0x61, 0x6c, 0x10, 0x03, 0x12, 0x07, 0x0a, 0x03, 0x50, 0x73, 0x64, 0x10, 0x04,
0x12, 0x0b, 0x0a, 0x07, 0x50, 0x73, 0x64, 0x43, 0x65, 0x6c, 0x6c, 0x10, 0x05, 0x12, 0x0d, 0x0a,
0x09, 0x41, 0x78, 0x6c, 0x65, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x10, 0x06, 0x2a, 0x81, 0x01, 0x0a,
0x0c, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x41, 0x73, 0x70, 0x65, 0x63, 0x74, 0x12, 0x06, 0x0a,
0x02, 0x4e, 0x6f, 0x10, 0x00, 0x12, 0x05, 0x0a, 0x01, 0x52, 0x10, 0x01, 0x12, 0x05, 0x0a, 0x01,
0x47, 0x10, 0x02, 0x12, 0x05, 0x0a, 0x01, 0x59, 0x10, 0x03, 0x12, 0x05, 0x0a, 0x01, 0x57, 0x10,
0x04, 0x12, 0x05, 0x0a, 0x01, 0x42, 0x10, 0x05, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x59, 0x10, 0x06,
0x12, 0x06, 0x0a, 0x02, 0x52, 0x57, 0x10, 0x07, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x47, 0x10, 0x08,
0x12, 0x06, 0x0a, 0x02, 0x47, 0x59, 0x10, 0x09, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x59, 0x10, 0x0a,
0x12, 0x06, 0x0a, 0x02, 0x52, 0x46, 0x10, 0x0b, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x46, 0x10, 0x0c,
0x12, 0x06, 0x0a, 0x02, 0x47, 0x46, 0x10, 0x0d, 0x12, 0x06, 0x0a, 0x02, 0x57, 0x46, 0x10, 0x0e,
0x2a, 0x35, 0x0a, 0x0f, 0x54, 0x72, 0x61, 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x45,
0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x4e, 0x10, 0x00, 0x12,
0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x41, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x48,
0x45, 0x41, 0x44, 0x5f, 0x42, 0x10, 0x02, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x73, 0x74, 0x61,
0x74, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -1213,33 +1281,34 @@ func file_status_proto_rawDescGZIP() []byte {
return file_status_proto_rawDescData
}
var file_status_proto_enumTypes = make([]protoimpl.EnumInfo, 2)
var file_status_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
var file_status_proto_msgTypes = make([]protoimpl.MessageInfo, 15)
var file_status_proto_goTypes = []interface{}{
(SignalAspect)(0), // 0: state.SignalAspect
(TrainActiveEnum)(0), // 1: state.TrainActiveEnum
(*DeviceIdentity)(nil), // 2: state.DeviceIdentity
(*SwitchRelayState)(nil), // 3: state.SwitchRelayState
(*PhysicalSectionState)(nil), // 4: state.PhysicalSectionState
(*SignalState)(nil), // 5: state.SignalState
(*SignalDisplayOperating)(nil), // 6: state.SignalDisplayOperating
(*PsdState)(nil), // 7: state.PsdState
(*PercentageDeviceState)(nil), // 8: state.PercentageDeviceState
(*PercentageDeviceOperating)(nil), // 9: state.PercentageDeviceOperating
(*BaliseState)(nil), // 10: state.BaliseState
(*BaliseContent)(nil), // 11: state.BaliseContent
(*OccupiedLinkPosition)(nil), // 12: state.OccupiedLinkPosition
(*TrainState)(nil), // 13: state.TrainState
(*ButtonState)(nil), // 14: state.ButtonState
(*ButtonPressOperating)(nil), // 15: state.ButtonPressOperating
(*ButtonConfirmOperating)(nil), // 16: state.ButtonConfirmOperating
(DeviceType)(0), // 0: state.DeviceType
(SignalAspect)(0), // 1: state.SignalAspect
(TrainActiveEnum)(0), // 2: state.TrainActiveEnum
(*DeviceIdentity)(nil), // 3: state.DeviceIdentity
(*SwitchRelayState)(nil), // 4: state.SwitchRelayState
(*PhysicalSectionState)(nil), // 5: state.PhysicalSectionState
(*SignalState)(nil), // 6: state.SignalState
(*SignalDisplayOperating)(nil), // 7: state.SignalDisplayOperating
(*PsdState)(nil), // 8: state.PsdState
(*PercentageDeviceState)(nil), // 9: state.PercentageDeviceState
(*PercentageDeviceOperating)(nil), // 10: state.PercentageDeviceOperating
(*BaliseState)(nil), // 11: state.BaliseState
(*BaliseContent)(nil), // 12: state.BaliseContent
(*OccupiedLinkPosition)(nil), // 13: state.OccupiedLinkPosition
(*TrainState)(nil), // 14: state.TrainState
(*ButtonState)(nil), // 15: state.ButtonState
(*ButtonPressOperating)(nil), // 16: state.ButtonPressOperating
(*ButtonConfirmOperating)(nil), // 17: state.ButtonConfirmOperating
}
var file_status_proto_depIdxs = []int32{
0, // 0: state.SignalState.display:type_name -> state.SignalAspect
0, // 1: state.SignalDisplayOperating.targetAspect:type_name -> state.SignalAspect
11, // 2: state.BaliseState.content:type_name -> state.BaliseContent
1, // 3: state.TrainState.activeHead:type_name -> state.TrainActiveEnum
12, // 4: state.TrainState.occupiedLinks:type_name -> state.OccupiedLinkPosition
1, // 0: state.SignalState.display:type_name -> state.SignalAspect
1, // 1: state.SignalDisplayOperating.targetAspect:type_name -> state.SignalAspect
12, // 2: state.BaliseState.content:type_name -> state.BaliseContent
2, // 3: state.TrainState.activeHead:type_name -> state.TrainActiveEnum
13, // 4: state.TrainState.occupiedLinks:type_name -> state.OccupiedLinkPosition
5, // [5:5] is the sub-list for method output_type
5, // [5:5] is the sub-list for method input_type
5, // [5:5] is the sub-list for extension type_name
@ -1439,7 +1508,7 @@ func file_status_proto_init() {
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_status_proto_rawDesc,
NumEnums: 2,
NumEnums: 3,
NumMessages: 15,
NumExtensions: 0,
NumServices: 0,