Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method:translate relative path of browse name to node id #237

Merged
merged 4 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/translate/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018-2019 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.

package main

import (
"context"
"flag"
"fmt"
"log"

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

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

flag.Parse()
log.SetFlags(0)

ctx := context.Background()

c := opcua.NewClient(*endpoint)
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()

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

import (
"time"

"github.com/gopcua/opcua/id"
sdghchj marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gopcua/opcua/ua"
"strings"
"time"
)

// Node is a high-level object to interact with a node in the
Expand Down Expand Up @@ -116,7 +117,7 @@ func (n *Node) Attribute(attrID ua.AttributeID) (*ua.Variant, error) {
return value, nil
}

// References retrns all references for the node.
// References returns all references for the node.
// todo(fs): this is not complete since it only returns the
// todo(fs): top-level reference at this point.
func (n *Node) References(refs *ua.NodeID) (*ua.BrowseResponse, error) {
Expand All @@ -141,3 +142,59 @@ func (n *Node) References(refs *ua.NodeID) (*ua.BrowseResponse, error) {
return n.c.Browse(req)
// implement browse_next
}

//TranslateBrowsePathToNodeId translate an array of browseName segment to NodeID
func (n *Node) TranslateBrowsePathToNodeId(pathNames []*ua.QualifiedName) (*ua.NodeID, error) {
req := ua.TranslateBrowsePathsToNodeIDsRequest{
RequestHeader: &ua.RequestHeader{AuthenticationToken: ua.NewFourByteNodeID(0, id.TranslateBrowsePathsToNodeIDsRequest_Encoding_DefaultBinary)},
sdghchj marked this conversation as resolved.
Show resolved Hide resolved
BrowsePaths: []*ua.BrowsePath{
{
StartingNode: n.ID,
RelativePath: &ua.RelativePath{
Elements: []*ua.RelativePathElement{},
},
},
}}

for _, pathSegment := 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,
},
)
}

var nodeID *ua.NodeID
err := n.c.Send(&req, func(i interface{}) error {
if resp, ok := i.(*ua.TranslateBrowsePathsToNodeIDsResponse); ok {
if len(resp.Results) == 0 {
return ua.StatusBadUnexpectedError
}

if resp.Results[0].StatusCode != ua.StatusOK {
return resp.Results[0].StatusCode
}

if len(resp.Results[0].Targets) == 0 {
return ua.StatusBadUnexpectedError
}
nodeID = resp.Results[0].Targets[0].TargetID.NodeID
return nil
}
return ua.StatusBadUnexpectedError
})
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
for _, segment := range segments {
pathNames = append(pathNames, &ua.QualifiedName{NamespaceIndex: uint16(ns), Name: segment})
}
return n.TranslateBrowsePathToNodeId(pathNames)
}