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