Skip to content

Commit

Permalink
refactor: more efficient production of nodes from expanded node id
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 29, 2023
1 parent 27ba3b2 commit 5f8da7d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,13 @@ func (c *Client) Node(id *ua.NodeID) *Node {
return &Node{ID: id, c: c}
}

// NodeFromExpandedNodeID returns a node object which accesses its attributes
// through this client connection. This is usually needed when working with node ids returned
// from browse responses by the server.
func (c *Client) NodeFromExpandedNodeID(id *ua.ExpandedNodeID) *Node {
return &Node{ID: ua.NewNodeIDFromExpandedNodeID(id), c: c}
}

// FindServers finds the servers available at an endpoint
func (c *Client) FindServers(ctx context.Context) (*ua.FindServersResponse, error) {
stats.Client().Add("FindServers", 1)
Expand Down
19 changes: 1 addition & 18 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package opcua

import (
"context"
"encoding/base64"
"strings"

"github.com/gopcua/opcua/id"
Expand Down Expand Up @@ -161,23 +160,7 @@ func (n *Node) ReferencedNodes(ctx context.Context, refs uint32, dir ua.BrowseDi
return nil, err
}
for _, r := range res {
nodeID := r.NodeID.NodeID
switch nodeID.Type() {
case ua.NodeIDTypeTwoByte:
nodeID = ua.NewTwoByteNodeID(uint8(nodeID.IntID()))
case ua.NodeIDTypeFourByte:
nodeID = ua.NewFourByteNodeID(uint8(nodeID.Namespace()), uint16(nodeID.IntID()))
case ua.NodeIDTypeNumeric:
nodeID = ua.NewNumericNodeID(nodeID.Namespace(), nodeID.IntID())
case ua.NodeIDTypeString:
nodeID = ua.NewStringNodeID(nodeID.Namespace(), nodeID.StringID())
case ua.NodeIDTypeGUID:
nodeID = ua.NewGUIDNodeID(nodeID.Namespace(), nodeID.String())
case ua.NodeIDTypeByteString:
bytes, _ := base64.StdEncoding.DecodeString(nodeID.String())
nodeID = ua.NewByteStringNodeID(nodeID.Namespace(), bytes)
}
nodes = append(nodes, n.c.Node(nodeID))
nodes = append(nodes, n.c.NodeFromExpandedNodeID(r.NodeID))
}
return nodes, nil
}
Expand Down
11 changes: 11 additions & 0 deletions ua/node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ func NewByteStringNodeID(ns uint16, id []byte) *NodeID {
}
}

// NewNodeIDFromExpandedNodeID returns a new NodeID derived from ExpandedNodeID
func NewNodeIDFromExpandedNodeID(id *ExpandedNodeID) *NodeID {
return &NodeID{
mask: id.NodeID.mask & 0b00111111, // expanded flag NamespaceURI and ServerIndex need to be unset
ns: id.NodeID.ns,
nid: id.NodeID.nid,
bid: id.NodeID.bid,
gid: id.NodeID.gid,
}
}

// MustParseNodeID returns a node id from a string definition
// if it is parseable by ParseNodeID. Otherwise, the function
// panics.
Expand Down

0 comments on commit 5f8da7d

Please sign in to comment.