rtss-core/example/build_repo_example/main.go

173 lines
6.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"log/slog"
"joylink.club/rtss-core/example/data_proto"
modelimpl "joylink.club/rtss-core/example/model_impl"
"joylink.club/rtss-core/example/repository"
"joylink.club/rtss-core/model"
)
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
repo1 := repository.NewRepository("test1")
rtssGraphicStorage := data_proto.GetXian6STYG()
dataMapping := repository.NewDataMapping("1", rtssGraphicStorage)
repo1.DataMapping["1"] = dataMapping
// log.Println(rtssGraphicStorage.Stations)
// log.Println(rtssGraphicStorage.Section)
// log.Println(rtssGraphicStorage.Turnouts)
for _, station := range rtssGraphicStorage.Stations {
dataMapping.StationDataMap[station.Common.Id] = station
uid := station.Code
dataMapping.AddIdMapping(repository.NewIdMapping(station.Common.Id, uid))
stationModel := model.NewStation(uid, station.ConcentrationStations)
repo1.StationMap[uid] = &modelimpl.Station{StationImpl: stationModel}
}
for _, section := range rtssGraphicStorage.Section {
if section.CentralizedStations == nil || len(section.CentralizedStations) == 0 {
continue
}
belongStation := dataMapping.StationDataMap[section.CentralizedStations[0]]
if belongStation == nil {
continue
}
dataMapping.SectionDataMap[section.Common.Id] = section
uid := getSectionUid(section, belongStation, dataMapping.GetLineInfo())
dataMapping.AddIdMapping(repository.NewIdMapping(section.Common.Id, uid))
sectionModel := model.NewSection(uid)
repo1.SectionMap[uid] = &modelimpl.Section{SectionImpl: sectionModel}
}
for _, turnout := range rtssGraphicStorage.Turnouts {
if turnout.CentralizedStations == nil || len(turnout.CentralizedStations) == 0 {
continue
}
belongStation := dataMapping.StationDataMap[turnout.CentralizedStations[0]]
if belongStation == nil {
continue
}
dataMapping.TurnoutDataMap[turnout.Common.Id] = turnout
uid := getTurnoutUid(turnout, belongStation, dataMapping.GetLineInfo())
dataMapping.AddIdMapping(repository.NewIdMapping(turnout.Common.Id, uid))
turnoutModel := model.NewTurnout(uid)
repo1.TurnoutMap[uid] = &modelimpl.Turnout{TurnoutImpl: turnoutModel}
}
// 构建区段关系
buildSectionRelationships(dataMapping, repo1)
// 构建道岔关系
buildTurnoutRelationships(dataMapping, repo1)
// 检查区段、道岔通道连接关系
err := repo1.CheckPipeLink()
if err != nil {
slog.Error("区段道岔连接关系检查错误", "errMsg", err)
return
} else {
slog.Info("区段、道岔连接关系检查通过")
}
// 构建link/linknode
repo1.BuildLinks()
// 检查link/linknode
err = repo1.CheckPipeLink()
if err != nil {
slog.Error("link/linknode连接关系检查错误", "errMsg", err)
return
} else {
slog.Info("link/linknode连接关系检查通过")
}
}
func buildTurnoutRelationships(dataMapping *repository.DataMapping, repo1 *repository.Repository) {
for _, turnout := range dataMapping.TurnoutDataMap {
idmapping := dataMapping.IdMappingMap[turnout.Common.Id]
if idmapping == nil {
panic(fmt.Errorf("构建道岔关系错误idmapping异常为空"))
}
turnoutModel := repo1.TurnoutMap[idmapping.Uid]
buildTurnoutPortLinkship(turnout.PaRef, turnoutModel, model.PipePortA, repo1, dataMapping)
buildTurnoutPortLinkship(turnout.PbRef, turnoutModel, model.PipePortB, repo1, dataMapping)
buildTurnoutPortLinkship(turnout.PcRef, turnoutModel, model.PipePortC, repo1, dataMapping)
}
}
func buildSectionRelationships(dataMapping *repository.DataMapping, repo1 *repository.Repository) {
for _, section := range dataMapping.SectionDataMap {
idmapping := dataMapping.IdMappingMap[section.Common.Id]
if idmapping == nil {
panic(fmt.Errorf("构建区段关系错误idmapping异常为空"))
}
sectionModel := repo1.SectionMap[idmapping.Uid]
buildSectionPortLinkRelation(section.PaRef, sectionModel, model.PipePortA, repo1, dataMapping)
buildSectionPortLinkRelation(section.PbRef, sectionModel, model.PipePortB, repo1, dataMapping)
}
}
func buildTurnoutPortLinkship(pref *data_proto.RelatedRef, sourceModel model.Turnout, port model.PipePort, repo1 *repository.Repository, dataMapping *repository.DataMapping) {
if pref == nil {
return
}
if pref.DeviceType == data_proto.RelatedRef_Section {
idmapping2 := dataMapping.IdMappingMap[pref.Id]
if idmapping2 != nil {
sectionModel := repo1.SectionMap[idmapping2.Uid]
if sectionModel != nil {
sourceModel.SetLinkedElement(port, model.NewPipeLink(sectionModel, convertPort(pref.DevicePort)))
}
}
}
if pref.DeviceType == data_proto.RelatedRef_Turnout {
idmapping2 := dataMapping.IdMappingMap[pref.Id]
if idmapping2 != nil {
turnoutModel2 := repo1.TurnoutMap[idmapping2.Uid]
if turnoutModel2 != nil {
sourceModel.SetLinkedElement(port, model.NewPipeLink(turnoutModel2, convertPort(pref.DevicePort)))
}
}
}
}
func buildSectionPortLinkRelation(pref *data_proto.RelatedRef, sourceModel model.Section, port model.PipePort, repo1 *repository.Repository, dataMapping *repository.DataMapping) {
if pref == nil {
return
}
if pref.DeviceType == data_proto.RelatedRef_Section {
idmapping2 := dataMapping.IdMappingMap[pref.Id]
if idmapping2 != nil {
sectionModel := repo1.SectionMap[idmapping2.Uid]
if sectionModel != nil {
sourceModel.SetLinkedElement(port, model.NewPipeLink(sectionModel, convertPort(pref.DevicePort)))
}
}
}
if pref.DeviceType == data_proto.RelatedRef_Turnout {
idmapping2 := dataMapping.IdMappingMap[pref.Id]
if idmapping2 != nil {
turnoutModel := repo1.TurnoutMap[idmapping2.Uid]
if turnoutModel != nil {
sourceModel.SetLinkedElement(port, model.NewPipeLink(turnoutModel, convertPort(pref.DevicePort)))
}
}
}
}
func convertPort(port data_proto.RelatedRef_DevicePort) model.PipePort {
if port == data_proto.RelatedRef_A {
return model.PipePortA
} else if port == data_proto.RelatedRef_B {
return model.PipePortB
} else if port == data_proto.RelatedRef_C {
return model.PipePortC
}
panic(fmt.Errorf("未知的数据关联端口类型: %v", port))
}
func getSectionUid(section *data_proto.Section, belongStation *data_proto.Station, lineInfo string) string {
return fmt.Sprintf("%s/%s/%s", lineInfo, belongStation.Code, section.Code)
}
func getTurnoutUid(turnout *data_proto.Turnout, belongStation *data_proto.Station, lineInfo string) string {
return fmt.Sprintf("%s/%s/%s", lineInfo, belongStation.Code, turnout.Code)
}