node-opcua-server 2.71.0 → 2.72.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base_server.d.ts +110 -110
- package/dist/base_server.js +473 -473
- package/dist/factory.d.ts +12 -12
- package/dist/factory.js +23 -23
- package/dist/history_server_capabilities.d.ts +35 -35
- package/dist/history_server_capabilities.js +43 -43
- package/dist/i_channel_data.d.ts +13 -13
- package/dist/i_channel_data.js +2 -2
- package/dist/i_register_server_manager.d.ts +16 -16
- package/dist/i_register_server_manager.js +2 -2
- package/dist/i_server_side_publish_engine.d.ts +36 -36
- package/dist/i_server_side_publish_engine.js +49 -49
- package/dist/i_socket_data.d.ts +11 -11
- package/dist/i_socket_data.js +2 -2
- package/dist/index.d.ts +15 -15
- package/dist/index.js +31 -31
- package/dist/monitored_item.d.ts +177 -177
- package/dist/monitored_item.js +998 -998
- package/dist/node_sampler.d.ts +3 -3
- package/dist/node_sampler.js +75 -75
- package/dist/opcua_server.d.ts +650 -647
- package/dist/opcua_server.js +2392 -2385
- package/dist/opcua_server.js.map +1 -1
- package/dist/queue.d.ts +11 -11
- package/dist/queue.js +71 -71
- package/dist/register_server_manager.d.ts +96 -96
- package/dist/register_server_manager.js +584 -584
- package/dist/register_server_manager_hidden.d.ts +17 -17
- package/dist/register_server_manager_hidden.js +27 -27
- package/dist/register_server_manager_mdns_only.d.ts +22 -22
- package/dist/register_server_manager_mdns_only.js +55 -55
- package/dist/server_capabilities.d.ts +148 -61
- package/dist/server_capabilities.js +91 -108
- package/dist/server_capabilities.js.map +1 -1
- package/dist/server_end_point.d.ts +183 -183
- package/dist/server_end_point.js +817 -816
- package/dist/server_end_point.js.map +1 -1
- package/dist/server_engine.d.ts +317 -316
- package/dist/server_engine.js +1716 -1688
- package/dist/server_engine.js.map +1 -1
- package/dist/server_publish_engine.d.ts +112 -112
- package/dist/server_publish_engine.js +530 -530
- package/dist/server_publish_engine_for_orphan_subscriptions.d.ts +16 -16
- package/dist/server_publish_engine_for_orphan_subscriptions.js +49 -49
- package/dist/server_session.d.ts +181 -181
- package/dist/server_session.js +738 -738
- package/dist/server_session.js.map +1 -1
- package/dist/server_subscription.d.ts +415 -395
- package/dist/server_subscription.js +1334 -1316
- package/dist/server_subscription.js.map +1 -1
- package/dist/sessions_compatible_for_transfer.d.ts +2 -2
- package/dist/sessions_compatible_for_transfer.js +36 -36
- package/dist/user_manager.d.ts +32 -32
- package/dist/user_manager.js +74 -74
- package/dist/user_manager_ua.d.ts +3 -3
- package/dist/user_manager_ua.js +39 -39
- package/dist/validate_filter.d.ts +5 -5
- package/dist/validate_filter.js +60 -60
- package/package.json +34 -34
- package/source/opcua_server.ts +27 -16
- package/source/server_capabilities.ts +178 -91
- package/source/server_end_point.ts +9 -8
- package/source/server_engine.ts +30 -1
- package/source/server_session.ts +1 -0
- package/source/server_subscription.ts +46 -5
|
@@ -1,1317 +1,1335 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @module node-opcua-server
|
|
4
|
-
*/
|
|
5
|
-
// tslint:disable:no-console
|
|
6
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.Subscription = exports.SubscriptionState = void 0;
|
|
17
|
-
const events_1 = require("events");
|
|
18
|
-
const chalk = require("chalk");
|
|
19
|
-
const node_opcua_address_space_1 = require("node-opcua-address-space");
|
|
20
|
-
const node_opcua_address_space_2 = require("node-opcua-address-space");
|
|
21
|
-
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
22
|
-
const node_opcua_common_1 = require("node-opcua-common");
|
|
23
|
-
const node_opcua_data_model_1 = require("node-opcua-data-model");
|
|
24
|
-
const node_opcua_debug_1 = require("node-opcua-debug");
|
|
25
|
-
const node_opcua_nodeid_1 = require("node-opcua-nodeid");
|
|
26
|
-
const node_opcua_object_registry_1 = require("node-opcua-object-registry");
|
|
27
|
-
const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
|
|
28
|
-
const node_opcua_service_filter_1 = require("node-opcua-service-filter");
|
|
29
|
-
const node_opcua_service_subscription_1 = require("node-opcua-service-subscription");
|
|
30
|
-
const node_opcua_service_subscription_2 = require("node-opcua-service-subscription");
|
|
31
|
-
const node_opcua_status_code_1 = require("node-opcua-status-code");
|
|
32
|
-
const node_opcua_types_1 = require("node-opcua-types");
|
|
33
|
-
const queue_1 = require("./queue");
|
|
34
|
-
const monitored_item_1 = require("./monitored_item");
|
|
35
|
-
const validate_filter_1 = require("./validate_filter");
|
|
36
|
-
const i_server_side_publish_engine_1 = require("./i_server_side_publish_engine");
|
|
37
|
-
const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
|
|
38
|
-
const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
|
|
39
|
-
const warningLog = (0, node_opcua_debug_1.make_warningLog)(__filename);
|
|
40
|
-
const maxNotificationMessagesInQueue = 100;
|
|
41
|
-
var SubscriptionState;
|
|
42
|
-
(function (SubscriptionState) {
|
|
43
|
-
SubscriptionState[SubscriptionState["CLOSED"] = 1] = "CLOSED";
|
|
44
|
-
SubscriptionState[SubscriptionState["CREATING"] = 2] = "CREATING";
|
|
45
|
-
SubscriptionState[SubscriptionState["NORMAL"] = 3] = "NORMAL";
|
|
46
|
-
// The keep-alive counter is not used in this state.
|
|
47
|
-
SubscriptionState[SubscriptionState["LATE"] = 4] = "LATE";
|
|
48
|
-
// ready to be sent, but there are no Publish requests queued. When in this state, the next Publish
|
|
49
|
-
// request is processed when it is received. The keep-alive counter is not used in this state.
|
|
50
|
-
SubscriptionState[SubscriptionState["KEEPALIVE"] = 5] = "KEEPALIVE";
|
|
51
|
-
// alive counter to count down to 0 from its maximum.
|
|
52
|
-
SubscriptionState[SubscriptionState["TERMINATED"] = 6] = "TERMINATED";
|
|
53
|
-
})(SubscriptionState = exports.SubscriptionState || (exports.SubscriptionState = {}));
|
|
54
|
-
function _adjust_publishing_interval(publishingInterval) {
|
|
55
|
-
publishingInterval =
|
|
56
|
-
publishingInterval === undefined || Number.isNaN(publishingInterval)
|
|
57
|
-
? Subscription.defaultPublishingInterval
|
|
58
|
-
: publishingInterval;
|
|
59
|
-
publishingInterval = Math.max(publishingInterval, Subscription.minimumPublishingInterval);
|
|
60
|
-
publishingInterval = Math.min(publishingInterval, Subscription.maximumPublishingInterval);
|
|
61
|
-
return publishingInterval;
|
|
62
|
-
}
|
|
63
|
-
const minimumMaxKeepAliveCount = 2;
|
|
64
|
-
const maximumMaxKeepAliveCount = 12000;
|
|
65
|
-
function _adjust_maxKeepAliveCount(maxKeepAliveCount /*,publishingInterval*/) {
|
|
66
|
-
maxKeepAliveCount = maxKeepAliveCount || minimumMaxKeepAliveCount;
|
|
67
|
-
maxKeepAliveCount = Math.max(maxKeepAliveCount, minimumMaxKeepAliveCount);
|
|
68
|
-
maxKeepAliveCount = Math.min(maxKeepAliveCount, maximumMaxKeepAliveCount);
|
|
69
|
-
return maxKeepAliveCount;
|
|
70
|
-
}
|
|
71
|
-
function _adjust_lifeTimeCount(lifeTimeCount, maxKeepAliveCount, publishingInterval) {
|
|
72
|
-
lifeTimeCount = lifeTimeCount || 1;
|
|
73
|
-
// let's make sure that lifeTimeCount is at least three time maxKeepAliveCount
|
|
74
|
-
// Note : the specs say ( part 3 - CreateSubscriptionParameter )
|
|
75
|
-
// "The lifetime count shall be a minimum of three times the keep keep-alive count."
|
|
76
|
-
lifeTimeCount = Math.max(lifeTimeCount, maxKeepAliveCount * 3);
|
|
77
|
-
const minTicks = Math.ceil((5 * 1000) / publishingInterval); // we want 5 seconds min
|
|
78
|
-
lifeTimeCount = Math.max(minTicks, lifeTimeCount);
|
|
79
|
-
return lifeTimeCount;
|
|
80
|
-
}
|
|
81
|
-
function _adjust_publishingEnable(publishingEnabled) {
|
|
82
|
-
return publishingEnabled === null || publishingEnabled === undefined ? true : !!publishingEnabled;
|
|
83
|
-
}
|
|
84
|
-
function _adjust_maxNotificationsPerPublish(maxNotificationsPerPublish) {
|
|
85
|
-
(0, node_opcua_assert_1.assert)(Subscription.maxNotificationPerPublishHighLimit > 0, "Subscription.maxNotificationPerPublishHighLimit must be positive");
|
|
86
|
-
maxNotificationsPerPublish = maxNotificationsPerPublish || 0;
|
|
87
|
-
(0, node_opcua_assert_1.assert)(typeof maxNotificationsPerPublish === "number");
|
|
88
|
-
// must be strictly positive
|
|
89
|
-
maxNotificationsPerPublish = maxNotificationsPerPublish >= 0 ? maxNotificationsPerPublish : 0;
|
|
90
|
-
if (maxNotificationsPerPublish === 0) {
|
|
91
|
-
// if zero then => use our HighLimit
|
|
92
|
-
maxNotificationsPerPublish = Subscription.maxNotificationPerPublishHighLimit;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
// if not zero then should be capped by maxNotificationPerPublishHighLimit
|
|
96
|
-
maxNotificationsPerPublish = Math.min(Subscription.maxNotificationPerPublishHighLimit, maxNotificationsPerPublish);
|
|
97
|
-
}
|
|
98
|
-
(0, node_opcua_assert_1.assert)(maxNotificationsPerPublish !== 0 && maxNotificationsPerPublish <= Subscription.maxNotificationPerPublishHighLimit);
|
|
99
|
-
return maxNotificationsPerPublish;
|
|
100
|
-
}
|
|
101
|
-
function w(s, length) {
|
|
102
|
-
return ("000" + s).padStart(length);
|
|
103
|
-
}
|
|
104
|
-
function t(d) {
|
|
105
|
-
return w(d.getHours(), 2) + ":" + w(d.getMinutes(), 2) + ":" + w(d.getSeconds(), 2) + ":" + w(d.getMilliseconds(), 3);
|
|
106
|
-
}
|
|
107
|
-
function _getSequenceNumbers(arr) {
|
|
108
|
-
return arr.map((notificationMessage) => notificationMessage.sequenceNumber);
|
|
109
|
-
}
|
|
110
|
-
function analyseEventFilterResult(node, eventFilter) {
|
|
111
|
-
/* istanbul ignore next */
|
|
112
|
-
if (!(eventFilter instanceof node_opcua_service_filter_1.EventFilter)) {
|
|
113
|
-
throw new Error("Internal Error");
|
|
114
|
-
}
|
|
115
|
-
const selectClauseResults = (0, node_opcua_address_space_1.checkSelectClauses)(node, eventFilter.selectClauses || []);
|
|
116
|
-
const whereClauseResult = new node_opcua_types_1.ContentFilterResult();
|
|
117
|
-
return new node_opcua_types_1.EventFilterResult({
|
|
118
|
-
selectClauseDiagnosticInfos: [],
|
|
119
|
-
selectClauseResults,
|
|
120
|
-
whereClauseResult
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
function analyseDataChangeFilterResult(node, dataChangeFilter) {
|
|
124
|
-
(0, node_opcua_assert_1.assert)(dataChangeFilter instanceof node_opcua_service_subscription_2.DataChangeFilter);
|
|
125
|
-
// the opcua specification doesn't provide dataChangeFilterResult
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
function analyseAggregateFilterResult(node, aggregateFilter) {
|
|
129
|
-
(0, node_opcua_assert_1.assert)(aggregateFilter instanceof node_opcua_service_subscription_1.AggregateFilter);
|
|
130
|
-
return new node_opcua_types_1.AggregateFilterResult({});
|
|
131
|
-
}
|
|
132
|
-
function _process_filter(node, filter) {
|
|
133
|
-
if (!filter) {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
if (filter instanceof node_opcua_service_filter_1.EventFilter) {
|
|
137
|
-
return analyseEventFilterResult(node, filter);
|
|
138
|
-
}
|
|
139
|
-
else if (filter instanceof node_opcua_service_subscription_2.DataChangeFilter) {
|
|
140
|
-
return analyseDataChangeFilterResult(node, filter);
|
|
141
|
-
}
|
|
142
|
-
else if (filter instanceof node_opcua_service_subscription_1.AggregateFilter) {
|
|
143
|
-
return analyseAggregateFilterResult(node, filter);
|
|
144
|
-
}
|
|
145
|
-
// istanbul ignore next
|
|
146
|
-
throw new Error("invalid filter");
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* @private
|
|
150
|
-
*/
|
|
151
|
-
function createSubscriptionDiagnostics(subscription) {
|
|
152
|
-
(0, node_opcua_assert_1.assert)(subscription instanceof Subscription);
|
|
153
|
-
const subscriptionDiagnostics = new node_opcua_common_1.SubscriptionDiagnosticsDataType({});
|
|
154
|
-
const subscription_subscriptionDiagnostics = subscriptionDiagnostics;
|
|
155
|
-
subscription_subscriptionDiagnostics.$subscription = subscription;
|
|
156
|
-
// "sessionId"
|
|
157
|
-
subscription_subscriptionDiagnostics.__defineGetter__("sessionId", function () {
|
|
158
|
-
if (!this.$subscription) {
|
|
159
|
-
return node_opcua_nodeid_1.NodeId.nullNodeId;
|
|
160
|
-
}
|
|
161
|
-
return this.$subscription.getSessionId();
|
|
162
|
-
});
|
|
163
|
-
subscription_subscriptionDiagnostics.__defineGetter__("subscriptionId", function () {
|
|
164
|
-
if (!this.$subscription) {
|
|
165
|
-
return 0;
|
|
166
|
-
}
|
|
167
|
-
return this.$subscription.id;
|
|
168
|
-
});
|
|
169
|
-
subscription_subscriptionDiagnostics.__defineGetter__("priority", function () {
|
|
170
|
-
if (!this.$subscription) {
|
|
171
|
-
return 0;
|
|
172
|
-
}
|
|
173
|
-
return this.$subscription.priority;
|
|
174
|
-
});
|
|
175
|
-
subscription_subscriptionDiagnostics.__defineGetter__("publishingInterval", function () {
|
|
176
|
-
if (!this.$subscription) {
|
|
177
|
-
return 0;
|
|
178
|
-
}
|
|
179
|
-
return this.$subscription.publishingInterval;
|
|
180
|
-
});
|
|
181
|
-
subscription_subscriptionDiagnostics.__defineGetter__("maxLifetimeCount", function () {
|
|
182
|
-
return this.$subscription.lifeTimeCount;
|
|
183
|
-
});
|
|
184
|
-
subscription_subscriptionDiagnostics.__defineGetter__("maxKeepAliveCount", function () {
|
|
185
|
-
if (!this.$subscription) {
|
|
186
|
-
return 0;
|
|
187
|
-
}
|
|
188
|
-
return this.$subscription.maxKeepAliveCount;
|
|
189
|
-
});
|
|
190
|
-
subscription_subscriptionDiagnostics.__defineGetter__("maxNotificationsPerPublish", function () {
|
|
191
|
-
if (!this.$subscription) {
|
|
192
|
-
return 0;
|
|
193
|
-
}
|
|
194
|
-
return this.$subscription.maxNotificationsPerPublish;
|
|
195
|
-
});
|
|
196
|
-
subscription_subscriptionDiagnostics.__defineGetter__("publishingEnabled", function () {
|
|
197
|
-
if (!this.$subscription) {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
return this.$subscription.publishingEnabled;
|
|
201
|
-
});
|
|
202
|
-
subscription_subscriptionDiagnostics.__defineGetter__("monitoredItemCount", function () {
|
|
203
|
-
if (!this.$subscription) {
|
|
204
|
-
return 0;
|
|
205
|
-
}
|
|
206
|
-
return this.$subscription.monitoredItemCount;
|
|
207
|
-
});
|
|
208
|
-
subscription_subscriptionDiagnostics.__defineGetter__("nextSequenceNumber", function () {
|
|
209
|
-
if (!this.$subscription) {
|
|
210
|
-
return 0;
|
|
211
|
-
}
|
|
212
|
-
return this.$subscription._get_future_sequence_number();
|
|
213
|
-
});
|
|
214
|
-
subscription_subscriptionDiagnostics.__defineGetter__("disabledMonitoredItemCount", function () {
|
|
215
|
-
if (!this.$subscription) {
|
|
216
|
-
return 0;
|
|
217
|
-
}
|
|
218
|
-
return this.$subscription.disabledMonitoredItemCount;
|
|
219
|
-
});
|
|
220
|
-
/* those member of self.subscriptionDiagnostics are handled directly
|
|
221
|
-
|
|
222
|
-
modifyCount
|
|
223
|
-
enableCount,
|
|
224
|
-
disableCount,
|
|
225
|
-
republishRequestCount,
|
|
226
|
-
notificationsCount,
|
|
227
|
-
publishRequestCount,
|
|
228
|
-
dataChangeNotificationsCount,
|
|
229
|
-
eventNotificationsCount,
|
|
230
|
-
*/
|
|
231
|
-
/*
|
|
232
|
-
those members are not updated yet in the code :
|
|
233
|
-
"republishMessageRequestCount",
|
|
234
|
-
"republishMessageCount",
|
|
235
|
-
"transferRequestCount",
|
|
236
|
-
"transferredToAltClientCount",
|
|
237
|
-
"transferredToSameClientCount",
|
|
238
|
-
"latePublishRequestCount",
|
|
239
|
-
"currentKeepAliveCount",
|
|
240
|
-
"currentLifetimeCount",
|
|
241
|
-
"unacknowledgedMessageCount",
|
|
242
|
-
"discardedMessageCount",
|
|
243
|
-
"monitoringQueueOverflowCount",
|
|
244
|
-
"eventQueueOverFlowCount"
|
|
245
|
-
*/
|
|
246
|
-
// add object in Variable SubscriptionDiagnosticArray (i=2290) ( Array of SubscriptionDiagnostics)
|
|
247
|
-
// add properties in Variable to reflect
|
|
248
|
-
return subscriptionDiagnostics;
|
|
249
|
-
}
|
|
250
|
-
let g_monitoredItemId = Math.ceil(Math.random() * 100000);
|
|
251
|
-
function getNextMonitoredItemId() {
|
|
252
|
-
return g_monitoredItemId++;
|
|
253
|
-
}
|
|
254
|
-
// function myFilter<T>(t1: any, chunk: any[]): T[] {
|
|
255
|
-
// return chunk.filter(filter_instanceof.bind(null, t1));
|
|
256
|
-
// }
|
|
257
|
-
// function makeNotificationData(notifications_chunk: QueueItem): NotificationData {
|
|
258
|
-
// const dataChangedNotificationData = myFilter<MonitoredItemNotification>(MonitoredItemNotification, notifications_chunk);
|
|
259
|
-
// const eventNotificationListData = myFilter<EventFieldList>(EventFieldList, notifications_chunk);
|
|
260
|
-
// assert(notifications_chunk.length === dataChangedNotificationData.length + eventNotificationListData.length);
|
|
261
|
-
// const notifications: (DataChangeNotification | EventNotificationList)[] = [];
|
|
262
|
-
// // add dataChangeNotification
|
|
263
|
-
// if (dataChangedNotificationData.length) {
|
|
264
|
-
// const dataChangeNotification = new DataChangeNotification({
|
|
265
|
-
// diagnosticInfos: [],
|
|
266
|
-
// monitoredItems: dataChangedNotificationData
|
|
267
|
-
// });
|
|
268
|
-
// notifications.push(dataChangeNotification);
|
|
269
|
-
// }
|
|
270
|
-
// // add dataChangeNotification
|
|
271
|
-
// if (eventNotificationListData.length) {
|
|
272
|
-
// const eventNotificationList = new EventNotificationList({
|
|
273
|
-
// events: eventNotificationListData
|
|
274
|
-
// });
|
|
275
|
-
// notifications.push(eventNotificationList);
|
|
276
|
-
// }
|
|
277
|
-
// return notifications.length === 0 ? null : notifications;
|
|
278
|
-
// }
|
|
279
|
-
const INVALID_ID = -1;
|
|
280
|
-
/**
|
|
281
|
-
* The Subscription class used in the OPCUA server side.
|
|
282
|
-
*/
|
|
283
|
-
class Subscription extends events_1.EventEmitter {
|
|
284
|
-
constructor(options) {
|
|
285
|
-
super();
|
|
286
|
-
this._keep_alive_counter = 0;
|
|
287
|
-
this._hasUncollectedMonitoredItemNotifications = false;
|
|
288
|
-
options = options || {};
|
|
289
|
-
Subscription.registry.register(this);
|
|
290
|
-
this.sessionId = options.sessionId || node_opcua_nodeid_1.NodeId.nullNodeId;
|
|
291
|
-
(0, node_opcua_assert_1.assert)(this.sessionId instanceof node_opcua_nodeid_1.NodeId, "expecting a sessionId NodeId");
|
|
292
|
-
this.publishEngine = options.publishEngine;
|
|
293
|
-
this.id = options.id || INVALID_ID;
|
|
294
|
-
this.priority = options.priority || 0;
|
|
295
|
-
this.publishingInterval = _adjust_publishing_interval(options.publishingInterval);
|
|
296
|
-
this.maxKeepAliveCount = _adjust_maxKeepAliveCount(options.maxKeepAliveCount); // , this.publishingInterval);
|
|
297
|
-
this.resetKeepAliveCounter();
|
|
298
|
-
this.lifeTimeCount = _adjust_lifeTimeCount(options.lifeTimeCount || 0, this.maxKeepAliveCount, this.publishingInterval);
|
|
299
|
-
this.maxNotificationsPerPublish = _adjust_maxNotificationsPerPublish(options.maxNotificationsPerPublish);
|
|
300
|
-
this._life_time_counter = 0;
|
|
301
|
-
this.resetLifeTimeCounter();
|
|
302
|
-
// notification message that are ready to be sent to the client
|
|
303
|
-
this._pending_notifications = new queue_1.Queue();
|
|
304
|
-
this._sent_notification_messages = [];
|
|
305
|
-
this._sequence_number_generator = new node_opcua_secure_channel_1.SequenceNumberGenerator();
|
|
306
|
-
// initial state of the subscription
|
|
307
|
-
this.state = SubscriptionState.CREATING;
|
|
308
|
-
this.publishIntervalCount = 0;
|
|
309
|
-
this.monitoredItems = {}; // monitored item map
|
|
310
|
-
this.monitoredItemIdCounter = 0;
|
|
311
|
-
this.publishingEnabled = _adjust_publishingEnable(options.publishingEnabled);
|
|
312
|
-
this.subscriptionDiagnostics = createSubscriptionDiagnostics(this);
|
|
313
|
-
// A boolean value that is set to TRUE to mean that either a NotificationMessage or a keep-alive
|
|
314
|
-
// Message has been sent on the Subscription. It is a flag that is used to ensure that either a
|
|
315
|
-
// NotificationMessage or a keep-alive Message is sent out the first time the publishing
|
|
316
|
-
// timer expires.
|
|
317
|
-
this.messageSent = false;
|
|
318
|
-
this.timerId = null;
|
|
319
|
-
this._start_timer();
|
|
320
|
-
debugLog(chalk.green(`creating subscription ${this.id}`));
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
return
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
this.
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
this.
|
|
357
|
-
this.
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
this.
|
|
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
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
*
|
|
419
|
-
*/
|
|
420
|
-
|
|
421
|
-
(
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
this.
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
// sampling
|
|
571
|
-
|
|
572
|
-
//
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
if (itemToMonitor.
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
return handle_error(node_opcua_status_code_1.StatusCodes.
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
(0,
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
this.
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
return
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
return
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
//
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
this.
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
response
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
debugLog("
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
this.
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
if (this.
|
|
978
|
-
debugLog(
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
//
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
//
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
this.
|
|
1253
|
-
|
|
1254
|
-
const
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module node-opcua-server
|
|
4
|
+
*/
|
|
5
|
+
// tslint:disable:no-console
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.Subscription = exports.SubscriptionState = void 0;
|
|
17
|
+
const events_1 = require("events");
|
|
18
|
+
const chalk = require("chalk");
|
|
19
|
+
const node_opcua_address_space_1 = require("node-opcua-address-space");
|
|
20
|
+
const node_opcua_address_space_2 = require("node-opcua-address-space");
|
|
21
|
+
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
22
|
+
const node_opcua_common_1 = require("node-opcua-common");
|
|
23
|
+
const node_opcua_data_model_1 = require("node-opcua-data-model");
|
|
24
|
+
const node_opcua_debug_1 = require("node-opcua-debug");
|
|
25
|
+
const node_opcua_nodeid_1 = require("node-opcua-nodeid");
|
|
26
|
+
const node_opcua_object_registry_1 = require("node-opcua-object-registry");
|
|
27
|
+
const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
|
|
28
|
+
const node_opcua_service_filter_1 = require("node-opcua-service-filter");
|
|
29
|
+
const node_opcua_service_subscription_1 = require("node-opcua-service-subscription");
|
|
30
|
+
const node_opcua_service_subscription_2 = require("node-opcua-service-subscription");
|
|
31
|
+
const node_opcua_status_code_1 = require("node-opcua-status-code");
|
|
32
|
+
const node_opcua_types_1 = require("node-opcua-types");
|
|
33
|
+
const queue_1 = require("./queue");
|
|
34
|
+
const monitored_item_1 = require("./monitored_item");
|
|
35
|
+
const validate_filter_1 = require("./validate_filter");
|
|
36
|
+
const i_server_side_publish_engine_1 = require("./i_server_side_publish_engine");
|
|
37
|
+
const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
|
|
38
|
+
const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
|
|
39
|
+
const warningLog = (0, node_opcua_debug_1.make_warningLog)(__filename);
|
|
40
|
+
const maxNotificationMessagesInQueue = 100;
|
|
41
|
+
var SubscriptionState;
|
|
42
|
+
(function (SubscriptionState) {
|
|
43
|
+
SubscriptionState[SubscriptionState["CLOSED"] = 1] = "CLOSED";
|
|
44
|
+
SubscriptionState[SubscriptionState["CREATING"] = 2] = "CREATING";
|
|
45
|
+
SubscriptionState[SubscriptionState["NORMAL"] = 3] = "NORMAL";
|
|
46
|
+
// The keep-alive counter is not used in this state.
|
|
47
|
+
SubscriptionState[SubscriptionState["LATE"] = 4] = "LATE";
|
|
48
|
+
// ready to be sent, but there are no Publish requests queued. When in this state, the next Publish
|
|
49
|
+
// request is processed when it is received. The keep-alive counter is not used in this state.
|
|
50
|
+
SubscriptionState[SubscriptionState["KEEPALIVE"] = 5] = "KEEPALIVE";
|
|
51
|
+
// alive counter to count down to 0 from its maximum.
|
|
52
|
+
SubscriptionState[SubscriptionState["TERMINATED"] = 6] = "TERMINATED";
|
|
53
|
+
})(SubscriptionState = exports.SubscriptionState || (exports.SubscriptionState = {}));
|
|
54
|
+
function _adjust_publishing_interval(publishingInterval) {
|
|
55
|
+
publishingInterval =
|
|
56
|
+
publishingInterval === undefined || Number.isNaN(publishingInterval)
|
|
57
|
+
? Subscription.defaultPublishingInterval
|
|
58
|
+
: publishingInterval;
|
|
59
|
+
publishingInterval = Math.max(publishingInterval, Subscription.minimumPublishingInterval);
|
|
60
|
+
publishingInterval = Math.min(publishingInterval, Subscription.maximumPublishingInterval);
|
|
61
|
+
return publishingInterval;
|
|
62
|
+
}
|
|
63
|
+
const minimumMaxKeepAliveCount = 2;
|
|
64
|
+
const maximumMaxKeepAliveCount = 12000;
|
|
65
|
+
function _adjust_maxKeepAliveCount(maxKeepAliveCount /*,publishingInterval*/) {
|
|
66
|
+
maxKeepAliveCount = maxKeepAliveCount || minimumMaxKeepAliveCount;
|
|
67
|
+
maxKeepAliveCount = Math.max(maxKeepAliveCount, minimumMaxKeepAliveCount);
|
|
68
|
+
maxKeepAliveCount = Math.min(maxKeepAliveCount, maximumMaxKeepAliveCount);
|
|
69
|
+
return maxKeepAliveCount;
|
|
70
|
+
}
|
|
71
|
+
function _adjust_lifeTimeCount(lifeTimeCount, maxKeepAliveCount, publishingInterval) {
|
|
72
|
+
lifeTimeCount = lifeTimeCount || 1;
|
|
73
|
+
// let's make sure that lifeTimeCount is at least three time maxKeepAliveCount
|
|
74
|
+
// Note : the specs say ( part 3 - CreateSubscriptionParameter )
|
|
75
|
+
// "The lifetime count shall be a minimum of three times the keep keep-alive count."
|
|
76
|
+
lifeTimeCount = Math.max(lifeTimeCount, maxKeepAliveCount * 3);
|
|
77
|
+
const minTicks = Math.ceil((5 * 1000) / publishingInterval); // we want 5 seconds min
|
|
78
|
+
lifeTimeCount = Math.max(minTicks, lifeTimeCount);
|
|
79
|
+
return lifeTimeCount;
|
|
80
|
+
}
|
|
81
|
+
function _adjust_publishingEnable(publishingEnabled) {
|
|
82
|
+
return publishingEnabled === null || publishingEnabled === undefined ? true : !!publishingEnabled;
|
|
83
|
+
}
|
|
84
|
+
function _adjust_maxNotificationsPerPublish(maxNotificationsPerPublish) {
|
|
85
|
+
(0, node_opcua_assert_1.assert)(Subscription.maxNotificationPerPublishHighLimit > 0, "Subscription.maxNotificationPerPublishHighLimit must be positive");
|
|
86
|
+
maxNotificationsPerPublish = maxNotificationsPerPublish || 0;
|
|
87
|
+
(0, node_opcua_assert_1.assert)(typeof maxNotificationsPerPublish === "number");
|
|
88
|
+
// must be strictly positive
|
|
89
|
+
maxNotificationsPerPublish = maxNotificationsPerPublish >= 0 ? maxNotificationsPerPublish : 0;
|
|
90
|
+
if (maxNotificationsPerPublish === 0) {
|
|
91
|
+
// if zero then => use our HighLimit
|
|
92
|
+
maxNotificationsPerPublish = Subscription.maxNotificationPerPublishHighLimit;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// if not zero then should be capped by maxNotificationPerPublishHighLimit
|
|
96
|
+
maxNotificationsPerPublish = Math.min(Subscription.maxNotificationPerPublishHighLimit, maxNotificationsPerPublish);
|
|
97
|
+
}
|
|
98
|
+
(0, node_opcua_assert_1.assert)(maxNotificationsPerPublish !== 0 && maxNotificationsPerPublish <= Subscription.maxNotificationPerPublishHighLimit);
|
|
99
|
+
return maxNotificationsPerPublish;
|
|
100
|
+
}
|
|
101
|
+
function w(s, length) {
|
|
102
|
+
return ("000" + s).padStart(length);
|
|
103
|
+
}
|
|
104
|
+
function t(d) {
|
|
105
|
+
return w(d.getHours(), 2) + ":" + w(d.getMinutes(), 2) + ":" + w(d.getSeconds(), 2) + ":" + w(d.getMilliseconds(), 3);
|
|
106
|
+
}
|
|
107
|
+
function _getSequenceNumbers(arr) {
|
|
108
|
+
return arr.map((notificationMessage) => notificationMessage.sequenceNumber);
|
|
109
|
+
}
|
|
110
|
+
function analyseEventFilterResult(node, eventFilter) {
|
|
111
|
+
/* istanbul ignore next */
|
|
112
|
+
if (!(eventFilter instanceof node_opcua_service_filter_1.EventFilter)) {
|
|
113
|
+
throw new Error("Internal Error");
|
|
114
|
+
}
|
|
115
|
+
const selectClauseResults = (0, node_opcua_address_space_1.checkSelectClauses)(node, eventFilter.selectClauses || []);
|
|
116
|
+
const whereClauseResult = new node_opcua_types_1.ContentFilterResult();
|
|
117
|
+
return new node_opcua_types_1.EventFilterResult({
|
|
118
|
+
selectClauseDiagnosticInfos: [],
|
|
119
|
+
selectClauseResults,
|
|
120
|
+
whereClauseResult
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function analyseDataChangeFilterResult(node, dataChangeFilter) {
|
|
124
|
+
(0, node_opcua_assert_1.assert)(dataChangeFilter instanceof node_opcua_service_subscription_2.DataChangeFilter);
|
|
125
|
+
// the opcua specification doesn't provide dataChangeFilterResult
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
function analyseAggregateFilterResult(node, aggregateFilter) {
|
|
129
|
+
(0, node_opcua_assert_1.assert)(aggregateFilter instanceof node_opcua_service_subscription_1.AggregateFilter);
|
|
130
|
+
return new node_opcua_types_1.AggregateFilterResult({});
|
|
131
|
+
}
|
|
132
|
+
function _process_filter(node, filter) {
|
|
133
|
+
if (!filter) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
if (filter instanceof node_opcua_service_filter_1.EventFilter) {
|
|
137
|
+
return analyseEventFilterResult(node, filter);
|
|
138
|
+
}
|
|
139
|
+
else if (filter instanceof node_opcua_service_subscription_2.DataChangeFilter) {
|
|
140
|
+
return analyseDataChangeFilterResult(node, filter);
|
|
141
|
+
}
|
|
142
|
+
else if (filter instanceof node_opcua_service_subscription_1.AggregateFilter) {
|
|
143
|
+
return analyseAggregateFilterResult(node, filter);
|
|
144
|
+
}
|
|
145
|
+
// istanbul ignore next
|
|
146
|
+
throw new Error("invalid filter");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
function createSubscriptionDiagnostics(subscription) {
|
|
152
|
+
(0, node_opcua_assert_1.assert)(subscription instanceof Subscription);
|
|
153
|
+
const subscriptionDiagnostics = new node_opcua_common_1.SubscriptionDiagnosticsDataType({});
|
|
154
|
+
const subscription_subscriptionDiagnostics = subscriptionDiagnostics;
|
|
155
|
+
subscription_subscriptionDiagnostics.$subscription = subscription;
|
|
156
|
+
// "sessionId"
|
|
157
|
+
subscription_subscriptionDiagnostics.__defineGetter__("sessionId", function () {
|
|
158
|
+
if (!this.$subscription) {
|
|
159
|
+
return node_opcua_nodeid_1.NodeId.nullNodeId;
|
|
160
|
+
}
|
|
161
|
+
return this.$subscription.getSessionId();
|
|
162
|
+
});
|
|
163
|
+
subscription_subscriptionDiagnostics.__defineGetter__("subscriptionId", function () {
|
|
164
|
+
if (!this.$subscription) {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
return this.$subscription.id;
|
|
168
|
+
});
|
|
169
|
+
subscription_subscriptionDiagnostics.__defineGetter__("priority", function () {
|
|
170
|
+
if (!this.$subscription) {
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
return this.$subscription.priority;
|
|
174
|
+
});
|
|
175
|
+
subscription_subscriptionDiagnostics.__defineGetter__("publishingInterval", function () {
|
|
176
|
+
if (!this.$subscription) {
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
return this.$subscription.publishingInterval;
|
|
180
|
+
});
|
|
181
|
+
subscription_subscriptionDiagnostics.__defineGetter__("maxLifetimeCount", function () {
|
|
182
|
+
return this.$subscription.lifeTimeCount;
|
|
183
|
+
});
|
|
184
|
+
subscription_subscriptionDiagnostics.__defineGetter__("maxKeepAliveCount", function () {
|
|
185
|
+
if (!this.$subscription) {
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
return this.$subscription.maxKeepAliveCount;
|
|
189
|
+
});
|
|
190
|
+
subscription_subscriptionDiagnostics.__defineGetter__("maxNotificationsPerPublish", function () {
|
|
191
|
+
if (!this.$subscription) {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
return this.$subscription.maxNotificationsPerPublish;
|
|
195
|
+
});
|
|
196
|
+
subscription_subscriptionDiagnostics.__defineGetter__("publishingEnabled", function () {
|
|
197
|
+
if (!this.$subscription) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return this.$subscription.publishingEnabled;
|
|
201
|
+
});
|
|
202
|
+
subscription_subscriptionDiagnostics.__defineGetter__("monitoredItemCount", function () {
|
|
203
|
+
if (!this.$subscription) {
|
|
204
|
+
return 0;
|
|
205
|
+
}
|
|
206
|
+
return this.$subscription.monitoredItemCount;
|
|
207
|
+
});
|
|
208
|
+
subscription_subscriptionDiagnostics.__defineGetter__("nextSequenceNumber", function () {
|
|
209
|
+
if (!this.$subscription) {
|
|
210
|
+
return 0;
|
|
211
|
+
}
|
|
212
|
+
return this.$subscription._get_future_sequence_number();
|
|
213
|
+
});
|
|
214
|
+
subscription_subscriptionDiagnostics.__defineGetter__("disabledMonitoredItemCount", function () {
|
|
215
|
+
if (!this.$subscription) {
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
return this.$subscription.disabledMonitoredItemCount;
|
|
219
|
+
});
|
|
220
|
+
/* those member of self.subscriptionDiagnostics are handled directly
|
|
221
|
+
|
|
222
|
+
modifyCount
|
|
223
|
+
enableCount,
|
|
224
|
+
disableCount,
|
|
225
|
+
republishRequestCount,
|
|
226
|
+
notificationsCount,
|
|
227
|
+
publishRequestCount,
|
|
228
|
+
dataChangeNotificationsCount,
|
|
229
|
+
eventNotificationsCount,
|
|
230
|
+
*/
|
|
231
|
+
/*
|
|
232
|
+
those members are not updated yet in the code :
|
|
233
|
+
"republishMessageRequestCount",
|
|
234
|
+
"republishMessageCount",
|
|
235
|
+
"transferRequestCount",
|
|
236
|
+
"transferredToAltClientCount",
|
|
237
|
+
"transferredToSameClientCount",
|
|
238
|
+
"latePublishRequestCount",
|
|
239
|
+
"currentKeepAliveCount",
|
|
240
|
+
"currentLifetimeCount",
|
|
241
|
+
"unacknowledgedMessageCount",
|
|
242
|
+
"discardedMessageCount",
|
|
243
|
+
"monitoringQueueOverflowCount",
|
|
244
|
+
"eventQueueOverFlowCount"
|
|
245
|
+
*/
|
|
246
|
+
// add object in Variable SubscriptionDiagnosticArray (i=2290) ( Array of SubscriptionDiagnostics)
|
|
247
|
+
// add properties in Variable to reflect
|
|
248
|
+
return subscriptionDiagnostics;
|
|
249
|
+
}
|
|
250
|
+
let g_monitoredItemId = Math.ceil(Math.random() * 100000);
|
|
251
|
+
function getNextMonitoredItemId() {
|
|
252
|
+
return g_monitoredItemId++;
|
|
253
|
+
}
|
|
254
|
+
// function myFilter<T>(t1: any, chunk: any[]): T[] {
|
|
255
|
+
// return chunk.filter(filter_instanceof.bind(null, t1));
|
|
256
|
+
// }
|
|
257
|
+
// function makeNotificationData(notifications_chunk: QueueItem): NotificationData {
|
|
258
|
+
// const dataChangedNotificationData = myFilter<MonitoredItemNotification>(MonitoredItemNotification, notifications_chunk);
|
|
259
|
+
// const eventNotificationListData = myFilter<EventFieldList>(EventFieldList, notifications_chunk);
|
|
260
|
+
// assert(notifications_chunk.length === dataChangedNotificationData.length + eventNotificationListData.length);
|
|
261
|
+
// const notifications: (DataChangeNotification | EventNotificationList)[] = [];
|
|
262
|
+
// // add dataChangeNotification
|
|
263
|
+
// if (dataChangedNotificationData.length) {
|
|
264
|
+
// const dataChangeNotification = new DataChangeNotification({
|
|
265
|
+
// diagnosticInfos: [],
|
|
266
|
+
// monitoredItems: dataChangedNotificationData
|
|
267
|
+
// });
|
|
268
|
+
// notifications.push(dataChangeNotification);
|
|
269
|
+
// }
|
|
270
|
+
// // add dataChangeNotification
|
|
271
|
+
// if (eventNotificationListData.length) {
|
|
272
|
+
// const eventNotificationList = new EventNotificationList({
|
|
273
|
+
// events: eventNotificationListData
|
|
274
|
+
// });
|
|
275
|
+
// notifications.push(eventNotificationList);
|
|
276
|
+
// }
|
|
277
|
+
// return notifications.length === 0 ? null : notifications;
|
|
278
|
+
// }
|
|
279
|
+
const INVALID_ID = -1;
|
|
280
|
+
/**
|
|
281
|
+
* The Subscription class used in the OPCUA server side.
|
|
282
|
+
*/
|
|
283
|
+
class Subscription extends events_1.EventEmitter {
|
|
284
|
+
constructor(options) {
|
|
285
|
+
super();
|
|
286
|
+
this._keep_alive_counter = 0;
|
|
287
|
+
this._hasUncollectedMonitoredItemNotifications = false;
|
|
288
|
+
options = options || {};
|
|
289
|
+
Subscription.registry.register(this);
|
|
290
|
+
this.sessionId = options.sessionId || node_opcua_nodeid_1.NodeId.nullNodeId;
|
|
291
|
+
(0, node_opcua_assert_1.assert)(this.sessionId instanceof node_opcua_nodeid_1.NodeId, "expecting a sessionId NodeId");
|
|
292
|
+
this.publishEngine = options.publishEngine;
|
|
293
|
+
this.id = options.id || INVALID_ID;
|
|
294
|
+
this.priority = options.priority || 0;
|
|
295
|
+
this.publishingInterval = _adjust_publishing_interval(options.publishingInterval);
|
|
296
|
+
this.maxKeepAliveCount = _adjust_maxKeepAliveCount(options.maxKeepAliveCount); // , this.publishingInterval);
|
|
297
|
+
this.resetKeepAliveCounter();
|
|
298
|
+
this.lifeTimeCount = _adjust_lifeTimeCount(options.lifeTimeCount || 0, this.maxKeepAliveCount, this.publishingInterval);
|
|
299
|
+
this.maxNotificationsPerPublish = _adjust_maxNotificationsPerPublish(options.maxNotificationsPerPublish);
|
|
300
|
+
this._life_time_counter = 0;
|
|
301
|
+
this.resetLifeTimeCounter();
|
|
302
|
+
// notification message that are ready to be sent to the client
|
|
303
|
+
this._pending_notifications = new queue_1.Queue();
|
|
304
|
+
this._sent_notification_messages = [];
|
|
305
|
+
this._sequence_number_generator = new node_opcua_secure_channel_1.SequenceNumberGenerator();
|
|
306
|
+
// initial state of the subscription
|
|
307
|
+
this.state = SubscriptionState.CREATING;
|
|
308
|
+
this.publishIntervalCount = 0;
|
|
309
|
+
this.monitoredItems = {}; // monitored item map
|
|
310
|
+
this.monitoredItemIdCounter = 0;
|
|
311
|
+
this.publishingEnabled = _adjust_publishingEnable(options.publishingEnabled);
|
|
312
|
+
this.subscriptionDiagnostics = createSubscriptionDiagnostics(this);
|
|
313
|
+
// A boolean value that is set to TRUE to mean that either a NotificationMessage or a keep-alive
|
|
314
|
+
// Message has been sent on the Subscription. It is a flag that is used to ensure that either a
|
|
315
|
+
// NotificationMessage or a keep-alive Message is sent out the first time the publishing
|
|
316
|
+
// timer expires.
|
|
317
|
+
this.messageSent = false;
|
|
318
|
+
this.timerId = null;
|
|
319
|
+
this._start_timer();
|
|
320
|
+
debugLog(chalk.green(`creating subscription ${this.id}`));
|
|
321
|
+
this.serverCapabilities = options.serverCapabilities;
|
|
322
|
+
this.serverCapabilities.maxMonitoredItems = this.serverCapabilities.maxMonitoredItems || Subscription.defaultMaxMonitoredItemCount;
|
|
323
|
+
this.serverCapabilities.maxMonitoredItemsPerSubscription =
|
|
324
|
+
this.serverCapabilities.maxMonitoredItemsPerSubscription || Subscription.defaultMaxMonitoredItemCount;
|
|
325
|
+
this.globalCounter = options.globalCounter;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* @deprecated use serverCapacity.maxMonitoredItems and serverCapacity.maxMonitoredItemsPerSubscription instead
|
|
329
|
+
*/
|
|
330
|
+
static get maxMonitoredItemCount() {
|
|
331
|
+
return Subscription.defaultMaxMonitoredItemCount;
|
|
332
|
+
}
|
|
333
|
+
getSessionId() {
|
|
334
|
+
return this.sessionId;
|
|
335
|
+
}
|
|
336
|
+
toString() {
|
|
337
|
+
let str = "Subscription:\n";
|
|
338
|
+
str += " subscriptionId " + this.id + "\n";
|
|
339
|
+
str += " sessionId " + this.getSessionId().toString() + "\n";
|
|
340
|
+
str += " publishingEnabled " + this.publishingEnabled + "\n";
|
|
341
|
+
str += " maxKeepAliveCount " + this.maxKeepAliveCount + "\n";
|
|
342
|
+
str += " publishingInterval " + this.publishingInterval + "\n";
|
|
343
|
+
str += " lifeTimeCount " + this.lifeTimeCount + "\n";
|
|
344
|
+
str += " maxKeepAliveCount " + this.maxKeepAliveCount + "\n";
|
|
345
|
+
return str;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* modify subscription parameters
|
|
349
|
+
* @param param
|
|
350
|
+
*/
|
|
351
|
+
modify(param) {
|
|
352
|
+
// update diagnostic counter
|
|
353
|
+
this.subscriptionDiagnostics.modifyCount += 1;
|
|
354
|
+
const publishingInterval_old = this.publishingInterval;
|
|
355
|
+
param.requestedPublishingInterval = param.requestedPublishingInterval || 0;
|
|
356
|
+
param.requestedMaxKeepAliveCount = param.requestedMaxKeepAliveCount || this.maxKeepAliveCount;
|
|
357
|
+
param.requestedLifetimeCount = param.requestedLifetimeCount || this.lifeTimeCount;
|
|
358
|
+
this.publishingInterval = _adjust_publishing_interval(param.requestedPublishingInterval);
|
|
359
|
+
this.maxKeepAliveCount = _adjust_maxKeepAliveCount(param.requestedMaxKeepAliveCount);
|
|
360
|
+
this.lifeTimeCount = _adjust_lifeTimeCount(param.requestedLifetimeCount, this.maxKeepAliveCount, this.publishingInterval);
|
|
361
|
+
this.maxNotificationsPerPublish = _adjust_maxNotificationsPerPublish(param.maxNotificationsPerPublish || 0);
|
|
362
|
+
this.priority = param.priority || 0;
|
|
363
|
+
this.resetLifeTimeAndKeepAliveCounters();
|
|
364
|
+
if (publishingInterval_old !== this.publishingInterval) {
|
|
365
|
+
// todo
|
|
366
|
+
}
|
|
367
|
+
this._stop_timer();
|
|
368
|
+
this._start_timer();
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* set publishing mode
|
|
372
|
+
* @param publishingEnabled
|
|
373
|
+
*/
|
|
374
|
+
setPublishingMode(publishingEnabled) {
|
|
375
|
+
this.publishingEnabled = !!publishingEnabled;
|
|
376
|
+
// update diagnostics
|
|
377
|
+
if (this.publishingEnabled) {
|
|
378
|
+
this.subscriptionDiagnostics.enableCount += 1;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
this.subscriptionDiagnostics.disableCount += 1;
|
|
382
|
+
}
|
|
383
|
+
this.resetLifeTimeCounter();
|
|
384
|
+
if (!publishingEnabled && this.state !== SubscriptionState.CLOSED) {
|
|
385
|
+
this.state = SubscriptionState.NORMAL;
|
|
386
|
+
}
|
|
387
|
+
return node_opcua_status_code_1.StatusCodes.Good;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* @private
|
|
391
|
+
*/
|
|
392
|
+
get keepAliveCounterHasExpired() {
|
|
393
|
+
return this._keep_alive_counter >= this.maxKeepAliveCount;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Reset the Lifetime Counter Variable to the value specified for the lifetime of a Subscription in
|
|
397
|
+
* the CreateSubscription Service( 5.13.2).
|
|
398
|
+
* @private
|
|
399
|
+
*/
|
|
400
|
+
resetLifeTimeCounter() {
|
|
401
|
+
this._life_time_counter = 0;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* @private
|
|
405
|
+
*/
|
|
406
|
+
increaseLifeTimeCounter() {
|
|
407
|
+
this._life_time_counter += 1;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* True if the subscription life time has expired.
|
|
411
|
+
*
|
|
412
|
+
*/
|
|
413
|
+
get lifeTimeHasExpired() {
|
|
414
|
+
(0, node_opcua_assert_1.assert)(this.lifeTimeCount > 0);
|
|
415
|
+
return this._life_time_counter >= this.lifeTimeCount;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* number of milliseconds before this subscription times out (lifeTimeHasExpired === true);
|
|
419
|
+
*/
|
|
420
|
+
get timeToExpiration() {
|
|
421
|
+
return (this.lifeTimeCount - this._life_time_counter) * this.publishingInterval;
|
|
422
|
+
}
|
|
423
|
+
get timeToKeepAlive() {
|
|
424
|
+
return (this.maxKeepAliveCount - this._keep_alive_counter) * this.publishingInterval;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Terminates the subscription.
|
|
428
|
+
* Calling this method will also remove any monitored items.
|
|
429
|
+
*
|
|
430
|
+
*/
|
|
431
|
+
terminate() {
|
|
432
|
+
(0, node_opcua_assert_1.assert)(arguments.length === 0);
|
|
433
|
+
debugLog("Subscription#terminate status", SubscriptionState[this.state]);
|
|
434
|
+
if (this.state === SubscriptionState.CLOSED) {
|
|
435
|
+
// todo verify if asserting is required here
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
// stop timer
|
|
439
|
+
this._stop_timer();
|
|
440
|
+
debugLog("terminating Subscription ", this.id, " with ", this.monitoredItemCount, " monitored items");
|
|
441
|
+
// dispose all monitoredItem
|
|
442
|
+
const keys = Object.keys(this.monitoredItems);
|
|
443
|
+
for (const key of keys) {
|
|
444
|
+
const status = this.removeMonitoredItem(parseInt(key, 10));
|
|
445
|
+
(0, node_opcua_assert_1.assert)(status === node_opcua_status_code_1.StatusCodes.Good);
|
|
446
|
+
}
|
|
447
|
+
(0, node_opcua_assert_1.assert)(this.monitoredItemCount === 0);
|
|
448
|
+
if (this.$session) {
|
|
449
|
+
this.$session._unexposeSubscriptionDiagnostics(this);
|
|
450
|
+
}
|
|
451
|
+
this.state = SubscriptionState.CLOSED;
|
|
452
|
+
/**
|
|
453
|
+
* notify the subscription owner that the subscription has been terminated.
|
|
454
|
+
* @event "terminated"
|
|
455
|
+
*/
|
|
456
|
+
this.emit("terminated");
|
|
457
|
+
if (this.publishEngine) {
|
|
458
|
+
this.publishEngine.on_close_subscription(this);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
setTriggering(triggeringItemId, linksToAdd, linksToRemove) {
|
|
462
|
+
/** Bad_NothingToDo, Bad_TooManyOperations,Bad_SubscriptionIdInvalid, Bad_MonitoredItemIdInvalid */
|
|
463
|
+
linksToAdd = linksToAdd || [];
|
|
464
|
+
linksToRemove = linksToRemove || [];
|
|
465
|
+
if (linksToAdd.length === 0 && linksToRemove.length === 0) {
|
|
466
|
+
return { statusCode: node_opcua_status_code_1.StatusCodes.BadNothingToDo, addResults: [], removeResults: [] };
|
|
467
|
+
}
|
|
468
|
+
const triggeringItem = this.getMonitoredItem(triggeringItemId);
|
|
469
|
+
const monitoredItemsToAdd = linksToAdd.map((id) => this.getMonitoredItem(id));
|
|
470
|
+
const monitoredItemsToRemove = linksToRemove.map((id) => this.getMonitoredItem(id));
|
|
471
|
+
if (!triggeringItem) {
|
|
472
|
+
const removeResults1 = monitoredItemsToRemove.map((m) => m ? node_opcua_status_code_1.StatusCodes.Good : node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid);
|
|
473
|
+
const addResults1 = monitoredItemsToAdd.map((m) => m ? node_opcua_status_code_1.StatusCodes.Good : node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid);
|
|
474
|
+
return {
|
|
475
|
+
statusCode: node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid,
|
|
476
|
+
addResults: addResults1,
|
|
477
|
+
removeResults: removeResults1
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
//
|
|
481
|
+
// note: it seems that CTT imposed that we do remove before add
|
|
482
|
+
const removeResults = monitoredItemsToRemove.map((m) => !m ? node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid : triggeringItem.removeLinkItem(m.monitoredItemId));
|
|
483
|
+
const addResults = monitoredItemsToAdd.map((m) => !m ? node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid : triggeringItem.addLinkItem(m.monitoredItemId));
|
|
484
|
+
const statusCode = node_opcua_status_code_1.StatusCodes.Good;
|
|
485
|
+
// do binding
|
|
486
|
+
return {
|
|
487
|
+
statusCode,
|
|
488
|
+
addResults,
|
|
489
|
+
removeResults
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
dispose() {
|
|
493
|
+
if (doDebug) {
|
|
494
|
+
debugLog("Subscription#dispose", this.id, this.monitoredItemCount);
|
|
495
|
+
}
|
|
496
|
+
(0, node_opcua_assert_1.assert)(this.monitoredItemCount === 0, "MonitoredItems haven't been deleted first !!!");
|
|
497
|
+
(0, node_opcua_assert_1.assert)(this.timerId === null, "Subscription timer haven't been terminated");
|
|
498
|
+
if (this.subscriptionDiagnostics) {
|
|
499
|
+
this.subscriptionDiagnostics.$subscription = null;
|
|
500
|
+
}
|
|
501
|
+
this.publishEngine = undefined;
|
|
502
|
+
this._pending_notifications.clear();
|
|
503
|
+
this._sent_notification_messages = [];
|
|
504
|
+
this.sessionId = new node_opcua_nodeid_1.NodeId();
|
|
505
|
+
this.$session = undefined;
|
|
506
|
+
this.removeAllListeners();
|
|
507
|
+
Subscription.registry.unregister(this);
|
|
508
|
+
}
|
|
509
|
+
get aborted() {
|
|
510
|
+
const session = this.$session;
|
|
511
|
+
if (!session) {
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
return session.aborted;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* number of pending notifications
|
|
518
|
+
*/
|
|
519
|
+
get pendingNotificationsCount() {
|
|
520
|
+
return this._pending_notifications ? this._pending_notifications.size : 0;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* is 'true' if there are pending notifications for this subscription. (i.e moreNotifications)
|
|
524
|
+
*/
|
|
525
|
+
get hasPendingNotifications() {
|
|
526
|
+
return this.pendingNotificationsCount > 0;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* number of sent notifications
|
|
530
|
+
*/
|
|
531
|
+
get sentNotificationMessageCount() {
|
|
532
|
+
return this._sent_notification_messages.length;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* @internal
|
|
536
|
+
*/
|
|
537
|
+
_flushSentNotifications() {
|
|
538
|
+
const tmp = this._sent_notification_messages;
|
|
539
|
+
this._sent_notification_messages = [];
|
|
540
|
+
return tmp;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* number of monitored items handled by this subscription
|
|
544
|
+
*/
|
|
545
|
+
get monitoredItemCount() {
|
|
546
|
+
return Object.keys(this.monitoredItems).length;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* number of disabled monitored items.
|
|
550
|
+
*/
|
|
551
|
+
get disabledMonitoredItemCount() {
|
|
552
|
+
return Object.values(this.monitoredItems).reduce((cumul, monitoredItem) => {
|
|
553
|
+
return cumul + (monitoredItem.monitoringMode === node_opcua_service_subscription_2.MonitoringMode.Disabled ? 1 : 0);
|
|
554
|
+
}, 0);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* The number of unacknowledged messages saved in the republish queue.
|
|
558
|
+
*/
|
|
559
|
+
get unacknowledgedMessageCount() {
|
|
560
|
+
return this.subscriptionDiagnostics.unacknowledgedMessageCount;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* adjust monitored item sampling interval
|
|
564
|
+
* - an samplingInterval ===0 means that we use a event-base model ( no sampling)
|
|
565
|
+
* - otherwise the sampling is adjusted
|
|
566
|
+
* @private
|
|
567
|
+
*/
|
|
568
|
+
adjustSamplingInterval(samplingInterval, node) {
|
|
569
|
+
if (samplingInterval < 0) {
|
|
570
|
+
// - The value -1 indicates that the default sampling interval defined by the publishing
|
|
571
|
+
// interval of the Subscription is requested.
|
|
572
|
+
// - Any negative number is interpreted as -1.
|
|
573
|
+
samplingInterval = this.publishingInterval;
|
|
574
|
+
}
|
|
575
|
+
else if (samplingInterval === 0) {
|
|
576
|
+
// OPCUA 1.0.3 Part 4 - 5.12.1.2
|
|
577
|
+
// The value 0 indicates that the Server should use the fastest practical rate.
|
|
578
|
+
// The fastest supported sampling interval may be equal to 0, which indicates
|
|
579
|
+
// that the data item is exception-based rather than being sampled at some period.
|
|
580
|
+
// An exception-based model means that the underlying system does not require
|
|
581
|
+
// sampling and reports data changes.
|
|
582
|
+
const dataValueSamplingInterval = node.readAttribute(node_opcua_address_space_2.SessionContext.defaultContext, node_opcua_data_model_1.AttributeIds.MinimumSamplingInterval);
|
|
583
|
+
// TODO if attributeId === AttributeIds.Value : sampling interval required here
|
|
584
|
+
if (dataValueSamplingInterval.statusCode === node_opcua_status_code_1.StatusCodes.Good) {
|
|
585
|
+
// node provides a Minimum sampling interval ...
|
|
586
|
+
samplingInterval = dataValueSamplingInterval.value.value;
|
|
587
|
+
(0, node_opcua_assert_1.assert)(samplingInterval >= 0 && samplingInterval <= monitored_item_1.MonitoredItem.maximumSamplingInterval);
|
|
588
|
+
// note : at this stage, a samplingInterval===0 means that the data item is really exception-based
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
else if (samplingInterval < monitored_item_1.MonitoredItem.minimumSamplingInterval) {
|
|
592
|
+
samplingInterval = monitored_item_1.MonitoredItem.minimumSamplingInterval;
|
|
593
|
+
}
|
|
594
|
+
else if (samplingInterval > monitored_item_1.MonitoredItem.maximumSamplingInterval) {
|
|
595
|
+
// If the requested samplingInterval is higher than the
|
|
596
|
+
// maximum sampling interval supported by the Server, the maximum sampling
|
|
597
|
+
// interval is returned.
|
|
598
|
+
samplingInterval = monitored_item_1.MonitoredItem.maximumSamplingInterval;
|
|
599
|
+
}
|
|
600
|
+
const node_minimumSamplingInterval = node && node.minimumSamplingInterval ? node.minimumSamplingInterval : 0;
|
|
601
|
+
samplingInterval = Math.max(samplingInterval, node_minimumSamplingInterval);
|
|
602
|
+
return samplingInterval;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* create a monitored item
|
|
606
|
+
* @param addressSpace - address space
|
|
607
|
+
* @param timestampsToReturn - the timestamp to return
|
|
608
|
+
* @param monitoredItemCreateRequest - the parameters describing the monitored Item to create
|
|
609
|
+
*/
|
|
610
|
+
preCreateMonitoredItem(addressSpace, timestampsToReturn, monitoredItemCreateRequest) {
|
|
611
|
+
(0, node_opcua_assert_1.assert)(monitoredItemCreateRequest instanceof node_opcua_service_subscription_2.MonitoredItemCreateRequest);
|
|
612
|
+
function handle_error(statusCode) {
|
|
613
|
+
return {
|
|
614
|
+
createResult: new node_opcua_service_subscription_2.MonitoredItemCreateResult({ statusCode }),
|
|
615
|
+
monitoredItemCreateRequest
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
const itemToMonitor = monitoredItemCreateRequest.itemToMonitor;
|
|
619
|
+
const node = addressSpace.findNode(itemToMonitor.nodeId);
|
|
620
|
+
if (!node) {
|
|
621
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadNodeIdUnknown);
|
|
622
|
+
}
|
|
623
|
+
if (itemToMonitor.attributeId === node_opcua_data_model_1.AttributeIds.Value && !(node.nodeClass === node_opcua_data_model_1.NodeClass.Variable)) {
|
|
624
|
+
// AttributeIds.Value is only valid for monitoring value of UAVariables.
|
|
625
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadAttributeIdInvalid);
|
|
626
|
+
}
|
|
627
|
+
if (itemToMonitor.attributeId === node_opcua_data_model_1.AttributeIds.INVALID) {
|
|
628
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadAttributeIdInvalid);
|
|
629
|
+
}
|
|
630
|
+
if (!itemToMonitor.indexRange.isValid()) {
|
|
631
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadIndexRangeInvalid);
|
|
632
|
+
}
|
|
633
|
+
// check dataEncoding applies only on Values
|
|
634
|
+
if (itemToMonitor.dataEncoding.name && itemToMonitor.attributeId !== node_opcua_data_model_1.AttributeIds.Value) {
|
|
635
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadDataEncodingInvalid);
|
|
636
|
+
}
|
|
637
|
+
// check dataEncoding
|
|
638
|
+
if (!(0, node_opcua_data_model_1.isValidDataEncoding)(itemToMonitor.dataEncoding)) {
|
|
639
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadDataEncodingUnsupported);
|
|
640
|
+
}
|
|
641
|
+
// check that item can be read by current user session
|
|
642
|
+
// filter
|
|
643
|
+
const requestedParameters = monitoredItemCreateRequest.requestedParameters;
|
|
644
|
+
const filter = requestedParameters.filter;
|
|
645
|
+
const statusCodeFilter = (0, validate_filter_1.validateFilter)(filter, itemToMonitor, node);
|
|
646
|
+
if (statusCodeFilter !== node_opcua_status_code_1.StatusCodes.Good) {
|
|
647
|
+
return handle_error(statusCodeFilter);
|
|
648
|
+
}
|
|
649
|
+
// do we have enough room for new monitored items ?
|
|
650
|
+
if (this.monitoredItemCount >= this.serverCapabilities.maxMonitoredItemsPerSubscription) {
|
|
651
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadTooManyMonitoredItems);
|
|
652
|
+
}
|
|
653
|
+
if (this.globalCounter.totalMonitoredItemCount >= this.serverCapabilities.maxMonitoredItems) {
|
|
654
|
+
return handle_error(node_opcua_status_code_1.StatusCodes.BadTooManyMonitoredItems);
|
|
655
|
+
}
|
|
656
|
+
const createResult = this._createMonitoredItemStep2(timestampsToReturn, monitoredItemCreateRequest, node);
|
|
657
|
+
(0, node_opcua_assert_1.assert)(createResult.statusCode === node_opcua_status_code_1.StatusCodes.Good);
|
|
658
|
+
const monitoredItem = this.getMonitoredItem(createResult.monitoredItemId);
|
|
659
|
+
// istanbul ignore next
|
|
660
|
+
if (!monitoredItem) {
|
|
661
|
+
throw new Error("internal error");
|
|
662
|
+
}
|
|
663
|
+
// TODO: fix old way to set node. !!!!
|
|
664
|
+
monitoredItem.setNode(node);
|
|
665
|
+
this.emit("monitoredItem", monitoredItem, itemToMonitor);
|
|
666
|
+
return { monitoredItem, monitoredItemCreateRequest, createResult };
|
|
667
|
+
}
|
|
668
|
+
applyOnMonitoredItem(functor) {
|
|
669
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
670
|
+
for (const m of Object.values(this.monitoredItems)) {
|
|
671
|
+
yield functor(m);
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
postCreateMonitoredItem(monitoredItem, monitoredItemCreateRequest, createResult) {
|
|
676
|
+
this._createMonitoredItemStep3(monitoredItem, monitoredItemCreateRequest);
|
|
677
|
+
}
|
|
678
|
+
createMonitoredItem(addressSpace, timestampsToReturn, monitoredItemCreateRequest) {
|
|
679
|
+
const { monitoredItem, createResult } = this.preCreateMonitoredItem(addressSpace, timestampsToReturn, monitoredItemCreateRequest);
|
|
680
|
+
this.postCreateMonitoredItem(monitoredItem, monitoredItemCreateRequest, createResult);
|
|
681
|
+
return createResult;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* get a monitoredItem by Id.
|
|
685
|
+
* @param monitoredItemId : the id of the monitored item to get.
|
|
686
|
+
* @return the monitored item matching monitoredItemId
|
|
687
|
+
*/
|
|
688
|
+
getMonitoredItem(monitoredItemId) {
|
|
689
|
+
return this.monitoredItems[monitoredItemId] || null;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* remove a monitored Item from the subscription.
|
|
693
|
+
* @param monitoredItemId : the id of the monitored item to get.
|
|
694
|
+
*/
|
|
695
|
+
removeMonitoredItem(monitoredItemId) {
|
|
696
|
+
debugLog("Removing monitoredIem ", monitoredItemId);
|
|
697
|
+
if (!Object.prototype.hasOwnProperty.call(this.monitoredItems, monitoredItemId.toString())) {
|
|
698
|
+
return node_opcua_status_code_1.StatusCodes.BadMonitoredItemIdInvalid;
|
|
699
|
+
}
|
|
700
|
+
const monitoredItem = this.monitoredItems[monitoredItemId];
|
|
701
|
+
monitoredItem.terminate();
|
|
702
|
+
monitoredItem.dispose();
|
|
703
|
+
/**
|
|
704
|
+
*
|
|
705
|
+
* notify that a monitored item has been removed from the subscription
|
|
706
|
+
* @param monitoredItem {MonitoredItem}
|
|
707
|
+
*/
|
|
708
|
+
this.emit("removeMonitoredItem", monitoredItem);
|
|
709
|
+
delete this.monitoredItems[monitoredItemId];
|
|
710
|
+
this.globalCounter.totalMonitoredItemCount -= 1;
|
|
711
|
+
this._removePendingNotificationsFor(monitoredItemId);
|
|
712
|
+
// flush pending notifications
|
|
713
|
+
// assert(this._pending_notifications.size === 0);
|
|
714
|
+
return node_opcua_status_code_1.StatusCodes.Good;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* rue if monitored Item have uncollected Notifications
|
|
718
|
+
*/
|
|
719
|
+
get hasUncollectedMonitoredItemNotifications() {
|
|
720
|
+
if (this._hasUncollectedMonitoredItemNotifications) {
|
|
721
|
+
return true;
|
|
722
|
+
}
|
|
723
|
+
const keys = Object.keys(this.monitoredItems);
|
|
724
|
+
const n = keys.length;
|
|
725
|
+
for (let i = 0; i < n; i++) {
|
|
726
|
+
const key = parseInt(keys[i], 10);
|
|
727
|
+
const monitoredItem = this.monitoredItems[key];
|
|
728
|
+
if (monitoredItem.hasMonitoredItemNotifications) {
|
|
729
|
+
this._hasUncollectedMonitoredItemNotifications = true;
|
|
730
|
+
return true;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
get subscriptionId() {
|
|
736
|
+
return this.id;
|
|
737
|
+
}
|
|
738
|
+
getMessageForSequenceNumber(sequenceNumber) {
|
|
739
|
+
const notification_message = this._sent_notification_messages.find((e) => e.sequenceNumber === sequenceNumber);
|
|
740
|
+
return notification_message || null;
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* returns true if the notification has expired
|
|
744
|
+
* @param notification
|
|
745
|
+
*/
|
|
746
|
+
notificationHasExpired(notification) {
|
|
747
|
+
(0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(notification, "start_tick"));
|
|
748
|
+
(0, node_opcua_assert_1.assert)(isFinite(notification.start_tick + this.maxKeepAliveCount));
|
|
749
|
+
return notification.start_tick + this.maxKeepAliveCount < this.publishIntervalCount;
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* returns in an array the sequence numbers of the notifications that have been sent
|
|
753
|
+
* and that haven't been acknowledged yet.
|
|
754
|
+
*/
|
|
755
|
+
getAvailableSequenceNumbers() {
|
|
756
|
+
const availableSequenceNumbers = _getSequenceNumbers(this._sent_notification_messages);
|
|
757
|
+
return availableSequenceNumbers;
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* acknowledges a notification identified by its sequence number
|
|
761
|
+
*/
|
|
762
|
+
acknowledgeNotification(sequenceNumber) {
|
|
763
|
+
debugLog("acknowledgeNotification ", sequenceNumber);
|
|
764
|
+
let foundIndex = -1;
|
|
765
|
+
this._sent_notification_messages.forEach((e, index) => {
|
|
766
|
+
if (e.sequenceNumber === sequenceNumber) {
|
|
767
|
+
foundIndex = index;
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
if (foundIndex === -1) {
|
|
771
|
+
if (doDebug) {
|
|
772
|
+
debugLog(chalk.red("acknowledging sequence FAILED !!! "), chalk.cyan(sequenceNumber.toString()));
|
|
773
|
+
}
|
|
774
|
+
return node_opcua_status_code_1.StatusCodes.BadSequenceNumberUnknown;
|
|
775
|
+
}
|
|
776
|
+
else {
|
|
777
|
+
if (doDebug) {
|
|
778
|
+
debugLog(chalk.yellow("acknowledging sequence "), chalk.cyan(sequenceNumber.toString()));
|
|
779
|
+
}
|
|
780
|
+
this._sent_notification_messages.splice(foundIndex, 1);
|
|
781
|
+
this.subscriptionDiagnostics.unacknowledgedMessageCount--;
|
|
782
|
+
return node_opcua_status_code_1.StatusCodes.Good;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* getMonitoredItems is used to get information about monitored items of a subscription.Its intended
|
|
787
|
+
* use is defined in Part 4. This method is the implementation of the Standard OPCUA GetMonitoredItems Method.
|
|
788
|
+
* from spec:
|
|
789
|
+
* This method can be used to get the list of monitored items in a subscription if CreateMonitoredItems
|
|
790
|
+
* failed due to a network interruption and the client does not know if the creation succeeded in the server.
|
|
791
|
+
*
|
|
792
|
+
*/
|
|
793
|
+
getMonitoredItems() {
|
|
794
|
+
const monitoredItems = Object.keys(this.monitoredItems);
|
|
795
|
+
const monitoredItemCount = monitoredItems.length;
|
|
796
|
+
const result = {
|
|
797
|
+
clientHandles: new Uint32Array(monitoredItemCount),
|
|
798
|
+
serverHandles: new Uint32Array(monitoredItemCount),
|
|
799
|
+
statusCode: node_opcua_status_code_1.StatusCodes.Good
|
|
800
|
+
};
|
|
801
|
+
for (let index = 0; index < monitoredItemCount; index++) {
|
|
802
|
+
const monitoredItemId = monitoredItems[index];
|
|
803
|
+
const serverHandle = parseInt(monitoredItemId, 10);
|
|
804
|
+
const monitoredItem = this.getMonitoredItem(serverHandle);
|
|
805
|
+
result.clientHandles[index] = monitoredItem.clientHandle;
|
|
806
|
+
// TODO: serverHandle is defined anywhere in the OPCUA Specification 1.02
|
|
807
|
+
// I am not sure what shall be reported for serverHandle...
|
|
808
|
+
// using monitoredItem.monitoredItemId instead...
|
|
809
|
+
// May be a clarification in the OPCUA Spec is required.
|
|
810
|
+
result.serverHandles[index] = serverHandle;
|
|
811
|
+
}
|
|
812
|
+
return result;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* @private
|
|
816
|
+
*/
|
|
817
|
+
resendInitialValues() {
|
|
818
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
819
|
+
const promises = [];
|
|
820
|
+
for (const monitoredItem of Object.values(this.monitoredItems)) {
|
|
821
|
+
(0, node_opcua_assert_1.assert)(monitoredItem.clientHandle !== 4294967295);
|
|
822
|
+
promises.push(monitoredItem.resendInitialValues());
|
|
823
|
+
}
|
|
824
|
+
yield Promise.all(promises);
|
|
825
|
+
this._harvestMonitoredItems();
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* @private
|
|
830
|
+
*/
|
|
831
|
+
notifyTransfer() {
|
|
832
|
+
var _a;
|
|
833
|
+
// OPCUA UA Spec 1.0.3 : part 3 - page 82 - 5.13.7 TransferSubscriptions:
|
|
834
|
+
// If the Server transfers the Subscription to the new Session, the Server shall issue
|
|
835
|
+
// a StatusChangeNotification notificationMessage with the status code
|
|
836
|
+
// Good_SubscriptionTransferred to the old Session.
|
|
837
|
+
debugLog(chalk.red(" Subscription => Notifying Transfer "));
|
|
838
|
+
const notificationData = new node_opcua_service_subscription_2.StatusChangeNotification({
|
|
839
|
+
status: node_opcua_status_code_1.StatusCodes.GoodSubscriptionTransferred
|
|
840
|
+
});
|
|
841
|
+
if (this.publishEngine.pendingPublishRequestCount) {
|
|
842
|
+
// the GoodSubscriptionTransferred can be processed immediately
|
|
843
|
+
this._addNotificationMessage(notificationData);
|
|
844
|
+
debugLog(chalk.red("pendingPublishRequestCount"), (_a = this.publishEngine) === null || _a === void 0 ? void 0 : _a.pendingPublishRequestCount);
|
|
845
|
+
this._publish_pending_notifications();
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
debugLog(chalk.red("Cannot send GoodSubscriptionTransferred => lets create a TransferredSubscription "));
|
|
849
|
+
const ts = new i_server_side_publish_engine_1.TransferredSubscription({
|
|
850
|
+
generator: this._sequence_number_generator,
|
|
851
|
+
id: this.id,
|
|
852
|
+
publishEngine: this.publishEngine
|
|
853
|
+
});
|
|
854
|
+
ts._pending_notification = notificationData;
|
|
855
|
+
this.publishEngine._closed_subscriptions.push(ts);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
*
|
|
860
|
+
* the server invokes the resetLifeTimeAndKeepAliveCounters method of the subscription
|
|
861
|
+
* when the server has send a Publish Response, so that the subscription
|
|
862
|
+
* can reset its life time counter.
|
|
863
|
+
*
|
|
864
|
+
* @private
|
|
865
|
+
*/
|
|
866
|
+
resetLifeTimeAndKeepAliveCounters() {
|
|
867
|
+
this.resetLifeTimeCounter();
|
|
868
|
+
this.resetKeepAliveCounter();
|
|
869
|
+
}
|
|
870
|
+
_updateCounters(notificationMessage) {
|
|
871
|
+
for (const notificationData of notificationMessage.notificationData || []) {
|
|
872
|
+
// update diagnostics
|
|
873
|
+
if (notificationData instanceof node_opcua_service_subscription_2.DataChangeNotification) {
|
|
874
|
+
const nbNotifs = notificationData.monitoredItems.length;
|
|
875
|
+
this.subscriptionDiagnostics.dataChangeNotificationsCount += nbNotifs;
|
|
876
|
+
this.subscriptionDiagnostics.notificationsCount += nbNotifs;
|
|
877
|
+
}
|
|
878
|
+
else if (notificationData instanceof node_opcua_service_subscription_2.EventNotificationList) {
|
|
879
|
+
const nbNotifs = notificationData.events.length;
|
|
880
|
+
this.subscriptionDiagnostics.eventNotificationsCount += nbNotifs;
|
|
881
|
+
this.subscriptionDiagnostics.notificationsCount += nbNotifs;
|
|
882
|
+
}
|
|
883
|
+
else {
|
|
884
|
+
(0, node_opcua_assert_1.assert)(notificationData instanceof node_opcua_service_subscription_2.StatusChangeNotification);
|
|
885
|
+
// TODO
|
|
886
|
+
// note: :there is no way to count StatusChangeNotifications in opcua yet.
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* _publish_pending_notifications send a "notification" event:
|
|
892
|
+
*
|
|
893
|
+
* @private
|
|
894
|
+
* @precondition
|
|
895
|
+
* - pendingPublishRequestCount > 0
|
|
896
|
+
*/
|
|
897
|
+
_publish_pending_notifications() {
|
|
898
|
+
var _a;
|
|
899
|
+
const publishEngine = this.publishEngine;
|
|
900
|
+
const subscriptionId = this.id;
|
|
901
|
+
// preconditions
|
|
902
|
+
(0, node_opcua_assert_1.assert)(publishEngine.pendingPublishRequestCount > 0);
|
|
903
|
+
(0, node_opcua_assert_1.assert)(this.hasPendingNotifications);
|
|
904
|
+
const notificationMessage = this._popNotificationToSend();
|
|
905
|
+
if (notificationMessage.notificationData.length === 0) {
|
|
906
|
+
return; // nothing to do
|
|
907
|
+
}
|
|
908
|
+
const moreNotifications = this.hasPendingNotifications;
|
|
909
|
+
this.emit("notification", notificationMessage);
|
|
910
|
+
// Update counters ....
|
|
911
|
+
this._updateCounters(notificationMessage);
|
|
912
|
+
(0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(notificationMessage, "sequenceNumber"));
|
|
913
|
+
(0, node_opcua_assert_1.assert)(Object.prototype.hasOwnProperty.call(notificationMessage, "notificationData"));
|
|
914
|
+
// update diagnostics
|
|
915
|
+
this.subscriptionDiagnostics.publishRequestCount += 1;
|
|
916
|
+
const response = new node_opcua_service_subscription_2.PublishResponse({
|
|
917
|
+
moreNotifications,
|
|
918
|
+
notificationMessage: {
|
|
919
|
+
notificationData: notificationMessage.notificationData,
|
|
920
|
+
sequenceNumber: this._get_next_sequence_number()
|
|
921
|
+
},
|
|
922
|
+
subscriptionId
|
|
923
|
+
});
|
|
924
|
+
this._sent_notification_messages.push(response.notificationMessage);
|
|
925
|
+
// get available sequence number;
|
|
926
|
+
const availableSequenceNumbers = this.getAvailableSequenceNumbers();
|
|
927
|
+
(0, node_opcua_assert_1.assert)(!response.notificationMessage ||
|
|
928
|
+
availableSequenceNumbers[availableSequenceNumbers.length - 1] === response.notificationMessage.sequenceNumber);
|
|
929
|
+
response.availableSequenceNumbers = availableSequenceNumbers;
|
|
930
|
+
publishEngine._send_response(this, response);
|
|
931
|
+
this.messageSent = true;
|
|
932
|
+
this.subscriptionDiagnostics.unacknowledgedMessageCount++;
|
|
933
|
+
this.resetLifeTimeAndKeepAliveCounters();
|
|
934
|
+
if (doDebug) {
|
|
935
|
+
debugLog("Subscription sending a notificationMessage subscriptionId=", subscriptionId, "sequenceNumber = ", notificationMessage.sequenceNumber.toString(), (_a = notificationMessage.notificationData) === null || _a === void 0 ? void 0 : _a.map((x) => x === null || x === void 0 ? void 0 : x.constructor.name).join(" "));
|
|
936
|
+
// debugLog(notificationMessage.toString());
|
|
937
|
+
}
|
|
938
|
+
if (this.state !== SubscriptionState.CLOSED) {
|
|
939
|
+
(0, node_opcua_assert_1.assert)(notificationMessage.notificationData.length > 0, "We are not expecting a keep-alive message here");
|
|
940
|
+
this.state = SubscriptionState.NORMAL;
|
|
941
|
+
debugLog("subscription " + this.id + chalk.bgYellow(" set to NORMAL"));
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
process_subscription() {
|
|
945
|
+
(0, node_opcua_assert_1.assert)(this.publishEngine.pendingPublishRequestCount > 0);
|
|
946
|
+
if (!this.publishingEnabled) {
|
|
947
|
+
// no publish to do, except keep alive
|
|
948
|
+
debugLog(" -> no publish to do, except keep alive");
|
|
949
|
+
this._process_keepAlive();
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
if (!this.hasPendingNotifications && this.hasUncollectedMonitoredItemNotifications) {
|
|
953
|
+
// collect notification from monitored items
|
|
954
|
+
this._harvestMonitoredItems();
|
|
955
|
+
}
|
|
956
|
+
// let process them first
|
|
957
|
+
if (this.hasPendingNotifications) {
|
|
958
|
+
this._publish_pending_notifications();
|
|
959
|
+
if (this.state === SubscriptionState.NORMAL && this.hasPendingNotifications) {
|
|
960
|
+
// istanbul ignore next
|
|
961
|
+
if (doDebug) {
|
|
962
|
+
debugLog(" -> pendingPublishRequestCount > 0 " + "&& normal state => re-trigger tick event immediately ");
|
|
963
|
+
}
|
|
964
|
+
// let process an new publish request
|
|
965
|
+
setImmediate(this._tick.bind(this));
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
else {
|
|
969
|
+
this._process_keepAlive();
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
_get_future_sequence_number() {
|
|
973
|
+
return this._sequence_number_generator ? this._sequence_number_generator.future() : 0;
|
|
974
|
+
}
|
|
975
|
+
_process_keepAlive() {
|
|
976
|
+
this.increaseKeepAliveCounter();
|
|
977
|
+
if (this.keepAliveCounterHasExpired) {
|
|
978
|
+
debugLog(` -> _process_keepAlive => keepAliveCounterHasExpired`);
|
|
979
|
+
if (this._sendKeepAliveResponse()) {
|
|
980
|
+
this.resetLifeTimeAndKeepAliveCounters();
|
|
981
|
+
}
|
|
982
|
+
else {
|
|
983
|
+
debugLog(" -> subscription.state === LATE , " +
|
|
984
|
+
"because keepAlive Response cannot be send due to lack of PublishRequest");
|
|
985
|
+
this.state = SubscriptionState.LATE;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
_stop_timer() {
|
|
990
|
+
if (this.timerId) {
|
|
991
|
+
debugLog(chalk.bgWhite.blue("Subscription#_stop_timer subscriptionId="), this.id);
|
|
992
|
+
clearInterval(this.timerId);
|
|
993
|
+
this.timerId = null;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
_start_timer() {
|
|
997
|
+
debugLog(chalk.bgWhite.blue("Subscription#_start_timer subscriptionId="), this.id, " publishingInterval = ", this.publishingInterval);
|
|
998
|
+
(0, node_opcua_assert_1.assert)(this.timerId === null);
|
|
999
|
+
// from the spec:
|
|
1000
|
+
// When a Subscription is created, the first Message is sent at the end of the first publishing cycle to
|
|
1001
|
+
// inform the Client that the Subscription is operational. A NotificationMessage is sent if there are
|
|
1002
|
+
// Notifications ready to be reported. If there are none, a keep-alive Message is sent instead that
|
|
1003
|
+
// contains a sequence number of 1, indicating that the first NotificationMessage has not yet been sent.
|
|
1004
|
+
// This is the only time a keep-alive Message is sent without waiting for the maximum keep-alive count
|
|
1005
|
+
// to be reached, as specified in (f) above.
|
|
1006
|
+
// make sure that a keep-alive Message will be send at the end of the first publishing cycle
|
|
1007
|
+
// if there are no Notifications ready.
|
|
1008
|
+
this._keep_alive_counter = this.maxKeepAliveCount;
|
|
1009
|
+
(0, node_opcua_assert_1.assert)(this.publishingInterval >= Subscription.minimumPublishingInterval);
|
|
1010
|
+
this.timerId = setInterval(this._tick.bind(this), this.publishingInterval);
|
|
1011
|
+
}
|
|
1012
|
+
// counter
|
|
1013
|
+
_get_next_sequence_number() {
|
|
1014
|
+
return this._sequence_number_generator ? this._sequence_number_generator.next() : 0;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* @private
|
|
1018
|
+
*/
|
|
1019
|
+
_tick() {
|
|
1020
|
+
// istanbul ignore next
|
|
1021
|
+
if (doDebug) {
|
|
1022
|
+
debugLog(`Subscription#_tick id ${this.id} aborted=${this.aborted} state=${SubscriptionState[this.state]}`);
|
|
1023
|
+
}
|
|
1024
|
+
if (this.aborted) {
|
|
1025
|
+
// xx console.log(" Log aborted")
|
|
1026
|
+
// xx // underlying channel has been aborted ...
|
|
1027
|
+
// xx self.publishEngine.cancelPendingPublishRequestBeforeChannelChange();
|
|
1028
|
+
// xx // let's still increase lifetime counter to detect timeout
|
|
1029
|
+
}
|
|
1030
|
+
if (this.state === SubscriptionState.CLOSED) {
|
|
1031
|
+
warningLog(`Warning: Subscription#_tick id ${this.id} called while subscription is CLOSED`);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
this.discardOldSentNotifications();
|
|
1035
|
+
// istanbul ignore next
|
|
1036
|
+
if (doDebug) {
|
|
1037
|
+
debugLog(t(new Date()) + " " + this._life_time_counter + "/" + this.lifeTimeCount + chalk.cyan(" Subscription#_tick"), " processing subscriptionId=", this.id, "hasUncollectedMonitoredItemNotifications = ", this.hasUncollectedMonitoredItemNotifications, " publishingIntervalCount =", this.publishIntervalCount);
|
|
1038
|
+
}
|
|
1039
|
+
this.publishEngine._on_tick();
|
|
1040
|
+
this.publishIntervalCount += 1;
|
|
1041
|
+
this.increaseLifeTimeCounter();
|
|
1042
|
+
if (this.lifeTimeHasExpired) {
|
|
1043
|
+
/* istanbul ignore next */
|
|
1044
|
+
if (doDebug) {
|
|
1045
|
+
debugLog(chalk.red.bold(`Subscription ${this.id} has expired !!!!! => Terminating`));
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* notify the subscription owner that the subscription has expired by exceeding its life time.
|
|
1049
|
+
* @event expired
|
|
1050
|
+
*
|
|
1051
|
+
*/
|
|
1052
|
+
this.emit("expired");
|
|
1053
|
+
// notify new terminated status only when subscription has timeout.
|
|
1054
|
+
debugLog("adding StatusChangeNotification notification message for BadTimeout subscription = ", this.id);
|
|
1055
|
+
this._addNotificationMessage(new node_opcua_service_subscription_2.StatusChangeNotification({ status: node_opcua_status_code_1.StatusCodes.BadTimeout }));
|
|
1056
|
+
// kill timer and delete monitored items and transfer pending notification messages
|
|
1057
|
+
this.terminate();
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
const publishEngine = this.publishEngine;
|
|
1061
|
+
// istanbul ignore next
|
|
1062
|
+
if (doDebug) {
|
|
1063
|
+
debugLog("Subscription#_tick self._pending_notifications= ", this._pending_notifications.size);
|
|
1064
|
+
}
|
|
1065
|
+
if (publishEngine.pendingPublishRequestCount === 0 &&
|
|
1066
|
+
(this.hasPendingNotifications || this.hasUncollectedMonitoredItemNotifications)) {
|
|
1067
|
+
// istanbul ignore next
|
|
1068
|
+
if (doDebug) {
|
|
1069
|
+
debugLog("subscription set to LATE hasPendingNotifications = ", this.hasPendingNotifications, " hasUncollectedMonitoredItemNotifications =", this.hasUncollectedMonitoredItemNotifications);
|
|
1070
|
+
}
|
|
1071
|
+
this.state = SubscriptionState.LATE;
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
if (publishEngine.pendingPublishRequestCount > 0) {
|
|
1075
|
+
if (this.hasPendingNotifications) {
|
|
1076
|
+
// simply pop pending notification and send it
|
|
1077
|
+
this.process_subscription();
|
|
1078
|
+
}
|
|
1079
|
+
else if (this.hasUncollectedMonitoredItemNotifications) {
|
|
1080
|
+
this.process_subscription();
|
|
1081
|
+
}
|
|
1082
|
+
else {
|
|
1083
|
+
this._process_keepAlive();
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
else {
|
|
1087
|
+
this._process_keepAlive();
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* @private
|
|
1092
|
+
*/
|
|
1093
|
+
_sendKeepAliveResponse() {
|
|
1094
|
+
const future_sequence_number = this._get_future_sequence_number();
|
|
1095
|
+
if (this.publishEngine.send_keep_alive_response(this.id, future_sequence_number)) {
|
|
1096
|
+
this.messageSent = true;
|
|
1097
|
+
// istanbul ignore next
|
|
1098
|
+
if (doDebug) {
|
|
1099
|
+
debugLog(` -> Subscription#_sendKeepAliveResponse subscriptionId ${this.id} future_sequence_number ${future_sequence_number}`);
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* notify the subscription owner that a keepalive message has to be sent.
|
|
1103
|
+
* @event keepalive
|
|
1104
|
+
*
|
|
1105
|
+
*/
|
|
1106
|
+
this.emit("keepalive", future_sequence_number);
|
|
1107
|
+
this.state = SubscriptionState.KEEPALIVE;
|
|
1108
|
+
return true;
|
|
1109
|
+
}
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Reset the Lifetime Counter Variable to the value specified for the lifetime of a Subscription in
|
|
1114
|
+
* the CreateSubscription Service( 5.13.2).
|
|
1115
|
+
* @private
|
|
1116
|
+
*/
|
|
1117
|
+
resetKeepAliveCounter() {
|
|
1118
|
+
this._keep_alive_counter = 0;
|
|
1119
|
+
// istanbul ignore next
|
|
1120
|
+
if (doDebug) {
|
|
1121
|
+
debugLog(" -> subscriptionId", this.id, " Resetting keepAliveCounter = ", this._keep_alive_counter, this.maxKeepAliveCount);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* @private
|
|
1126
|
+
*/
|
|
1127
|
+
increaseKeepAliveCounter() {
|
|
1128
|
+
this._keep_alive_counter += 1;
|
|
1129
|
+
// istanbul ignore next
|
|
1130
|
+
if (doDebug) {
|
|
1131
|
+
debugLog(" -> subscriptionId", this.id, " Increasing keepAliveCounter = ", this._keep_alive_counter, this.maxKeepAliveCount);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* @private
|
|
1136
|
+
*/
|
|
1137
|
+
_addNotificationMessage(notificationData, monitoredItemId) {
|
|
1138
|
+
// istanbul ignore next
|
|
1139
|
+
if (doDebug) {
|
|
1140
|
+
debugLog(chalk.yellow("Subscription#_addNotificationMessage"), notificationData.toString());
|
|
1141
|
+
}
|
|
1142
|
+
this._pending_notifications.push({
|
|
1143
|
+
monitoredItemId,
|
|
1144
|
+
notification: notificationData,
|
|
1145
|
+
publishTime: new Date(),
|
|
1146
|
+
start_tick: this.publishIntervalCount
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* @internal
|
|
1151
|
+
* @param monitoredItemId
|
|
1152
|
+
*/
|
|
1153
|
+
_removePendingNotificationsFor(monitoredItemId) {
|
|
1154
|
+
const nbRemovedNotification = this._pending_notifications.filterOut((e) => e.monitoredItemId === monitoredItemId);
|
|
1155
|
+
if (doDebug) {
|
|
1156
|
+
debugLog(`Removed ${nbRemovedNotification} notifications`);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Extract the next Notification that is ready to be sent to the client.
|
|
1161
|
+
* @return the Notification to send._pending_notifications
|
|
1162
|
+
*/
|
|
1163
|
+
_popNotificationToSend() {
|
|
1164
|
+
(0, node_opcua_assert_1.assert)(this._pending_notifications.size > 0);
|
|
1165
|
+
const notificationMessage = new node_opcua_service_subscription_2.NotificationMessage({
|
|
1166
|
+
sequenceNumber: 0xffffffff,
|
|
1167
|
+
notificationData: [],
|
|
1168
|
+
publishTime: new Date()
|
|
1169
|
+
}); //
|
|
1170
|
+
const dataChangeNotifications = new node_opcua_service_subscription_2.DataChangeNotification({
|
|
1171
|
+
monitoredItems: []
|
|
1172
|
+
});
|
|
1173
|
+
const eventNotificationList = new node_opcua_service_subscription_2.EventNotificationList({
|
|
1174
|
+
events: []
|
|
1175
|
+
});
|
|
1176
|
+
let statusChangeNotification;
|
|
1177
|
+
let i = 0;
|
|
1178
|
+
let hasEventFieldList = 0;
|
|
1179
|
+
let hasMonitoredItemNotification = 0;
|
|
1180
|
+
const m = this.maxNotificationsPerPublish;
|
|
1181
|
+
while (i < m && this._pending_notifications.size > 0) {
|
|
1182
|
+
if (hasEventFieldList || hasMonitoredItemNotification) {
|
|
1183
|
+
const notification1 = this._pending_notifications.first().notification;
|
|
1184
|
+
if (notification1 instanceof node_opcua_service_subscription_2.StatusChangeNotification) {
|
|
1185
|
+
break;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
const notification = this._pending_notifications.shift().notification;
|
|
1189
|
+
if (notification instanceof node_opcua_service_subscription_2.MonitoredItemNotification) {
|
|
1190
|
+
(0, node_opcua_assert_1.assert)(notification.clientHandle !== 4294967295);
|
|
1191
|
+
dataChangeNotifications.monitoredItems.push(notification);
|
|
1192
|
+
hasMonitoredItemNotification = 1;
|
|
1193
|
+
}
|
|
1194
|
+
else if (notification instanceof node_opcua_types_1.EventFieldList) {
|
|
1195
|
+
eventNotificationList.events.push(notification);
|
|
1196
|
+
hasEventFieldList = 1;
|
|
1197
|
+
}
|
|
1198
|
+
else if (notification instanceof node_opcua_service_subscription_2.StatusChangeNotification) {
|
|
1199
|
+
// to do
|
|
1200
|
+
statusChangeNotification = notification;
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
i += 1;
|
|
1204
|
+
}
|
|
1205
|
+
if (dataChangeNotifications.monitoredItems.length) {
|
|
1206
|
+
notificationMessage.notificationData.push(dataChangeNotifications);
|
|
1207
|
+
}
|
|
1208
|
+
if (eventNotificationList.events.length) {
|
|
1209
|
+
notificationMessage.notificationData.push(eventNotificationList);
|
|
1210
|
+
}
|
|
1211
|
+
if (statusChangeNotification) {
|
|
1212
|
+
notificationMessage.notificationData.push(statusChangeNotification);
|
|
1213
|
+
}
|
|
1214
|
+
return notificationMessage;
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* discardOldSentNotification find all sent notification message that have expired keep-alive
|
|
1218
|
+
* and destroy them.
|
|
1219
|
+
* @private
|
|
1220
|
+
*
|
|
1221
|
+
* Subscriptions maintain a retransmission queue of sent NotificationMessages.
|
|
1222
|
+
* NotificationMessages are retained in this queue until they are acknowledged or until they have
|
|
1223
|
+
* been in the queue for a minimum of one keep-alive interval.
|
|
1224
|
+
*
|
|
1225
|
+
*/
|
|
1226
|
+
discardOldSentNotifications() {
|
|
1227
|
+
// Sessions maintain a retransmission queue of sent NotificationMessages. NotificationMessages
|
|
1228
|
+
// are retained in this queue until they are acknowledged. The Session shall maintain a
|
|
1229
|
+
// retransmission queue size of at least two times the number of Publish requests per Session the
|
|
1230
|
+
// Server supports. Clients are required to acknowledge NotificationMessages as they are received. In the
|
|
1231
|
+
// case of a retransmission queue overflow, the oldest sent NotificationMessage gets deleted. If a
|
|
1232
|
+
// Subscription is transferred to another Session, the queued NotificationMessages for this
|
|
1233
|
+
// Subscription are moved from the old to the new Session.
|
|
1234
|
+
if (maxNotificationMessagesInQueue <= this._sent_notification_messages.length) {
|
|
1235
|
+
debugLog("discardOldSentNotifications = ", this._sent_notification_messages.length);
|
|
1236
|
+
this._sent_notification_messages.splice(this._sent_notification_messages.length - maxNotificationMessagesInQueue);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* @param timestampsToReturn
|
|
1241
|
+
* @param monitoredItemCreateRequest
|
|
1242
|
+
* @param node
|
|
1243
|
+
* @private
|
|
1244
|
+
*/
|
|
1245
|
+
_createMonitoredItemStep2(timestampsToReturn, monitoredItemCreateRequest, node) {
|
|
1246
|
+
// note : most of the parameter inconsistencies shall have been handled by the caller
|
|
1247
|
+
// any error here will raise an assert here
|
|
1248
|
+
(0, node_opcua_assert_1.assert)(monitoredItemCreateRequest instanceof node_opcua_service_subscription_2.MonitoredItemCreateRequest);
|
|
1249
|
+
const itemToMonitor = monitoredItemCreateRequest.itemToMonitor;
|
|
1250
|
+
// xx check if attribute Id invalid (we only support Value or EventNotifier )
|
|
1251
|
+
// xx assert(itemToMonitor.attributeId !== AttributeIds.INVALID);
|
|
1252
|
+
this.monitoredItemIdCounter += 1;
|
|
1253
|
+
const monitoredItemId = getNextMonitoredItemId();
|
|
1254
|
+
const requestedParameters = monitoredItemCreateRequest.requestedParameters;
|
|
1255
|
+
// adjust requestedParameters.samplingInterval
|
|
1256
|
+
requestedParameters.samplingInterval = this.adjustSamplingInterval(requestedParameters.samplingInterval, node);
|
|
1257
|
+
// reincorporate monitoredItemId and itemToMonitor into the requestedParameters
|
|
1258
|
+
const options = requestedParameters;
|
|
1259
|
+
options.monitoredItemId = monitoredItemId;
|
|
1260
|
+
options.itemToMonitor = itemToMonitor;
|
|
1261
|
+
const monitoredItem = new monitored_item_1.MonitoredItem(options);
|
|
1262
|
+
monitoredItem.timestampsToReturn = timestampsToReturn;
|
|
1263
|
+
monitoredItem.$subscription = this;
|
|
1264
|
+
(0, node_opcua_assert_1.assert)(monitoredItem.monitoredItemId === monitoredItemId);
|
|
1265
|
+
this.monitoredItems[monitoredItemId] = monitoredItem;
|
|
1266
|
+
this.globalCounter.totalMonitoredItemCount += 1;
|
|
1267
|
+
(0, node_opcua_assert_1.assert)(monitoredItem.clientHandle !== 4294967295);
|
|
1268
|
+
const filterResult = _process_filter(node, requestedParameters.filter);
|
|
1269
|
+
const monitoredItemCreateResult = new node_opcua_service_subscription_2.MonitoredItemCreateResult({
|
|
1270
|
+
filterResult,
|
|
1271
|
+
monitoredItemId,
|
|
1272
|
+
revisedQueueSize: monitoredItem.queueSize,
|
|
1273
|
+
revisedSamplingInterval: monitoredItem.samplingInterval,
|
|
1274
|
+
statusCode: node_opcua_status_code_1.StatusCodes.Good
|
|
1275
|
+
});
|
|
1276
|
+
// this.emit("monitoredItem", monitoredItem, itemToMonitor);
|
|
1277
|
+
return monitoredItemCreateResult;
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
*
|
|
1281
|
+
* @param monitoredItem
|
|
1282
|
+
* @param monitoredItemCreateRequest
|
|
1283
|
+
* @private
|
|
1284
|
+
*/
|
|
1285
|
+
_createMonitoredItemStep3(monitoredItem, monitoredItemCreateRequest) {
|
|
1286
|
+
if (!monitoredItem) {
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
(0, node_opcua_assert_1.assert)(monitoredItem.monitoringMode === node_opcua_service_subscription_2.MonitoringMode.Invalid);
|
|
1290
|
+
(0, node_opcua_assert_1.assert)(typeof monitoredItem.samplingFunc === "function", " expecting a sampling function here");
|
|
1291
|
+
const monitoringMode = monitoredItemCreateRequest.monitoringMode; // Disabled, Sampling, Reporting
|
|
1292
|
+
monitoredItem.setMonitoringMode(monitoringMode);
|
|
1293
|
+
}
|
|
1294
|
+
_harvestMonitoredItems() {
|
|
1295
|
+
for (const monitoredItem of Object.values(this.monitoredItems)) {
|
|
1296
|
+
const notifications_chunks = monitoredItem.extractMonitoredItemNotifications();
|
|
1297
|
+
for (const chunk of notifications_chunks) {
|
|
1298
|
+
this._addNotificationMessage(chunk, monitoredItem.monitoredItemId);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
this._hasUncollectedMonitoredItemNotifications = false;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
exports.Subscription = Subscription;
|
|
1305
|
+
Subscription.minimumPublishingInterval = 50; // fastest possible
|
|
1306
|
+
Subscription.defaultPublishingInterval = 1000; // one second
|
|
1307
|
+
Subscription.maximumPublishingInterval = 1000 * 60 * 60 * 24 * 15; // 15 days
|
|
1308
|
+
Subscription.maxNotificationPerPublishHighLimit = 1000;
|
|
1309
|
+
/**
|
|
1310
|
+
* maximum number of monitored item in a subscription to be used
|
|
1311
|
+
* when serverCapacity.maxMonitoredItems and serverCapacity.maxMonitoredItemsPerSubscription are not set.
|
|
1312
|
+
*/
|
|
1313
|
+
Subscription.defaultMaxMonitoredItemCount = 20000;
|
|
1314
|
+
Subscription.registry = new node_opcua_object_registry_1.ObjectRegistry();
|
|
1315
|
+
/**
|
|
1316
|
+
* extract up to maxNotificationsPerPublish notifications
|
|
1317
|
+
* @param the full array of monitored items
|
|
1318
|
+
* @param maxNotificationsPerPublish the maximum number of notification to extract
|
|
1319
|
+
* @return an extract of array of monitored item matching at most maxNotificationsPerPublish
|
|
1320
|
+
* @private
|
|
1321
|
+
*/
|
|
1322
|
+
function extract_notifications_chunk(monitoredItems, maxNotificationsPerPublish) {
|
|
1323
|
+
let n = maxNotificationsPerPublish === 0 ? monitoredItems.size : Math.min(monitoredItems.size, maxNotificationsPerPublish);
|
|
1324
|
+
const chunk_monitoredItems = [];
|
|
1325
|
+
while (n) {
|
|
1326
|
+
chunk_monitoredItems.push(monitoredItems.shift());
|
|
1327
|
+
n--;
|
|
1328
|
+
}
|
|
1329
|
+
return chunk_monitoredItems;
|
|
1330
|
+
}
|
|
1331
|
+
function filter_instanceof(Class, e) {
|
|
1332
|
+
return e instanceof Class;
|
|
1333
|
+
}
|
|
1334
|
+
(0, node_opcua_assert_1.assert)(Subscription.maximumPublishingInterval < 2147483647, "maximumPublishingInterval cannot exceed (2**31-1) ms ");
|
|
1317
1335
|
//# sourceMappingURL=server_subscription.js.map
|