-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
168 lines (139 loc) · 5.91 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict'
/**
* Nested namespace settings.
*
* @type {}
*
* @Namespace core
*/
var core = core || { opcClients: [] }
core.opcua = core.opcua || require('node-opcua')
core.opcClients = core.opcClients || {}
const EventEmitter = require('events');
core.eventEmitter = core.eventEmitter || new EventEmitter()
core.createOpcUaClient = function (connectionId, name, authOption) {
// caused the connect method to fail after one single unsuccessful retry
const connectionStrategy = {
initialDelay: 1000,
maxDelay: 10000,
maxRetry: 100
};
const existingClient = core.opcClients[connectionId];
if (existingClient) return existingClient;
const newClient = core.opcua.OPCUAClient.create({
applicationName: name,
keepSessionAlive: true,
keepAliveInterval: 1000,
connectionStrategy: connectionStrategy,
securityMode: authOption.securityMode,
securityPolicy: authOption.securityPolicy,
endpointMustExist: false
});
core.opcClients[connectionId] = newClient;
return newClient;
}
core.connect = async function (connectionId, host, userOption) {
const existingClient = core.opcClients[connectionId];
if (!existingClient) return;
existingClient.on("abort", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.on("close", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.on("connection_reestablished", () => updateClientConnectionStatus(connectionId, "connected"));
existingClient.on("connection_lost", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.on("start_reconnection", () => updateClientConnectionStatus(connectionId, "reconnecting"));
existingClient.on("after_reconnection", () => updateClientConnectionStatus(connectionId, "reconnecting"));
try {
await existingClient.connect(host);
const session = await existingClient.createSession(userOption);
core.eventEmitter.emit('session_created', connectionId);
// session.on("session_closed", (statusCode) => {
// console.log(" Session has been closed with statusCode = ", statusCode.toString());
// })
// session.on("session_restored", () => {
// console.log(" Session has been restored");
// });
// session.on("keepalive", (lastKnownServerState) => {
// console.log("KeepAlive lastKnownServerState", core.opcua.ServerState[lastKnownServerState]);
// });
// session.on("keepalive_failure", () => {
// console.log("KeepAlive failure");
// });
existingClient['session'] = session;
core.createSubscription(connectionId, session);
}
catch (err) {
console.error(err.message);
updateClientConnectionStatus(connectionId, "disconnected");
return;
}
// if all went well, set status connected!
updateClientConnectionStatus(connectionId, "connected");
}
core.close = async function (connectionId) {
let existingClient = core.opcClients[connectionId];
if (!existingClient) return;
try {
await closeSession(existingClient);
// detach all events before destroy client
existingClient.removeListener("abort", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.removeListener("close", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.removeListener("connection_reestablished", () => updateClientConnectionStatus(connectionId, "connected"));
existingClient.removeListener("connection_lost", () => updateClientConnectionStatus(connectionId, "disconnected"));
existingClient.removeListener("start_reconnection", () => updateClientConnectionStatus(connectionId, "reconnecting"));
existingClient.removeListener("after_reconnection", () => updateClientConnectionStatus(connectionId, "reconnecting"));
if (!existingClient.isReconnecting) {
existingClient.disconnect();
}
existingClient = null;
core.opcClients[connectionId] = null;
} catch (err) {
console.error(err.message);
}
}
async function closeSession(client) {
let session = client.session;
if (!session) return;
await core.closeSubscription(session);
await session.close();
session = null;
client.session = null;
}
core.closeSubscription = async function(session){
let subscription = session.subscription;
if(!subscription) return;
await subscription.terminate();
subscription = null;
session.subscription = null;
}
core.createSubscription = function (connectionId, session) {
if (session.subscription) return session.subscription;
const subscriptionOptions = {
requestedPublishingInterval: 1000,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 10,
maxNotificationsPerPublish: 100,
publishingEnabled: true,
priority: 10
};
const subscription = core.opcua.ClientSubscription.create(session, subscriptionOptions);
session['subscription'] = subscription;
core.eventEmitter.emit('subscription_created', connectionId);
return subscription;
}
function updateClientConnectionStatus(connectionId, status) {
const existingClient = core.opcClients[connectionId];
if (!existingClient) return;
switch (status) {
case "connected":
console.debug(existingClient.applicationName + ":client has reconnected");
break;
case "reconnecting":
console.debug(existingClient.applicationName + ":client is trying to reconnect");
break
case "disconnected":
console.debug(existingClient.applicationName + ":client has lost connection");
break;
}
core.eventEmitter.emit('client_connection_state', connectionId, status);
existingClient['clientState'] = status;
}
module.exports = core