-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
918 lines (848 loc) · 40.7 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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
// @ts-nocheck
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
/*
* Created with @iobroker/create-adapter v1.20.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// Load modules here, e.g.:
const vbus = require('resol-vbus');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const adapterName = require('./package.json').name.split('.').pop();
const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const fqdnformat = /^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/;
const serialformat = /^(COM|com)[0-9][0-9]?$|^\/dev\/tty.*$/;
const vbusioformat = /d[0-9]{10}.[vV][bB][uU][sS].[iInN][oOeE][tT]?/;
const distPath = './lib/resol-setup/';
const setupFileResolTypes = distPath + 'Setup-Resol-Types.js';
const actionPath = '.Actions.';
const ctx = {
headerSet: new vbus.HeaderSet(),
hsc: new vbus.HeaderSetConsolidator(),
connection: new vbus.Connection()
};
let jsoncontrollerSetupItems;
let myDeviceAddress;
let myDeviceID;
let myDeviceVersion;
class resol extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({...options, name: adapterName});
// this.on('objectChange', this.onObjectChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
this.on('ready', this.onReady.bind(this));
this.on('message', this.onMessage.bind(this));
}
/**
* onMessage
*
* Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
* Using this method requires "common.messagebox" property to be set to true in io-package.json
* This function exchanges information between the admin frontend and the backend.
* In detail: it performs the 2 FA login at the dyson API. Therefore it receives messages from admin,
* sends them to dyson and reaches the received data back to admin.
*
* @param {object} msg - Message object containing all necessary data to request the needed information
*/
async onMessage( msg ) {
this.log.debug('OnMessage: Received some request - Level 1.');
if (typeof msg === 'object' && msg.callback && msg.from && msg.from.startsWith('system.adapter.admin') ) {
this.log.debug('OnMessage: Received some request - Level 2.');
if (msg.command === 'detectController'){
this.log.debug('OnMessage: Received detect controller request.');
this.detectController()
.then((response) => this.sendTo(msg.from, msg.command, response, msg.callback))
.catch((e) => {
this.log.warn(`Couldn't handle detectController message: ${e}`);
this.sendTo(msg.from, msg.command, { error: e || 'No data' }, msg.callback);
});
} else if ( msg.command=== 'getControllersForAdminSelect') {
this.getControllersForAdminSelect()
.then((response) => this.sendTo(msg.from, msg.command, response, msg.callback))
.catch((e) =>{
this.log.warn(`Couldn't handle getControllersForAdminSelect message: ${e}`);
this.sendTo(msg.from, msg.command, { error: e || 'No data' }, msg.callback);
});
}
}
}
async detectController(){
this.log.info('Trying to detect controller...');
const context = {connection: ctx.connection, deviceAddress: myDeviceAddress, saveConfig: {}};
try{
const datagram = await context.connection.waitForFreeBus();
this.log.debug(`Got Data from controller: ${JSON.stringify(datagram)}`);
const controller = await this.getJSONByResolId(datagram.sourceAddress,myDeviceVersion);
this.log.debug(`Got Data from controller-setup-file: ${JSON.stringify(controller)}`);
this.log.info(`Found controller: ${controller.friendlyName}`);
return {native: {controller:controller.type}};
} catch(error){
this.log.error(error);
}
}
/**
* currently not used
* @returns {Promise<void>}
*/
async getControllersForAdminSelect(){
this.log.debug('Trying to get controllers from file...');
this.loadJsonFile(setupFileResolTypes)
.then((data) => {
data = JSON.parse(data);
this.log.debug(`Got Data from file: ${JSON.stringify(data)}`);
const result = [];
for (const row of data){
this.log.debug(`Row in file: ${JSON.stringify(row)}`);
result.push({label: row.friendlyName, value: row.type});
}
this.log.debug(`getControllersForAdminSelect file-return value: ${JSON.stringify(result)}`);
return result;
})
.catch( (error) => {
this.log.error(error);
return [{label:'test-1', value:'TEST-1'}, {label:'test-2', value:'TEST-2'}];
});
}
//--- vbus write
async runShot(context) {
try {
this.log.debug('Waiting for free bus...');
const datagram = await context.connection.waitForFreeBus();
context.masterAddress = datagram.sourceAddress;
this.log.debug('Found master with address 0x' + context.masterAddress.toString(16));
context.deviceAddress = context.masterAddress;
switch (context.deviceAddress){
case 30753: // Surrogat : Cosmo-Multi-2 is a relabled DeltaSol-E
context.deviceAddress = 4176; // fake a DeltaSol-E since Cosmo-Controllers aren't supported by the Resol-Lib
break;
}
//const optimizer = await vbus.ConfigurationOptimizerFactory.createOptimizerByDeviceAddress(context.deviceAddress);
const options1 = {
deviceAddress : context.deviceAddress,
version : context.deviceVersion
}
this.log.debug('options1 ' + JSON.stringify(options1));
const optimizer = await vbus.ConfigurationOptimizerFactory.createOptimizer(options1);
context.optimizer = optimizer;
if (!optimizer) {
// log error and exit function
this.log.error('Unable to create optimizer for master with address 0x' + context.deviceAddress.toString(16));
return;
}
context.customizer = new vbus.ConnectionCustomizer({
deviceAddress: context.deviceAddress,
connection: context.connection,
optimizer: context.optimizer,
});
let savedConfig;
if (context.saveConfig !== undefined) {
const saveConfig = context.saveConfig;
const oldConfig = context.oldConfig;
const options = {
optimize: false,
};
this.log.debug('Start Optimizer');
savedConfig = await context.customizer.saveConfiguration(saveConfig, oldConfig, options) ;
} else {
this.log.debug('Optimizer savedConfig = loadedConfig ', savedConfig || 'Not initialized!');
}
this.log.debug('Save config ' + JSON.stringify(savedConfig));
savedConfig.reduce((memo, value) => {
if (!value.ignored) {
memo [value.valueId] = value.value;
}
return memo;
}, {});
} catch (e) {
this.log.error('[runShot] Error: ' + e);
} finally {
this.log.debug('Finishing runshot ...');
}
}
//--- end vbus write
async loadJsonFile(filename) {
const pathToFile = path.resolve(__dirname, filename);
return new Promise((resolve, reject) => {
fs.readFile(pathToFile, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
isNumber(n) {
return /^-?[\d.]+(?:e-?\d+)?$/.test(n);
}
async getJSONByResolId(resolId,deviceVersion) {
let result;
this.log.debug('[getJSONByResolId] given Resol-ID : ' + resolId+ ' Version '+deviceVersion);
this.log.debug('[getJSONByResolId] Reading File: [' + setupFileResolTypes + ']');
await this.loadJsonFile(setupFileResolTypes)
.then((setupResolTypes) => {
this.log.debug('[getJSONByResolId] Successfully read File. Content: [' + setupResolTypes + ']');
const jsetupResolTypes = JSON.parse(setupResolTypes);
this.log.debug('jsetupResolTypes : ' + JSON.stringify(jsetupResolTypes));
jsetupResolTypes.forEach(item => {
if (resolId === item.id) {
this.log.debug('[getJSONByResolId] Resol-ID found : ' + resolId);
//myDeviceVersion == item.majorVersion;
if (item.majorVersion) {
this.log.debug('[getJSONByResolId] majorVersion exists in file: ' + item.majorVersion);
if (item.majorVersion==deviceVersion) result = item; else this.log.error('[getJSONByResolId] device version not correct, please set in config');
} else result = item;
}
});
if (!result) this.log.error('[getJSONByResolId] Controller type not found in setup file.');
this.log.debug('[getJSONByResolId] result : ' + JSON.stringify(result));
})
.catch((err) => {
this.log.error('[getJSONByResolId] Error: ' + err);
});
return result;
}
// generate all dp read from file
async generateDP(resolAddr, resolId) {
try {
this.createOrExtendObject(resolId + '.Actions', {
type: 'channel',
common: {
name: 'These data fields trigger actions on your controller.',
type: 'string'
},
native: {}
}, null);
this.log.debug('[generateDP]->Resol-Address/Resol-ID: [' + resolAddr + '] : [' + resolId + ']');
const setupResolType = await this.getJSONByResolId(resolAddr,myDeviceVersion);
this.log.debug('[generateDP]->setupResolType: ' + JSON.stringify(setupResolType));
const controllerSetupFile = distPath + setupResolType.setup + '.js';
this.log.debug('[generateDP] Loading Controller-config-file: ' + controllerSetupFile);
this.loadJsonFile(controllerSetupFile)
.then(controllerSetupItems => {
this.log.debug('[generateDP] Controller-config-file content: ' + JSON.stringify(controllerSetupItems));
jsoncontrollerSetupItems = JSON.parse(String(controllerSetupItems));
this.log.debug('[generateDP]->jsoncontrollerSetupItems: ' + JSON.stringify(jsoncontrollerSetupItems));
jsoncontrollerSetupItems.dp.forEach(item => {
this.log.debug('[generateDP]->item ' + JSON.stringify(item));
// create dp
let thisRole = 'value';
if (item.states) thisRole = 'level';
this.createOrExtendObject(resolId + actionPath + item.dpName, {
type: 'state',
common: {
name: item.dpName,
type: item.type,
min: item.min,
max: item.max,
states: item.states,
role: thisRole,
read: true,
write: true
},
native: {}
}, null);
this.subscribeStates(resolId + actionPath + item.dpName);
});
})
.catch(err => {
this.log.error('[generateDP] Controller-config-file (' + controllerSetupFile + ') load error: Please check if the file structure is valid.');
this.log.error('[generateDP] Controller-config-file load error: ' + err);
});
} catch (err) {
this.log.error('[generateDP] Error: ' + err);
} finally {
this.log.debug('Finishing generateDP...');
}
}
getDpFunction(dpName, Value) {
try {
const myDpNameArray = dpName.split('.');
const len = myDpNameArray.length;
// if the JSON was triggered
if (len === 3) {
if (myDpNameArray[len - 1] === 'JSON') {
return (Value);
}
}
const myDpName = myDpNameArray[len - 1];
let myfctItem;
this.log.debug(JSON.stringify('getDpFunction jsoncontrollerSetupItems ' + jsoncontrollerSetupItems));
jsoncontrollerSetupItems.fct.forEach(item => {
this.log.debug('getDpFunction jsoncontrollerSetupItems->item ' + JSON.stringify(item));
if (myDpName === item.name) {
myfctItem = item;
}
});
// throw if error
if (!myfctItem) {
this.log.error('[getDpFunction] : fctItem not defined!');
return;
}
let jsonValue;
// easy way, only 1 cmd : {"valueId": "Handbetrieb1", "value": 0}
if (myfctItem.cmd) {
jsonValue = [];
const jsonItem = {};
jsonItem.valueId = myfctItem.cmd;
jsonItem.value = Value;
jsonValue.push(jsonItem);
}
// more then 1 cmd : [{"valueId": "ORueckkuehlung", "value": 0},{"valueId":"OHolyCool","value": 0}]
if (myfctItem.cmds) {
jsonValue = [];
myfctItem.cmds.forEach(item => {
this.log.debug(JSON.stringify(item));
const jsonItem = {};
jsonItem.valueId = item.cmd;
jsonItem.value = Value;
jsonValue.push(jsonItem);
});
}
this.log.debug(JSON.stringify(jsonValue));
return jsonValue;
} catch (e) {
this.log.error('[getDpFunction] Error: ' + e);
} finally {
this.log.debug('Finishing Dpfunction...');
}
}
searchForFctItem(thisName) {
let result;
jsoncontrollerSetupItems.fct.forEach(item => {
if (item.name === thisName) {
this.log.debug('fct->item found' + JSON.stringify(item));
result = item;
}
});
return result;
}
searchForDpItem(thisName) {
let result;
jsoncontrollerSetupItems.fct.forEach(item => {
if (item.cmd === thisName) {
this.log.debug('dp->item found' + JSON.stringify(item));
result = item;
}
});
return result;
}
getDpType (thisName) {
let result;
jsoncontrollerSetupItems.dp.forEach(item => {
if (item.dpName===thisName) {
this.log.debug('dp->Name found'+JSON.stringify(item));
result=item.type;
}
});
return result;
}
async loadMyConfig (context) {
this.log.debug('loadMyConfig (context) ' + JSON.stringify(context));
try{
if (jsoncontrollerSetupItems) {
switch (context.deviceAddress){
case 30753: // Surrogat : Cosmo-Multi-2 is a relabled DeltaSol-E
context.deviceAddress = 4176; // fake a DeltaSol-E since Cosmo-Controllers aren't supported by the Resol-Lib
break;
}
//const optimizer = await vbus.ConfigurationOptimizerFactory.createOptimizerByDeviceAddress(context.deviceAddress);
const options1 = {
deviceAddress : context.deviceAddress,
version : context.deviceVersion
}
this.log.debug('options1 ' + JSON.stringify(options1));
const optimizer = await vbus.ConfigurationOptimizerFactory.createOptimizer(options1);
context.optimizer = optimizer;
if (!optimizer) {
// log error and exit function
this.log.error('Unable to create optimizer for master with address 0x' + context.deviceAddress.toString(16));
return;
}
context.customizer = new vbus.ConnectionCustomizer({
deviceAddress: context.deviceAddress,
connection: context.connection,
optimizer: context.optimizer,
});
let readConfig = [];
jsoncontrollerSetupItems.dp.forEach(item => {
const fct = this.searchForFctItem(item.dpName);
if (fct.cmd) {
// generate readConfig
const thisConfig = {valueId: fct.cmd};
readConfig.push(thisConfig);
}
});
const options = {
optimize: !readConfig
};
const loadedConfig = await context.customizer.loadConfiguration(readConfig, options);
this.log.debug('loadedConfig ' + JSON.stringify(loadedConfig));
loadedConfig.forEach (item => {
const fctItem=this.searchForDpItem(item.valueId);
const thisDpName = context.deviceID + actionPath + fctItem.name;
const thisType = this.getDpType (fctItem.name);
if (thisType === 'number') {
item.value = parseFloat(item.value);
}
this.setStateAsync(thisDpName,item.value,true);
});
// read combined functions
readConfig = [];
for (const item of jsoncontrollerSetupItems.dp) {
const fctItem = this.searchForFctItem(item.dpName);
if (fctItem.cmds) {
console.log('fct->item ' + JSON.stringify(fctItem));
fctItem.cmds.forEach(item => {
const thisConfig = {valueId: item.cmd};
readConfig.push(thisConfig);
});
const loadedConfig = await context.customizer.loadConfiguration(readConfig, options);
this.log.debug('loadedConfig ' + JSON.stringify(loadedConfig));
// check if all items are numbers
let checkNumber = true;
loadedConfig.forEach(item => {
if (!this.isNumber(item.value)) checkNumber = false;
});
if (checkNumber) {
let thisValue = 1;
loadedConfig.forEach(item => {
thisValue &= item.value;
});
const thisDpName = context.deviceID + actionPath + fctItem.name;
this.setStateAsync(thisDpName, thisValue, true);
}
}
}
}
} catch (e) {
this.log.error(e);
} finally {
this.log.debug('Finishing loadMyConfig...');
}
}
async onStateChange(id, state) {
// Warning, state can be null if it was deleted
if (state && !state.ack) {
this.log.debug('Change on Object: ' + JSON.stringify(id));
this.log.debug('State of Object: ' + JSON.stringify(state));
this.log.debug('State :' + state.val);
const value = JSON.parse(state.val);
const myJSON = this.getDpFunction(id, value);
this.log.debug('myJSON: ' + JSON.stringify(myJSON));
const context = {connection: ctx.connection, deviceAddress: myDeviceAddress, deviceVersion: myDeviceVersion,saveConfig: myJSON};
await this.runShot(context);
}
}
/*
* @param {object} config Current active config for adapter
* @returns {string} Result of the check
* */
async configIsValid(config) {
this.log.debug('configIsValid Function ');
this.log.debug('Entering Function [configIsValid]');
// Log the current config given to the function
this.log.debug(`Connection Type: ${this.config.connectionDevice}`);
this.log.debug(`Connection Identifier: ${this.config.connectionIdentifier}`);
this.log.debug(`Connection Port: ${this.config.connectionPort}`);
this.log.debug(`VBus Password encrypted: ${this.config.vbusPassword}`);
this.log.debug(`VBus Channel: ${this.config.vbusChannel}`);
this.log.debug(`VBus Via Tag: ${this.config.vbusViaTag}`);
this.log.debug(`VBus Interval: ${this.config.vbusInterval}`);
this.log.debug(`Major Version: ${this.config.controllerMajorVersion}`);
return new Promise(
function (resolve, reject) {
// some helper functions
function testSerialformat(config) {
if (!config.connectionIdentifier.match(serialformat)) {
reject('Serialformat is invalid! Please fix.');
}
}
function testIP_and_FQDN_Format(config) {
if (!config.connectionIdentifier.match(ipformat) && !config.connectionIdentifier.match(fqdnformat)) {
reject('[' + config.connectionIdentifier + '] is neither a valid IP-Format nor a fully qualified domain name (FQDN)!');
}
}
function testVBusIOFormat(config) {
if (!config.vbusViaTag.match(vbusioformat)) {
reject('VBusIO-Format is invalid! Should be something like [d01234567890.vbus.io] or [d01234567890.vbus.net].');
}
}
function testPassword(config) {
if (!config.vbusPassword || '' === config.vbusPassword) {
reject('Password is missing!');
}
}
function testPort(config) {
if ('' === config.connectionPort || 0 === config.connectionPort) {
reject('Invalid connection port given! Should be > 0.');
}
}
// switch connectionDevice seleted by User
if (config.connectionDevice === 'serial') {
testSerialformat(config);
resolve('Config seems to be valid for USB/Serial.');
} else if (config.connectionDevice === 'lan') {
testIP_and_FQDN_Format(config);
testPassword(config);
testPort(config);
resolve('Config seems to be valid for LAN.');
} else if (config.connectionDevice === 'langw') {
testIP_and_FQDN_Format(config);
testPort(config);
resolve('Config seems to be valid for Serial-to-LAN-Gateway.');
} else if (config.connectionDevice === 'dl2' || config.connectionDevice === 'dl3') {
testIP_and_FQDN_Format(config);
testPort(config);
testPassword(config);
resolve('Config seems to be valid for KM2/DL2/DL3.');
} else if (config.connectionDevice === 'inet') {
testPort(config);
testVBusIOFormat(config);
testPassword(config);
resolve('Config seems to be valid for KM2/DL2/DL3 via VBus.net.');
} else {
reject('Config is invalid! Please select at least a connection device to get further tests - or even better, complete your whole config.');
}
}
);
}
async main() {
let relayActive = 'Relay X active';
let language = 'en';
try {
// Get system language and set it for this adapter
await this.getForeignObjectAsync('system.config').then(sysConf => {
this.log.debug('Requesting language from system.');
if (sysConf && (sysConf.common.language === 'de' || sysConf.common.language === 'fr')) {
// switch language to a language supported by Resol-Lib (de, fr), or default to english
language = sysConf.common.language;
}
// Set translation for relay active state
switch (language) {
case 'de':
relayActive = 'Relais X aktiv';
break;
case 'fr':
relayActive = 'Relais X actif';
break;
}
}).catch(err => {
this.log.error(JSON.stringify(err));
});
const spec = new vbus.Specification({
language: language
});
// set up device version
myDeviceVersion = this.config.controllerMajorVersion;
// Set up connection depending on connection device and check connection identifier
switch (this.config.connectionDevice) {
case 'lan':
ctx.connection = new vbus.TcpConnection({
host: this.config.connectionIdentifier,
port: this.config.connectionPort,
password: this.config.vbusPassword
});
this.log.info('TCP Connection via LAN to [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected');
break;
case 'serial':
ctx.connection = new vbus.SerialConnection({
path: this.config.connectionIdentifier
});
this.log.info('Serial Connection at [' + this.config.connectionIdentifier + '] selected');
break;
case 'langw':
ctx.connection = new vbus.TcpConnection({
host: this.config.connectionIdentifier,
port: this.config.connectionPort,
rawVBusDataOnly: true
});
this.log.info('TCP Connection via LAN-gw to [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected');
break;
case 'inet':
this.log.debug('VBus.net Connection via [' + this.config.vbusViaTag.substring(12, this.config.vbusViaTag.length) + '] selected');
this.log.debug('VBus.net Connection via [' + this.config.vbusViaTag.substring(0, 11) + '] selected');
ctx.connection = new vbus.TcpConnection({
//host: this.config.connectionIdentifier,
host: this.config.vbusViaTag.substring(12, this.config.vbusViaTag.length),
port: this.config.connectionPort,
password: this.config.vbusPassword,
viaTag: this.config.vbusViaTag.substring(0, 11)
});
this.log.info('VBus.net Connection via [' + this.config.vbusViaTag + '] selected');
break;
case 'dl2':
ctx.connection = new vbus.TcpConnection({
host: this.config.connectionIdentifier,
port: this.config.connectionPort,
password: this.config.vbusPassword
});
this.log.info('TCP Connection to KM2/DL2 on [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected');
break;
case 'dl3':
ctx.connection = new vbus.TcpConnection({
host: this.config.connectionIdentifier,
port: this.config.connectionPort,
password: this.config.vbusPassword,
channel: this.config.vbusChannel
});
this.log.info('TCP Connection to DL3 on [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected');
}
// Connection state handler
ctx.connection.on('connectionState', (connectionState) => {
if (connectionState === 'CONNECTED') {
this.log.info('Connection established');
this.setStateAsync('info.connection', true, true);
} else {
this.log.info('Connection state changed to ' + connectionState);
this.setStateAsync('info.connection', false, true);
}
});
ctx.headerSet = new vbus.HeaderSet();
let hasSettled = false;
let settledCountdown = 0;
// Packet handler
ctx.connection.on('packet', (packet) => {
if (!hasSettled) {
const headerCountBefore = ctx.headerSet.getHeaderCount();
ctx.headerSet.addHeader(packet);
ctx.hsc.addHeader(packet);
const headerCountAfter = ctx.headerSet.getHeaderCount();
if (headerCountBefore !== headerCountAfter) {
ctx.hsc.emit('headerSet', ctx.hsc);
settledCountdown = headerCountAfter * 2;
} else if (settledCountdown > 0) {
settledCountdown -= 1;
} else {
hasSettled = true;
}
} else {
ctx.headerSet.addHeader(packet);
ctx.hsc.addHeader(packet);
}
});
ctx.hsc = new vbus.HeaderSetConsolidator({
interval: this.config.vbusInterval * 1000,
timeToLive: (this.config.vbusInterval * 1000) + 1000
});
// HeaderSetConsolidator handler - creates object tree and updates values in preset interval
ctx.hsc.on('headerSet', () => {
const packetFields = spec.getPacketFieldsForHeaders(ctx.headerSet.getSortedHeaders());
const data = _.map(packetFields, function (pf) {
return {
id: pf.id,
name: _.get(pf, ['packetFieldSpec', 'name', language]),
rawValue: pf.rawValue,
deviceName: pf.packetSpec.sourceDevice.fullName,
deviceId: pf.packetSpec.sourceDevice.deviceId.replace(/_/g, ''),
addressId: pf.packetSpec.sourceDevice.selfAddress,
unitId: pf.packetFieldSpec.type.unit.unitId,
unitText: pf.packetFieldSpec.type.unit.unitText,
typeId: pf.packetFieldSpec.type.typeId,
precision: pf.packetFieldSpec.type.precision,
rootTypeId: pf.packetFieldSpec.type.rootTypeId,
parts: pf.packetFieldSpec.parts,
};
});
this.log.debug('received data: ' + JSON.stringify(data));
if (data[1]) {
// create device
this.createOrExtendObject(data[1].deviceId, {
type: 'device',
common: {
name: data[1].deviceName,
type: 'string'
},
native: {}
}, undefined);
// create channel
this.createOrExtendObject(data[1].deviceId + '.' + data[1].addressId, {
type: 'channel',
common: {
name: data[1].deviceId + '.' + data[1].addressId,
type: 'string'
},
native: {}
}, undefined);
// create write dps
if (!myDeviceAddress) {
myDeviceAddress = data[1].addressId;
this.log.debug('myDeviceAddress: ' + myDeviceAddress+' myDeviceVersion: '+myDeviceVersion);
myDeviceID = data[1].deviceId;
this.generateDP(myDeviceAddress, myDeviceID);
}
const thisContext = {
connection: ctx.connection,
deviceAddress: myDeviceAddress,
deviceID: myDeviceID,
deviceVersion: myDeviceVersion
};
this.loadMyConfig(thisContext);
}
// iterate over all data to create datapoints
_.forEach(data, (item) => {
// this.log.debug('received item-data: ' + JSON.stringify(item));
const deviceId = item.deviceId.replace(/_/g, '');
const channelId = deviceId + '.' + item.addressId;
const objectId = channelId + '.' + item.id.replace(/_/g, '');
const isBitField = ((item.parts.length === 1) && (item.parts[0].mask !== 0xFF));
const isTimeField = ((item.rootTypeId === 'Time') || (item.rootTypeId === 'Weektime') || (item.rootTypeId === 'DateTime'));
const common = {
name: item.name,
type: 'number',
unit: item.unitText,
read: true,
write: false
};
let value;
if ((item.rawValue === undefined) || (item.rawValue === null)) {
value = 0;
} else if (item.rootTypeId === 'Number') {
value = Math.round(parseFloat(item.rawValue.toFixed(item.precision))*100)/100;
} else if (item.rootTypeId === 'Time') {
value = spec.i18n.moment(item.rawValue * 60000).utc().format('HH:mm');
} else if (item.rootTypeId === 'Weektime') {
value = spec.i18n.moment((item.rawValue + 5760) * 60000).utc().format('dd,HH:mm');
} else if (item.rootTypeId === 'DateTime') {
value = spec.i18n.moment((item.rawValue + 978307200) * 1000).utc().format('L HH:mm:ss');
}
switch (item.unitId) {
case 'DegreesCelsius':
common.min = -100;
common.max = +300;
common.role = 'value.temperature';
common.type = 'number';
if (Math.abs(Math.trunc(value)) === 999 || Math.abs(Math.trunc(value)) === 888) value = null;
break;
case 'Percent':
common.min = 0;
common.max = 100;
common.role = 'level.volume';
common.type = 'number';
// create Relay X active state (as far as we know these are the only percent-unit states )
this.createOrExtendObject(objectId + '_1', {
type: 'state',
common: {
name: relayActive.replace('X', item.name.substr(item.name.length - 2).replace(' ', '')),
type: 'boolean',
role: 'indicator.activity',
unit: '',
read: true,
write: false
}
}, (value > 0));
break;
case 'Hours':
common.role = 'value';
common.type = 'number';
break;
case 'WattHours':
common.role = 'value.power.generation';
common.type = 'number';
break;
case 'None':
if (!isBitField) {
if (isTimeField) {
common.role = 'value';
common.type = 'string';
} else {
common.role = 'value';
}
} else {
common.role = 'indicator.maintenance.alarm';
common.type = 'boolean';
value = (value === 1);
}
break;
default:
common.role = 'value';
common.type = 'number';
break;
}
this.createOrExtendObject(objectId, {type: 'state', common}, value);
});
});
// Establish connection
this.log.info('Waiting for Connection...');
let connected = false;
do {
try {
await ctx.connection.connect();
connected = true;
} catch(error){
await clearTimeout( setTimeout(()=>{
this.log.error(`Error on connection attempt: ${error.message}.`);
}, 10000));
}
} while ( !connected );
ctx.hsc.startTimer();
} catch (error) {
this.log.error(`[main()] error: ${error.message}, stack: ${error.stack}`);
}
}
// Is called when databases are connected and adapter received configuration.
async onReady() {
try {
// Terminate adapter after first start because configuration is not yet received
// Adapter is restarted automatically when config page is closed
this.configIsValid(this.config)
.then(result => {
this.log.info(result);
this.main();
})
.catch(err => {
this.log.error(err);
this.setStateAsync('info.connection', false, true);
this.terminate('Terminating Adapter until Configuration is completed', 11);
});
} catch (error) {
this.log.error(`[onReady] error: ${error.message}, stack: ${error.stack}`);
}
}
// Create or extend object
createOrExtendObject(id, objData, value) {
const self = this;
this.getObject(id, function (err, oldObj) {
if (!err && oldObj) {
self.extendObject(id, objData, () => {
if (typeof value !== 'undefined') self.setState(id, value, true);
});
} else {
self.setObjectNotExists(id, objData, () => {
if (typeof value !== 'undefined') self.setState(id, value, true);
});
}
});
}
// Exit adapter
onUnload(callback) {
try {
ctx.connection.disconnect();
this.log.info('Cleaned up everything...');
callback();
} catch (e) {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new resol(options);
} else {
// otherwise start the instance directly
new resol();
}