forked from klein0r/ioBroker.comfoairq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
444 lines (350 loc) · 15.3 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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core');
const comfoconnect = require('comfoairq');
const adapterName = require('./package.json').name.split('.').pop();
class Comfoairq extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: adapterName,
});
this.connected = false;
this.uuid = '20200428000000000000000009080408';
this.deviceName = 'iobroker';
this.zehnder = null;
this.sensors = [];
this.sensorUnits = {
117: '%',
118: '%',
119: 'm³/h',
120: 'm³/h',
121: 'rpm',
122: 'rpm',
128: 'W',
129: 'kWh',
130: 'kWh',
144: 'kWh',
145: 'kWh',
146: 'W',
192: 'days',
209: '°C',
213: 'W',
214: 'kWh',
215: 'kWh',
216: 'W',
217: 'kWh',
218: 'kWh',
221: '°C',
227: '%',
274: '°C',
275: '°C',
276: '°C',
290: '%',
291: '%',
292: '%',
294: '%'
};
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
await this.setStateAsync('info.connection', false, true);
// Get active sensors by configuration
for (const key of Object.keys(this.config)) {
if (key.indexOf('sensor_') === 0 && this.config[key]) {
this.sensors.push(Number(key.slice(7)));
}
}
/*
this.getForeignObjectAsync('system.adapter.' + this.namespace).then(data => {
this.log.debug('Current configuration: ' + JSON.stringify(data));
});
*/
await this.startZehnder();
}
async startZehnder() {
this.log.debug('start Zehnder');
if (this.config.host && this.config.port && this.config.uuid && this.config.pin) {
if (this.sensors.length > 0) {
this.log.debug('Active sensors by configuration: ' + JSON.stringify(this.sensors));
this.zehnder = new comfoconnect(
{
uuid: this.uuid,
device: this.deviceName,
comfoair: this.config.host,
port: Number(this.config.port),
comfouuid: this.config.uuid,
pin: parseInt(this.config.pin),
debug: false,
logger: this.log.debug
}
);
this.log.debug('register receive handler...');
this.zehnder.on('receive', async (data) => {
//this.log.debug('received: ' + JSON.stringify(data));
if (data && data.result.error == 'OK') {
if (data.kind == 40) { // 40 = CnRpdoNotification
const sensorId = data.result.data.pdid;
const sensorName = data.result.data.name;
const sensorNameClean = this.cleanNamespace(sensorName.replace('SENSOR', ''));
const sensorValue = data.result.data.data;
const unit = Object.prototype.hasOwnProperty.call(this.sensorUnits, sensorId) ? this.sensorUnits[sensorId] : '';
await this.setObjectNotExistsAsync('sensor.' + sensorNameClean, {
type: 'state',
common: {
name: sensorName + ' (' + sensorId + ')',
type: 'number',
role: 'value',
unit: unit,
read: true,
write: false
},
native: {
sensorId: sensorId
}
});
await this.setStateAsync('sensor.' + sensorNameClean, sensorValue, true);
} else if (data.kind == 68) { // 68 = VersionConfirm
await this.setStateAsync('version.comfonet', data.result.data.comfoNetVersion, true);
await this.setStateAsync('version.serial', data.result.data.serialNumber, true);
await this.setStateAsync('version.gateway', data.result.data.gatewayVersion, true);
}
}
});
this.log.debug('register disconnect handler...');
this.zehnder.on('disconnect', async (reason) => {
if (reason.state == 'OTHER_SESSION') {
this.log.warn('Other session started: ' + JSON.stringify(reason));
}
this.log.debug('disconnet_reason: ' + reason.state);
await this.disconnectZehnder(true);
/* this.connected = false;
this.setStateAsync('info.connection', false, true); */
});
// /* this.log.debug('register the app...');
const registerAppResult = await this.zehnder.RegisterApp();
this.log.debug('registerAppResult: ' + JSON.stringify(registerAppResult));
await this.connectZehnder();
//Start the session
// this.log.debug('startSession');
// const startSessionResult = await this.zehnder.StartSession(true);
// this.log.debug('startSessionResult:' + JSON.stringify(startSessionResult));
// for (let i = 0; i < this.sensors.length; i++) {
// const registerResult = await this.zehnder.RegisterSensor(this.sensors[i]);
// this.log.debug('Registered sensor "' + this.sensors[i] + '" with result: ' + JSON.stringify(registerResult));
// }
// this.zehnder.VersionRequest();
// await this.setStateAsync('info.connection', true, true);
// this.connected = true;
// this.subscribeStates('*'); */
} else {
this.log.warn('No active sensors found in configuration - stopping');
}
} else if (this.config.multicastAddr && this.config.port) {
// Dicover Zehnder devices
this.log.warn('Device information not configured - starting discovery on ' + this.config.multicastAddr);
this.zehnder = new comfoconnect(
{
uuid: this.uuid,
device: this.deviceName,
port: Number(this.config.port),
debug: false,
logger: this.log.debug
}
);
try {
const discoverResult = await this.zehnder.discover(this.config.multicastAddr);
this.log.info('Device discovery finished - use these information for instance configuration: ' + JSON.stringify(discoverResult));
} catch (ex) {
this.log.error('error while discovery: ' + JSON.stringify(ex));
}
} else {
this.log.error('Instance configuration invalid');
}
}
async connectZehnder() {
// this.log.debug('register the app...');
// const registerAppResult = await this.zehnder.RegisterApp();
// this.log.debug('registerAppResult: ' + JSON.stringify(registerAppResult));
// Start the session
this.log.debug('startSession');
const startSessionResult = await this.zehnder.StartSession(true);
this.log.debug('startSessionResult:' + JSON.stringify(startSessionResult));
for (let i = 0; i < this.sensors.length; i++) {
const registerResult = await this.zehnder.RegisterSensor(this.sensors[i]);
this.log.debug('Registered sensor "' + this.sensors[i] + '" with result: ' + JSON.stringify(registerResult));
}
this.zehnder.VersionRequest();
await this.setStateAsync('info.connection', true, true);
this.connected = true;
this.subscribeStates('*');
}
cleanNamespace(id) {
return id
.trim()
.replace(/\s/g, '_') // Replace whitespaces with underscores
.replace(/[^\p{Ll}\p{Lu}\p{Nd}]+/gu, '_') // Replace not allowed chars with underscore
.replace(/[_]+$/g, '') // Remove underscores end
.replace(/^[_]+/g, '') // Remove underscores beginning
.replace(/_+/g, '_') // Replace multiple underscores with one
.toLowerCase()
.replace(/_([a-z])/g, (m, w) => {
return w.toUpperCase();
});
}
async onUnload(callback) {
try {
/* this.log.debug('unloading...');
this.zehnder.CloseSession();
this.zehnder = null;
this.connected = false;
this.setStateAsync('info.connection', false, true);
*/
await this.disconnectZehnder(false);
callback();
} catch (e) {
callback();
}
}
async disconnectZehnder(restart) {
try {
if (this.connected) {
this.log.debug('unloading...');
this.zehnder.CloseSession();
this.connected = false;
this.setStateAsync('info.connection', false, true);
}
if (restart) {
this.log.debug('do restart...');
//await this.connectZehnder();
}
else {
this.log.debug('no restart...');
this.zehnder = null;
}
} catch (e) {
this.log.debug('disconnect failed...');
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (id && state && !state.ack) {
//this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (this.connected) {
const matches = id.match(new RegExp(this.namespace + '.command.([a-zA-Z0-9]+)'));
if (matches) {
const command = matches[1];
//this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
switch (command) {
case 'fanModeAway':
//this.log.debug('Sending command: FAN_MODE_AWAY');
this.zehnder.SendCommand(1, 'FAN_MODE_AWAY');
break;
case 'fanModeLow':
//this.log.debug('Sending command: FAN_MODE_LOW');
this.zehnder.SendCommand(1, 'FAN_MODE_LOW');
break;
case 'fanModeMedium':
//this.log.debug('Sending command: FAN_MODE_MEDIUM');
this.zehnder.SendCommand(1, 'FAN_MODE_MEDIUM');
break;
case 'fanModeHigh':
//this.log.debug('Sending command: FAN_MODE_HIGH');
this.zehnder.SendCommand(1, 'FAN_MODE_HIGH');
break;
case 'fanBoost10m':
//this.log.debug('Sending command: FAN_BOOST_10M');
this.zehnder.SendCommand(1, 'FAN_BOOST_10M');
break;
case 'fanBoost20m':
//this.log.debug('Sending command: FAN_BOOST_20M');
this.zehnder.SendCommand(1, 'FAN_BOOST_20M');
break;
case 'fanBoost30m':
//this.log.debug('Sending command: FAN_BOOST_30M');
this.zehnder.SendCommand(1, 'FAN_BOOST_30M');
break;
case 'fanBoostEnd':
//this.log.debug('Sending command: FAN_BOOST_END');
this.zehnder.SendCommand(1, 'FAN_BOOST_END');
break;
case 'modeAuto':
//this.log.debug('Sending command: MODE_AUTO');
this.zehnder.SendCommand(1, 'MODE_AUTO');
break;
case 'modeManual':
//this.log.debug('Sending command: MODE_MANUAL');
this.zehnder.SendCommand(1, 'MODE_MANUAL');
break;
case 'ventmodeSupply':
//this.log.debug('Sending command: VENTMODE_SUPPLY');
this.zehnder.SendCommand(1, 'VENTMODE_SUPPLY');
break;
case 'ventmodeBalance':
//this.log.debug('Sending command: VENTMODE_BALANCE');
this.zehnder.SendCommand(1, 'VENTMODE_BALANCE');
break;
case 'tempprofNormal':
//this.log.debug('Sending command: TEMPPROF_NORMAL');
this.zehnder.SendCommand(1, 'TEMPPROF_NORMAL');
break;
case 'tempprofCool':
//this.log.debug('Sending command: TEMPPROF_COOL');
this.zehnder.SendCommand(1, 'TEMPPROF_COOL');
break;
case 'tempprofWarm':
//this.log.debug('Sending command: TEMPPROF_WARM');
this.zehnder.SendCommand(1, 'TEMPPROF_WARM');
break;
case 'bypassOn':
//this.log.debug('Sending command: BYPASS_ON');
this.zehnder.SendCommand(1, 'BYPASS_ON');
break;
case 'bypassOff':
//this.log.debug('Sending command: BYPASS_OFF');
this.zehnder.SendCommand(1, 'BYPASS_OFF');
break;
case 'bypassAuto':
//this.log.debug('Sending command: BYPASS_AUTO');
this.zehnder.SendCommand(1, 'BYPASS_AUTO');
break;
}
}
else
{
this.log.debug(`No match!: state ${id} changed: ${state.val} (ack = ${state.ack})`);
}
}
else
{
this.log.debug(`Not connected!: state ${id} changed: ${state.val} (ack = ${state.ack})`);
}
}
else if (!state.ack)
{
this.log.debug(`OnStateChange: state ${id} changed: ${state.val} (ack = ${state.ack})`);
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Comfoairq(options);
} else {
// otherwise start the instance directly
new Comfoairq();
}