Skip to content

Commit

Permalink
added opcua_tag_group and opcua_tag_name
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyTheocharis committed Jul 11, 2024
1 parent 79a38c9 commit 83a49da
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
30 changes: 23 additions & 7 deletions opcua_plugin/opcua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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; {
Expand Down Expand Up @@ -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
}
Expand Down
53 changes: 53 additions & 0 deletions opcua_plugin/opcua_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 971 in opcua_plugin/opcua_simulator_test.go

View workflow job for this annotation

GitHub Actions / go-test

It 07/11/24 15:55:40.627

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) {
Expand Down

0 comments on commit 83a49da

Please sign in to comment.