forked from iobroker-community-adapters/ioBroker.km200
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkm200.js
511 lines (472 loc) · 19.9 KB
/
km200.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
/**
*
* Buderus KM200 Adapter
* v 1.1.1 2017.10.03
*/
/*jshint -W030*/
//// jxshint node:true, esversion:6, strict:true, undef:true, unused:true
"use strict";
const MCrypt = require('mcrypt').MCrypt,
utils = require('@iobroker/adapter-core'),
adapter = utils.Adapter('km200'),
crypto = require('crypto'),
// assert = require('assert'),
A = require('./myAdapter');
//const EventEmitter = require('events').EventEmitter;
const km200_crypt_md5_salt = new Uint8Array([
0x86, 0x78, 0x45, 0xe9, 0x7c, 0x4e, 0x29, 0xdc,
0xe5, 0x22, 0xb9, 0xa7, 0xd3, 0xa3, 0xe0, 0x7b,
0x15, 0x2b, 0xff, 0xad, 0xdd, 0xbe, 0xd7, 0xf5,
0xff, 0xd8, 0x42, 0xe9, 0x89, 0x5a, 0xd1, 0xe4
]);
A.init(adapter, main); // associate adapter and main with MyAdapter
class KM200 {
constructor() {
this.crypt = null;
this.aesKey = null; // buffer will be generated on init from accessKey
this.options = null;
this.scannedServices = null;
this.blocked = [];
this.pushed = [];
this.basicServices = [
"/dhwCircuits",
"/gateway",
"/heatingCircuits",
"/heatSources",
"/notifications",
"/recordings",
"/solarCircuits",
"/system",
];
}
/*
* initialize KM200
* accessUrl = string z.b. 192.168.1.xxx oder wie bei mir BuderusKM200.fritz.box
* accessPort = sollte 80 sein außer er ist über den Router umgebogen :), wenn leer oder 0 wird er auf 80 gesetzt
* accessKey = hex string like 'b742c3085bcaeac989353b7655c016dda46e567fe6e8a609e8ea796e20a78a33' which you got from https://ssl-account.com/km200.andreashahn.info/
* pollTime = in wie vielen Minuten werden
*/
init(accessUrl, accessKey) {
if (!accessUrl || !accessKey)
return A.W(`KM200.init argument error:init(${accessUrl}, ${accessKey}), no init done!`);
this.aesKey = new Buffer(accessKey, 'hex');
this.scannedServices = null;
this.blocked = [];
this.crypt = new MCrypt('rijndael-128', 'ecb');
this.crypt.open(this.aesKey);
this.options = {
hostname: accessUrl,
timeout: 10000,
status: [200],
encoding: 'utf8',
port: 80,
headers: {
'agent': 'TeleHeater/2.2.3',
'User-Agent': 'TeleHeater/2.2.3',
'Accept': 'application/json',
}
};
if (accessUrl.indexOf(':') > 1) {
let au = A.trim(A.split(accessUrl, ':'));
this.options.hostname = accessUrl = au[0];
this.options.port = au[1];
}
A.D(`KM200 init(${accessUrl}, ${accessKey}) done!`);
}
addBlocked(list) {
if (!list)
return A.W('KM200.setBlocked no list provided as argument!');
if (!Array.isArray(list))
list = [list];
// for(let i=0; i<list.length;i++) {
// let li = list[i];
for (let li of list) {
let ispush = false;
if (li.startsWith('+')) {
li = li.slice(1, li.length);
ispush = true;
} else if (li.startsWith('-'))
li = li.slice(1, li.length);
if (!li.startsWith('^/')) {
if (!li.startsWith('/')) {
if (!li.startsWith('^'))
li = '^/' + li;
else
li = '^/' + li.slice(1, li.length);
} else
li = '^' + li;
}
if (!li.endsWith('$'))
li += '$';
const j = li.indexOf('*');
if (j > 1 && li[j - 1] !== '.')
li = li.slice(0, j) + '.' + li.slice(j, li.length);
(ispush ? this.pushed : this.blocked).push(A.D(`add to ${ispush ? 'pushed' : 'blocked'} ${li}`, new RegExp(li)));
}
}
/*
* Get data from KM200
* service = string of service like '/system' to access
* callback(err,data) with received data, either an array or an object
*
*/
get(service) {
if (!service || service.length < 2 || service[0] !== '/')
return Promise.reject(A.W(`KM200.get service parameter not as requested '${A.O(service)}'`));
if (!this.crypt || !this.options)
return Promise.reject(A.W(`KM200.get not initialized for decryption! Will not work ${A.O(service)}'`));
const opt = A.url('http://' + this.options.hostname + service, this.options);
opt.method = 'GET';
// opt.url = opt.hostname + service;
opt.status = [200, 403];
return A.retry(2, () => A.request(opt)
.then((data) => {
if (!data)
return Promise.reject(`No Data for ${service}!`);
const b = new Buffer(data, 'base64');
let o = null;
try {
let s = b.toString('hex');
// A.D('fh'+s);
s = this.crypt.decrypt(b).toString('utf8');
while (s.charCodeAt(s.length - 1) === 0)
s = s.slice(0, s.length - 1);
o = A.J(s);
adapter.log.debug('Service: ' + service + ' Payload: ' + JSON.stringify(s));
} catch (e) {
return Promise.reject(`KM200 response Error for ${service}, most probabloy Key not accepted :${A.O(e, 3)}`);
}
if (o && o.references)
o = o.references;
return o;
}, (err) => err.indexOf('status 403/') > 0 ? Promise.resolve() : Promise.reject(err)));
// A.D(A.O(opt));
}
set(service, value) {
const post = this.crypt.encrypt(JSON.stringify({
value: value
})).toString('base64');
const opt = A.url('http://' + this.options.hostname + service, this.options);
opt.headers["Content-Type"] = "application/json";
opt.path = service;
opt.method = 'POST';
opt.status = [200, 204];
return A.request(opt, post);
}
isBlocked(id) {
for (let i = 0; i < this.pushed.length; ++i)
if (this.pushed[i].test(id))
return false;
for (let i = 0; i < this.blocked.length; ++i)
if (this.blocked[i].test(id))
return true;
return false;
}
getServices(service) {
let level = false;
if (!service) {
service = this.basicServices;
this.scannedServices = {};
level = true;
}
if (!Array.isArray(service))
return Promise.reject(A.I(`Invalid (not Array) getService for ${A.O(service)}`));
A.D(`try to get services for ${A.O(service)}`);
return A.seriesOf(service, (item) => {
if (this.isBlocked(item))
return Promise.resolve(null);
return this.get(item)
.then((data) => {
// A.D(`get returned ${A.O(data)}`)
if (!data)
return null;
if (Array.isArray(data)) {
return A.seriesOf(data, (di) => {
// A.D(`array had ${A.O(di)}`)
if (di && di.id && di.uri && !this.isBlocked(di.id))
return this.getServices([di.id]);
return Promise.resolve();
}, 10);
} else {
if (!this.isBlocked(item)) {
if (data.setpointProperty)
return this.getServices(A.D(`setPointProperty = ${data.setpointProperty.id}`, [data.setpointProperty.id]));
if (data.recordedResource)
return this.getServices(A.D(`recordedResource = ${data.recordedResource.id}`, [data.recordedResource.id]));
const d = item.split('/').slice(1).join('.');
this.scannedServices[d] = data;
return Promise.resolve(A.D(`Service[${d}]=${A.O(data)}`, null));
}
}
return null;
}).catch((err) => A.D(`could not get data for '${item} with err=${err}`));
}, 10).then(() => {
if (!level) return Promise.resolve();
const s = Object.keys(this.scannedServices);
if (s.length === 0)
return A.W(`Did not get any Services from KLM200!: ${A.O(this.scannedServices)}`);
const ns = {};
for (let i of s.sort())
ns[i] = this.scannedServices[i];
this.scannedServices = ns;
return ns;
});
}
}
const km200 = new KM200();
var mtimeout = null;
var states = {};
// is called if a subscribed state changes
A.stateChange = function (id, state) {
// Warning, state can be null if it was deleted
// adapter.log.info(adapter.instance + ' stateChange ' + id + ' ' + A.O(state));
// you can use the ack flag to detect if it is status (true) or command (false)
// adapter.log.info(id+' stateChange to '+ A.O(state));
let iid = id.split('.').slice(2);
const serv = '/' + iid.join('/');
iid = iid.join('.');
// adapter.log.info("km200.set "+serv+" = "+state.val);
let val = state.val;
iid = states[iid];
if (iid && iid.common.states) { // convert states in ioBroker to allowed string values for KM200
const sa = iid.common.states.split(';');
val = sa[state.val].split(':')[1];
// adapter.log.info('Check Converted for '+iid+' State '+A.O(iid) + ' to ' + val);
}
return km200.set(serv, val)
.then(() => {
A.I(`Set ${id} to ${state.val}`);
// adapter.log.info('KM200.set '+serv + " changed to "+state.val);
const ids = id.split('.').slice(2).join('.');
const ite = {};
ite[ids] = states[ids];
// adapter.log.info('Set KM200 returned: '+ A.O(ite));
return true;
}, (err) => A.W(`Set KM200 err: ${A.O(err)}`, err))
.then(() => (A.wait(2000).then(() => updateStates(id)), true));
};
function minutes(min) {
const val = min * 1000 * 60;
const d = Math.floor(val / (1000 * 60.0 * 60 * 24));
return (d > 0 ? d.toString() + "-" : "") + new Date(val).toUTCString().split(" ")[4].slice(0, 5);
}
function createStates() {
states = {};
// I got Types:{ floatValue: 89, stringValue: 36, switchProgram: 4, systeminfo: 3, errorList: 1, yRecording: 8, arrayData: 5 }
// Units:{ C: 34, undefined: 57, 'l/min': 1, mins: 7, '%': 12, kW: 13, 'µA': 2, Pascal: 2, kWh: 6, 'kg/l': 2, ' ': 6, 'l/h': 2, bar: 2 }
// looks like: { id: 146, type: 146, writeable: 146, recordable: 142, value: 125, unitOfMeasure: 89,
// allowedValues: 27, setpointProperty: 4, maxNbOfSwitchPoints: 4, maxNbOfSwitchPointsPerDay: 4, switchPointTimeRaster: 4,
// switchPoints: 4, minValue: 12, maxValue: 12, values: 9, recordedResource: 8, interval: 8, sampleRate: 8,
// 'recording-type': 8, recording: 8 }
return A.seriesIn(km200.scannedServices, (n) => {
let o = km200.scannedServices[n];
let t = o.type;
let u = o.unitOfMeasure;
let v = o.value;
o.valIs = "value";
if (v === -3276.8) // remove unused/unconnected values
return Promise.resolve('');
let w = !!o.writeable;
let r = w ? 'level' : 'value';
let s = false;
if (u === 'C') {
u = '°C';
r += '.temperature';
} else if (typeof u === 'undefined')
u = "";
switch (t) {
case 'stringValue':
if (Array.isArray(o.allowedValues)) {
o.valIs = 'states';
t = 'number';
v = o.allowedValues.indexOf(o.value);
s = [];
for (let ii = 0; ii < o.allowedValues.length; ++ii)
s.push(ii.toString() + ':' + o.allowedValues[ii]);
s = s.join(';');
} else
t = 'string';
break;
case 'floatValue':
t = 'number';
break;
case 'systeminfo':
case 'errorList':
case 'arrayData':
v = A.O(o.values);
o.valIs = "values";
t = 'string';
w = false;
break;
case 'switchProgram':
v = o.switchPoints;
o.valIs = "switchPoints";
t = 'array';
w = false;
break;
default: // don't process others'
return Promise.resolve(null);
}
if (u === 'mins') {
t = 'string';
v = minutes(parseInt(v));
}
const c = {
type: 'state',
id: n,
common: {
id: n,
name: n,
type: t,
unit: u,
read: true,
write: w,
role: r,
},
native: {}
};
if (s) {
c.common.states = s;
c.common.min = 0;
c.common.max = o.allowedValues.length - 1;
}
if (typeof o.minValue !== 'undefined')
c.common.min = o.minValue;
if (typeof o.maxValue !== 'undefined')
c.common.max = o.maxValue;
c.native.km200 = o;
c.common.native = { km200: o };
states[n] = c;
return A.makeState(c.common, v, true);
}, 10).then(() => {
const st = Object.keys(states);
A.I(`KM200 finished creation of ${st.length} states: ${A.O(st)}`);
// subscribe to states only now, but after we managed to write the TODO:
return Promise.resolve(adapter.subscribeStates('*'));
});
}
function updateStates(items) {
if (typeof items === 'string') {
const ai = adapter.name + '.' + adapter.instance + '.';
if (items.startsWith(ai))
items = items.slice(ai.length, items.length);
const ni = {};
ni[items] = states[items];
if (!states[items])
return A.I(`Could not find state for ${items}`);
else
A.I(`Update ${A.O(ni)}`);
items = ni;
} else
if (!items) items = states;
A.seriesIn(items, (n) => {
const o = items[n];
const km = o.native.km200;
return km200.get(km.id)
.then((data) => {
let val = null;
if (km.valIs === 'states')
val = data.allowedValues.indexOf(data.value);
else
val = data[km.valIs];
if (km.unitOfMeasure === 'mins')
val = minutes(parseInt(val));
return A.makeState(n, val, true);
}).catch((err) => A.I(`Update State ${n} err: ${A.O(err)}`));
}, 5);
}
var ain = '';
function md5(text) {
return crypto.createHash('md5').update(text).digest("hex");
}
function str2ab(str) {
let buf = new ArrayBuffer(str.length * 1); // 2 bytes for each char
let bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return bufView;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function concatUint8Array(array1, array2) {
const array3 = new Uint8Array(array1.length + array2.length);
for (let i = 0; i < array1.length; i++) {
array3[i] = array1[i];
}
for (let i = 0; i < array2.length; i++) {
array3[array1.length + i] = array2[i];
}
return array3;
}
function getAccesskey() {
if (adapter.config.gatewaypassword && adapter.config.privatepassword) {
adapter.config.gatewaypassword = adapter.config.gatewaypassword.replace(/-/g, '');
let km200_gateway_password = str2ab(adapter.config.gatewaypassword);
let km200_private_password = str2ab(adapter.config.privatepassword);
// Erste Hälfte des Schlüssels: MD5 von ( Gerätepasswort . Salt )
let key_1 = md5(concatUint8Array(km200_gateway_password, km200_crypt_md5_salt));
// Zweite Hälfte des Schlüssels - initial: MD5 von ( Salt )
let key_2_initial = md5(km200_crypt_md5_salt);
// Zweite Hälfte des Schlüssels - privat: MD5 von ( Salt . privates Passwort )
let key_2_private = md5(concatUint8Array(km200_crypt_md5_salt, km200_private_password));
let km200_crypt_key_initial = key_1 + key_2_initial;
let km200_crypt_key_private = key_1 + key_2_private;
return km200_crypt_key_private.trim().toLowerCase();
} else {
return undefined;
}
}
function main() {
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config:
if (parseInt(adapter.config.interval) < 5)
adapter.config.interval = 5;
if ((A.debug = adapter.config.adresse.startsWith('debug!')))
adapter.config.adresse = adapter.config.adresse.slice(A.D(`Debug mode on!`, 6)).trim();
if (!adapter.config.adresse || adapter.config.adresse.length < 2)
return A.W(`config KM200 Addresse not available or too short: ${adapter.config.adresse}`);
if (!adapter.config.accesskey.length === 0 || (adapter.config.privatepassword.length > 0 && adapter.config.gatewaypassword.length > 0)) {
adapter.config.accesskey = getAccesskey();
}
adapter.log.info("Acceskey for Device : " + adapter.config.accesskey);
// adapter.config.accesskey = adapter.config.accesskey.trim().toLowerCase();
if (!adapter.config.accesskey || !(/^[0-9a-f]{64}$/.test(adapter.config.accesskey)))
return A.W(`config KM200 AccessKey seems to be invalid (need to be a hex string of 64 characters):
${A.O(adapter.config.accesskey)}`);
ain = adapter.name + '.' + adapter.instance + '.';
A.I(`${ain} address: http://${adapter.config.adresse}`);
km200.init(adapter.config.adresse, adapter.config.accesskey);
// var blacklist = A.J(adapter.config.blacklist);
let blacklist = A.trim(A.split(adapter.config.blacklist.replace(/"|\[|\]/g, ' '), ','));
if (blacklist && Array.isArray(blacklist))
km200.addBlocked(blacklist);
else
A.W(`KM200: invalid black/whitelist will be ignored:'${adapter.config.blacklist}'
need to be an Array with []`);
A.I(`Interval=${adapter.config.interval} min, Black/Push-list: ${blacklist}`);
km200.getServices()
.then((obj) => {
if (!obj || Object.keys(obj).length === 0) {
adapter.log.error(`Did not get any Services from KLM200!: ${A.O(obj)}, will stop adapter.`);
// adapter.stop();
// process.exit();
return Promise.reject('Did not get any Services from KLM200!');
}
A.I(`Services found: ${Object.keys(obj).length}`);
return createStates();
})
.then(() => A.getObjectList({
startkey: ain,
endkey: ain + '\u9999'
})).then((res) => A.seriesOf(res.rows, (item) => { // clean all states which are not part of the list
if (states[item.id.slice(ain.length)])
return Promise.resolve();
return A.deleteState(item.id)
.then(() => A.D(`Del State: ${item.id}`), () => null) ///TC
.then(() => A.delObject(item.id))
.then(() => A.D(`Del Object: ${item.id}`), () => null) ///TC
.catch(() => null);
}, 10)).then(() => A.timer = mtimeout = setInterval(updateStates, adapter.config.interval * 1000 * 60));
}