loupedeck-commander 1.2.1 → 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,283 +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
- 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
- }
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
+ }
283
283
 
@@ -1,47 +1,47 @@
1
- import { exec } from 'child_process'
2
- //import { exec } from 'node:child_process'
3
- import { BaseIf } from './baseif.mjs'
4
-
5
- /**
6
- * Our Special-Handler just used the Default - and adds Vibration after triggers through Button-Releases
7
- */
8
- export class SHELLif extends BaseIf {
9
- async call (cmd, options = {}) {
10
- cmd = super.call(cmd, options)
11
- return await this.sh(cmd)
12
- }
13
-
14
- async stop(){
15
- this.LogInfo("SHELLif: Stopping")
16
- }
17
-
18
- Check(options) {
19
- var res= super.Check(options)
20
- if (res <0)
21
- return res
22
- }
23
-
24
- /**
25
- * Run a Shell command in ASYNC mode
26
- * @param {*} cmd
27
- * @returns
28
- */
29
- async sh (cmd) {
30
- let self = this;
31
- this.LogDebug(`ShellIf: runCmd: ${cmd}\n`)
32
-
33
- return new Promise(function (resolve, reject) {
34
- exec(cmd, (err, stdout, stderr) => {
35
- if (stdout.length>0)
36
- self.LogInfo(`SHELLif Out: ${stdout}`)
37
- if (stderr.length>0)
38
- self.LogError(`SHELLif Err: ${stderr}`)
39
- if (err) {
40
- reject(err)
41
- } else {
42
- resolve({ stdout, stderr })
43
- }
44
- })
45
- })
46
- }
47
- }
1
+ import { exec } from 'child_process'
2
+ //import { exec } from 'node:child_process'
3
+ import { BaseIf } from './baseif.mjs'
4
+
5
+ /**
6
+ * Our Special-Handler just used the Default - and adds Vibration after triggers through Button-Releases
7
+ */
8
+ export class SHELLif extends BaseIf {
9
+ async call (cmd, options = {}) {
10
+ cmd = super.call(cmd, options)
11
+ return await this.sh(cmd)
12
+ }
13
+
14
+ async stop(){
15
+ this.LogInfo("SHELLif: Stopping")
16
+ }
17
+
18
+ Check(options) {
19
+ var res= super.Check(options)
20
+ if (res <0)
21
+ return res
22
+ }
23
+
24
+ /**
25
+ * Run a Shell command in ASYNC mode
26
+ * @param {*} cmd
27
+ * @returns
28
+ */
29
+ async sh (cmd) {
30
+ let self = this;
31
+ this.LogDebug(`ShellIf: runCmd: ${cmd}\n`)
32
+
33
+ return new Promise(function (resolve, reject) {
34
+ exec(cmd, (err, stdout, stderr) => {
35
+ if (stdout.length>0)
36
+ self.LogInfo(`SHELLif Out: ${stdout}`)
37
+ if (stderr.length>0)
38
+ self.LogError(`SHELLif Err: ${stderr}`)
39
+ if (err) {
40
+ reject(err)
41
+ } else {
42
+ resolve({ stdout, stderr })
43
+ }
44
+ })
45
+ })
46
+ }
47
+ }