forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
402 lines (356 loc) · 14.2 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2018-2020 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 opcua
import (
"context"
"strings"
"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
)
// Node is a high-level object to interact with a node in the
// address space. It provides common convenience functions to
// access and manipulate the common attributes of a node.
type Node struct {
// ID is the node id of the node.
ID *ua.NodeID
c *Client
}
func (n *Node) String() string {
return n.ID.String()
}
// NodeClass returns the node class attribute.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) NodeClass(ctx context.Context) (ua.NodeClass, error) {
return n.NodeClassWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) NodeClassWithContext(ctx context.Context) (ua.NodeClass, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDNodeClass)
if err != nil {
return 0, err
}
return ua.NodeClass(v.Int()), nil
}
// BrowseName returns the browse name of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) BrowseName() (*ua.QualifiedName, error) {
return n.BrowseNameWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) BrowseNameWithContext(ctx context.Context) (*ua.QualifiedName, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDBrowseName)
if err != nil {
return nil, err
}
return v.Value().(*ua.QualifiedName), nil
}
// Description returns the description of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) Description() (*ua.LocalizedText, error) {
return n.DescriptionWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) DescriptionWithContext(ctx context.Context) (*ua.LocalizedText, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDDescription)
if err != nil {
return nil, err
}
return v.Value().(*ua.LocalizedText), nil
}
// DisplayName returns the display name of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) DisplayName() (*ua.LocalizedText, error) {
return n.DisplayNameWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) DisplayNameWithContext(ctx context.Context) (*ua.LocalizedText, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDDisplayName)
if err != nil {
return nil, err
}
return v.Value().(*ua.LocalizedText), nil
}
// AccessLevel returns the access level of the node.
// The returned value is a mask where multiple values can be
// set, e.g. read and write.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) AccessLevel() (ua.AccessLevelType, error) {
return n.AccessLevelWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) AccessLevelWithContext(ctx context.Context) (ua.AccessLevelType, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDAccessLevel)
if err != nil {
return 0, err
}
return ua.AccessLevelType(v.Value().(uint8)), nil
}
// HasAccessLevel returns true if all bits from mask are
// set in the access level mask of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) HasAccessLevel(mask ua.AccessLevelType) (bool, error) {
return n.HasAccessLevelWithContext(context.Background(), mask)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) HasAccessLevelWithContext(ctx context.Context, mask ua.AccessLevelType) (bool, error) {
v, err := n.AccessLevelWithContext(ctx)
if err != nil {
return false, err
}
return (v & mask) == mask, nil
}
// UserAccessLevel returns the access level of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) UserAccessLevel() (ua.AccessLevelType, error) {
return n.UserAccessLevelWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) UserAccessLevelWithContext(ctx context.Context) (ua.AccessLevelType, error) {
v, err := n.AttributeWithContext(ctx, ua.AttributeIDUserAccessLevel)
if err != nil {
return 0, err
}
return ua.AccessLevelType(v.Value().(uint8)), nil
}
// HasUserAccessLevel returns true if all bits from mask are
// set in the user access level mask of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) HasUserAccessLevel(mask ua.AccessLevelType) (bool, error) {
return n.HasUserAccessLevelWithContext(context.Background(), mask)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) HasUserAccessLevelWithContext(ctx context.Context, mask ua.AccessLevelType) (bool, error) {
v, err := n.UserAccessLevelWithContext(ctx)
if err != nil {
return false, err
}
return (v & mask) == mask, nil
}
// Value returns the value of the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) Value() (*ua.Variant, error) {
return n.ValueWithContext(context.Background())
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) ValueWithContext(ctx context.Context) (*ua.Variant, error) {
return n.AttributeWithContext(ctx, ua.AttributeIDValue)
}
// Attribute returns the attribute of the node. with the given id.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) Attribute(attrID ua.AttributeID) (*ua.Variant, error) {
return n.AttributeWithContext(context.Background(), attrID)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) AttributeWithContext(ctx context.Context, attrID ua.AttributeID) (*ua.Variant, error) {
rv := &ua.ReadValueID{NodeID: n.ID, AttributeID: attrID}
req := &ua.ReadRequest{NodesToRead: []*ua.ReadValueID{rv}}
res, err := n.c.ReadWithContext(ctx, req)
if err != nil {
return nil, err
}
if len(res.Results) == 0 {
// #188: we return StatusBadUnexpectedError because it is unclear, under what
// circumstances the server would return no error and no results in the response
return nil, ua.StatusBadUnexpectedError
}
value := res.Results[0].Value
if res.Results[0].Status != ua.StatusOK {
return value, res.Results[0].Status
}
return value, nil
}
// Attributes returns the given node attributes.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) Attributes(attrID ...ua.AttributeID) ([]*ua.DataValue, error) {
return n.AttributesWithContext(context.Background(), attrID...)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) AttributesWithContext(ctx context.Context, attrID ...ua.AttributeID) ([]*ua.DataValue, error) {
req := &ua.ReadRequest{}
for _, id := range attrID {
rv := &ua.ReadValueID{NodeID: n.ID, AttributeID: id}
req.NodesToRead = append(req.NodesToRead, rv)
}
res, err := n.c.ReadWithContext(ctx, req)
if err != nil {
return nil, err
}
return res.Results, nil
}
// Children returns the child nodes which match the node class mask.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) Children(refs uint32, mask ua.NodeClass) ([]*Node, error) {
return n.ChildrenWithContext(context.Background(), refs, mask)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) ChildrenWithContext(ctx context.Context, refs uint32, mask ua.NodeClass) ([]*Node, error) {
if refs == 0 {
refs = id.HierarchicalReferences
}
return n.ReferencedNodesWithContext(ctx, refs, ua.BrowseDirectionForward, mask, true)
}
// ReferencedNodes returns the nodes referenced by this node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) ReferencedNodes(refs uint32, dir ua.BrowseDirection, mask ua.NodeClass, includeSubtypes bool) ([]*Node, error) {
return n.ReferencedNodesWithContext(context.Background(), refs, dir, mask, includeSubtypes)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) ReferencedNodesWithContext(ctx context.Context, refs uint32, dir ua.BrowseDirection, mask ua.NodeClass, includeSubtypes bool) ([]*Node, error) {
if refs == 0 {
refs = id.References
}
var nodes []*Node
res, err := n.ReferencesWithContext(ctx, refs, dir, mask, includeSubtypes)
if err != nil {
return nil, err
}
for _, r := range res {
nodes = append(nodes, n.c.Node(r.NodeID.NodeID))
}
return nodes, nil
}
// References returns all references for the node.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
//
// todo(fs): this is not complete since it only returns the
// todo(fs): top-level reference at this point.
func (n *Node) References(refType uint32, dir ua.BrowseDirection, mask ua.NodeClass, includeSubtypes bool) ([]*ua.ReferenceDescription, error) {
return n.ReferencesWithContext(context.Background(), refType, dir, mask, includeSubtypes)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) ReferencesWithContext(ctx context.Context, refType uint32, dir ua.BrowseDirection, mask ua.NodeClass, includeSubtypes bool) ([]*ua.ReferenceDescription, error) {
if refType == 0 {
refType = id.References
}
if mask == 0 {
mask = ua.NodeClassAll
}
desc := &ua.BrowseDescription{
NodeID: n.ID,
BrowseDirection: dir,
ReferenceTypeID: ua.NewNumericNodeID(0, refType),
IncludeSubtypes: includeSubtypes,
NodeClassMask: uint32(mask),
ResultMask: uint32(ua.BrowseResultMaskAll),
}
req := &ua.BrowseRequest{
View: &ua.ViewDescription{
ViewID: ua.NewTwoByteNodeID(0),
},
RequestedMaxReferencesPerNode: 0,
NodesToBrowse: []*ua.BrowseDescription{desc},
}
resp, err := n.c.BrowseWithContext(ctx, req)
if err != nil {
return nil, err
}
return n.browseNext(ctx, resp.Results)
}
func (n *Node) browseNext(ctx context.Context, results []*ua.BrowseResult) ([]*ua.ReferenceDescription, error) {
refs := results[0].References
for len(results[0].ContinuationPoint) > 0 {
req := &ua.BrowseNextRequest{
ContinuationPoints: [][]byte{results[0].ContinuationPoint},
ReleaseContinuationPoints: false,
}
resp, err := n.c.BrowseNextWithContext(ctx, req)
if err != nil {
return nil, err
}
results = resp.Results
refs = append(refs, results[0].References...)
}
return refs, nil
}
// TranslateBrowsePathsToNodeIDs translates an array of browseName segments to NodeIDs.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) TranslateBrowsePathsToNodeIDs(pathNames []*ua.QualifiedName) (*ua.NodeID, error) {
return n.TranslateBrowsePathsToNodeIDsWithContext(context.Background(), pathNames)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) TranslateBrowsePathsToNodeIDsWithContext(ctx context.Context, pathNames []*ua.QualifiedName) (*ua.NodeID, error) {
req := ua.TranslateBrowsePathsToNodeIDsRequest{
BrowsePaths: []*ua.BrowsePath{
{
StartingNode: n.ID,
RelativePath: &ua.RelativePath{
Elements: []*ua.RelativePathElement{},
},
},
}}
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: name,
},
)
}
var nodeID *ua.NodeID
err := n.c.SendWithContext(ctx, &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
}
// TranslateBrowsePathInNamespaceToNodeID translates a browseName to a NodeID within the same namespace.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (n *Node) TranslateBrowsePathInNamespaceToNodeID(ns uint16, browsePath string) (*ua.NodeID, error) {
return n.TranslateBrowsePathInNamespaceToNodeIDWithContext(context.Background(), ns, browsePath)
}
// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (n *Node) TranslateBrowsePathInNamespaceToNodeIDWithContext(ctx context.Context, ns uint16, browsePath string) (*ua.NodeID, error) {
segments := strings.Split(browsePath, ".")
var names []*ua.QualifiedName
for _, segment := range segments {
qn := &ua.QualifiedName{NamespaceIndex: ns, Name: segment}
names = append(names, qn)
}
return n.TranslateBrowsePathsToNodeIDsWithContext(ctx, names)
}