-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
851 lines (788 loc) · 35.8 KB
/
main.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/* SPDX-License-Identifier: GPL-3.0-only */
/*
* Copyright Contributors to the ioBroker.openknx project
*/
"use strict";
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const knx = require(__dirname + "/lib/knx");
const loadMeasurement = require("./lib/loadMeasurement");
const projectImport = require("./lib/projectImport");
const tools = require("./lib/tools.js");
const DoubleKeyedMap = require("./lib/doubleKeyedMap.js");
const detect = require("./lib/openknx.js");
const os = require("os");
const exitHook = require("async-exit-hook");
class openknx extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "openknx",
});
this.gaList = new DoubleKeyedMap();
this.autoreaddone = false;
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
this.mynamespace = this.namespace;
this.knxConnection;
this.knx = knx;
this.connected = false;
this.timeout1;
this.timeout2;
this.interval1;
// redirect log from knx.js that contain [..] to adapter log
console.log = (args) => {
if (args && typeof args === "string") {
// handling special messages of the KNX lib
if (args.indexOf("deferring outbound_TUNNELING_REQUEST") !== -1) {
return;
} else if (args.indexOf("empty internal fsm queue due to inbound_DISCONNECT_REQUEST") !== -1) {
// this.log.warn("possible data loss due to gateway reset, consider increasing minimum send delay between two frames");
}
if (args.indexOf("[trace]") !== -1) this.log.silly(args);
else if (args.indexOf("[debug]") !== -1) this.log.debug(args);
else if (args.indexOf("[info]") !== -1) this.log.info(args);
else if (args.indexOf("[warn]") !== -1) this.log.warn(args);
else if (args.indexOf("[error]") !== -1) {
this.log.error(args);
if (args.indexOf("Conversion error DPT") == -1) {
// do not report errors from bad bus data
if (this.getSentry()) {
this.getSentry().withScope((scope) => {
scope.setLevel("error");
scope.setExtra("error message", args);
this.getSentry().captureMessage("knx library error event", "error");
});
}
}
} else {
// dont forward all other internal console.logs
// this.log.info(args);
}
}
};
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// adapter initialization
this.getSentry()?.Sentry?.init({
// "development" or "production"
// environment: "production",
});
//after installation
if (tools.isEmptyObject(this.config)) {
this.log.warn("Adapter configuration missing, please do configuration first.");
return;
}
// In order to get state updates, you need to subscribe to them.
this.subscribeStates("*");
this.setState("info.busload", 0, true);
this.main(true);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
clearTimeout(this.timeout1);
clearTimeout(this.timeout2);
clearInterval(this.interval1);
this.connected = false;
if (this.knxConnection) {
exitHook((cb) => {
this.knxConnection.Disconnect(() => {
callback();
});
});
} else callback();
} catch (e) {
callback();
}
}
// New message arrived. obj is array with current messages
// triggered from admin page read in knx project
onMessage(obj) {
if (typeof obj === "object") {
switch (obj.command) {
case "import":
this.log.info("ETS project import...");
projectImport.parseInput(this, obj.message.xml, (parseError, res) => {
this.updateObjects(res, 0, obj.message.onlyAddNewObjects, (updateError, length) => {
const msg = {
error:
parseError && parseError.length == 0
? updateError
: (parseError ? parseError : "") + "<br/>" + updateError,
count: length,
};
this.removeUnusedObjects(res, obj.message.removeUnusedObjects);
this.log.info("Project import finished with " + length + " GAs");
if (obj.callback) {
this.sendTo(obj.from, obj.command, msg, obj.callback);
}
});
});
break;
case "createAlias":
this.log.info("Create aliases...");
projectImport.findStatusGAs(
this,
this.gaList,
obj.message.aliasRegexp,
obj.message.aliasSimilarity,
obj.message.aliasPath,
obj.message.aliasRange,
(count, err) => {
if (obj.callback) {
const res = {
error: err,
count: count,
};
this.sendTo(obj.from, obj.command, res, obj.callback);
}
},
);
break;
case "detectInterface":
this.log.info("Detect Interface...");
detect.detect(
obj.message.ip,
0,
null,
(err, isFound, addr, port, knxAdr, deviceName, devicesFound) => {
if (obj.callback) {
const res = {
error: null,
ip: addr,
port: port,
knxAdr: knxAdr,
deviceName: deviceName,
devicesFound: devicesFound,
};
this.sendTo(obj.from, obj.command, res, obj.callback);
}
},
);
break;
case "restart":
this.log.info("Restarting...");
this.restart();
// @ts-ignore
break;
default:
this.log.warn("Unknown command: " + obj.command);
break;
}
}
return true;
}
/*
* remove knx elements that are not found in the current import file
*/
async removeUnusedObjects(importObjects, removeUnusedObjects) {
const objects = await this.getAdapterObjectsAsync();
Object.entries(objects).map((object) => {
console.log(object);
if (
object[1].native &&
Object.keys(object[1].native).length === 0 &&
Object.getPrototypeOf(object[1].native) === Object.prototype
) {
// object is no knx element, skip
} else {
const found = importObjects.find((element) => this.mynamespace + "." + element._id === object[0]);
if (!found) {
// knx element in object tree not found in import file
this.log.info(
`${removeUnusedObjects ? "deleting" : ""}
existing element in object tree not found in import file: ${object[0]}`,
);
if (removeUnusedObjects) {
this.delObject(object[0], (err) => {
if (err) {
this.log.warn("could not delete object " + object[0]);
}
});
}
}
}
});
}
// write found communication objects to adapter object tree
updateObjects(objects, i, onlyAddNewObjects, callback) {
if (i >= objects.length) {
// end of recursion reached
let err = this.warnDuplicates(objects);
this.getObjectList(
{
startkey: this.namespace,
endkey: this.namespace + "\u9999",
},
(e, result) => {
const gas = [];
const duplicates = [];
if (result)
result.rows.forEach((element) => {
if (
Object.prototype.hasOwnProperty.call(element.value, "native") &&
Object.prototype.hasOwnProperty.call(element.value, "address")
)
gas.push(element.value.native.address + " " + element.value.common.name);
});
const tempArray = [...gas].sort();
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i + 1] === tempArray[i]) {
duplicates.push(tempArray[i]);
}
}
const message =
"Objects imported where objects exist that have same KNX group address: " + duplicates;
if (duplicates.length) {
this.log.warn(message);
err ? (err = err + "<br/>" + message) : (err = message);
}
if (typeof callback === "function") {
callback(err, objects.length);
}
},
);
return;
}
if (onlyAddNewObjects) {
// if user setting Add only new Objects write only new objects
// extend object would overwrite user made element changes if known in the import, not intended
this.setObjectNotExists(this.mynamespace + "." + objects[i]._id, objects[i], (err) => {
if (err) {
this.log.warn("error store Object " + objects[i]._id + " " + (err ? " " + err : ""));
}
this.timeout1 = setTimeout(
this.updateObjects.bind(this),
0,
objects,
i + 1,
onlyAddNewObjects,
callback,
);
});
} else {
// setObjet to overwrite all existing settings, default
this.setObject(this.mynamespace + "." + objects[i]._id, objects[i], (err) => {
if (err) {
this.log.warn("error store Object " + objects[i]._id + (err ? " " + err : ""));
}
this.timeout2 = setTimeout(
this.updateObjects.bind(this),
0,
objects,
i + 1,
onlyAddNewObjects,
callback,
);
});
}
}
/*
* IOBroker Object tree cannot store 2 objects of same name, warn
* In ETS it is possible as long as GA is different
*/
warnDuplicates(objects) {
const arr = [];
const duplicates = [];
for (const object of objects) {
arr.push(object._id);
}
const tempArray = [...arr].sort();
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i + 1] === tempArray[i]) {
duplicates.push(tempArray[i]);
}
}
const message = "New object with an already existing Group Address name has not been created: " + duplicates;
if (duplicates.length && this.log) {
this.log.warn(message);
}
return duplicates.length ? message : "";
}
// obj to string and date to number for iobroker from knx stack
convertType(val) {
let ret;
// convert, state value for iobroker to set has to be one of type "string", "number", "boolean" and additionally type "object"
if (val instanceof Date) {
// convert Date to number
ret = Number(new Date(val));
} else if (Buffer.isBuffer(val)) {
// before object check
ret = val.toString("hex");
} else if (typeof val === "object") {
// https://github.com/ioBroker/ioBroker.docs/blob/master/docs/en/dev/objectsschema.md#states
ret = JSON.stringify(val);
} else {
// keep string, boolean and number
ret = val;
}
return ret;
}
/**
* Is called if a subscribed state changes
* state.ack is received with value false if set by user (nodered, script...), here we set it.
* https://github.com/ioBroker/ioBroker.docs/blob/master/docs/en/dev/adapterdev.md
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
let isRaw = false;
if (!id || !state /*obj deleted*/ || typeof state !== "object") {
return "invalid input";
}
if (
!this.gaList.getDataById(id) ||
!this.gaList.getDataById(id).native ||
!this.gaList.getDataById(id).native.address
) {
return "not a KNX object";
}
if (this.knxConnection == undefined) {
return "KNX not started";
}
if (state.ack) {
// only continue when application triggered a change without ack flag, filter out reception state changes
// enable this for system testing:
// this.interfaceTest(id, state);
return "ack is set";
}
if (!this.connected) {
this.log.warn("onStateChange: not connected to KNX bus");
return "not connected to KNX bus";
}
const dpt = this.gaList.getDataById(id).native.dpt;
const ga = this.gaList.getDataById(id).native.address;
let knxVal = state.val;
let rawVal;
// plausibilize against configured datatype
if (this.gaList.getDataById(id).common && this.gaList.getDataById(id).common.type == "boolean") {
knxVal = knxVal ? true : false;
} else if (
this.gaList?.getDataById(id)?.common?.type == "number" ||
this.gaList?.getDataById(id)?.common?.type == "enum"
) {
if (isNaN(Number(knxVal))) {
this.log.warn("Value " + knxVal + " for " + id + " is not a number");
}
// else take plain value
} else if (tools.isDateDPT(dpt)) {
// before composite check, date is possibly composite
knxVal = new Date(knxVal);
} else if (this.gaList.getDataById(id).native.valuetype == "composite") {
// input from IOB is either object or string in object notation, type of this conversion is object needed by the knx lib
if (typeof knxVal != "object") {
try {
knxVal = JSON.parse(knxVal);
} catch (e) {
this.log.warn("stateChange: unsupported value format " + knxVal + " for " + ga);
return "unsupported value";
}
}
} else if (tools.isStringDPT(dpt)) {
// take plain value
} else if (tools.isUnknownDPT(dpt)) {
// write raw buffers for unknown dpts, iterface is a hex value
// bitlength is the buffers bytelength * 8.
if (typeof knxVal != "string") {
this.log.warn("unsupported datatype for raw value");
return "unsupported datatype";
}
rawVal = Buffer.from(knxVal, "hex");
isRaw = true;
this.log.info("Unhandeled DPT " + dpt + ", assuming raw value");
} else {
let error;
if (!this.gaList.getDataById(id).common || !this.gaList.getDataById(id).common.type)
// configuration that is checked before does not exist, unplausible
error = "bad or missing configuration for object with id: " + id;
else {
error =
"cannot interprete data, please check your configuration. " +
dpt +
" unplausible type: " +
this.gaList?.getDataById(id)?.common?.type;
if (this.getSentry()) this.getSentry().captureException(error);
}
console.warn(error);
this.log.warn(error);
}
// @ts-ignore
if (state.c == "GroupValue_Read" || state.q == 0x10) {
// interface to trigger GrouValue_Read is this object comment or StateQuality 16
this.log.debug("Outbound GroupValue_Read to GA " + ga);
this.knxConnection.read(ga, () => {
// ack is generated with GroupValue_Response
});
return "read";
} else if (this.gaList.getDataById(id).common.write) {
this.log.debug(
`Outbound GroupValue_Write to " ${ga} value: ${isRaw ? rawVal : JSON.stringify(knxVal)} from ${id}`,
);
if (isRaw) {
this.knxConnection.writeRaw(ga, rawVal, (grpaddr, confirmed, timeout) => {
// l_data.con confirmation set by any receiver connected to the ga
if (confirmed) {
//set iob ack when value was sent successful on the bus
this.log.debug(`Inbound GroupValue_Write confirmation true received for ${grpaddr} ${id}`);
this.setState(id, {
ack: true,
});
} else if (timeout) this.log.info(`GroupValue_Write timeout for ${grpaddr} ${id}`);
else this.log.info(`Inbound GroupValue_Write confirmation false received for ${grpaddr} ${id}`);
});
return "write raw";
} else {
this.knxConnection.write(ga, knxVal, dpt, (grpaddr, confirmed, timeout) => {
// l_data.con confirmation set by any receiver connected to the ga
if (confirmed) {
//set iob ack when value was sent successful on the bus
this.log.debug(`Inbound GroupValue_Write confirmation true received for ${grpaddr} ${id}`);
this.setState(id, {
ack: true,
});
} else if (timeout) this.log.info(`GroupValue_Write timeout for ${grpaddr} ${id}`);
else this.log.info(`Inbound GroupValue_Write confirmation false received for ${grpaddr} ${id}`);
});
return "write";
}
} else {
this.log.warn("not configured write to ga: " + state.val);
return "configuration error";
}
}
startKnxStack() {
this.knxConnection = this.knx.Connection({
ipAddr: this.config.gwip,
ipPort: this.config.gwipport,
physAddr: "0.0.0",
interface: this.translateInterface(this.config.localInterface),
minimumDelay: this.config.minimumDelay,
// https://github.com/Supergiovane/node-red-contrib-knx-ultimate/issues/78, some receivers cannot handle a ack request, spec makes no difference
suppress_ack_ldatareq: true,
// map set the log level for messsages printed on the console. This can be 'error', 'warn', 'info' (default), 'debug', or 'trace'.
// log is written to console, not in IoB log
loglevel: this.log.level == "silly" ? "trace" : this.log.level,
handlers: {
connected: () => {
this.log.info("Connected!");
this.setState("info.messagecount", 0, true);
// create new knx datapoint and bind to connection
// in order to have autoread work
let cnt_withDPT = 0;
if (!this.autoreaddone) {
// do autoread on start of adapter and not every connection
for (const key of this.gaList) {
try {
const datapoint = new this.knx.Datapoint(
{
ga: this.gaList.getDataById(key).native.address,
dpt: this.gaList.getDataById(key).native.dpt,
// issue a GroupValue_Read request to try to get the initial state from the bus (if any)
autoread:
this.gaList.getDataById(key).native.autoread && this.config.autoreadEnabled,
},
this.knxConnection,
);
datapoint.on("error", (ga, dptid) => {
this.log.warn(
"Received data length for GA " + ga + " does not match configured " + dptid,
);
});
this.gaList.setDpById(key, datapoint);
cnt_withDPT++;
this.log.debug(
`Datapoint ${
this.gaList.getDataById(key).native.autoread
? "autoread created and GroupValueRead sent"
: "created"
} ${this.gaList.getDataById(key).native.address} ${key}`,
);
} catch (e) {
this.log.error("Could not create KNX Datapoint for " + key + " with error: " + e);
}
}
this.autoreaddone = true;
this.countObjectsNotification(cnt_withDPT);
}
this.connected = true;
this.setState("info.connection", this.connected, true);
},
disconnected: () => {
if (this.connected) this.log.error("Connection lost");
this.connected = false;
this.setState("info.connection", this.connected, true);
this.setState("info.busload", 0, true);
},
error: (connstatus) => {
this.log.warn(connstatus);
},
// l_data.con, confirmation set by a receiver which has the sending flag
confirmed: (dest, confirmed) => {
for (const id of this.gaList.getIdsByGa(dest)) {
if (confirmed) {
this.log.debug(`A receiver confirmed reception of our message for ${dest} ${id}`);
} else {
// otherwise keep unset
this.log.info(
`Got confirmation flag false for ${dest} ${id}. Possibly no receiver available or missing ETS receiver configuration.`,
);
}
}
},
// KNX Bus event received
// src: KnxDeviceAddress, dest: KnxGroupAddress,
// val: raw value not used, using dp interface instead
// @ts-ignore
event: (
/** @type {string} */ evt,
/** @type {string} */ src,
/** @type {string} */ dest,
/** @type {string} val ,*/
) => {
let convertedVal = [];
let ret = "unknown";
if (!this.autoreaddone) {
this.log.info(`received data although connection process not completed - skiped`);
return "illegal state";
}
loadMeasurement.logBusEvent();
/* some checks */
if (dest == "0/0/0" || tools.isDeviceAddress(dest)) {
// seems that knx lib does not guarantee dest group adresses
return "bad address";
}
if (!this.config.noWarnUnknownGa && !this.gaList.getIdsByGa(dest).length) {
this.log.warn(`Ignoring ${evt} of unknown GA ${dest}`);
return "unknown GA";
}
for (const id of this.gaList.getIdsByGa(dest)) {
const data = this.gaList.getDataById(id);
const dp = this.gaList.getDpById(id);
if (id == undefined || data == undefined || dp == undefined) {
// debug trap, should not be reached
throw new Error(`Invalid data for GA ${dest} id ${id} data ${data} dp ${dp}`);
}
if (tools.isStringDPT(data.native.dpt)) {
convertedVal = dp.current_value;
} else {
convertedVal = this.convertType(dp.current_value);
}
switch (evt) {
case "GroupValue_Read":
// fetch val from addressed object and write on bus if configured to answer
this.getState(id, (err, state) => {
let ret;
if (state) {
this.log.debug(`Inbound GroupValue_Read from ${src} GA ${dest} to ${id}`);
ret = "GroupValue_Read";
if (this.gaList.getDataById(id).native.answer_groupValueResponse) {
let stateval = state.val;
try {
// @ts-ignore
stateval = JSON.parse(state.val);
} catch (e) {
/* empty */
}
this.knxConnection.respond(
dest,
stateval,
this.gaList.getDataById(id).native.dpt,
);
this.log.debug("responding with value " + state.val);
ret = "GroupValue_Read Respond";
}
return ret;
}
});
break;
case "GroupValue_Response":
this.setState(id, {
val: convertedVal,
ack: true,
});
this.log.debug(
`Inbound GroupValue_Response from ${src} GA ${dest} to Object ${id} value: ${convertedVal} dpt: ${data.native.dpt}`,
);
ret = "GroupValue_Response";
break;
case "GroupValue_Write":
this.setState(id, {
val: convertedVal,
ack: true,
});
this.log.debug(
`Inbound GroupValue_Write from ${src} GA ${dest} to Object ${id} value: ${convertedVal} dpt: ${data.native.dpt}`,
);
ret = "GroupValue_Write";
break;
default:
this.log.debug(`received unhandeled event " ${evt} ${src} ${dest} ${convertedVal}`);
ret = "unhandeled";
}
}
return ret; // last processed
},
},
});
}
/* for testing, forward msg from one to another test address
better approach: send all test values via ets, send received value back from iobroker, compare in ets
*/
interfaceTest(id, state) {
const inpath = this.mynamespace + ".test.testin";
const outpath = this.mynamespace + ".test.testout";
if (id.startsWith(inpath)) {
const out = outpath + id.replace(inpath, "");
this.setState(out, {
val: state.val,
ack: false,
});
}
}
// admin dialog uses different name than knx lib, translate ip to interface name
translateInterface(interfaceIp) {
const interfaces = os.networkInterfaces();
for (const [iface, addrs] of Object.entries(interfaces)) {
if (addrs)
for (const addr of addrs) {
if (addr.address == interfaceIp) {
return iface;
}
}
}
return interfaceIp;
}
countObjectsNotification(cnt_withDPT) {
this.getObjectList(
{
startkey: this.namespace,
endkey: this.namespace + "\u9999",
},
(e, result) => {
if (result)
this.log.info(
"Found " +
cnt_withDPT +
" valid KNX objects of " +
result.rows.length +
" objects in this adapter.",
);
},
);
}
main(startKnxConnection) {
if (!this.config.gwip) {
this.log.warn("Gateway IP is missing please enter Gateway IP in the instance settings.");
startKnxConnection = false;
} else
this.log.info(
"Connecting to knx gateway: " +
this.config.gwip +
":" +
this.config.gwipport +
" device name: " +
this.config.deviceName +
" with physical adr: " +
this.config.eibadr +
" minimum send delay: " +
this.config.minimumDelay +
" ms" +
" debug level: " +
this.log.level,
);
const self = this;
this.connected = false;
this.setState("info.connection", this.connected, true);
this.interval1 = setInterval(function () {
const busload = loadMeasurement.cyclic();
self.setState("info.busload", busload, true);
self.setState("info.messagecount", loadMeasurement.gettelegramCount(), true);
}, loadMeasurement.intervalTime);
// fill gaList from iobroker objects
this.getObjectView(
"system",
"state",
{
startkey: this.mynamespace + ".",
endkey: this.mynamespace + ".\u9999",
include_docs: true,
},
(err, res) => {
if (err) {
this.log.error("Cannot get objects: " + err);
} else if (res) {
for (let i = res.rows.length - 1; i >= 0; i--) {
const id = res.rows[i].id;
const value = res.rows[i].value;
if (
value &&
value.native &&
value.native.address != undefined &&
value.native.address.match(/\d*\/\d*\/\d*/) &&
value.native.dpt
) {
// add only elements from tree that are knx objects, identified by a group adress
this.gaList.set(id, value.native.address, res.rows[i].value);
if (this.gaList.getIdsByGa(value.native.address).length > 1)
this.log.warn(
id +
" has assigned a non exclusive group address: " +
value.native.address +
". Consider to delete duplicate entries.",
);
} else if (!id.startsWith(this.mynamespace + ".info."))
this.log.warn(`Incomplete configuration in iob object ${id}`);
}
if (startKnxConnection)
try {
this.startKnxStack();
} catch (e) {
if (e.toString().indexOf("not found or has no useful IPv4 address!") !== -1)
// ipaddr: the address has neither IPv6 nor IPv4 format ??
// only handle certain exceptions
this.log.error(`Cannot start KNX Stack ${e}`);
else throw e;
}
}
},
);
}
getSentry() {
if (this.supportsFeature && this.supportsFeature("PLUGINS")) {
const sentryInstance = this.getPluginInstance("sentry");
if (sentryInstance) {
return sentryInstance.getSentryObject();
}
}
}
}
if (require.main !== module) {
// this module was run directly from the command line as in node xxx.js
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new openknx(options);
//module.exports = new openknx();
} else {
// this module was not run directly from the command line and probably loaded by something else
// otherwise start the instance directly
new openknx();
}