forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
143 lines (125 loc) · 3.92 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
// 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 opcua
import (
"time"
"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.
func (n *Node) NodeClass() (ua.NodeClass, error) {
v, err := n.Attribute(ua.AttributeIDNodeClass)
if err != nil {
return 0, err
}
return ua.NodeClass(v.Int()), nil
}
// BrowseName returns the browse name of the node.
func (n *Node) BrowseName() (*ua.QualifiedName, error) {
v, err := n.Attribute(ua.AttributeIDBrowseName)
if err != nil {
return nil, err
}
return v.Value.(*ua.QualifiedName), nil
}
// DisplayName returns the display name of the node.
func (n *Node) DisplayName() (*ua.LocalizedText, error) {
v, err := n.Attribute(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.
func (n *Node) AccessLevel() (ua.AccessLevelType, error) {
v, err := n.Attribute(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.
func (n *Node) HasAccessLevel(mask ua.AccessLevelType) (bool, error) {
v, err := n.AccessLevel()
if err != nil {
return false, err
}
return (v & mask) == mask, nil
}
// UserAccessLevel returns the access level of the node.
func (n *Node) UserAccessLevel() (ua.AccessLevelType, error) {
v, err := n.Attribute(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.
func (n *Node) HasUserAccessLevel(mask ua.AccessLevelType) (bool, error) {
v, err := n.UserAccessLevel()
if err != nil {
return false, err
}
return (v & mask) == mask, nil
}
// Value returns the value of the node.
func (n *Node) Value() (*ua.Variant, error) {
return n.Attribute(ua.AttributeIDValue)
}
// Attribute returns the attribute of the node. with the given id.
func (n *Node) Attribute(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.Read(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
}
// References retrns 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) {
desc := &ua.BrowseDescription{
NodeID: n.ID,
BrowseDirection: ua.BrowseDirectionBoth,
ReferenceTypeID: refs,
IncludeSubtypes: true,
NodeClassMask: uint32(ua.NodeClassAll),
ResultMask: uint32(ua.BrowseResultMaskAll),
}
req := &ua.BrowseRequest{
View: &ua.ViewDescription{
ViewID: ua.NewTwoByteNodeID(0),
Timestamp: time.Now(),
},
RequestedMaxReferencesPerNode: 1000,
NodesToBrowse: []*ua.BrowseDescription{desc},
}
return n.c.Browse(req)
// implement browse_next
}