forked from systec-electronic/node-red-contrib-ctr700-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctr700_switch.js
510 lines (363 loc) · 17.1 KB
/
ctr700_switch.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
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
/****************************************************************************
(c) SYSTEC electronic AG, D-08468 Heinsdorfergrund, Am Windrad 2
www.systec-electronic.com
Project: Node-RED Node 'ctr700 switch'
Description: Node implementation
-------------------------------------------------------------------------
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------------
Revision History:
2018/02/25 -rs: V1.00 Initial version
2019/03/20 -ad: V1.01 Fix handling for initial value
****************************************************************************/
module.exports = function(RED)
{
//*********************************************************************//
// //
// G L O B A L S //
// //
//*********************************************************************//
"use strict";
//=======================================================================
// Import external modules
//=======================================================================
const ctr700drv = require('./ctr700drv.js');
//=======================================================================
// Runtime Configuration
//=======================================================================
// Disable runtime warning: "Possible EventEmitter memory leak detected. 11 nodes-started listeners added. Use emitter.setMaxListeners() to increase limit"
require('events').EventEmitter.defaultMaxListeners = 0;
const TRACE_ENABLE_ALL = false; // Enables traces for all nodes
const TRACE_ENABLE_NODE_NAME_DBG = true; // Enables traces only for nodes which node names starts with 'DBG_'
const RUN_STOP_SW_CHANNEL_NUMBER = 0x80; // virtual input channel for Run/Stop switch
//=======================================================================
// Register node to Node-RED nodes palette
//=======================================================================
RED.nodes.registerType ("ctr700_switch", Ctr700_Switch_Node);
//=======================================================================
// Node implementation
//=======================================================================
function Ctr700_Switch_Node (NodeConfig_p)
{
//-------------------------------------------------------------------
// Node main function
//-------------------------------------------------------------------
let ThisNode = this;
// create new node instance
RED.nodes.createNode (this, NodeConfig_p);
// register handler for event type 'input'
ThisNode.on ('input', Ctr700_Switch_NodeHandler_OnInput);
// register handler for event type 'close'
ThisNode.on ('close', Ctr700_Switch_NodeHandler_OnClose);
// register one-time handler for sending the initial value
ThisNode.m_injectImmediate = setImmediate(function()
{
Ctr700_Switch_NodeHandler_OnNodesStarted();
});
// run handler for event type 'open'
Ctr700_Switch_NodeHandler_OnOpen (NodeConfig_p);
return;
//-------------------------------------------------------------------
// Node event handler [NODE / OPEN]
//-------------------------------------------------------------------
function Ctr700_Switch_NodeHandler_OnOpen (NodeConfig_p)
{
var strName;
var strActType;
var strActData;
var strInactType;
var strInactData;
var fAltTopic;
var strAltTopic;
ThisNode.m_NodeConfig = NodeConfig_p;
TraceMsg ('{Ctr700_Switch_Node} creating...');
// create and initialize members
ThisNode.m_strActType = "";
ThisNode.m_strActData = "";
ThisNode.m_strInactType = "";
ThisNode.m_strInactData = "";
ThisNode.m_strTopic = "";
ThisNode.m_strName = "";
// create I/O driver instance
TraceMsg ('{Ctr700_Switch_Node} new ctr700drv.Ctr700Drv');
ThisNode.m_ObjCtr700Drv = new ctr700drv.Ctr700Drv;
// initialize I/O driver instance
TraceMsg ('{Ctr700_Switch_Node} ObjCtr700Drv.init()');
ThisNode.m_ObjCtr700Drv.init();
// get node configuration
strActType = ThisNode.m_NodeConfig.acttype;
strActData = ThisNode.m_NodeConfig.actdata;
strInactType = ThisNode.m_NodeConfig.inacttype;
strInactData = ThisNode.m_NodeConfig.inactdata;
fAltTopic = ThisNode.m_NodeConfig.optalttopic;
strAltTopic = ThisNode.m_NodeConfig.alttopic;
strName = ThisNode.m_NodeConfig.name;
TraceMsg ('{Ctr700_Switch_Node} strActType ' + strActType);
TraceMsg ('{Ctr700_Switch_Node} strActData: ' + strActData);
TraceMsg ('{Ctr700_Switch_Node} strInactType: ' + strInactType);
TraceMsg ('{Ctr700_Switch_Node} strInactData: ' + strInactData);
TraceMsg ('{Ctr700_Switch_Node} fAltTopic: ' + fAltTopic);
TraceMsg ('{Ctr700_Switch_Node} strAltTopic: ' + strAltTopic);
TraceMsg ('{Ctr700_Switch_Node} strName: ' + strName);
// get payload specification
ThisNode.m_strActType = strActType;
ThisNode.m_strActData = strActData;
ThisNode.m_strInactType = strInactType;
ThisNode.m_strInactData = strInactData;
// get topic for publishing input channel state messages
ThisNode.m_strTopic = Ctr700_Switch_BuildTopicString (fAltTopic, strAltTopic);
TraceMsg ('{Ctr700_Switch_Node} m_strTopic: ' + ThisNode.m_strTopic);
// get node name
ThisNode.m_strName = strName;
// register I/O driver callback handler to input channel
const fRisingEdge = true;
const fFallingEdge = true;
TraceMsg ('{Ctr700_Switch_Node} ObjCtr700Drv.registerInterrupt(' + RUN_STOP_SW_CHANNEL_NUMBER + ')');
ThisNode.m_ObjCtr700Drv.registerInterrupt(RUN_STOP_SW_CHANNEL_NUMBER, fRisingEdge, fFallingEdge, Ctr700_Switch_CbHandlerInputChannelStateChanged);
// publishing initial input channel state is done in callback function 'Ctr700_Switch_NodeHandler_OnNodesStarted()'
return;
}
//-------------------------------------------------------------------
// Node event handler [NODE / CLOSE]
//-------------------------------------------------------------------
function Ctr700_Switch_NodeHandler_OnClose ()
{
TraceMsg ('{Ctr700_Switch_Node} closing...');
// clear immediate timeout
if (ThisNode.m_injectImmediate)
{
clearImmediate(ThisNode.m_injectImmediate);
}
// unregister callback handler to input channel
TraceMsg ('{Ctr700_Switch_Node} ObjCtr700Drv.unregisterInterrupt(' + RUN_STOP_SW_CHANNEL_NUMBER + ')');
ThisNode.m_ObjCtr700Drv.unregisterInterrupt(RUN_STOP_SW_CHANNEL_NUMBER);
// shutdown driver instance
TraceMsg ('{Ctr700_Switch_Node} ObjCtr700Drv.shutDown()');
ThisNode.m_ObjCtr700Drv.shutDown();
return;
};
//-------------------------------------------------------------------
// Node event handler [NODE / EVENT_INPUT]
//-------------------------------------------------------------------
function Ctr700_Switch_NodeHandler_OnInput (Msg_p)
{
TraceMsg ('{Ctr700_Switch_Node} procesing input message...');
// This node is an input node and therefore it does not process any messages
return;
};
//-------------------------------------------------------------------
// Node event handler [EVENTS / NODE_STARTED]
//-------------------------------------------------------------------
function Ctr700_Switch_NodeHandler_OnNodesStarted ()
{
var fChannelValue;
TraceMsg ('{Ctr700_Switch_Node} process initial input state...');
// publish initial input channel state
TraceMsg ('{Ctr700_Switch_Node} ObjCtr700Drv.getRunSwitch()');
fChannelValue = ThisNode.m_ObjCtr700Drv.getRunSwitch();
TraceMsg ('{Ctr700_Switch_Node} initial state -> fChannelValue: ' + fChannelValue.toString());
Ctr700_Switch_PublishInputChannelState (fChannelValue);
return;
}
//-------------------------------------------------------------------
// Private: Callback handler for input channel state changes
//-------------------------------------------------------------------
function Ctr700_Switch_CbHandlerInputChannelStateChanged (iChannelNumber_p, iChannelValue_p)
{
TraceMsg ('{Ctr700_Switch_Node} Ctr700_Switch_CbHandlerInputChannelStateChanged: iChannelNumber_p=' + iChannelNumber_p + ', iChannelValue_p=' + iChannelValue_p);
// check if received channel number matches to configured channel number
if (iChannelNumber_p != RUN_STOP_SW_CHANNEL_NUMBER)
{
// mismatch in channel number -> abort processing
TraceMsg ('{Ctr700_Switch_Node} ERROR: Channel Mismatch (' + iChannelNumber_p + ' <> ' + RUN_STOP_SW_CHANNEL_NUMBER + ') -> Ignore Message');
return;
}
Ctr700_Switch_PublishInputChannelState (iChannelValue_p);
return;
}
//-------------------------------------------------------------------
// Private: Publish input channel state
//-------------------------------------------------------------------
function Ctr700_Switch_PublishInputChannelState (iChannelValue_p)
{
var fChannelValue;
var vChannelData;
var Msg = { topic: "", payload: 0 };
TraceMsg ('{Ctr700_Switch_Node} Ctr700_Switch_PublishInputChannelState: iChannelValue_p=' + iChannelValue_p);
// get input channel value
fChannelValue = iChannelValue_p ? true : false;
// show input channel state in editor
if ( fChannelValue )
{
ThisNode.status ({fill:"green", shape:"dot", text:"Run"});
}
else
{
ThisNode.status ({fill:"grey", shape:"ring", text:"Stop"});
}
// build payload data for message to send
vChannelData = Ctr700_Switch_BuildPayloadData (fChannelValue, ThisNode.m_strActType, ThisNode.m_strActData, ThisNode.m_strInactType, ThisNode.m_strInactData);
// send message
Msg.topic = ThisNode.m_strTopic;
Msg.payload = vChannelData;
TraceMsg ('{Ctr700_Switch_Node} SendMsg (topic=' + Msg.topic + ', payload=' + Msg.payload.toString() + ')');
ThisNode.send (Msg);
return;
}
//-------------------------------------------------------------------
// Private: Build payload data for message to send
//-------------------------------------------------------------------
function Ctr700_Switch_BuildPayloadData (fChannelValue_p, strActType_p, strActData_p, strInactType_p, strInactData_p)
{
var strData;
var vChannelData;
if ( fChannelValue_p )
{
// -------- Active --------
strData = strActData_p.toLowerCase();
switch (strActType_p)
{
case "bool":
{
vChannelData = (strData == "true") ? true : false;
break;
}
case "num":
{
vChannelData = parseInt (strData, 10);
if (vChannelData == NaN)
{
vChannelData = 0;
}
break;
}
case "str":
{
vChannelData = strData;
break;
}
}
}
else
{
// -------- Inactive --------
strData = strInactData_p.toLowerCase();
switch (strInactType_p)
{
case "bool":
{
vChannelData = (strData == "true") ? true : false;
break;
}
case "num":
{
vChannelData = parseInt (strData, 10);
if (vChannelData == NaN)
{
vChannelData = 0;
}
break;
}
case "str":
{
vChannelData = strData;
break;
}
}
}
return (vChannelData);
}
//-------------------------------------------------------------------
// Private: Build topic string for message to send
//-------------------------------------------------------------------
function Ctr700_Switch_BuildTopicString (fAltTopic_p, strAltTopic_p)
{
var strTopic;
// select between default and alternative topic
if ( !fAltTopic_p )
{
strTopic = '/switch';
}
else
{
strTopic = strAltTopic_p;
}
// check if topic is valid
if (strTopic == null)
{
strTopic = "";
}
// normalize topic
strTopic = strTopic.trim();
strTopic = strTopic.toLowerCase();
// prevent empty topic
if (strTopic.length == 0)
{
strTopic = 'undefined';
}
return (strTopic);
}
//-------------------------------------------------------------------
// Private: Trace logging message
//-------------------------------------------------------------------
function TraceMsg (strTraceMsg_p, strNodeName_p)
{
var fEnable = false;
var strNodeName;
// check if any enable option is set
if ( TRACE_ENABLE_ALL )
{
// enable all -> no additional checks necessary
fEnable = true;
}
else if ( TRACE_ENABLE_NODE_NAME_DBG )
{
// check if node name is given as runtime parameter to this function
strNodeName = "";
if (strNodeName_p !== undefined)
{
// reuse node name given as parameter
strNodeName = strNodeName_p;
}
else
{
// try to get node name from node configuration
// -> evaluate first if 'ThisNode.m_NodeConfig.name' is valid
if (ThisNode.m_NodeConfig !== undefined)
{
if (ThisNode.m_NodeConfig != null)
{
if ( ThisNode.m_NodeConfig.hasOwnProperty('name') )
{
if (ThisNode.m_NodeConfig.name != undefined)
{
if (ThisNode.m_NodeConfig.name != null)
{
strNodeName = ThisNode.m_NodeConfig.name;
}
}
}
}
}
}
if (strNodeName.substr(0,4) == 'DBG_')
{
fEnable = true;
}
}
if ( fEnable )
{
ThisNode.log (strTraceMsg_p);
}
return;
}
} // function Ctr700_Switch_Node (NodeConfig_p)
} // module.exports = function(RED)