From 8d9ea2a084d66f6eb877320ecd13108432aaa205 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Tue, 17 Oct 2023 15:05:13 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=88=9B=E5=BB=BA=E8=BD=A6=E7=AB=99?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entity/ibp.go | 13 +++----- entity/init.go | 5 +++ entity/station.go | 32 ++++++++++++++++++ repository/ibp.go | 2 +- repository/repository.go | 13 ++++++-- repository/repository_manager.go | 56 ++++++++++++++++++++++++++++++++ repository/station.go | 13 ++++++-- 7 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 entity/station.go diff --git a/entity/ibp.go b/entity/ibp.go index 85ed464..3231cad 100644 --- a/entity/ibp.go +++ b/entity/ibp.go @@ -9,13 +9,6 @@ import ( "joylink.club/rtsssimulation/repository/model/proto" ) -// 创建IBP实体 -func NewIBPEntity(w ecs.World, ibp *repository.IBP, entityMap map[string]*ecs.Entry) { - entry := w.Entry(w.Create(component.IBP)) - loadSPKEntity(w, entry, ibp.Spk, entityMap) - loadEMPEntity(w, entry, ibp.Emp, entityMap) -} - // 创建按钮实体 func NewButtonEntity(w ecs.World, data *repository.Button, entityMap map[string]*ecs.Entry) *ecs.Entry { uid := data.Id() @@ -56,7 +49,8 @@ func NewAlarmEntity(w ecs.World, alarm *repository.Alarm, entityMap map[string]* } // 构建人员防护实体 -func loadSPKEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, entityMap map[string]*ecs.Entry) error { +func LoadSPKEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, worldData *component.WorldData) error { + entityMap := worldData.EntityMap spk := &component.SpkElectronic{} // 按钮组 for _, btn := range spkData.Buttons() { @@ -118,7 +112,8 @@ func setSpkDefault(spk *component.SpkElectronic) { } // 构建紧急关闭实体 -func loadEMPEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, entityMap map[string]*ecs.Entry) error { +func LoadEMPEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, worldData *component.WorldData) error { + entityMap := worldData.EntityMap emp := &component.EmpElectronic{} // 按钮组 for _, btn := range spkData.Buttons() { diff --git a/entity/init.go b/entity/init.go index 0a6abf4..c5f9b98 100644 --- a/entity/init.go +++ b/entity/init.go @@ -24,5 +24,10 @@ func Load(w ecs.World, repo *repository.Repository) error { if err != nil { return err } + // 加载车站实体 + err = LoadStations(w) + if err != nil { + return err + } return err } diff --git a/entity/station.go b/entity/station.go new file mode 100644 index 0000000..5e357d2 --- /dev/null +++ b/entity/station.go @@ -0,0 +1,32 @@ +package entity + +import ( + "joylink.club/ecs" + "joylink.club/rtsssimulation/component" +) + +func LoadStations(w ecs.World) error { + data := GetWorldData(w) + stations := data.Repo.StationList() + for _, station := range stations { + entry := NewStationEntity(w, station.Id(), data) + if station.GetIbpSpk() != nil { // 人员防护 + LoadSPKEntity(w, entry, station.GetIbpSpk(), data) + } + if station.GetIbpEmp() != nil { // 紧急停车 + LoadEMPEntity(w, entry, station.GetIbpEmp(), data) + } + } + return nil +} + +// 新建车站实体 +func NewStationEntity(w ecs.World, uid string, worldData *component.WorldData) *ecs.Entry { + entry, ok := worldData.EntityMap[uid] + if !ok { + entry = w.Entry(w.Create(component.UidType)) + component.UidType.SetValue(entry, component.Uid{Id: uid}) + worldData.EntityMap[uid] = entry + } + return entry +} diff --git a/repository/ibp.go b/repository/ibp.go index 24f634c..56ef810 100644 --- a/repository/ibp.go +++ b/repository/ibp.go @@ -5,7 +5,7 @@ type IBP struct { Emp *IBPRefMap } -func NewIBP(id string) *IBP { +func NewIBP() *IBP { return &IBP{ Spk: &IBPRefMap{ buttonMap: make(map[string]*Button), diff --git a/repository/repository.go b/repository/repository.go index c405b39..3bc626f 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -1,7 +1,6 @@ package repository import ( - "errors" "fmt" "joylink.club/rtsssimulation/repository/model/proto" @@ -152,6 +151,14 @@ func (repo *Repository) PsdList() []*Psd { return list } +func (repo *Repository) StationList() []*Station { + var list []*Station + for _, model := range repo.stationMap { + list = append(list, model) + } + return list +} + func (repo *Repository) FindModel(deviceId string, deviceType proto.DeviceType) (Identity, error) { switch deviceType { case proto.DeviceType_DeviceType_PhysicalSection: @@ -171,7 +178,7 @@ func (repo *Repository) FindModel(deviceId string, deviceType proto.DeviceType) case proto.DeviceType_DeviceType_Link: return repo.linkMap[deviceId], nil default: - return nil, errors.New(fmt.Sprintf("仓库中不存在[%s]类型的模型", deviceType)) + return nil, fmt.Errorf("仓库中不存在[%s]类型的模型", deviceType) } } @@ -223,7 +230,7 @@ func (repo *Repository) addKilometerConvert(kc *proto.KilometerConvert) { func (repo *Repository) getKilometerConvert(cs1, cs2 string) (*proto.KilometerConvert, error) { convert := repo.kilometerConvertMap[buildKilometerConvertKey(cs1, cs2)] if convert == nil { - return nil, errors.New(fmt.Sprintf("没有[%s-%s]的公里标转换数据", cs1, cs2)) + return nil, fmt.Errorf("没有[%s-%s]的公里标转换数据", cs1, cs2) } return convert, nil } diff --git a/repository/repository_manager.go b/repository/repository_manager.go index 245f487..9e2558e 100644 --- a/repository/repository_manager.go +++ b/repository/repository_manager.go @@ -3,6 +3,7 @@ package repository import ( "errors" "fmt" + "log/slog" "math" "strconv" @@ -94,6 +95,18 @@ func buildModels(source *proto.Repository, repository *Repository) { m := newPsd(protoData.Id) repository.psdMap[m.Id()] = m } + for _, protoData := range source.Lights { + m := NewLight(protoData.Id, protoData.Code) + repository.lightMap[m.Id()] = m + } + for _, protoData := range source.Alarms { + m := NewAlarm(protoData.Id, protoData.Code) + repository.alarmMap[m.Id()] = m + } + for _, protoData := range source.Stations { + m := NewStation(protoData.Id, protoData.Code) + repository.stationMap[m.Id()] = m + } } func buildModelRelationship(source *proto.Repository, repository *Repository) error { @@ -121,6 +134,10 @@ func buildModelRelationship(source *proto.Repository, repository *Repository) er if err != nil { return err } + err = buildStationRelationShip(source, repository) + if err != nil { + return err + } return err } @@ -760,3 +777,42 @@ func sectionalCurvatureRelateLink(repo *Repository) error { } return nil } + +func buildStationRelationShip(source *proto.Repository, repo *Repository) error { + for _, protoData := range source.Stations { + station, ok := repo.stationMap[protoData.Id] + if !ok { + return fmt.Errorf("缺失车站[%s]", protoData.Id) + } + if len(protoData.ElectronicGroup) == 0 { + continue + } + for _, ref := range protoData.ElectronicGroup { + switch ref.Code { + case "EMP": + bindIbpDevice(repo, ref.Components, station.GetIbpEmp()) + case "SPKS": + bindIbpDevice(repo, ref.Components, station.GetIbpSpk()) + default: + slog.Warn("车站实体未实现【%s】绑定逻辑", ref.Code) + } + } + } + return nil +} + +// 将设备绑入IBP +func bindIbpDevice(repo *Repository, comps []*proto.ElectronicComponent, refMap *IBPRefMap) { + for _, data := range comps { + switch data.DeviceType { + case proto.DeviceType_DeviceType_Alarm: + refMap.AddAlarm(repo.alarmMap[data.Id]) + case proto.DeviceType_DeviceType_Button: + refMap.AddButton(repo.buttonMap[data.Id]) + case proto.DeviceType_DeviceType_Relay: + refMap.AddRelay(repo.relayMap[data.Id]) + default: + slog.Warn("IBP未绑定【%s】类型设备", data.DeviceType.String()) + } + } +} diff --git a/repository/station.go b/repository/station.go index 77ec7e8..f9ba6c0 100644 --- a/repository/station.go +++ b/repository/station.go @@ -4,13 +4,22 @@ import "joylink.club/rtsssimulation/repository/model/proto" type Station struct { Identity - code string - componentGroups []*ElectronicComponentGroup + code string + ibp *IBP } func NewStation(id, code string) *Station { return &Station{ Identity: identity{id, proto.DeviceType_DeviceType_Station}, code: code, + ibp: NewIBP(), } } + +func (s *Station) GetIbpEmp() *IBPRefMap { + return s.ibp.Emp +} + +func (s *Station) GetIbpSpk() *IBPRefMap { + return s.ibp.Spk +}