From c82ad773a35fd82960637555be9810ceb42ad974 Mon Sep 17 00:00:00 2001 From: douxu Date: Wed, 17 Jun 2026 10:47:35 +0800 Subject: [PATCH] refactor: lowercase channel name suffixes and rename PS to PF - change all ChannelSuffix values from uppercase to lowercase - rename ChannelSuffixPS ("PS") to ChannelSuffixPF ("pf") - align channel suffix naming with downstream measurement keys --- constants/measurement.go | 18 ++++----- diagram/anchor_set.go | 14 +++---- diagram/component_set.go | 17 +++------ handler/circuit_diagram_update.go | 2 +- model/measurement_protol_model.go | 61 ++++++++++++++++++------------- 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/constants/measurement.go b/constants/measurement.go index d95864b..cfaed67 100644 --- a/constants/measurement.go +++ b/constants/measurement.go @@ -19,15 +19,15 @@ const ( // channel name suffix const ( - ChannelSuffixP = "P" - ChannelSuffixQ = "Q" - ChannelSuffixS = "S" - ChannelSuffixPS = "PS" - ChannelSuffixF = "F" - ChannelSuffixDeltaF = "deltaF" - ChannelSuffixUAB = "UAB" - ChannelSuffixUBC = "UBC" - ChannelSuffixUCA = "UCA" + ChannelSuffixP = "p" + ChannelSuffixQ = "q" + ChannelSuffixS = "s" + ChannelSuffixPF = "pf" + ChannelSuffixF = "f" + ChannelSuffixDeltaF = "df" + ChannelSuffixUAB = "uab" + ChannelSuffixUBC = "ubc" + ChannelSuffixUCA = "uca" ) const ( diff --git a/diagram/anchor_set.go b/diagram/anchor_set.go index a6447ca..2923ee7 100644 --- a/diagram/anchor_set.go +++ b/diagram/anchor_set.go @@ -2,24 +2,20 @@ package diagram import ( - "errors" "fmt" - "sync" + + "modelRT/util" ) -// anchorValueOverview define struct of storage all anchor value -var anchorValueOverview sync.Map +// anchorValueOverview define struct of storage all anchor value keyed by component uuid +var anchorValueOverview util.TypedMap[string, string] // GetAnchorValue define func of get circuit diagram data by componentID func GetAnchorValue(componentUUID string) (string, error) { - value, ok := diagramsOverview.Load(componentUUID) + anchorValue, ok := anchorValueOverview.Load(componentUUID) if !ok { return "", fmt.Errorf("can not find anchor value by componentUUID:%s", componentUUID) } - anchorValue, ok := value.(string) - if !ok { - return "", errors.New("convert to string failed") - } return anchorValue, nil } diff --git a/diagram/component_set.go b/diagram/component_set.go index da9bddf..c06ad85 100644 --- a/diagram/component_set.go +++ b/diagram/component_set.go @@ -2,32 +2,27 @@ package diagram import ( - "errors" "fmt" - "sync" "modelRT/orm" + "modelRT/util" ) -// diagramsOverview define struct of storage all circuit diagram data -var diagramsOverview sync.Map +// diagramsOverview define struct of storage all circuit diagram data keyed by component uuid +var diagramsOverview util.TypedMap[string, *orm.Component] // GetComponentMap define func of get circuit diagram data by component uuid func GetComponentMap(componentUUID string) (*orm.Component, error) { - value, ok := diagramsOverview.Load(componentUUID) + componentInfo, ok := diagramsOverview.Load(componentUUID) if !ok { return nil, fmt.Errorf("can not find graph by global uuid:%s", componentUUID) } - componentInfo, ok := value.(*orm.Component) - if !ok { - return nil, errors.New("convert to component map struct failed") - } return componentInfo, nil } // UpdateComponentMap define func of update circuit diagram data by component uuid and component info -func UpdateComponentMap(componentID int64, componentInfo *orm.Component) bool { - _, result := diagramsOverview.Swap(componentID, componentInfo) +func UpdateComponentMap(componentUUID string, componentInfo *orm.Component) bool { + _, result := diagramsOverview.Swap(componentUUID, componentInfo) return result } diff --git a/handler/circuit_diagram_update.go b/handler/circuit_diagram_update.go index 33bb46b..e28a413 100644 --- a/handler/circuit_diagram_update.go +++ b/handler/circuit_diagram_update.go @@ -137,7 +137,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) return } - diagram.UpdateComponentMap(info.ID, component) + diagram.UpdateComponentMap(info.UUID, component) } if len(request.FreeVertexs) > 0 { diff --git a/model/measurement_protol_model.go b/model/measurement_protol_model.go index 7fdce12..228f51f 100644 --- a/model/measurement_protol_model.go +++ b/model/measurement_protol_model.go @@ -59,36 +59,36 @@ func NewPower104DataSource(station string, packet, offset int) (*MeasurementData } func generateChannelName(prefix string, number int, suffix string) (string, error) { + // shortPrefix is the literal prefix written into the channel name (tm/ts/tc), + // maxNumber is the inclusive upper bound, padWidth is the zero-padded digit width. + var shortPrefix string + var maxNumber, padWidth int switch prefix { case constants.ChannelPrefixTelemetry: - if number > 10 { - return "", common.ErrExceedsLimitType - } - var builder strings.Builder - numberStr := strconv.Itoa(number) - builder.Grow(len(prefix) + len(numberStr) + len(suffix)) - builder.WriteString(prefix) - builder.WriteString(numberStr) - builder.WriteString(suffix) - channelName := builder.String() - return channelName, nil + shortPrefix, maxNumber, padWidth = "tm", 8, 1 case constants.ChannelPrefixTelesignal: - var numberStr string - if number < 10 { - numberStr = "0" + strconv.Itoa(number) - } - numberStr = strconv.Itoa(number) - - var builder strings.Builder - builder.Grow(len(prefix) + len(numberStr) + len(suffix)) - builder.WriteString(prefix) - builder.WriteString(numberStr) - builder.WriteString(suffix) - channelName := builder.String() - return channelName, nil + shortPrefix, maxNumber, padWidth = "ts", 16, 2 + case constants.ChannelPrefixTelecommand: + shortPrefix, maxNumber, padWidth = "tc", 9, 1 default: return "", common.ErrUnsupportedChannelPrefixType } + + if number < 1 || number > maxNumber { + return "", common.ErrExceedsLimitType + } + + numberStr := strconv.Itoa(number) + if len(numberStr) < padWidth { + numberStr = strings.Repeat("0", padWidth-len(numberStr)) + numberStr + } + + var builder strings.Builder + builder.Grow(len(shortPrefix) + len(numberStr) + len(suffix)) + builder.WriteString(shortPrefix) + builder.WriteString(numberStr) + builder.WriteString(suffix) + return builder.String(), nil } // NewTelemetryChannel define func of generate telemetry channel CL3611 data source @@ -109,6 +109,15 @@ func NewTelesignalChannel(station, device, channelNameSuffix string, channelNumb return NewCL3611DataSource(station, device, channelName) } +// NewTelecommandChannel define func of generate telecommand channel CL3611 data source +func NewTelecommandChannel(station, device, channelNameSuffix string, channelNumber int) (*MeasurementDataSource, error) { + channelName, err := generateChannelName(constants.ChannelPrefixTelecommand, channelNumber, channelNameSuffix) + if err != nil { + return nil, fmt.Errorf("failed to generate channel name: %w", err) + } + return NewCL3611DataSource(station, device, channelName) +} + // NewStandardChannel define func of generate standard channel CL3611 data source func NewStandardChannel(station, device, channelType string) (*MeasurementDataSource, error) { return NewCL3611DataSource(station, device, channelType) @@ -264,9 +273,9 @@ func GenerateMeasureIdentifier(source map[string]any) (string, error) { func concatP104WithPlus(station string, packet int, offset int) string { packetStr := strconv.Itoa(packet) offsetStr := strconv.Itoa(offset) - return station + ":" + packetStr + ":" + offsetStr + return strings.ToLower(station + ":104:" + packetStr + ":" + offsetStr) } func concatCL361WithPlus(station, device, channel string) string { - return station + ":" + device + ":" + "phasor" + ":" + channel + return strings.ToLower(station + ":" + device + ":" + "phasor" + ":" + channel) }