Skip to content

Commit

Permalink
Merge pull request #244 from gopcua/pr-236-followup
Browse files Browse the repository at this point in the history
Follow-up to PR #237
  • Loading branch information
magiconair authored Jul 19, 2019
2 parents 5e4a1a1 + f8fd527 commit 569363a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions examples/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

func main() {
endpoint := flag.String("endpoint", "opc.tcp://localhost:4840", "OPC UA Endpoint URL")
ns := flag.Int("namespace", 0, "namespace of node")
nodePath := flag.String("path", "device_led.temperature", "path of a node's browse name")
ns := flag.Int("namespace", 0, "namespace of the node")
flag.BoolVar(&debug.Enable, "debug", false, "enable debug logging")

flag.Parse()
Expand All @@ -34,9 +34,9 @@ func main() {
defer c.Close()

root := c.Node(ua.NewTwoByteNodeID(id.ObjectsFolder))
nodeId, err := root.TranslateBrowsePathInSameNamespaceToNodeId(uint8(*ns), *nodePath)
nodeID, err := root.TranslateBrowsePathInNamespaceToNodeID(uint16(*ns), *nodePath)
if err != nil {
log.Fatal(err)
}
fmt.Println(nodeId)
fmt.Println(nodeID)
}
28 changes: 14 additions & 14 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package opcua

import (
"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
"strings"
"time"

"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
)

// Node is a high-level object to interact with a node in the
Expand Down Expand Up @@ -143,10 +144,9 @@ func (n *Node) References(refs *ua.NodeID) (*ua.BrowseResponse, error) {
// implement browse_next
}

//TranslateBrowsePathToNodeId translate an array of browseName segment to NodeID
func (n *Node) TranslateBrowsePathToNodeId(pathNames []*ua.QualifiedName) (*ua.NodeID, error) {
// TranslateBrowsePathsToNodeIDs translates an array of browseName segments to NodeIDs.
func (n *Node) TranslateBrowsePathsToNodeIDs(pathNames []*ua.QualifiedName) (*ua.NodeID, error) {
req := ua.TranslateBrowsePathsToNodeIDsRequest{
RequestHeader: &ua.RequestHeader{AuthenticationToken: ua.NewFourByteNodeID(0, id.TranslateBrowsePathsToNodeIDsRequest_Encoding_DefaultBinary)},
BrowsePaths: []*ua.BrowsePath{
{
StartingNode: n.ID,
Expand All @@ -156,12 +156,12 @@ func (n *Node) TranslateBrowsePathToNodeId(pathNames []*ua.QualifiedName) (*ua.N
},
}}

for _, pathSegment := range pathNames {
for _, name := range pathNames {
req.BrowsePaths[0].RelativePath.Elements = append(req.BrowsePaths[0].RelativePath.Elements,
&ua.RelativePathElement{ReferenceTypeID: ua.NewTwoByteNodeID(id.HierarchicalReferences),
IsInverse: false,
IncludeSubtypes: true,
TargetName: pathSegment,
TargetName: name,
},
)
}
Expand All @@ -188,13 +188,13 @@ func (n *Node) TranslateBrowsePathToNodeId(pathNames []*ua.QualifiedName) (*ua.N
return nodeID, err
}

//TranslateBrowsePathInSameNamespaceToNodeId translate a browseName to NodeID
//here we assume that all parts of path are in the same namespace
func (n *Node) TranslateBrowsePathInSameNamespaceToNodeId(ns uint8, browseNamePath string) (*ua.NodeID, error) {
segments := strings.Split(browseNamePath, ".")
var pathNames []*ua.QualifiedName
// TranslateBrowsePathInNamespaceToNodeID translates a browseName to a NodeID within the same namespace.
func (n *Node) TranslateBrowsePathInNamespaceToNodeID(ns uint16, browsePath string) (*ua.NodeID, error) {
segments := strings.Split(browsePath, ".")
var names []*ua.QualifiedName
for _, segment := range segments {
pathNames = append(pathNames, &ua.QualifiedName{NamespaceIndex: uint16(ns), Name: segment})
qn := &ua.QualifiedName{NamespaceIndex: ns, Name: segment}
names = append(names, qn)
}
return n.TranslateBrowsePathToNodeId(pathNames)
return n.TranslateBrowsePathsToNodeIDs(names)
}

0 comments on commit 569363a

Please sign in to comment.