From e0085c45134c47d98f2749ab2e8c71c6ae46a600 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Fri, 15 Dec 2023 17:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BC=93=E5=AD=98=E7=BB=93=E6=9E=84=E3=80=91?= =?UTF-8?q?=E3=80=90=E6=95=B0=E6=8D=AE=E7=89=88=E6=9C=AC=E5=A4=84=E7=90=86?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message_server/ibp_ms.go | 2 +- message_server/psl_ms.go | 3 +- service/publishedGi.go | 4 +- .../wayside/memory/wayside_memory_ibp.go | 3 +- .../wayside/memory/wayside_memory_map.go | 110 ++++++++++-------- .../wayside/memory/wayside_simulation.go | 9 +- 6 files changed, 74 insertions(+), 57 deletions(-) diff --git a/message_server/ibp_ms.go b/message_server/ibp_ms.go index 1501bfc..b6632a3 100644 --- a/message_server/ibp_ms.go +++ b/message_server/ibp_ms.go @@ -62,7 +62,7 @@ func (ms *IbpMs) collectStationIbpState(station *graphicData.Station) (*state.Pu } sid := memory.GetMapElementId(station.Common) stationUid := memory.QueryUidByMidAndComId(ms.mapId, sid, &graphicData.Station{}) - ibpMapId, ibpStorage := memory.GetStorageIBPMapData(station.RefIbpMapCode) + ibpMapId, ibpStorage := memory.QueryGiDataByName[*graphicData.IBPGraphicStorage](station.RefIbpMapCode) ibpUidsMap := memory.QueryUidStructure[*memory.IBPUidStructure](ibpMapId) buttonStates, err := ms.collectIBPButtonState(stationUid, ibpUidsMap, ibpStorage.IbpButtons) if err != nil { diff --git a/message_server/psl_ms.go b/message_server/psl_ms.go index 0eab9fb..0562af1 100644 --- a/message_server/psl_ms.go +++ b/message_server/psl_ms.go @@ -72,8 +72,7 @@ func (p *PslMs) collectGateBoxPSLState(box *graphicData.GatedBox) (*state.Pushed mkx := component.MkxType.Get(mkxEntry) var buttonStateArr []*state.ButtonState if ok { - pslMapId := memory.QueryGiId(box.RefGatedBoxMapCode) - pslStorage := memory.QueryGiData[*graphicData.PslGraphicStorage](pslMapId) + _, pslStorage := memory.QueryGiDataByName[*graphicData.PslGraphicStorage](box.RefGatedBoxMapCode) btnUidMap := make(map[string]uint32, len(pslStorage.PslButtons)) for _, button := range pslStorage.PslButtons { btnUidMap[boxUid+"_"+button.Code] = memory.GetMapElementId(button.Common) diff --git a/service/publishedGi.go b/service/publishedGi.go index 5ba7a57..c48da57 100644 --- a/service/publishedGi.go +++ b/service/publishedGi.go @@ -62,7 +62,7 @@ func ListQueryPublished(req *dto.PublishedListReqDto) []*dto.PublishedDto { // 项目启动时查询发布地图信息 func ListAllPublished() []*dto.PublishedDto { p, pv := dbquery.Published, dbquery.PublishedVersion - where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto). + where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto, pv.Version). LeftJoin(pv, p.DataID.EqCol(pv.ID)). Where(p.Status.Eq(1), p.Type.Neq(trainDataType)).Order(pv.PublishAt) var records []*dto.PublishedDto @@ -76,7 +76,7 @@ func ListAllPublished() []*dto.PublishedDto { // 查询详细信息 func GetPublishedById(id int32) *dto.PublishedDto { p, pv := dbquery.Published, dbquery.PublishedVersion - where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto). + where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto, pv.Version). LeftJoin(pv, p.DataID.EqCol(pv.ID)). Where(p.ID.Eq(id), p.Status.Eq(1)) var record dto.PublishedDto diff --git a/ts/simulation/wayside/memory/wayside_memory_ibp.go b/ts/simulation/wayside/memory/wayside_memory_ibp.go index fc1678c..66d60a0 100644 --- a/ts/simulation/wayside/memory/wayside_memory_ibp.go +++ b/ts/simulation/wayside/memory/wayside_memory_ibp.go @@ -54,5 +54,6 @@ func getIbpUidByMapIdAndStationId(mapId int32, stationId uint32) (*IBPUidStructu if station.RefIbpMapCode == "" { return nil, fmt.Errorf("车站【%s】未关联IBP地图", station.StationName) } - return QueryUidStructure[*IBPUidStructure](QueryGiId(station.RefIbpMapCode)), nil + ibpMapId, _ := QueryGiDataByName[*graphicData.IBPGraphicStorage](station.RefIbpMapCode) + return QueryUidStructure[*IBPUidStructure](ibpMapId), nil } diff --git a/ts/simulation/wayside/memory/wayside_memory_map.go b/ts/simulation/wayside/memory/wayside_memory_map.go index 8e9b78c..de0ec80 100644 --- a/ts/simulation/wayside/memory/wayside_memory_map.go +++ b/ts/simulation/wayside/memory/wayside_memory_map.go @@ -18,16 +18,17 @@ import ( "joylink.club/bj-rtsts-server/ts/protos/graphicData" ) -var ( - giTypeMap sync.Map - giNameMap sync.Map - giDataMap sync.Map -) +var giPublishData sync.Map + +type giStoreData struct { + name string + giType graphicData.PictureType + message proto.Message + version int32 +} // 将发布的地图数据放入内存中 func PublishMapVerifyStructure(graphic *dto.PublishedDto) { - giTypeMap.Store(graphic.ID, graphicData.PictureType(graphic.Type)) - giNameMap.Store(graphic.Name, graphic.ID) var message proto.Message switch graphicData.PictureType(graphic.Type) { case graphicData.PictureType_StationLayout: @@ -43,13 +44,18 @@ func PublishMapVerifyStructure(graphic *dto.PublishedDto) { if err != nil { panic(&dto.ErrorDto{Code: dto.LogicError, Message: fmt.Sprintf("[id:%d]proto数据反序列化失败:%s", graphic.ID, err)}) } - giDataMap.Store(graphic.ID, message) + // 缓存数据 + giPublishData.Store(graphic.ID, &giStoreData{ + name: graphic.Name, + giType: graphicData.PictureType(graphic.Type), + message: message, + version: graphic.Version, + }) // 初始化地图结构 switch graphicData.PictureType(graphic.Type) { case graphicData.PictureType_StationLayout: graphicStorage := message.(*graphicData.RtssGraphicStorage) - // 处理掉其他线路的设备 - filterOtherLineDevice(graphicStorage) + filterOtherLineDevice(graphicStorage) // 处理掉其他线路的设备 giUidMap.Store(graphic.ID, initStationUid(graphicStorage)) case graphicData.PictureType_RelayCabinetLayout: graphicStorage := message.(*graphicData.RelayCabinetGraphicStorage) @@ -71,57 +77,67 @@ func GenerateElementUid(city, lineId string, stationIndexList []string, code str // 移除内存中的地图信息 func DeleteMapVerifyStructure(mapId int32) { - giTypeMap.Delete(mapId) - giDataMap.Delete(mapId) + giPublishData.Delete(mapId) giUidMap.Delete(mapId) - var name string - giNameMap.Range(func(key, value any) bool { - id := value.(int32) - if id == mapId { - name = key.(string) - return true - } - return false - }) - if name != "" { - giNameMap.Delete(name) - } } +// 获取地图类型 func QueryGiType(mapId int32) graphicData.PictureType { - value, ok := giTypeMap.Load(mapId) + value, ok := giPublishData.Load(mapId) if !ok { graphic := service.GetPublishedById(mapId) PublishMapVerifyStructure(graphic) return graphicData.PictureType(graphic.Type) } - return value.(graphicData.PictureType) + d, ok := value.(*giStoreData) + if !ok { + panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId))) + } + return d.giType } +// 获取地图版本 +func QueryGiVersion(mapId int32) int32 { + value, ok := giPublishData.Load(mapId) + if !ok { + panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据丢失", mapId))) + } + d, ok := value.(*giStoreData) + if !ok { + panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId))) + } + return d.version +} + +// 根据地图ID获取地图数据 func QueryGiData[T proto.Message](mapId int32) T { - value, _ := giDataMap.Load(mapId) - return value.(T) + value, ok := giPublishData.Load(mapId) + if !ok { + panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据丢失", mapId))) + } + d, ok := value.(*giStoreData) + if !ok { + panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId))) + } + return d.message.(T) } -func QueryGiId(name string) int32 { - value, _ := giNameMap.Load(name) - return value.(int32) -} - -func GetStorageIBPMapData(mapCode string) (int32, *graphicData.IBPGraphicStorage) { - // 处理关联的IBP盘信息 - if mapCode == "" { - return 0, nil - } - ibpId, ok := giNameMap.Load(mapCode) - if !ok { - return 0, nil - } - ibpMapData, ok := giDataMap.Load(ibpId) - if !ok { - return ibpId.(int32), nil - } - return ibpId.(int32), ibpMapData.(*graphicData.IBPGraphicStorage) +// 根据地图名称获取地图数据 +func QueryGiDataByName[T proto.Message](name string) (int32, T) { + var ( + id int32 + giData T + ) + giPublishData.Range(func(key, value any) bool { + d, _ := value.(*giStoreData) + if d.name == name { + id = key.(int32) + giData = d.message.(T) + return false + } + return true + }) + return id, giData } // 转换成统一坐标公里标 diff --git a/ts/simulation/wayside/memory/wayside_simulation.go b/ts/simulation/wayside/memory/wayside_simulation.go index a402127..ea02618 100644 --- a/ts/simulation/wayside/memory/wayside_simulation.go +++ b/ts/simulation/wayside/memory/wayside_simulation.go @@ -515,11 +515,13 @@ func (s *VerifySimulation) EvnWorld() ecs.World { func (s *VerifySimulation) initRepository() error { // 构建Repository var mapIdStrSlice []string + var mapVersion []string for _, id := range s.MapIds { mapIdStrSlice = append(mapIdStrSlice, strconv.Itoa(int(id))) + mapVersion = append(mapVersion, strconv.Itoa(int(QueryGiVersion(id)))) } repoId := strings.Join(mapIdStrSlice, "|") - repoVersion := "0.1" + repoVersion := strings.Join(mapVersion, ".") repo := repository.FindRepository(repoId, repoVersion) if repo == nil { protoRepo, err := buildProtoRepository(s.MapIds) @@ -1150,8 +1152,7 @@ func fillProtoRepository(repo *proto.Repository, storage *graphicData.RtssGraphi PsdId: uidsMap.PsdIds[data.RefScreenDoor].Uid, } repo.Mkxs = append(repo.Mkxs, mkx) - pslMapId := QueryGiId(data.RefGatedBoxMapCode) - pslStorage := QueryGiData[*graphicData.PslGraphicStorage](pslMapId) + _, pslStorage := QueryGiDataByName[*graphicData.PslGraphicStorage](data.RefGatedBoxMapCode) for _, button := range pslStorage.PslButtons { repoButton := &proto.Button{ Id: boxUidInfo.Uid + "_" + button.Code, @@ -1205,7 +1206,7 @@ func fillProtoRepository(repo *proto.Repository, storage *graphicData.RtssGraphi // 将IBP的设备关联到车站中 func handlerIBPDeviceToStation(station *proto.Station, repo *proto.Repository, ibpMapCode string) { - mapId, storage := GetStorageIBPMapData(ibpMapCode) + mapId, storage := QueryGiDataByName[*graphicData.IBPGraphicStorage](ibpMapCode) if storage == nil { return }