forked from Supergiovane/node-red-contrib-knx-ultimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knxUltimate-config.js
677 lines (593 loc) · 35 KB
/
knxUltimate-config.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
const knx = require('knx')
const dptlib = require('knx/src/dptlib')
const oOS = require('os')
//Helpers
sortBy = (field) => (a, b) => {
if (a[field] > b[field]) { return 1 } else { return -1 }
}
onlyDptKeys = (kv) => {
return kv[0].startsWith("DPT")
}
extractBaseNo = (kv) => {
return {
subtypes: kv[1].subtypes,
base: parseInt(kv[1].id.replace("DPT", ""))
}
}
convertSubtype = (baseType) => (kv) => {
let value = `${baseType.base}.${kv[0]}`
return {
value: value
, text: value + ` (${kv[1].name})`
}
}
toConcattedSubtypes = (acc, baseType) => {
let subtypes =
Object.entries(baseType.subtypes)
.sort(sortBy(0))
.map(convertSubtype(baseType))
return acc.concat(subtypes)
}
module.exports = (RED) => {
RED.httpAdmin.get("/knxUltimateDpts", RED.auth.needsPermission('knxUltimate-config.read'), function (req, res) {
const dpts =
Object.entries(dptlib)
.filter(onlyDptKeys)
.map(extractBaseNo)
.sort(sortBy("base"))
.reduce(toConcattedSubtypes, [])
res.json(dpts)
})
function knxUltimateConfigNode(config) {
RED.nodes.createNode(this, config)
var node = this
node.host = config.host
node.port = config.port
node.physAddr = config.physAddr || "15.15.22"; // the KNX physical address we'd like to use
node.suppressACKRequest = typeof config.suppressACKRequest ==="undefined" ? false:config.suppressACKRequest; // enable this option to suppress the acknowledge flag with outgoing L_Data.req requests. LoxOne needs this
node.csv = readCSV(config.csv); // Array from ETS CSV Group Addresses
node.linkStatus = "disconnected";
node.nodeClients = [] // Stores the registered clients
node.KNXEthInterface = typeof config.KNXEthInterface === "undefined" ? "Auto" : config.KNXEthInterface;
node.KNXEthInterfaceManuallyInput = typeof config.KNXEthInterfaceManuallyInput === "undefined" ? "" : config.KNXEthInterfaceManuallyInput; // If you manually set the interface name, it will be wrote here
node.statusDisplayLastUpdate = config.statusDisplayLastUpdate || true;
node.statusDisplayDeviceNameWhenALL = config.statusDisplayDeviceNameWhenALL || false;
node.statusDisplayDataPoint = config.statusDisplayDataPoint || false;
// Endpoint for reading csv from the other nodes
RED.httpAdmin.get("/knxUltimatecsv", RED.auth.needsPermission('knxUltimate-config.read'), function (req, res) {
res.json(RED.nodes.getNode(node.id).csv);
});
// 14/08/2019 Endpoint for retrieving the ethernet interfaces
RED.httpAdmin.get("/knxUltimateETHInterfaces", RED.auth.needsPermission('knxUltimate-config.read'), function (req, res) {
var oiFaces = oOS.networkInterfaces();
var jListInterfaces = [];
try {
Object.keys(oiFaces).forEach(ifname => {
// Interface with single IP
if (Object.keys(oiFaces[ifname]).length === 1) {
if (Object.keys(oiFaces[ifname])[0].internal == false) jListInterfaces.push({ name: ifname, address:Object.keys(oiFaces[ifname])[0].address});
} else {
var sAddresses = "";
oiFaces[ifname].forEach(function (iface) {
if (iface.internal == false) sAddresses += "+" + iface.address;
});
if (sAddresses!=="") jListInterfaces.push({ name: ifname, address:sAddresses});
}
})
} catch (error) {}
res.json(jListInterfaces)
});
node.setAllClientsStatus = (_status, _color, _text) => {
function nextStatus(oClient) {
oClient.setNodeStatus({ fill: _color, shape: "dot", text: _status + " " + _text ,payload: "", GA: oClient.topic, dpt:"", devicename:""})
}
node.nodeClients.map(nextStatus);
}
node.Disconnect = () => {
node.setAllClientsStatus("Waiting", "grey", "")
// Remove listener
try {
node.knxConnection.removeListener("event");
} catch (error) {
}
try {
node.knxConnection.off("event");
} catch (error) {
}
node.linkStatus = "disconnected"; // 29/08/2019 signal disconnection
try {
node.knxConnection.Disconnect();
} catch (error) {
}
node.knxConnection = null;
}
node.addClient = (_Node) => {
// Check if node already exists
if (node.nodeClients.filter(x => x.id === _Node.id).length === 0) {
// Check if the node has a valid topic and dpt
if (_Node.listenallga==false) {
if (typeof _Node.topic == "undefined" || typeof _Node.dpt == "undefined") {
_Node.setNodeStatus({ fill: "red", shape: "dot", text: "Empty group address (topic) or datapoint.",payload: "", GA: "", dpt:"", devicename:"" })
return;
} else {
// Topic must be in formar x/x/x
if (_Node.topic.split("\/").length < 3) {
_Node.setNodeStatus({ fill: "red", shape: "dot", text: "Wrong group address (topic: " + _Node.topic + ") format.",payload: "", GA: "", dpt:"", devicename:"" })
return;
}
}
}
// Add _Node to the clients array
node.nodeClients.push(_Node)
}
// At first node client connection, this node connects to the bus
if (node.nodeClients.length === 1) {
// 14/08/2018 Initialize the connection
node.initKNXConnection();
}
if (_Node.initialread) {
node.readValue(_Node.topic);
}
}
node.removeClient = (_Node) => {
// Remove the client node from the clients array
//RED.log.info( "BEFORE Node " + _Node.id + " has been unsubscribed from receiving KNX messages. " + node.nodeClients.length);
try {
node.nodeClients = node.nodeClients.filter(x => x.id !== _Node.id)
} catch (error) {}
//RED.log.info("AFTER Node " + _Node.id + " has been unsubscribed from receiving KNX messages. " + node.nodeClients.length);
// If no clien nodes, disconnect from bus.
if (node.nodeClients.length === 0) {
node.linkStatus = "disconnected";
node.Disconnect();
}
}
node.readInitialValues = () => {
if (node.linkStatus !== "connected") return; // 29/08/2019 If not connected, exit
if (node.knxConnection) {
var readHistory = [];
let delay = 0;
node.nodeClients
.filter(oClient => oClient.initialread)
.forEach(oClient => {
if (oClient.listenallga==true) {
delay = delay + 200
for (let index = 0; index < node.csv.length; index++) {
const element = node.csv[index];
if (readHistory.includes(element.ga)) return
setTimeout(() => node.readValue(element.ga), delay)
readHistory.push(element.ga)
}
} else {
if (readHistory.includes(oClient.topic)) return
setTimeout(() => node.readValue(oClient.topic), delay)
delay = delay + 200
readHistory.push(oClient.topic)
}
})
}
}
node.readValue = topic => {
if (node.linkStatus !== "connected") return; // 29/08/2019 If not connected, exit
if (node.knxConnection) {
try {
node.knxConnection.read(topic)
} catch (error) {
RED.log.error('knxUltimate: readValue: (' + topic + ') ' + error);
}
}
}
node.initKNXConnection = () => {
node.Disconnect();
node.setAllClientsStatus("Waiting", "grey", "")
if (node.KNXEthInterface !== "Auto")
{
var sIfaceName = "";
if (node.KNXEthInterface === "Manual") {
sIfaceName = node.KNXEthInterfaceManuallyInput;
RED.log.info("knxUltimate: Bind KNX Bus to interface : " + sIfaceName + " (Interface's name entered by hand)");
} else
{
sIfaceName = node.KNXEthInterface;
RED.log.info("knxUltimate: Bind KNX Bus to interface : " + sIfaceName + " (Interface's name selected from dropdown list)");
}
node.knxConnection = new knx.Connection({
ipAddr: node.host,
ipPort: node.port,
physAddr: node.physAddr, // the KNX physical address we'd like to use
interface: sIfaceName,
suppress_ack_ldatareq: node.suppressACKRequest,
handlers: {
connected: () => {
node.linkStatus = "connected";
node.setAllClientsStatus("Connected", "green", "Waiting for telegram.")
node.readInitialValues() // Perform initial read if applicable
},
error: function (connstatus) {
// NO_ERROR: 0x00, // E_NO_ERROR - The connection was established succesfully
// E_HOST_PROTOCOL_TYPE: 0x01,
// E_VERSION_NOT_SUPPORTED: 0x02,
// E_SEQUENCE_NUMBER: 0x04,
// E_CONNSTATE_LOST: 0x15, // typo in eibd/libserver/eibnetserver.cpp:394, forgot 0x prefix ??? "uchar res = 21;"
// E_CONNECTION_ID: 0x21, // - The KNXnet/IP server device could not find an active data connection with the given ID
// E_CONNECTION_TYPE: 0x22, // - The requested connection type is not supported by the KNXnet/IP server device
// E_CONNECTION_OPTION: 0x23, // - The requested connection options is not supported by the KNXnet/IP server device
// E_NO_MORE_CONNECTIONS: 0x24, // - The KNXnet/IP server could not accept the new data connection (Maximum reached)
// E_DATA_CONNECTION: 0x26,// - The KNXnet/IP server device detected an erro concerning the Dat connection with the given ID
// E_KNX_CONNECTION: 0x27, // - The KNXnet/IP server device detected an error concerning the KNX Bus with the given ID
// E_TUNNELING_LAYER: 0x29,
node.linkStatus = "disconnected";
if (connstatus == "E_KNX_CONNECTION") {
setTimeout(() => node.setAllClientsStatus(connstatus, "red", "Error on KNX BUS. Check KNX red/black connector and cable."), 2000)
RED.log.error("knxUltimate: Bind KNX Bus to interface error: " + connstatus);
} else {
setTimeout(() => node.setAllClientsStatus(connstatus, "red", "Error"), 2000)
RED.log.error("knxUltimate: knxConnection error: " + connstatus);
}
}
}
})
} else {
RED.log.info("knxUltimate: Bind KNX Bus to interface (Auto)");
node.knxConnection = new knx.Connection({
ipAddr: node.host,
ipPort: node.port,
physAddr: node.physAddr, // the KNX physical address we'd like to use
suppress_ack_ldatareq: node.suppressACKRequest,
handlers: {
connected: () => {
node.linkStatus = "connected";
node.setAllClientsStatus("Connected", "green", "Waiting for telegram.")
node.readInitialValues() // Perform initial read if applicable
},
error: function (connstatus) {
// NO_ERROR: 0x00, // E_NO_ERROR - The connection was established succesfully
// E_HOST_PROTOCOL_TYPE: 0x01,
// E_VERSION_NOT_SUPPORTED: 0x02,
// E_SEQUENCE_NUMBER: 0x04,
// E_CONNSTATE_LOST: 0x15, // typo in eibd/libserver/eibnetserver.cpp:394, forgot 0x prefix ??? "uchar res = 21;"
// E_CONNECTION_ID: 0x21, // - The KNXnet/IP server device could not find an active data connection with the given ID
// E_CONNECTION_TYPE: 0x22, // - The requested connection type is not supported by the KNXnet/IP server device
// E_CONNECTION_OPTION: 0x23, // - The requested connection options is not supported by the KNXnet/IP server device
// E_NO_MORE_CONNECTIONS: 0x24, // - The KNXnet/IP server could not accept the new data connection (Maximum reached)
// E_DATA_CONNECTION: 0x26,// - The KNXnet/IP server device detected an erro concerning the Dat connection with the given ID
// E_KNX_CONNECTION: 0x27, // - The KNXnet/IP server device detected an error concerning the KNX Bus with the given ID
// E_TUNNELING_LAYER: 0x29,
node.linkStatus = "disconnected";
if (connstatus == "E_KNX_CONNECTION") {
setTimeout(() => node.setAllClientsStatus(connstatus, "red", "Error on KNX BUS. Check KNX red/black connector and cable."), 2000)
RED.log.error("knxUltimate: Bind KNX Bus to interface error: " + connstatus);
} else {
setTimeout(() => node.setAllClientsStatus(connstatus, "red", "Error"), 2000)
RED.log.error("knxUltimate: knxConnection error: " + connstatus);
}
}
}
})
}
// Handle BUS events
node.knxConnection.on("event", function (evt, src, dest, rawValue) {
// if (dest == "0/0/50") RED.log.error("RX FROM BUS : " + src + " " + dest + " " + evt + rawValue);
// if (dest == "0/0/50") {
// node.nodeClients.filter(input => input.notifywrite == true).forEach(input => {
// RED.log.error("ID=" + input.id + " " + input.topic + " dest=" + dest + " notifywrite=" + input.notifywrite + " listenallga="+input.listenallga);
// });
// }
switch (evt) {
case "GroupValue_Write": {
node.nodeClients
.filter(input => input.notifywrite == true)
.forEach(input => {
if (input.listenallga == true) {
// Get the GA from CVS
let oGA;
try {
oGA=node.csv.filter(sga => sga.ga == dest)[0];
} catch (error) { }
// 25/10/2019 TRY TO AUTO DECODE
// --------------------------------
if (typeof oGA === "undefined") {
// 25/10/2019 from v. 1.1.11, try to decode and output a datapoint.
let msg = buildInputMessage(src, dest, evt, rawValue, tryToFigureOutDataPointFromRawValue(rawValue,dest), "")
input.setNodeStatus({ fill: "green", shape: "dot", text: "Try to decode",payload: msg.payload, GA: msg.knx.destination, dpt:"", devicename:"" });
input.send(msg)
// --------------------------------
} else {
let msg = buildInputMessage(src, dest, evt, rawValue, oGA.dpt, oGA.devicename)
input.setNodeStatus({ fill: "green", shape: "dot", text: "",payload: msg.payload, GA: msg.knx.destination, dpt:msg.knx.dpt, devicename:msg.devicename });
input.send(msg)
}
} else if (input.topic == dest) {
let msg = buildInputMessage(src, dest, evt, rawValue, input.dpt, input.name ? input.name : "")
// Check RBE INPUT from KNX Bus, to avoid send the payload to the flow, if it's equal to the current payload
if (!checkRBEInputFromKNXBusAllowSend(input, msg.payload)) {
input.setNodeStatus({fill: "grey", shape: "ring", text: "rbe block ("+msg.payload+") from KNX",payload: "", GA: "", dpt:"", devicename:""})
return;
};
input.currentPayload = msg.payload;// Set the current value for the RBE input
input.setNodeStatus({fill: "green", shape: "dot", text: "", payload: msg.payload, GA: input.topic, dpt:input.dpt, devicename:""});
//RED.log.error("RX FROM BUS : " + input.id +" " + src + " " + dest + " " + evt)
input.send(msg)
}
})
break;
}
case "GroupValue_Response": {
node.nodeClients
.filter(input => input.notifyresponse==true)
.forEach(input => {
if (input.listenallga==true) {
// Get the DPT
let oGA;
try {
oGA=node.csv.filter(sga => sga.ga == dest)[0];
} catch (error) { }
// 25/10/2019 TRY TO AUTO DECODE
// --------------------------------
if (typeof oGA === "undefined") {
let msg = buildInputMessage(src, dest, evt, rawValue, tryToFigureOutDataPointFromRawValue(rawValue,dest), "")
input.setNodeStatus({ fill: "green", shape: "dot", text: "Try to decode",payload: msg.payload, GA: msg.knx.destination, dpt:"", devicename:"" });
input.send(msg)
// --------------------------------
} else {
let msg = buildInputMessage(src, dest, evt, rawValue, oGA.dpt, oGA.devicename)
input.setNodeStatus({ fill: "blue", shape: "dot", text: "", payload: msg.payload, GA: msg.knx.destination, dpt: msg.knx.dpt, devicename: msg.devicename });
input.send(msg)
}
} else if (input.topic == dest) {
let msg = buildInputMessage(src, dest, evt, rawValue, input.dpt, input.name ? input.name : "")
// Check RBE INPUT from KNX Bus, to avoid send the payload to the flow, if it's equal to the current payload
if (!checkRBEInputFromKNXBusAllowSend(input, msg.payload)) {
input.setNodeStatus({ fill: "grey", shape: "ring", text: "rbe INPUT filter applied on " + msg.payload })
return;
};
input.currentPayload = msg.payload; // Set the current value for the RBE input
input.setNodeStatus({ fill: "blue", shape: "dot", text: "", payload: msg.payload, GA: input.topic, dpt:msg.knx.dpt, devicename:msg.devicename });
input.send(msg)
}
})
break;
}
case "GroupValue_Read": {
node.nodeClients
.filter(input => input.notifyreadrequest==true)
.forEach(input => {
if (input.listenallga==true) {
// Get the DPT
let oGA;
try {
oGA=node.csv.filter(sga => sga.ga == dest)[0];
} catch (error) { }
// 25/10/2019 TRY TO AUTO DECODE
// --------------------------------
if (typeof oGA === "undefined") {
// 25/10/2019 from v. 1.1.11, try to decode and output a datapoint.
let msg = buildInputMessage(src, dest, evt, null, tryToFigureOutDataPointFromRawValue(rawValue,dest), "")
input.setNodeStatus({ fill: "green", shape: "dot", text: "Try to decode",payload: msg.payload, GA: msg.knx.destination, dpt:"", devicename:"" });
input.send(msg)
// --------------------------------
} else {
let msg = buildInputMessage(src, dest, evt, null, oGA.dpt, oGA.devicename)
input.setNodeStatus({ fill: "grey", shape: "dot", text: "Read", payload: msg.payload, GA: msg.knx.destination, dpt: msg.knx.dpt, devicename: msg.devicename });
input.send(msg)
}
} else if (input.topic == dest) {
let msg = buildInputMessage(src, dest, evt, null, input.dpt, input.name ? input.name : "")
// 24/09/2019 Autorespond to BUS
if (input.notifyreadrequestalsorespondtobus===true) {
if (typeof input.currentPayload === "undefined" || input.currentPayload === "") {
setTimeout(() => {
node.knxConnection.respond(dest, input.notifyreadrequestalsorespondtobusdefaultvalueifnotinitialized, input.dpt);
input.setNodeStatus({ fill: "blue", shape: "ring", text: "Read & Autorespond with default", payload: input.notifyreadrequestalsorespondtobusdefaultvalueifnotinitialized, GA: input.topic, dpt: msg.knx.dpt, devicename: "" });
}, 200);
} else {
setTimeout(() => {
node.knxConnection.respond(dest, input.currentPayload, input.dpt);
input.setNodeStatus({ fill: "blue", shape: "ring", text: "Read & Autorespond", payload: input.currentPayload, GA: input.topic, dpt: msg.knx.dpt, devicename: "" });
}, 200);
}
} else {
input.setNodeStatus({ fill: "grey", shape: "dot", text: "Read", payload: msg.payload, GA: input.topic , dpt:msg.knx.dpt, devicename:"" });
}
input.send(msg)
}
})
break;
}
default: return
}
})
}
// 26/10/2019 Try to figure out the datapoint type from raw value
function tryToFigureOutDataPointFromRawValue(_rawValue,dest) {
// 25/10/2019 Try some Datapoints
if (_rawValue.length == 1) {
if (_rawValue[0].toString() == "0" || _rawValue[0].toString() == "1") {
return "1.001"; // True/False?
} else {
return "5.001"; // Absolute Brightness ?
}
} else if (_rawValue.length == 4) {
return "14.056"; // Watt ?
} else if (_rawValue.length == 2) {
return "9.001";
} else if (_rawValue.length == 14) {
return "16.001"; // Text ?
} else {
// Dont' know, try until no errors
let dpts =
Object.entries(dptlib)
.filter(onlyDptKeys)
.map(extractBaseNo)
.sort(sortBy("base"))
.reduce(toConcattedSubtypes, []);
for (let index = 0; index < dpts.length; index++) {
const element = dpts[index];
try {
//dpt.value)
//dpt.text))
var dpt = dptlib.resolve(element.value);
if (typeof dpt !== "undefined")
{
var jsValue = dptlib.fromBuffer(_rawValue, dpt)
if (typeof jsValue !== "undefined") {
//RED.log.info("Trying for " + dest + ". FOUND " + element.value);
return element.value;
}
}
} catch (error) {
}
}
}
}
// 14/08/2019 If the node has payload same as the received telegram, return false
checkRBEInputFromKNXBusAllowSend = (_node, _KNXTelegramPayload) => {
if (_node.inputRBE !== true) return true;
if (typeof _node.currentPayload === "undefined") return true;
var curVal = _node.currentPayload.toString().toLowerCase();
var newVal = _KNXTelegramPayload.toString().toLowerCase();
if (curVal==="false") {
curVal = "0";
}
if (curVal==="true") {
curVal = "1";
}
if (newVal==="false") {
newVal = "0";
}
if (newVal==="true") {
newVal = "1";
}
if (curVal === newVal) {
return false;
}
return true;
}
function buildInputMessage(src, dest, evt, value, inputDpt, _devicename) {
// Resolve DPT and convert value if available
//if (dest=="0/0/50") RED.log.error("Buildinputmessage src=" + src + " dest" + dest + " value=" + value + " inputDpt=" + inputDpt + " _devicename="+_devicename);
var dpt = dptlib.resolve(inputDpt)
var jsValue = null
if (dpt && value) {
var jsValue = dptlib.fromBuffer(value, dpt)
}
// Build final input message object
return {
topic: dest
, payload: jsValue
, knx:
{
event: evt
, dpt: inputDpt
//, dptDetails: dpt
, source: src
, destination: dest
, rawValue: value
}
, devicename: (typeof _devicename !== 'undefined') ? _devicename : ""
}
}
node.on("close", function () {
node.Disconnect();
})
function readCSV(_csvText) {
var ajsonOutput = new Array(); // Array: qui va l'output totale con i nodi per node-red
if (_csvText == "") {
RED.log.info('knxUltimate: no csv ETS found');
return;
} else {
RED.log.info('knxUltimate: csv ETS found !');
// 23/08/2019 Delete inwanted CRLF in the GA description
let sTemp = correctCRLFInCSV(_csvText);
// Read and decode the CSV in an Array containing: "group address", "DPT", "Device Name"
let fileGA = sTemp.split("\n");
// Controllo se le righe dei gruppi contengono il separatore di tabulazione
if (fileGA[0].search("\t") == -1) {
RED.log.error('knxUltimate: ERROR: the csv ETS file must have the tabulation as separator')
return;
}
var sFirstGroupName = "";
var sSecondGroupName = "";
var sFather="";
for (let index = 0; index < fileGA.length; index++) {
var element = fileGA[index];
element = element.replace(/\"/g, ""); // Rimuovo le virgolette
if (element !== "") {
// Main and secondary group names
if ((element.split("\t")[1].match(/-/g) || []).length == 2) {
// Found main group family name (Example Light Actuators)
sFirstGroupName = element.split("\t")[0] || "";
sSecondGroupName = "";
}
if ((element.split("\t")[1].match(/-/g)||[]).length==1) {
// Found second group family name (Example First Floor light)
sSecondGroupName = element.split("\t")[0] || "";
}
if(sFirstGroupName!=="" && sSecondGroupName !==""){sFather="(" + sFirstGroupName + "->" +sSecondGroupName + ") " }
if (element.split("\t")[1].search("-") == -1 && element.split("\t")[1].search("/") !== -1) {
// Ho trovato una riga contenente un GA valido, cioè con 2 "/"
if (element.split("\t")[5] == "") {
RED.log.error("knxUltimate: ERROR: Datapoint not set in ETS CSV. Please set the datapoint with ETS and export the group addresses again. ->" + element.split("\t")[0] + " " + element.split("\t")[1])
return;
}
var DPTa = element.split("\t")[5].split("-")[1];
var DPTb = element.split("\t")[5].split("-")[2];
if (typeof DPTb == "undefined") {
RED.log.warn("knxUltimate: WARNING: Datapoint not fully set (there is only the first part on the left of the '.'). I applied a default .001, but please set the datapoint with ETS and export the group addresses again. ->" + element.split("\t")[0] + " " + element.split("\t")[1] + " Datapoint: " + element.split("\t")[5] );
DPTb = "001"; // default
}
// Trailing zeroes
if (DPTb.length == 1) {
DPTb = "00" + DPTb;
} else if (DPTb.length == 2) {
DPTb = "0" + DPTb;
} if (DPTb.length == 3) {
DPTb = "" + DPTb; // stupid, but for readability
}
ajsonOutput.push({ga:element.split("\t")[1], dpt:DPTa + "." + DPTb, devicename: sFather + element.split("\t")[0]});
}
}
}
return ajsonOutput;
}
}
// 23/08/2019 Delete unwanted CRLF in the GA description
function correctCRLFInCSV(_csv) {
var sOut = ""; // fixed output text to return
var sChar = "";
var bStart = false;
for (let index = 0; index < _csv.length; index++) {
sChar = _csv.substr(index, 1);
if (sChar == "\"")
{
if (!bStart) {
bStart = true;
}else
{
bStart = false;
}
sOut += sChar;
} else
{
if (bStart) {
// i'm in the phrase, delimited by "". No CRLF should be there
if (sChar !== "\n" && sChar !== "\r")
{
sOut += sChar;
} else
{
sOut += " "; // Where it was a CRLF, i put a space
}
} else
{
sOut += sChar;
}
}
}
// Replace all parenthesis with []
sOut = sOut.replace(/\(/g, "[").replace(/\)/g, "]");
return sOut;
}
}
RED.nodes.registerType("knxUltimate-config", knxUltimateConfigNode);
}