From 83a49da6c81cd75339efbefe1dcf18840dbb9a98 Mon Sep 17 00:00:00 2001 From: Jeremy Theocharis Date: Thu, 11 Jul 2024 15:44:35 +0000 Subject: [PATCH] added opcua_tag_group and opcua_tag_name --- opcua_plugin/opcua.go | 30 ++++++++++++---- opcua_plugin/opcua_simulator_test.go | 53 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/opcua_plugin/opcua.go b/opcua_plugin/opcua.go index b13c5ca..e974fb3 100644 --- a/opcua_plugin/opcua.go +++ b/opcua_plugin/opcua.go @@ -80,8 +80,21 @@ func browse(ctx context.Context, n *opcua.Node, path string, level int, logger * return nil, err } + browseName, err := n.BrowseName(ctx) + if err != nil { + return nil, err + } + + var newPath string + if path == "" { + newPath = sanitize(browseName.Name) + } else { + newPath = path + "." + sanitize(browseName.Name) + } + var def = NodeDef{ NodeID: n.ID, + Path: newPath, } switch err := attrs[0].Status; { @@ -456,15 +469,18 @@ func (g *OPCUAInput) createMessageFromValue(variant *ua.Variant, nodeDef NodeDef message := service.NewMessage(b) message.MetaSet("opcua_path", sanitize(nodeDef.NodeID.String())) - message.MetaSet("opcua_tag_path", sanitize(nodeDef.Path)) + message.MetaSet("opcua_tag_path", sanitize(nodeDef.BrowseName)) message.MetaSet("opcua_parent_path", sanitize(nodeDef.ParentNodeID)) - op, _ := message.MetaGet("opcua_path") - pp, _ := message.MetaGet("opcua_parent_path") - tp, _ := message.MetaGet("opcua_tag_path") - g.Log.Debugf("Created message with opcua_path: %s", op) - g.Log.Debugf("Created message with opcua_parent_path: %s", pp) - g.Log.Debugf("Created message with opcua_tag_path: %s", tp) + // Tag Group + tagGroup := nodeDef.Path + // remove nodeDef.BrowseName from tagGroup + tagGroup = strings.Replace(tagGroup, nodeDef.BrowseName, "", 1) + // remove trailing dot + tagGroup = strings.TrimSuffix(tagGroup, ".") + + message.MetaSet("opcua_tag_group", tagGroup) + message.MetaSet("opcua_tag_name", sanitize(nodeDef.BrowseName)) return message } diff --git a/opcua_plugin/opcua_simulator_test.go b/opcua_plugin/opcua_simulator_test.go index 6e73190..ef51c20 100644 --- a/opcua_plugin/opcua_simulator_test.go +++ b/opcua_plugin/opcua_simulator_test.go @@ -949,6 +949,59 @@ var _ = Describe("Test Against Microsoft OPC UA simulator", Serial, func() { }) + FDescribe("opcua_tag_path", func() { + It("should create a proper opcua_tag_group and opcua_tag_name", func() { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + var nodeIDStrings []string = []string{"ns=3;s=OpcPlc"} + + parsedNodeIDs := ParseNodeIDs(nodeIDStrings) + + input := &OPCUAInput{ + Endpoint: "opc.tcp://localhost:50000", + Username: "", + Password: "", + NodeIDs: parsedNodeIDs, + SubscribeEnabled: false, + } + + // Attempt to connect + err := input.Connect(ctx) + Expect(err).NotTo(HaveOccurred()) + + messageBatch, _, err := input.ReadBatch(ctx) + Expect(err).NotTo(HaveOccurred()) + + // for each + for _, message := range messageBatch { + opcuaTagPath, err := message.MetaGet("opcua_tag_path") + Expect(err).To(BeTrue(), "Could not find opcua_tag_path") + GinkgoT().Log("opcua_tag_path: ", opcuaTagPath) + + opcuaTagGroup, err := message.MetaGet("opcua_tag_group") + Expect(err).To(BeTrue(), "Could not find opcua_tag_group") + GinkgoT().Log("opcua_tag_group: ", opcuaTagGroup) + + opcuaTagName, err := message.MetaGet("opcua_tag_name") + Expect(err).To(BeTrue(), "Could not find opcua_tag_name") + GinkgoT().Log("opcua_tag_name: ", opcuaTagName) + + if opcuaTagPath == "StepUp" { + Expect(opcuaTagGroup).To(Equal("OpcPlc.Telemetry.Basic")) + Expect(opcuaTagName).To(Equal("StepUp")) + } + } + + Fail("Test not implemented") + + // Close connection + if input.Client != nil { + input.Client.Close(ctx) + } + }) + }) + }) func checkDatatypeOfOPCUATag(dataType string, messageParsed interface{}, opcuapath string) {