diff --git a/cgrepo/model/relay.go b/cgrepo/model/relay.go deleted file mode 100644 index 6233b3c..0000000 --- a/cgrepo/model/relay.go +++ /dev/null @@ -1,11 +0,0 @@ -package model - -// 继电器位置 -type RelayPosition int - -const ( - // 继电器位置-后(表示落下/反位) - RelayPosition_H RelayPosition = 0 - // 继电器位置-前(表示吸起/定位) - RelayPosition_Q RelayPosition = 1 -) diff --git a/component/singleton/load.go b/component/singleton/load.go new file mode 100644 index 0000000..4b8bf93 --- /dev/null +++ b/component/singleton/load.go @@ -0,0 +1,42 @@ +package singleton + +import ( + "joylink.club/ecs" + "joylink.club/ecs/filter" + "joylink.club/rtsssimulation/modelrepo" +) + +// 加载并初始化单例组件 +func LoadSingletons(w ecs.World, r modelrepo.Repo) { + loadWorldRepo(w, r) + loadWorldTime(w) + loadUidEntityIndex(w) +} + +var worldRepoQuery = ecs.NewQuery(filter.Contains(WorldRepoType)) +var worldTimeQuery = ecs.NewQuery(filter.Contains(WorldTimeType)) +var uidEntityIndexQuery = ecs.NewQuery(filter.Contains(EntityUidIndexType)) + +func GetWorldTime(w ecs.World) *WorldTime { + entry, _ := worldTimeQuery.First(w) + if entry == nil { + panic("不存在世界时间组件") + } + return WorldTimeType.Get(entry) +} + +func GetWorldRepo(w ecs.World) *WorldRepo { + entry, _ := worldRepoQuery.First(w) + if entry == nil { + panic("不存在世界数据组件") + } + return WorldRepoType.Get(entry) +} + +func GetEntityUidIndex(w ecs.World) *EntityUidIndex { + entry, _ := uidEntityIndexQuery.First(w) + if entry == nil { + panic("不存在UidEntityIndex组件") + } + return EntityUidIndexType.Get(entry) +} diff --git a/component/singleton/repo.go b/component/singleton/repo.go new file mode 100644 index 0000000..ce28105 --- /dev/null +++ b/component/singleton/repo.go @@ -0,0 +1,19 @@ +package singleton + +import ( + "joylink.club/ecs" + "joylink.club/rtsssimulation/modelrepo" +) + +var WorldRepoType = ecs.NewComponentType[WorldRepo]() + +type WorldRepo struct { + r modelrepo.Repo +} + +func loadWorldRepo(w ecs.World, r modelrepo.Repo) { + entry := w.Entry(w.Create(WorldRepoType)) + WorldRepoType.Set(entry, &WorldRepo{ + r: r, + }) +} diff --git a/component/singleton/uid_entity_index.go b/component/singleton/uid_entity_index.go new file mode 100644 index 0000000..6f39534 --- /dev/null +++ b/component/singleton/uid_entity_index.go @@ -0,0 +1,34 @@ +package singleton + +import ( + "sync" + + "joylink.club/ecs" +) + +var EntityUidIndexType = ecs.NewComponentType[EntityUidIndex]() + +// 对象实体Uid索引 +type EntityUidIndex struct { + mu sync.RWMutex + entityMap map[string]ecs.Entity +} + +func (idx *EntityUidIndex) Add(uid string, entity ecs.Entity) { + idx.mu.Lock() + defer idx.mu.Unlock() + idx.entityMap[uid] = entity +} + +func (idx *EntityUidIndex) Get(uid string) ecs.Entity { + idx.mu.RLock() + defer idx.mu.RUnlock() + return idx.entityMap[uid] +} + +func loadUidEntityIndex(w ecs.World) { + entry := w.Entry(w.Create(EntityUidIndexType)) + EntityUidIndexType.Set(entry, &EntityUidIndex{ + entityMap: make(map[string]ecs.Entity, 512), + }) +} diff --git a/component/singleton/world_time.go b/component/singleton/world_time.go new file mode 100644 index 0000000..3c9ae95 --- /dev/null +++ b/component/singleton/world_time.go @@ -0,0 +1,37 @@ +package singleton + +import ( + "time" + + "joylink.club/ecs" +) + +var WorldTimeType = ecs.NewComponentType[WorldTime]() + +type WorldTime struct { + time int64 +} + +func (w *WorldTime) SetTime(t time.Time) { + w.time = t.UnixMilli() +} + +// 增加时间 +func (w *WorldTime) Run(ms int) { + w.time += int64(ms) +} + +// 获取时间 +func (w *WorldTime) GetTime() time.Time { + return time.UnixMilli(w.time) +} + +// 获取时间戳 +func (w *WorldTime) GetMilli() int64 { + return w.time +} + +func loadWorldTime(w ecs.World) { + entry := w.Entry(w.Create(WorldTimeType)) + WorldTimeType.Set(entry, &WorldTime{time: time.Now().UnixMilli()}) +} diff --git a/entity/cg_load.go b/entity/cg_load.go new file mode 100644 index 0000000..0178326 --- /dev/null +++ b/entity/cg_load.go @@ -0,0 +1,22 @@ +package entity + +import ( + "joylink.club/ecs" + "joylink.club/rtsssimulation/component/singleton" + "joylink.club/rtsssimulation/modelrepo" + "joylink.club/rtsssimulation/modelrepo/model" +) + +// 加载城轨仿真实体 +func LoadCg(w ecs.World, repo modelrepo.Repo) error { + singleton.LoadSingletons(w, repo) + for _, s := range repo.GetEcses() { + // 加载道岔实体 + loadTurnouts(w, s.GetTurnouts()) + } + return nil +} + +func loadTurnouts(w ecs.World, turnout []model.Turnout) { + panic("unimplemented") +} diff --git a/init.go b/init.go index 28a2737..a0207a0 100644 --- a/init.go +++ b/init.go @@ -3,6 +3,7 @@ package rtss_simulation import ( "joylink.club/ecs" "joylink.club/rtsssimulation/entity" + "joylink.club/rtsssimulation/modelrepo" "joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/sys" ) @@ -19,3 +20,16 @@ func NewSimulation(repo *repository.Repository) (ecs.World, error) { err := entity.Load(w, repo) return w, err } + +// 加载城轨仿真 +func LoadCgSimulation(repo modelrepo.Repo) (ecs.World, error) { + w := ecs.NewWorld(RtssSimulationTick) + // 加载组件实体 + err := entity.LoadCg(w, repo) + if err != nil { + return nil, err + } + // 加载系统 + sys.BindSystem(w) + return w, err +} diff --git a/cgrepo/check_test.go b/modelrepo/check_test.go similarity index 96% rename from cgrepo/check_test.go rename to modelrepo/check_test.go index 5fc7f71..ed3f4c4 100644 --- a/cgrepo/check_test.go +++ b/modelrepo/check_test.go @@ -1,4 +1,4 @@ -package repo +package modelrepo import "testing" diff --git a/cgrepo/dto/cg_repo.pb.go b/modelrepo/dto/cg_repo.pb.go similarity index 100% rename from cgrepo/dto/cg_repo.pb.go rename to modelrepo/dto/cg_repo.pb.go diff --git a/cgrepo/error_record.go b/modelrepo/error_record.go similarity index 95% rename from cgrepo/error_record.go rename to modelrepo/error_record.go index 312fd52..6424902 100644 --- a/cgrepo/error_record.go +++ b/modelrepo/error_record.go @@ -1,4 +1,4 @@ -package repo +package modelrepo // 构建错误记录 type ErrorRecord struct { diff --git a/cgrepo/idmapping_build.go b/modelrepo/idmapping_build.go similarity index 99% rename from cgrepo/idmapping_build.go rename to modelrepo/idmapping_build.go index 9f07b16..82b6373 100644 --- a/cgrepo/idmapping_build.go +++ b/modelrepo/idmapping_build.go @@ -1,4 +1,4 @@ -package repo +package modelrepo import ( "fmt" @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "joylink.club/rtsssimulation/cgrepo/dto" + "joylink.club/rtsssimulation/modelrepo/dto" ) // 城轨uid diff --git a/cgrepo/manage.go b/modelrepo/manage.go similarity index 79% rename from cgrepo/manage.go rename to modelrepo/manage.go index 2156fe6..3f30951 100644 --- a/cgrepo/manage.go +++ b/modelrepo/manage.go @@ -1,19 +1,19 @@ -package repo +package modelrepo import "sync" // Repo管理 type repoManager struct { - repoMap map[string]CgRepo + repoMap map[string]Repo lock sync.Mutex } var defaultManager = &repoManager{ - repoMap: make(map[string]CgRepo), + repoMap: make(map[string]Repo), } // 获取Repo -func GetRepo(id string) CgRepo { +func GetRepo(id string) Repo { manager := defaultManager manager.lock.Lock() defer manager.lock.Unlock() @@ -25,7 +25,7 @@ func GetRepo(id string) CgRepo { } // 保存Repo -func SaveRepo(r CgRepo) { +func SaveRepo(r Repo) { manager := defaultManager manager.lock.Lock() defer manager.lock.Unlock() @@ -41,7 +41,7 @@ func RemoveRepo(id string) { } // 获取或构建Repo,若存在则返回已存在的,若不存在则构建保存并返回 -func GetOrBuildRepo(id string, builder func() (CgRepo, error)) (CgRepo, error) { +func GetOrBuildRepo(id string, builder func() (Repo, error)) (Repo, error) { r := GetRepo(id) if r == nil { // 构建模型Repo diff --git a/cgrepo/model/common.go b/modelrepo/model/common.go similarity index 100% rename from cgrepo/model/common.go rename to modelrepo/model/common.go diff --git a/modelrepo/model/elec_comp.go b/modelrepo/model/elec_comp.go new file mode 100644 index 0000000..b6a6293 --- /dev/null +++ b/modelrepo/model/elec_comp.go @@ -0,0 +1,22 @@ +package model + +// 开关 +type PowerSwitch interface { + Model + // 编号 + Code() +} + +// 信号状态表示灯 +type Lamp interface { + Model + // 编号 + Code() +} + +// 蜂鸣器/报警电铃 +type Buzzer interface { + Model + // 编号 + Code() +} diff --git a/modelrepo/model/ibp.go b/modelrepo/model/ibp.go new file mode 100644 index 0000000..9b7a1ef --- /dev/null +++ b/modelrepo/model/ibp.go @@ -0,0 +1,12 @@ +package model + +// IBP盘(综合后备盘) +type Ibp interface { + Model + // 获取所有开关 + GetPowerSwitches() []PowerSwitch + // 获取所有信号状态表示灯 + GetLamps() []Lamp + // 获取所有蜂鸣器 + GetBuzzer() []Buzzer +} diff --git a/cgrepo/model/link.go b/modelrepo/model/link.go similarity index 100% rename from cgrepo/model/link.go rename to modelrepo/model/link.go diff --git a/cgrepo/model/psd.go b/modelrepo/model/psd.go similarity index 67% rename from cgrepo/model/psd.go rename to modelrepo/model/psd.go index 2fd5b11..56cc8e1 100644 --- a/cgrepo/model/psd.go +++ b/modelrepo/model/psd.go @@ -1,6 +1,6 @@ package model // 站台屏蔽门 -type PSD interface { +type Psd interface { Model } diff --git a/modelrepo/model/relay.go b/modelrepo/model/relay.go new file mode 100644 index 0000000..30c8b70 --- /dev/null +++ b/modelrepo/model/relay.go @@ -0,0 +1,32 @@ +package model + +// 继电器位置 +type Relay_Position int + +const ( + // 继电器位置-后(表示落下/反位) + RelayPos_H Relay_Position = 0 + // 继电器位置-前(表示吸起/定位) + RelayPos_Q Relay_Position = 1 +) + +// 继电器型号 +type Relay_Model int + +const ( + RelayModel_JPXC_1000 Relay_Model = 1 + RelayModel_JPXC_1700 Relay_Model = 2 + RelayModel_JWJXC_480 Relay_Model = 3 + RelayModel_JWJXC_H125_80 Relay_Model = 4 + RelayModel_JWXC_1700 Relay_Model = 5 + RelayModel_JWXC_H340 Relay_Model = 6 + RelayModel_JYJXC_160_260 Relay_Model = 7 + RelayModel_JZXC_H18 Relay_Model = 8 +) + +// 继电器 +type Relay interface { + Model + Code() + Model() Relay_Model +} diff --git a/cgrepo/model/section.go b/modelrepo/model/section.go similarity index 100% rename from cgrepo/model/section.go rename to modelrepo/model/section.go diff --git a/cgrepo/model/signal.go b/modelrepo/model/signal.go similarity index 100% rename from cgrepo/model/signal.go rename to modelrepo/model/signal.go diff --git a/cgrepo/model/station.go b/modelrepo/model/station.go similarity index 77% rename from cgrepo/model/station.go rename to modelrepo/model/station.go index 0ed1850..1af32c6 100644 --- a/cgrepo/model/station.go +++ b/modelrepo/model/station.go @@ -7,21 +7,23 @@ type Station interface { Name() string // 是否设备集中站 IsEcs() bool + // 获取IBP + GetIbp() Ibp } // 设备集中站(Equipment centralized station) -type Ecs interface { +type EcStation interface { Station // 获取所有道岔 - Turnouts() []Turnout + GetTurnouts() []Turnout // 获取所有信号机 - Signals() []Signal + GetSignals() []Signal // 获取所有站台屏蔽门 - PSDs() []PSD + GetPsds() []Psd // 获取所有物理检测区段 - PhysicalSections() []PhysicalSection + GetPhysicalSections() []PhysicalSection // 获取联锁驱采表 - CiQCTable() + GetCiQCTable() } // 联锁驱采表 @@ -42,5 +44,5 @@ type CiCJPos interface { // 继电器uid RelayId() string // 继电器位置 - Pos() RelayPosition + Pos() Relay_Position } diff --git a/cgrepo/model/turnout.go b/modelrepo/model/turnout.go similarity index 81% rename from cgrepo/model/turnout.go rename to modelrepo/model/turnout.go index e880e9c..7d8413e 100644 --- a/cgrepo/model/turnout.go +++ b/modelrepo/model/turnout.go @@ -16,9 +16,9 @@ const ( type Turnout_Port int const ( - TPort_A Turnout_Port = 0 - TPort_B Turnout_Port = 1 - TPort_C Turnout_Port = 2 + TPort_A Turnout_Port = 1 + TPort_B Turnout_Port = 2 + TPort_C Turnout_Port = 3 ) // 道岔牵引类型 @@ -26,9 +26,9 @@ type TurnoutTractionType int const ( // ZDJ9单机牵引 - TTT_ZDJ9_1 TurnoutTractionType = 0 + TTT_ZDJ9_1 TurnoutTractionType = 1 // ZDJ9双机牵引 - TTT_ZDJ9_2 TurnoutTractionType = 1 + TTT_ZDJ9_2 TurnoutTractionType = 2 ) // 道岔 diff --git a/cgrepo/model_impl/link.go b/modelrepo/model_impl/link.go similarity index 94% rename from cgrepo/model_impl/link.go rename to modelrepo/model_impl/link.go index 64df9b4..cd602ff 100644 --- a/cgrepo/model_impl/link.go +++ b/modelrepo/model_impl/link.go @@ -3,8 +3,8 @@ package modelimpl import ( "sync/atomic" - "joylink.club/rtsssimulation/cgrepo/dto" - "joylink.club/rtsssimulation/cgrepo/model" + "joylink.club/rtsssimulation/modelrepo/dto" + "joylink.club/rtsssimulation/modelrepo/model" ) // link生成uid基础值 diff --git a/cgrepo/model_impl/physical_section.go b/modelrepo/model_impl/physical_section.go similarity index 92% rename from cgrepo/model_impl/physical_section.go rename to modelrepo/model_impl/physical_section.go index b448241..e315513 100644 --- a/cgrepo/model_impl/physical_section.go +++ b/modelrepo/model_impl/physical_section.go @@ -1,7 +1,7 @@ package modelimpl import ( - "joylink.club/rtsssimulation/cgrepo/model" + "joylink.club/rtsssimulation/modelrepo/model" ) type PhysicalSection struct { diff --git a/cgrepo/model_impl/section_check_device.go b/modelrepo/model_impl/section_check_device.go similarity index 100% rename from cgrepo/model_impl/section_check_device.go rename to modelrepo/model_impl/section_check_device.go diff --git a/cgrepo/model_impl/turnout.go b/modelrepo/model_impl/turnout.go similarity index 95% rename from cgrepo/model_impl/turnout.go rename to modelrepo/model_impl/turnout.go index 3946c49..df7bcd3 100644 --- a/cgrepo/model_impl/turnout.go +++ b/modelrepo/model_impl/turnout.go @@ -1,6 +1,6 @@ package modelimpl -import "joylink.club/rtsssimulation/cgrepo/model" +import "joylink.club/rtsssimulation/modelrepo/model" var _turnout model.Turnout = (*Turnout)(nil) diff --git a/cgrepo/model_impl/uid.go b/modelrepo/model_impl/uid.go similarity index 100% rename from cgrepo/model_impl/uid.go rename to modelrepo/model_impl/uid.go diff --git a/cgrepo/repo.go b/modelrepo/repo.go similarity index 80% rename from cgrepo/repo.go rename to modelrepo/repo.go index 1452086..27df5b1 100644 --- a/cgrepo/repo.go +++ b/modelrepo/repo.go @@ -1,16 +1,18 @@ -package repo +package modelrepo -import "joylink.club/rtsssimulation/cgrepo/model" +import "joylink.club/rtsssimulation/modelrepo/model" -type CgRepo interface { +type Repo interface { // 模型仓库id Id() string // 获取所有设备集中站 - Ecses() []model.Ecs + GetEcses() []model.EcStation // 获取所有道岔 - Turnouts() []model.Turnout - // 通过uid查询模型对象 - FindByUid(uid string) model.Model + GetTurnouts() []model.Turnout + // 获取所有信号机 + GetSignals() []model.Signal + // 获取所有站台屏蔽门 + GetPsds() []model.Psd } type IdMap interface { @@ -29,7 +31,7 @@ type IdMap interface { // turnoutMap map[string]*impl.Turnout // 道岔map,key为uid // } -// func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) { +// func BuildFrom(msgs *dto.modelrepo) (modelrepo, *ErrorRecord) { // errRecord := NewErrorRecord() // idMapping := BuildIdMapping(msgs, errRecord) // if errRecord.HasError() { diff --git a/proto/src/cg_repo.proto b/proto/src/cg_repo.proto index 647bdbc..02674ca 100644 --- a/proto/src/cg_repo.proto +++ b/proto/src/cg_repo.proto @@ -5,7 +5,7 @@ package message; option go_package = "./repo/dto"; // 城轨数据 -message CgRepo { +message modelrepo { string id = 1; // 线路数据 repeated Line lines = 2; diff --git a/sys/world_time.go b/sys/world_time.go index 9c55d31..c91b56f 100644 --- a/sys/world_time.go +++ b/sys/world_time.go @@ -2,25 +2,19 @@ package sys import ( "joylink.club/ecs" - "joylink.club/ecs/filter" - "joylink.club/rtsssimulation/component" + "joylink.club/rtsssimulation/component/singleton" ) -// 世界世界更新系统 +// 世界时间更新系统 type WorldTimeSys struct { - query *ecs.Query } func NewWorldTimeSys() *WorldTimeSys { - return &WorldTimeSys{ - query: ecs.NewQuery(filter.Contains(component.WorldDataType)), - } + return &WorldTimeSys{} } func (s *WorldTimeSys) Update(w ecs.World) { - entry, _ := s.query.First(w) - data := component.WorldDataType.Get(entry) - data.Time += int64(w.Tick()) + singleton.GetWorldTime(w).Run(w.Tick()) // t := time.UnixMilli(data.Time) // fmt.Println(t) }