forked from systec-electronic/node-red-contrib-ctr700-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctr700_di.js
622 lines (451 loc) · 20.9 KB
/
ctr700_di.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/****************************************************************************
(c) SYSTEC electronic AG, D-08468 Heinsdorfergrund, Am Windrad 2
www.systec-electronic.com
Project: Node-RED Node 'ctr700 di'
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_'
//=======================================================================
// Register node to Node-RED nodes palette
//=======================================================================
RED.nodes.registerType ("ctr700_di", Ctr700_DI_Node);
//=======================================================================
// Node implementation
//=======================================================================
function Ctr700_DI_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_DI_NodeHandler_OnInput);
// register handler for event type 'close'
ThisNode.on ('close', Ctr700_DI_NodeHandler_OnClose);
// register one-time handler for sending the initial value
ThisNode.m_injectImmediate = setImmediate(function()
{
Ctr700_DI_NodeHandler_OnNodesStarted();
});
// run handler for event type 'open'
Ctr700_DI_NodeHandler_OnOpen (NodeConfig_p);
return;
//-------------------------------------------------------------------
// Node event handler [NODE / OPEN]
//-------------------------------------------------------------------
function Ctr700_DI_NodeHandler_OnOpen (NodeConfig_p)
{
var strName;
var strChannel;
var strEdge;
var strActType;
var strActData;
var strInactType;
var strInactData;
var fAltTopic;
var strAltTopic;
ThisNode.m_NodeConfig = NodeConfig_p;
TraceMsg ('{Ctr700_DI_Node} creating...');
// create and initialize members
ThisNode.m_iChannelNumber = -1;
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_DI_Node} new ctr700drv.Ctr700Drv');
ThisNode.m_ObjCtr700Drv = new ctr700drv.Ctr700Drv;
// initialize I/O driver instance
TraceMsg ('{Ctr700_DI_Node} ObjCtr700Drv.init()');
ThisNode.m_ObjCtr700Drv.init();
// get node configuration
strChannel = ThisNode.m_NodeConfig.channel;
strEdge = ThisNode.m_NodeConfig.edge;
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_DI_Node} strChannel: ' + strChannel);
TraceMsg ('{Ctr700_DI_Node} strEdge: ' + strEdge);
TraceMsg ('{Ctr700_DI_Node} strActType ' + strActType);
TraceMsg ('{Ctr700_DI_Node} strActData: ' + strActData);
TraceMsg ('{Ctr700_DI_Node} strInactType: ' + strInactType);
TraceMsg ('{Ctr700_DI_Node} strInactData: ' + strInactData);
TraceMsg ('{Ctr700_DI_Node} fAltTopic: ' + fAltTopic);
TraceMsg ('{Ctr700_DI_Node} strAltTopic: ' + strAltTopic);
TraceMsg ('{Ctr700_DI_Node} strName: ' + strName);
// get input channel number
ThisNode.m_iChannelNumber = Ctr700_DI_GetInputChannelNumber (strChannel);
TraceMsg ('{Ctr700_DI_Node} m_iChannelNumber: ' + ThisNode.m_iChannelNumber);
// get input channel edge
ThisNode.m_strEdge = strEdge;
// 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_DI_BuildTopicString (ThisNode.m_iChannelNumber, fAltTopic, strAltTopic);
TraceMsg ('{Ctr700_DI_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_DI_Node} ObjCtr700Drv.registerInterrupt(' + ThisNode.m_iChannelNumber + ')');
ThisNode.m_ObjCtr700Drv.registerInterrupt(ThisNode.m_iChannelNumber, fRisingEdge, fFallingEdge, Ctr700_DI_CbHandlerInputChannelStateChanged);
// publishing initial input channel state is done in callback function 'Ctr700_DI_NodeHandler_OnNodesStarted()'
return;
}
//-------------------------------------------------------------------
// Node event handler [NODE / CLOSE]
//-------------------------------------------------------------------
function Ctr700_DI_NodeHandler_OnClose ()
{
TraceMsg ('{Ctr700_DI_Node} closing...');
// clear immediate timeout
if (ThisNode.m_injectImmediate)
{
clearImmediate(ThisNode.m_injectImmediate);
}
// unregister callback handler to input channel
TraceMsg ('{Ctr700_DI_Node} ObjCtr700Drv.unregisterInterrupt(' + ThisNode.m_iChannelNumber + ')');
ThisNode.m_ObjCtr700Drv.unregisterInterrupt(ThisNode.m_iChannelNumber);
// shutdown driver instance
TraceMsg ('{Ctr700_DI_Node} ObjCtr700Drv.shutDown()');
ThisNode.m_ObjCtr700Drv.shutDown();
return;
};
//-------------------------------------------------------------------
// Node event handler [NODE / EVENT_INPUT]
//-------------------------------------------------------------------
function Ctr700_DI_NodeHandler_OnInput (Msg_p)
{
TraceMsg ('{Ctr700_DI_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_DI_NodeHandler_OnNodesStarted ()
{
var fChannelValue;
TraceMsg ('{Ctr700_DI_Node} process initial input state...');
// publish initial input channel state
fChannelValue = ThisNode.m_ObjCtr700Drv.getDigitalInput(ThisNode.m_iChannelNumber);
TraceMsg ('{Ctr700_DI_Node} initial state -> fChannelValue: ' + fChannelValue.toString());
Ctr700_DI_PublishInputChannelState (ThisNode.m_iChannelNumber, fChannelValue);
return;
}
//-------------------------------------------------------------------
// Private: Callback handler for input channel state changes
//-------------------------------------------------------------------
function Ctr700_DI_CbHandlerInputChannelStateChanged (iChannelNumber_p, iChannelValue_p)
{
TraceMsg ('{Ctr700_DI_Node} Ctr700_DI_CbHandlerInputChannelStateChanged: iChannelNumber_p=' + iChannelNumber_p + ', iChannelValue_p=' + iChannelValue_p);
Ctr700_DI_PublishInputChannelState (iChannelNumber_p, iChannelValue_p);
return;
}
//-------------------------------------------------------------------
// Private: Publish input channel state
//-------------------------------------------------------------------
function Ctr700_DI_PublishInputChannelState (iChannelNumber_p, iChannelValue_p)
{
var iChannelNumber;
var fChannelValue;
var fEdgeMatch;
var vChannelData;
var Msg = { topic: "", payload: 0 };
TraceMsg ('{Ctr700_DI_Node} Ctr700_DI_PublishInputChannelState: iChannelNumber_p=' + iChannelNumber_p + ', iChannelValue_p=' + iChannelValue_p);
// get node configuration
iChannelNumber = ThisNode.m_iChannelNumber;
TraceMsg ('{Ctr700_DI_Node} iChannelNumber: ' + iChannelNumber);
// check if received channel number matches to configured channel number
if (iChannelNumber_p != iChannelNumber)
{
// mismatch in channel number -> abort processing
TraceMsg ('{Ctr700_DI_Node} ERROR: Channel Mismatch (' + iChannelNumber_p + ' <> ' + iChannelNumber + ') -> Ignore Message');
return;
}
// get input channel value
fChannelValue = iChannelValue_p ? true : false;
// show input channel state in editor (independent from pubishing or supressing message according to edge configurtaion)
if ( fChannelValue )
{
ThisNode.status ({fill:"green", shape:"dot", text:"1"});
}
else
{
ThisNode.status ({fill:"grey", shape:"dot", text:"0"});
}
// check if input state matches to edge configuration
fEdgeMatch = Ctr700_DI_GetEdgeMatch (fChannelValue, ThisNode.m_strEdge);
TraceMsg ('{Ctr700_DI_Node} EdgeMatch(' + fChannelValue + ', ' + ThisNode.m_strEdge + ') -> ' + fEdgeMatch.toString());
if ( !fEdgeMatch )
{
// new input state doesn't matches to configured channel edge -> abort processing
TraceMsg ('{Ctr700_DI_Node} Edge Mismatch -> Ignore input change');
return;
}
// build payload data for message to send
vChannelData = Ctr700_DI_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_DI_Node} SendMsg (topic=' + Msg.topic + ', payload=' + Msg.payload.toString() + ')');
ThisNode.send (Msg);
return;
}
//-------------------------------------------------------------------
// Private: Get input channel number
//-------------------------------------------------------------------
function Ctr700_DI_GetInputChannelNumber (strChannelName_p)
{
var strChannelName;
var strChannelNumber;
var iChannelNumber;
// check if channel name is valid
if ((strChannelName_p == null) || (strChannelName_p.length == 0))
{
ThisNode.error ('{Ctr700_DI_Node} Invalid Output ChannelName (strChannelName_p is null or empty)');
return (-1);
}
// normalize channel name
strChannelName = strChannelName_p.trim();
strChannelName = strChannelName.toUpperCase();
// check for valid prefix
if ( !strChannelName.startsWith('IN_DI') )
{
ThisNode.error ('{Ctr700_DI_Node} Invalid Output ChannelName: ' + strChannelName_p);
return (-1);
}
// get channel number form channel name
strChannelNumber = strChannelName.substr(5);
iChannelNumber = parseInt(strChannelNumber);
if (iChannelNumber === NaN)
{
ThisNode.error ('{Ctr700_DI_Node} Invalid Output Channelumber: ' + strChannelNumber);
return (-1);
}
return (iChannelNumber);
}
//-------------------------------------------------------------------
// Private: Check if input value matches to edge configuration
//-------------------------------------------------------------------
function Ctr700_DI_GetEdgeMatch (fChannelValue_p, strChannelEdge_p)
{
var fEdgeMatch = false;
switch (strChannelEdge_p)
{
case "EDGE_BOTH":
{
fEdgeMatch = true;
break;
}
case "EDGE_RISING":
{
fEdgeMatch = fChannelValue_p ? true : false;
break;
}
case "EDGE_FALLING":
{
fEdgeMatch = fChannelValue_p ? false : true;
break;
}
default:
{
fEdgeMatch = false;
break;
}
}
return (fEdgeMatch);
}
//-------------------------------------------------------------------
// Private: Build payload data for message to send
//-------------------------------------------------------------------
function Ctr700_DI_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_DI_BuildTopicString (iChannelNumber_p, fAltTopic_p, strAltTopic_p)
{
var strTopic;
// select between default and alternative topic
if ( !fAltTopic_p )
{
strTopic = '/di/' + iChannelNumber_p.toString();
}
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_DI_Node (NodeConfig_p)
} // module.exports = function(RED)