forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
339 lines (300 loc) · 9.91 KB
/
subscription.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
package opcua
import (
"context"
"fmt"
"time"
"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/errors"
"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
"github.com/gopcua/opcua/uasc"
)
const (
DefaultSubscriptionMaxNotificationsPerPublish = 10000
DefaultSubscriptionLifetimeCount = 10000
DefaultSubscriptionMaxKeepAliveCount = 3000
DefaultSubscriptionInterval = 100 * time.Millisecond
DefaultSubscriptionPriority = 0
)
const terminatedSubscriptionID uint32 = 0xC0CAC01B
type Subscription struct {
SubscriptionID uint32
RevisedPublishingInterval time.Duration
RevisedLifetimeCount uint32
RevisedMaxKeepAliveCount uint32
Notifs chan *PublishNotificationData
params *SubscriptionParameters
items []*monitoredItem
lastSeq uint32
nextSeq uint32
c *Client
}
type SubscriptionParameters struct {
Interval time.Duration
LifetimeCount uint32
MaxKeepAliveCount uint32
MaxNotificationsPerPublish uint32
Priority uint8
}
type monitoredItem struct {
ItemToMonitor *ua.ReadValueID
MonitoringParameters *ua.MonitoringParameters
MonitoringMode ua.MonitoringMode
TimestampsToReturn ua.TimestampsToReturn
createResult *ua.MonitoredItemCreateResult
}
func NewMonitoredItemCreateRequestWithDefaults(nodeID *ua.NodeID, attributeID ua.AttributeID, clientHandle uint32) *ua.MonitoredItemCreateRequest {
if attributeID == 0 {
attributeID = ua.AttributeIDValue
}
return &ua.MonitoredItemCreateRequest{
ItemToMonitor: &ua.ReadValueID{
NodeID: nodeID,
AttributeID: attributeID,
DataEncoding: &ua.QualifiedName{},
},
MonitoringMode: ua.MonitoringModeReporting,
RequestedParameters: &ua.MonitoringParameters{
ClientHandle: clientHandle,
DiscardOldest: true,
Filter: nil,
QueueSize: 10,
SamplingInterval: 0.0,
},
}
}
type PublishNotificationData struct {
SubscriptionID uint32
Error error
Value interface{}
}
// Cancel stops the subscription and removes it
// from the client and the server.
func (s *Subscription) Cancel() error {
s.c.forgetSubscription(s.SubscriptionID)
return s.delete()
}
// delete removes the subscription from the server.
func (s *Subscription) delete() error {
req := &ua.DeleteSubscriptionsRequest{
SubscriptionIDs: []uint32{s.SubscriptionID},
}
var res *ua.DeleteSubscriptionsResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
switch {
case err == ua.StatusOK:
return nil
case err != nil:
return err
default:
return res.ResponseHeader.ServiceResult
}
}
func (s *Subscription) Monitor(ts ua.TimestampsToReturn, items ...*ua.MonitoredItemCreateRequest) (*ua.CreateMonitoredItemsResponse, error) {
// Part 4, 5.12.2.2 CreateMonitoredItems Service Parameters
req := &ua.CreateMonitoredItemsRequest{
SubscriptionID: s.SubscriptionID,
TimestampsToReturn: ts,
ItemsToCreate: items,
}
var res *ua.CreateMonitoredItemsResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
if err != nil {
return nil, err
}
for _, result := range res.Results {
if status := result.StatusCode; status != ua.StatusOK {
return nil, status
}
}
// store monitored items
for i, item := range items {
result := res.Results[i]
mi := &monitoredItem{
ItemToMonitor: item.ItemToMonitor,
MonitoringParameters: item.RequestedParameters,
MonitoringMode: item.MonitoringMode,
TimestampsToReturn: ts,
createResult: result,
}
s.items = append(s.items, mi)
}
return res, err
}
func (s *Subscription) Unmonitor(monitoredItemIDs ...uint32) (*ua.DeleteMonitoredItemsResponse, error) {
req := &ua.DeleteMonitoredItemsRequest{
MonitoredItemIDs: monitoredItemIDs,
SubscriptionID: s.SubscriptionID,
}
var res *ua.DeleteMonitoredItemsResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
return res, err
}
// SetTriggering sends a request to the server to add and/or remove triggering links from a triggering item.
// To add links from a triggering item to an item to report provide the server assigned ID(s) in the `add` argument.
// To remove links from a triggering item to an item to report provide the server assigned ID(s) in the `remove` argument.
func (s *Subscription) SetTriggering(triggeringItemID uint32, add, remove []uint32) (*ua.SetTriggeringResponse, error) {
// Part 4, 5.12.5.2 SetTriggering Service Parameters
req := &ua.SetTriggeringRequest{
SubscriptionID: s.SubscriptionID,
TriggeringItemID: triggeringItemID,
LinksToAdd: add,
LinksToRemove: remove,
}
var res *ua.SetTriggeringResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
return res, err
}
func (s *Subscription) publishTimeout() time.Duration {
timeout := time.Duration(s.RevisedMaxKeepAliveCount) * s.RevisedPublishingInterval // expected keepalive interval
if timeout > uasc.MaxTimeout {
return uasc.MaxTimeout
}
if timeout < s.c.cfg.RequestTimeout {
return s.c.cfg.RequestTimeout
}
return timeout
}
func (s *Subscription) notify(ctx context.Context, data *PublishNotificationData) {
select {
case <-ctx.Done():
return
case s.Notifs <- data:
}
}
// Stats returns a diagnostic struct with metadata about the current subscription
func (s *Subscription) Stats() (*ua.SubscriptionDiagnosticsDataType, error) {
// TODO(kung-foo): once browsing feature is merged, attempt to get direct access to the
// diagnostics node. for example, Prosys lists them like:
// i=2290/ns=1;g=918ee6f4-2d25-4506-980d-e659441c166d
// maybe cache the nodeid to speed up future stats queries
node := s.c.Node(ua.NewNumericNodeID(0, id.Server_ServerDiagnostics_SubscriptionDiagnosticsArray))
v, err := node.Value()
if err != nil {
return nil, err
}
for _, eo := range v.Value().([]*ua.ExtensionObject) {
stat := eo.Value.(*ua.SubscriptionDiagnosticsDataType)
if stat.SubscriptionID == s.SubscriptionID {
return stat, nil
}
}
return nil, errors.Errorf("unable to find SubscriptionDiagnostics for sub=%d", s.SubscriptionID)
}
func (p *SubscriptionParameters) setDefaults() {
if p.MaxNotificationsPerPublish == 0 {
p.MaxNotificationsPerPublish = DefaultSubscriptionMaxNotificationsPerPublish
}
if p.LifetimeCount == 0 {
p.LifetimeCount = DefaultSubscriptionLifetimeCount
}
if p.MaxKeepAliveCount == 0 {
p.MaxKeepAliveCount = DefaultSubscriptionMaxKeepAliveCount
}
if p.Interval == 0 {
p.Interval = DefaultSubscriptionInterval
}
if p.Priority == 0 {
// DefaultSubscriptionPriority is 0 at the time of writing, so this redundant assignment is
// made only to allow for a one-liner change of default priority should a need arise
// and to explicitly expose the default priority as a constant
p.Priority = DefaultSubscriptionPriority
}
}
// recreate creates a new subscription based on the previous subscription
// parameters and monitored items.
func (s *Subscription) recreate() error {
dlog := debug.NewPrefixLogger("sub %d: recreate: ", s.SubscriptionID)
if s.SubscriptionID == terminatedSubscriptionID {
dlog.Printf("subscription is not in a valid state")
return nil
}
params := s.params
{
req := &ua.DeleteSubscriptionsRequest{
SubscriptionIDs: []uint32{s.SubscriptionID},
}
var res *ua.DeleteSubscriptionsResponse
_ = s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
dlog.Print("subscription deleted")
}
s.c.forgetSubscription(s.SubscriptionID)
dlog.Printf("subscription forgotton")
req := &ua.CreateSubscriptionRequest{
RequestedPublishingInterval: float64(params.Interval / time.Millisecond),
RequestedLifetimeCount: params.LifetimeCount,
RequestedMaxKeepAliveCount: params.MaxKeepAliveCount,
PublishingEnabled: true,
MaxNotificationsPerPublish: params.MaxNotificationsPerPublish,
Priority: params.Priority,
}
var res *ua.CreateSubscriptionResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
if err != nil {
dlog.Printf("failed to recreate subscription")
return err
}
// todo (unknownet): check if necessary
if status := res.ResponseHeader.ServiceResult; status != ua.StatusOK {
return status
}
dlog.Printf("recreated as subscription %d", res.SubscriptionID)
dlog.SetPrefix(fmt.Sprintf("sub %d: recreate: ", res.SubscriptionID))
s.SubscriptionID = res.SubscriptionID
s.RevisedPublishingInterval = time.Duration(res.RevisedPublishingInterval) * time.Millisecond
s.RevisedLifetimeCount = res.RevisedLifetimeCount
s.RevisedMaxKeepAliveCount = res.RevisedMaxKeepAliveCount
s.lastSeq = 0
s.nextSeq = 1
if err := s.c.registerSubscription(s); err != nil {
return err
}
dlog.Printf("subscription registered")
// Sort by timestamp to return
itemsByTs := make(map[ua.TimestampsToReturn][]*ua.MonitoredItemCreateRequest)
for _, m := range s.items {
cr := &ua.MonitoredItemCreateRequest{
ItemToMonitor: m.ItemToMonitor,
MonitoringMode: m.MonitoringMode,
RequestedParameters: m.MonitoringParameters,
}
itemsByTs[m.TimestampsToReturn] = append(itemsByTs[m.TimestampsToReturn], cr)
}
for ts, items := range itemsByTs {
req := &ua.CreateMonitoredItemsRequest{
SubscriptionID: s.SubscriptionID,
TimestampsToReturn: ts,
ItemsToCreate: items,
}
var res *ua.CreateMonitoredItemsResponse
err := s.c.Send(req, func(v interface{}) error {
return safeAssign(v, &res)
})
if err != nil {
dlog.Printf("failed to create monitored items: %v", err)
return err
}
for _, result := range res.Results {
if status := result.StatusCode; status != ua.StatusOK {
return status
}
}
for i, m := range s.items {
m.createResult = res.Results[i]
}
}
dlog.Printf("subscription successfully recreated")
return nil
}