loupedeck-commander 1.2.3 → 1.2.5

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.
@@ -1,292 +1,297 @@
1
- import {
2
- OPCUAClient,
3
- MessageSecurityMode,
4
- SecurityPolicy,
5
- BrowseDirection,
6
- AttributeIds,
7
- NodeClassMask,
8
- makeBrowsePath,
9
- resolveNodeId,
10
- TimestampsToReturn,
11
- coerceInt32,
12
- coerceByteString,
13
- DataType
14
- } from "node-opcua";
15
-
16
- import { BaseIf } from './baseif.mjs'
17
-
18
- const subscriptionParameters = {
19
- maxNotificationsPerPublish: 1000,
20
- publishingEnabled: true,
21
- requestedLifetimeCount: 100,
22
- requestedMaxKeepAliveCount: 10,
23
- requestedPublishingInterval: 1000
24
- };
25
-
26
-
27
- /**
28
- * Our Special-Handler just used the Default - and adds Vibration after triggers through Button-Releases
29
- */
30
- export class OPCUAIf extends BaseIf {
31
-
32
- #client
33
- #session
34
- #sub
35
- #connected
36
- #endpointurl
37
- monitoreditems
38
- buttons
39
- #callback
40
- constructor() {
41
- super()
42
-
43
- this.LogInfo(`OPCUAIf Constructed`);
44
- }
45
-
46
- async stop(){
47
- if (!this.#client)
48
- return
49
-
50
- this.LogInfo(`OPCUAIf Stopping`)
51
- await this.#client.closeSession(this.#session,true)
52
- await this.#client.disconnect()
53
- this.#connected = false
54
- this.#client = null
55
- this.LogInfo(`OPCUAIf Stopped\n`)
56
- }
57
-
58
- async init( options = {},config = {},callbackFunction){
59
- var res = this.Check(options)
60
- if (res<0){
61
- this.LogError(`OPCUAIf: Missing essential options in dictionary => Quitting $res $options\n`)
62
- }
63
- try{
64
- this.#endpointurl = options.endpointurl
65
- this.#callback = callbackFunction
66
- this.monitoreditems = {}
67
- this.buttons = {}
68
- this.LogInfo(`OPCUAIf init ${this.#endpointurl}\n`);
69
-
70
- await this.Connect(this.#endpointurl);
71
-
72
- let field=config.touch.center
73
- const keys = Object.keys(field)
74
- for (let i = 0; i < keys.length; i++) {
75
- const key = keys[i]
76
- const elem = config.touch.center[key]
77
- if (elem.nodeid){
78
- let format = this.formatString(elem.nodeid,options)
79
- let monitoredItemId = await this.Subscribe(format)
80
- this.buttons[monitoredItemId] = i
81
- }
82
-
83
- }
84
- } catch (error) {
85
- this.LogError(`OPCUAIf: Error $error\n`)
86
- }
87
- }
88
-
89
- async call (opcuaNode, options = {}) {
90
- var res = this.Check(options)
91
- if (res<0){
92
- this.LogError(`OPCUAIf call: Missing essential options in dictionary => Quitting $res\n`)
93
- return false
94
- }
95
-
96
- var nodeId = super.formatString(opcuaNode, options)
97
- var value = super.formatString(options.value, options)
98
-
99
- //this.LogInfo(`OPCUAIf: write ${nodeId} => ${value}\n`)
100
- await this.Write(nodeId,value)
101
-
102
- var NewState = "waiting"
103
- return NewState
104
- }
105
-
106
- Check(options) {
107
- var res= super.Check(options)
108
- if (res <0)
109
- return res
110
- if (!options.endpointurl)
111
- return -11
112
- if (!options.nodeid)
113
- return -12
114
- if (!options.value)
115
- return -13
116
- return 0
117
- }
118
-
119
- async Disconnect() {
120
- if (this.#client){
121
- this.LogInfo(`OPCUAIf: Disconnect\n`);
122
- await this.#client.Disconnect()
123
- this.LogInfo(`OPCUAIf: Disconnected\n`);
124
- }
125
- }
126
- async Connect(url) {
127
- let self = this
128
- this.#client = OPCUAClient.create({
129
- applicationName: "NodeOPCUA-Client",
130
-
131
- endpointMustExist: false,
132
- // keepSessionAlive: true,
133
- requestedSessionTimeout: 60 * 1000,
134
- securityMode: MessageSecurityMode.None,
135
- securityPolicy: SecurityPolicy.None,
136
- connectionStrategy: {
137
- maxRetry: -1,
138
- maxDelay: 5000,
139
- initialDelay: 2500
140
- },
141
-
142
- defaultSecureTokenLifetime: 20000,
143
- tokenRenewalInterval: 1000
144
- });
145
-
146
- this.#client.on("backoff", (retry, delay) => {
147
- if((retry%10) == 0)
148
- self.LogInfo(`OPCUAIf Try Reconnection ${retry} next attempt in ${delay}ms ${self.#endpointurl}\n`);
149
- });
150
-
151
- this.#client.on("connection_lost", () => {
152
- self.LogInfo(`OPCUAIf: Connection lost\n`);
153
- });
154
-
155
- this.#client.on("connection_reestablished", () => {
156
- self.LogInfo(`OPCUAIf: Connection re-established\n`);
157
- });
158
-
159
- this.#client.on("connection_failed", () => {
160
- self.LogInfo(`OPCUAIf: Connection failed\n`);
161
- });
162
- this.#client.on("start_reconnection", () => {
163
- self.LogInfo(`OPCUAIf: Starting reconnection\n`);
164
- });
165
-
166
- this.#client.on("after_reconnection", (err) => {
167
- self.LogInfo(`OPCUAIf: After Reconnection event => ${err}\n`);
168
- });
169
- this.#client.on("security_token_renewed", () => {
170
- self.LogDebug(`OPCUAIf: security_token_renewed\n`);
171
- })
172
- this.#client.on("lifetime_75", (token) => {})
173
-
174
- this.LogInfo(`OPCUAIf: connecting client to ${url}\n`);//, this.#session.toString());
175
- await this.#client.connect(url);
176
-
177
- this.#session = await this.#client.createSession();
178
-
179
- this.#session.on("session_closed", (statusCode) => {
180
- self.LogInfo(`OPCUAIf: Session has been closed\n`);
181
- })
182
- this.#session.on("session_restored", () => {
183
- self.LogInfo(`OPCUAIf: Session has been restored\n`);
184
- });
185
- this.#session.on("keepalive", (lastKnownServerState) => {
186
- self.LogInfo(`OPCUAIf: KeepAlive lastKnownServerState ${lastKnownServerState}\n`);
187
- });
188
- this.#session.on("keepalive_failure", () => {
189
- self.LogInfo(`OPCUAIf: KeepAlive failure\n`);
190
- });
191
-
192
- this.#sub = await this.#session.createSubscription2({
193
- maxNotificationsPerPublish: 9000,
194
- publishingEnabled: true,
195
- requestedLifetimeCount: 10,
196
- requestedMaxKeepAliveCount: 10,
197
- requestedPublishingInterval: 1000
198
- });
199
-
200
- this.LogInfo(`OPCUAIf: session created\n`);//, this.#session.toString());
201
- this.LogInfo(`OPCUAIf: client\n`);
202
- this.LogInfo(`OPCUAIf: subscription\n`);
203
- this.#connected = true
204
- this.#endpointurl = url
205
- }
206
-
207
- async Subscribe(nodeID) {
208
- // install monitored item
209
-
210
- const keys = Object.keys(this.monitoreditems)
211
- for (let i = 0; i < keys.length; i++) {
212
- const key = keys[i]
213
- const elem = this.monitoreditems[key]
214
- if (elem == nodeID){
215
- // already registered => return itemid
216
- return key
217
- }
218
- }
219
-
220
- const itemToMonitor = {
221
- nodeId: resolveNodeId(nodeID),
222
- attributeId: AttributeIds.Value
223
- };
224
- const monitoringParameters = {
225
- samplingInterval: 100,
226
- discardOldest: true,
227
- queueSize: 10
228
- };
229
-
230
- if (!this.#sub){
231
- this.LogError(`OPCUAIf: not register monitored items $itemToMonitor\n`);
232
- return
233
- }
234
- const monitoredItem = await this.#sub.monitor(itemToMonitor, monitoringParameters, TimestampsToReturn.Both);
235
- this.monitoreditems[monitoredItem.monitoredItemId] = nodeID
236
- var self=this
237
- monitoredItem.on("changed", function (dataValue) {
238
- var nodeId = self.monitoreditems[this.monitoredItemId]
239
- var buttonID = self.buttons[this.monitoredItemId]
240
- self.LogInfo(`OPCUAIf: monitored item changed: ${nodeId} => ${dataValue.value.value}\n`);
241
- self.emit('monitored item changed',buttonID,nodeId, dataValue.value.value)
242
- });
243
-
244
- return monitoredItem.monitoredItemId;
245
- }
246
-
247
- async Read(nodeID) {
248
- const nodeToRead = {
249
- nodeId: nodeID,
250
- attributeId: AttributeIds.Value
251
- };
252
- if (!this.#connected){
253
- this.LogError(`OPCUAIf: not connected, cannot read ${nodeID}\n`);
254
- return
255
- }
256
- const dataValue2 = await this.#session.read(nodeToRead, 0);
257
- this.LogError("OPCUAIf: read nodeID ",nodeID, dataValue2.toString(),"\n");
258
- return dataValue2
259
- }
260
-
261
- async Write(nodeID,value,datatype=DataType.String) {
262
- let self = this
263
- if (!this.#connected){
264
- self.LogError("OPCUAIf: not connected, cannot write",nodeID, value,"\n");
265
- return
266
- }
267
- var nodesToWrite = [{
268
- nodeId: nodeID,
269
- attributeId: AttributeIds.Value,
270
- indexRange: null,
271
- value: {
272
- value: {
273
- dataType: datatype,
274
- value: value
275
- }
276
- }
277
- }];
278
- await this.#session.write(nodesToWrite, function(err,statusCodes) {
279
- if (!err) {
280
- if (statusCodes && statusCodes[0].value != 0){
281
- self.LogInfo(`OPCUAIf: status ${statusCodes}\n`);
282
- }else{
283
- self.LogInfo(`OPCUAIf: wrote ${nodeID} => ${value}\n`);
284
- }
285
- }else{
286
- self.LogError("OPCUAIf: write NOT ok",nodeID, value,"\n");
287
- self.LogError(err)
288
- }
289
- });
290
- }
291
- }
1
+ import {
2
+ OPCUAClient,
3
+ MessageSecurityMode,
4
+ SecurityPolicy,
5
+ BrowseDirection,
6
+ AttributeIds,
7
+ NodeClassMask,
8
+ makeBrowsePath,
9
+ resolveNodeId,
10
+ TimestampsToReturn,
11
+ coerceInt32,
12
+ coerceByteString,
13
+ DataType
14
+ } from "node-opcua";
15
+
16
+ import { BaseIf } from './baseif.mjs'
17
+
18
+ const subscriptionParameters = {
19
+ maxNotificationsPerPublish: 1000,
20
+ publishingEnabled: true,
21
+ requestedLifetimeCount: 100,
22
+ requestedMaxKeepAliveCount: 10,
23
+ requestedPublishingInterval: 1000
24
+ };
25
+
26
+
27
+ /**
28
+ * Our Special-Handler just used the Default - and adds Vibration after triggers through Button-Releases
29
+ */
30
+ export class OPCUAIf extends BaseIf {
31
+
32
+ #client
33
+ #session
34
+ #sub
35
+ #connected
36
+ #endpointurl
37
+ monitoreditems
38
+ buttons
39
+ #callback
40
+ constructor() {
41
+ super()
42
+
43
+ this.LogInfo(`OPCUAIf Constructed`);
44
+ }
45
+
46
+ async stop(){
47
+ if (!this.#client)
48
+ return
49
+
50
+ this.LogInfo(`OPCUAIf Stopping\n`)
51
+ await this.#client.closeSession(this.#session,true)
52
+ await this.#client.disconnect()
53
+ this.#connected = false
54
+ this.#client = null
55
+ this.LogInfo(`OPCUAIf Stopped\n`)
56
+ }
57
+
58
+ async init( options = {},config = {},callbackFunction){
59
+ var res = this.Check(options)
60
+ if (res<0){
61
+ this.LogError(`OPCUAIf: Missing essential options in dictionary => Quitting $res $options\n`)
62
+ }
63
+ try{
64
+ this.#endpointurl = this.formatString(options.endpointurl,options)
65
+ this.#callback = callbackFunction
66
+ this.monitoreditems = {}
67
+ this.buttons = {}
68
+ this.LogInfo(`OPCUAIf init ${this.#endpointurl}\n`);
69
+
70
+ await this.Connect(this.#endpointurl);
71
+
72
+ let field=config.touch.center
73
+ const keys = Object.keys(field)
74
+ for (let i = 0; i < keys.length; i++) {
75
+ const key = keys[i]
76
+ const elem = config.touch.center[key]
77
+ if (elem.nodeid){
78
+ let format = this.formatString(elem.nodeid,options)
79
+ let monitoredItemId = await this.Subscribe(format)
80
+ this.buttons[monitoredItemId] = i
81
+ }
82
+
83
+ }
84
+ } catch (error) {
85
+ this.LogError(`OPCUAIf: Error $error\n`)
86
+ }
87
+ }
88
+
89
+ async call (opcuaNode, options = {}) {
90
+ var res = this.Check(options)
91
+ if (res<0){
92
+ // this.LogError(`OPCUAIf call: Missing essential options in dictionary => Quitting $res\n`)
93
+ return false
94
+ }
95
+
96
+ var nodeId = super.formatString(opcuaNode, options)
97
+ var value = super.formatString(options.value, options)
98
+
99
+ //this.LogInfo(`OPCUAIf: write ${nodeId} => ${value}\n`)
100
+ await this.Write(nodeId,value)
101
+
102
+ var NewState = "waiting"
103
+ return NewState
104
+ }
105
+
106
+ Check(options) {
107
+ var res= super.Check(options)
108
+ if (res <0){
109
+ this.LogError(`OPCUAIf: mandatory parameter missing\n`)
110
+ return res
111
+ }
112
+ if (!options.endpointurl){
113
+ this.LogError(`OPCUAIf: mandatory parameter endpointurl missing\n`)
114
+ return -11}
115
+ if (!options.nodeid){
116
+ this.LogError(`OPCUAIf: mandatory parameter nodeid missing\n`)
117
+ return -12}
118
+ if (!options.value){
119
+ this.LogError(`OPCUAIf: mandatory parameter value missing\n`)
120
+ return -13}
121
+ return 0
122
+ }
123
+
124
+ async Disconnect() {
125
+ if (this.#client){
126
+ this.LogInfo(`OPCUAIf: Disconnect\n`);
127
+ await this.#client.Disconnect()
128
+ this.LogInfo(`OPCUAIf: Disconnected\n`);
129
+ }
130
+ }
131
+ async Connect(url) {
132
+ let self = this
133
+ this.#client = OPCUAClient.create({
134
+ applicationName: "NodeOPCUA-Client",
135
+
136
+ endpointMustExist: false,
137
+ // keepSessionAlive: true,
138
+ requestedSessionTimeout: 60 * 1000,
139
+ securityMode: MessageSecurityMode.None,
140
+ securityPolicy: SecurityPolicy.None,
141
+ connectionStrategy: {
142
+ maxRetry: -1,
143
+ maxDelay: 5000,
144
+ initialDelay: 2500
145
+ },
146
+
147
+ defaultSecureTokenLifetime: 20000,
148
+ tokenRenewalInterval: 1000
149
+ });
150
+
151
+ this.#client.on("backoff", (retry, delay) => {
152
+ if((retry%10) == 0)
153
+ self.LogInfo(`OPCUAIf Try Reconnection ${retry} next attempt in ${delay}ms ${self.#endpointurl}\n`);
154
+ });
155
+
156
+ this.#client.on("connection_lost", () => {
157
+ self.LogInfo(`OPCUAIf: Connection lost\n`);
158
+ });
159
+
160
+ this.#client.on("connection_reestablished", () => {
161
+ self.LogInfo(`OPCUAIf: Connection re-established\n`);
162
+ });
163
+
164
+ this.#client.on("connection_failed", () => {
165
+ self.LogInfo(`OPCUAIf: Connection failed\n`);
166
+ });
167
+ this.#client.on("start_reconnection", () => {
168
+ self.LogInfo(`OPCUAIf: Starting reconnection\n`);
169
+ });
170
+
171
+ this.#client.on("after_reconnection", (err) => {
172
+ self.LogInfo(`OPCUAIf: After Reconnection event => ${err}\n`);
173
+ });
174
+ this.#client.on("security_token_renewed", () => {
175
+ self.LogDebug(`OPCUAIf: security_token_renewed\n`);
176
+ })
177
+ this.#client.on("lifetime_75", (token) => {})
178
+
179
+ this.LogInfo(`OPCUAIf: connecting client to ${url}\n`);//, this.#session.toString());
180
+ await this.#client.connect(url);
181
+
182
+ this.#session = await this.#client.createSession();
183
+
184
+ this.#session.on("session_closed", (statusCode) => {
185
+ self.LogInfo(`OPCUAIf: Session has been closed\n`);
186
+ })
187
+ this.#session.on("session_restored", () => {
188
+ self.LogInfo(`OPCUAIf: Session has been restored\n`);
189
+ });
190
+ this.#session.on("keepalive", (lastKnownServerState) => {
191
+ self.LogInfo(`OPCUAIf: KeepAlive lastKnownServerState ${lastKnownServerState}\n`);
192
+ });
193
+ this.#session.on("keepalive_failure", () => {
194
+ self.LogInfo(`OPCUAIf: KeepAlive failure\n`);
195
+ });
196
+
197
+ this.#sub = await this.#session.createSubscription2({
198
+ maxNotificationsPerPublish: 9000,
199
+ publishingEnabled: true,
200
+ requestedLifetimeCount: 10,
201
+ requestedMaxKeepAliveCount: 10,
202
+ requestedPublishingInterval: 1000
203
+ });
204
+
205
+ this.LogInfo(`OPCUAIf: session created\n`);//, this.#session.toString());
206
+ this.LogInfo(`OPCUAIf: client\n`);
207
+ this.LogInfo(`OPCUAIf: subscription\n`);
208
+ this.#connected = true
209
+ this.#endpointurl = url
210
+ }
211
+
212
+ async Subscribe(nodeID) {
213
+ // install monitored item
214
+
215
+ const keys = Object.keys(this.monitoreditems)
216
+ for (let i = 0; i < keys.length; i++) {
217
+ const key = keys[i]
218
+ const elem = this.monitoreditems[key]
219
+ if (elem == nodeID){
220
+ // already registered => return itemid
221
+ return key
222
+ }
223
+ }
224
+
225
+ const itemToMonitor = {
226
+ nodeId: resolveNodeId(nodeID),
227
+ attributeId: AttributeIds.Value
228
+ };
229
+ const monitoringParameters = {
230
+ samplingInterval: 100,
231
+ discardOldest: true,
232
+ queueSize: 10
233
+ };
234
+
235
+ if (!this.#sub){
236
+ this.LogError(`OPCUAIf: not register monitored items $itemToMonitor\n`);
237
+ return
238
+ }
239
+ const monitoredItem = await this.#sub.monitor(itemToMonitor, monitoringParameters, TimestampsToReturn.Both);
240
+ this.monitoreditems[monitoredItem.monitoredItemId] = nodeID
241
+ var self=this
242
+ monitoredItem.on("changed", function (dataValue) {
243
+ var nodeId = self.monitoreditems[this.monitoredItemId]
244
+ var buttonID = self.buttons[this.monitoredItemId]
245
+ self.LogInfo(`OPCUAIf: monitored item changed: ${nodeId} => ${dataValue.value.value}\n`);
246
+ self.emit('monitored item changed',buttonID,nodeId, dataValue.value.value)
247
+ });
248
+
249
+ return monitoredItem.monitoredItemId;
250
+ }
251
+
252
+ async Read(nodeID) {
253
+ const nodeToRead = {
254
+ nodeId: nodeID,
255
+ attributeId: AttributeIds.Value
256
+ };
257
+ if (!this.#connected){
258
+ this.LogError(`OPCUAIf: not connected, cannot read ${nodeID}\n`);
259
+ return
260
+ }
261
+ const dataValue2 = await this.#session.read(nodeToRead, 0);
262
+ this.LogError("OPCUAIf: read nodeID ",nodeID, dataValue2.toString(),"\n");
263
+ return dataValue2
264
+ }
265
+
266
+ async Write(nodeID,value,datatype=DataType.String) {
267
+ let self = this
268
+ if (!this.#connected){
269
+ self.LogError("OPCUAIf: not connected, cannot write",nodeID, value,"\n");
270
+ return
271
+ }
272
+ var nodesToWrite = [{
273
+ nodeId: nodeID,
274
+ attributeId: AttributeIds.Value,
275
+ indexRange: null,
276
+ value: {
277
+ value: {
278
+ dataType: datatype,
279
+ value: value
280
+ }
281
+ }
282
+ }];
283
+ await this.#session.write(nodesToWrite, function(err,statusCodes) {
284
+ if (!err) {
285
+ if (statusCodes && statusCodes[0].value != 0){
286
+ self.LogInfo(`OPCUAIf: error with Node: "${nodeID}", status ${statusCodes[0]}\n`);
287
+ }else{
288
+ self.LogInfo(`OPCUAIf: wrote ${nodeID} => ${value}\n`);
289
+ }
290
+ }else{
291
+ self.LogError("OPCUAIf: write NOT ok",nodeID, value,"\n");
292
+ self.LogError(err)
293
+ }
294
+ });
295
+ }
296
+ }
292
297