From 5f8da7d9cddbd1a1b22e54a8c4c36e8e38f3466c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20R=C3=BChl?= Date: Tue, 29 Aug 2023 09:39:12 +0200 Subject: [PATCH] refactor: more efficient production of nodes from expanded node id --- client.go | 7 +++++++ node.go | 19 +------------------ ua/node_id.go | 11 +++++++++++ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/client.go b/client.go index d0520c72..b8954cd4 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/node.go b/node.go index 6e172c7e..2d083297 100644 --- a/node.go +++ b/node.go @@ -6,7 +6,6 @@ package opcua import ( "context" - "encoding/base64" "strings" "github.com/gopcua/opcua/id" @@ -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 } diff --git a/ua/node_id.go b/ua/node_id.go index 594829d3..9fdb8f2b 100644 --- a/ua/node_id.go +++ b/ua/node_id.go @@ -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.