rts-sim-testing-service/protobuf/main_test.go

256 lines
7.0 KiB
Go
Raw Normal View History

package protobuf
import (
"fmt"
"google.golang.org/protobuf/proto"
"joylink.club/bj-rtsts-server/ats/verify/protos/graphicData"
proto2 "joylink.club/rtsssimulation/repository/model/proto"
"os"
"testing"
)
func TestBuildRepository(t *testing.T) {
bytes, err := os.ReadFile("./file.bin")
dir, _ := os.Getwd()
println(dir)
if err != nil {
panic(err)
}
storage := &graphicData.RtssGraphicStorage{}
err = proto.Unmarshal(bytes, storage)
if err != nil {
panic(err)
}
repo := &proto2.Repository{}
axleCountingMap := make(map[string]*graphicData.AxleCounting)
for _, data := range storage.AxleCountings {
axleCountingMap[data.Common.Id] = data
cpType := proto2.CheckPointType_AxleCounter
if data.Invent {
cpType = proto2.CheckPointType_Boundary
}
cp := &proto2.CheckPoint{
Id: data.Common.Id,
Km: convertKm(data.KilometerSystem),
Type: cpType,
DevicePorts: convertDevicePorts(data.AxleCountingRef),
}
repo.CheckPoints = append(repo.CheckPoints, cp)
}
for _, data := range storage.Section {
var turnoutIds []string
if data.SectionType == graphicData.Section_TurnoutPhysical {
turnoutIds = findTurnoutIds(axleCountingMap, data.AxleCountings)
}
physicalSection := &proto2.PhysicalSection{
Id: data.Common.Id,
ADevicePort: convertDevicePort(data.PaRef),
BDevicePort: convertDevicePort(data.PbRef),
TurnoutIds: turnoutIds,
}
repo.PhysicalSections = append(repo.PhysicalSections, physicalSection)
}
for _, data := range storage.Turnouts {
var km *proto2.Kilometer
for _, ks := range data.KilometerSystem {
if ks.Kilometer != 0 {
km = convertKm(ks)
break
}
}
for _, kc := range buildKmConverts(data.KilometerSystem) {
repo.KilometerConverts = append(repo.KilometerConverts, kc)
}
turnout := &proto2.Turnout{
Id: data.Common.Id,
Km: km,
ADevicePort: convertDevicePort(data.PaRef),
BDevicePort: convertDevicePort(data.PbRef),
CDevicePort: convertDevicePort(data.PcRef),
}
repo.Turnouts = append(repo.Turnouts, turnout)
}
for _, data := range storage.Signals {
var sectionId string
var turnoutPort *proto2.DevicePort
switch data.RefDev.DeviceType {
case graphicData.RelatedRef_Section:
sectionId = data.RefDev.Id
case graphicData.RelatedRef_Turnout:
turnoutPort = convertDevicePort(data.RefDev)
}
signal := &proto2.Signal{
Id: data.Common.Id,
Km: convertKm(data.KilometerSystem),
SectionId: sectionId,
TurnoutPort: turnoutPort,
}
repo.Signals = append(repo.Signals, signal)
}
for _, data := range storage.Transponders {
var sectionId string
var turnoutPort *proto2.DevicePort
switch data.TransponderRef.DeviceType {
case graphicData.RelatedRef_Section:
sectionId = data.TransponderRef.Id
case graphicData.RelatedRef_Turnout:
turnoutPort = convertDevicePort(data.TransponderRef)
}
responder := &proto2.Responder{
Id: data.Common.Id,
Km: convertKm(data.KilometerSystem),
SectionId: sectionId,
TurnoutPort: turnoutPort,
}
repo.Responders = append(repo.Responders, responder)
}
slopeKsMap := make(map[string]*graphicData.SlopeKiloMarker)
for _, data := range storage.SlopeKiloMarker {
slopeKsMap[data.Common.Id] = data
for _, kc := range buildKmConverts(data.KilometerSystem) {
repo.KilometerConverts = append(repo.KilometerConverts, kc)
}
}
curveKsMap := make(map[string]*graphicData.CurvatureKiloMarker)
for _, data := range storage.CurvatureKiloMarker {
curveKsMap[data.Common.Id] = data
for _, kc := range buildKmConverts(data.KilometerSystem) {
repo.KilometerConverts = append(repo.KilometerConverts, kc)
}
}
for _, data := range storage.Slopes {
var kms []*proto2.Kilometer
for _, id := range data.RefDeviceId {
kms = append(kms, convertKm(slopeKsMap[id].KilometerSystem[0]))
}
slope := &proto2.Slope{
Id: data.Common.Id,
Kms: kms,
Degree: data.SlopeNumber,
}
repo.Slopes = append(repo.Slopes, slope)
}
for _, data := range storage.Curvatures {
var kms []*proto2.Kilometer
for _, id := range data.RefDeviceId {
kms = append(kms, convertKm(curveKsMap[id].KilometerSystem[0]))
}
slope := &proto2.SectionalCurvature{
Id: data.Common.Id,
Kms: kms,
Radius: data.CurvatureNumber,
}
repo.SectionalCurvatures = append(repo.SectionalCurvatures, slope)
}
repoBytes, err := proto.Marshal(repo)
if err != nil {
panic(err)
}
err = os.WriteFile("./repo.bin", repoBytes, os.ModePerm)
println(err)
}
func convertKm(ks *graphicData.KilometerSystem) *proto2.Kilometer {
var dir proto2.Direction
switch ks.Direction {
case graphicData.Direction_LEFT:
dir = proto2.Direction_LEFT
case graphicData.Direction_RIGHT:
dir = proto2.Direction_RIGHT
}
return &proto2.Kilometer{
Value: ks.Kilometer,
CoordinateSystem: ks.CoordinateSystem,
Direction: dir,
}
}
func convertDevicePort(ref *graphicData.RelatedRef) *proto2.DevicePort {
if ref == nil {
return nil
}
var deviceType proto2.DeviceType
var port proto2.Port
switch ref.DevicePort {
case graphicData.RelatedRef_A:
port = proto2.Port_A
case graphicData.RelatedRef_B:
port = proto2.Port_B
case graphicData.RelatedRef_C:
port = proto2.Port_C
}
switch ref.DeviceType {
case graphicData.RelatedRef_Section:
deviceType = proto2.DeviceType_DeviceType_PhysicalSection
case graphicData.RelatedRef_Turnout:
deviceType = proto2.DeviceType_DeviceType_Turnout
default:
panic(fmt.Sprintf("异常的设备类型-%s", ref.DeviceType))
}
return &proto2.DevicePort{
DeviceId: ref.Id,
DeviceType: deviceType,
Port: port,
}
}
func convertDevicePorts(refList []*graphicData.RelatedRef) []*proto2.DevicePort {
var dps []*proto2.DevicePort
for _, ref := range refList {
dps = append(dps, convertDevicePort(ref))
}
return dps
}
func findTurnoutIds(axleCountingMap map[string]*graphicData.AxleCounting, axleIds []string) []string {
if len(axleIds) <= 2 {
return nil
}
turnoutMap := make(map[string]bool)
for _, axleId := range axleIds {
axle := axleCountingMap[axleId]
relTurnoutCount := 0
var turnoutId string
for _, ref := range axle.AxleCountingRef {
if ref.DeviceType == graphicData.RelatedRef_Turnout {
relTurnoutCount++
turnoutId = ref.Id
}
}
if relTurnoutCount == 1 {
turnoutMap[turnoutId] = true
}
}
var turnoutIds []string
for id, _ := range turnoutMap {
turnoutIds = append(turnoutIds, id)
}
return turnoutIds
}
func buildKmConverts(ksList []*graphicData.KilometerSystem) []*proto2.KilometerConvert {
var kmConvers []*proto2.KilometerConvert
for i, ks := range ksList {
if ks.Kilometer == 0 {
continue
}
for j := i + 1; j < len(ksList); j++ {
if ks.Kilometer == 0 {
continue
}
kmConvers = append(kmConvers, buildKmConvert(ks, ksList[j]))
}
}
return kmConvers
}
func buildKmConvert(ks1 *graphicData.KilometerSystem, ks2 *graphicData.KilometerSystem) *proto2.KilometerConvert {
return &proto2.KilometerConvert{
KmA: convertKm(ks1),
KmB: convertKm(ks2),
SameTrend: false,
}
}