This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
368 lines (350 loc) · 13.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
'use strict';
/*
* Created with @iobroker/create-adapter v2.1.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 your modules here, e.g.:
const {EasyControlClient} = require('bosch-xmpp');
const {setIntervalAsync, clearIntervalAsync} = require('set-interval-async/fixed');
class Boscheasycontrol extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options, name: 'boscheasycontrol'
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
// own variables
this.initializing = false;
this.timers = {};
this.starttimers = {};
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
this.client = EasyControlClient({
serialNumber: this.config.serial, accessKey: this.config.accesskey, password: this.config.password
});
this.initializing = true;
if (!(
(
(this.config.serial && this.config.accesskey && this.config.password)
)
)) {
this.log.error('Serial, Access Key and Password needs to be defined for this adapter to work.');
}
else {
this.log.debug('connecting');
try {
await this.client.connect();
}
catch (e) {
this.log.error(e.stack || e);
}
this.setState('info.connection', true, true);
await this.subscribeStatesAsync('*');
await this.subscribeObjectsAsync('*');
await this.processurl('/');
this.initializing = false;
this.log.info('startup ... done');
await this.startalltimers();
}
}
/**
* @param {string} path
*/
async processurl(path) {
// /devices/productLookup is not working, so skipping
if (path === '/devices/productLookup') {
return;
}
let data;
this.log.debug('fetching new state for ' + path);
try {
data = await this.client.get(path);
}
catch (e) {
this.log.error(e.stack || e);
return;
}
this.log.debug('got data: ' + JSON.stringify(data));
if (data.type === 'refEnum') {
const s = data.id.split('/');
if (s.length === 3 && /[0-9]$/.test(s[2])) {
this.log.debug('creating device for ' + data.id);
await this.setObjectAsync(data.id.substring(1).split('/').join('.'), {
type: 'device', common: {
name: s[s.length-1]
}, native: {}
});
}
for (const ref in data.references) {
await this.processurl(data.references[ref].id);
}
}
if ('value' in data) {
await this.processdata(data);
}
}
/**
* @param {{ id: string; type: string; writeable: string; recordable: string; value: string | number | object;
* used: string; unitOfMeasure: string; minValue: number; maxValue: number; stepSize: number; }} data
*/
async processdata(data) {
this.log.debug('Data: id:' + data.id + ' type:' + data.type + ' writeable:' + data.writeable + ' recordable:' +
data.recordable + ' value:' + data.value + ' used: ' + data.used + ' unitOfMeasure:' + data.unitOfMeasure +
' minValue:' + data.minValue + ' maxValue: ' + data.maxValue + ' stepSize:' + data.stepSize);
const name = data.id.substring(1).split('/').join('.');
let mytype;
let value;
switch (data.type) {
case 'stringValue':
case 'stringArray':
mytype = 'string';
if (name.endsWith('.name') || name.endsWith('.email') || name.endsWith('.currency') || name.endsWith('.phone')) {
value = Buffer.from(data.value, 'base64').toString();
}
else {
value = data.value;
}
break;
case 'floatValue':
mytype = 'number';
value = data.value;
break;
case 'systeminfo':
mytype = 'object';
value = JSON.stringify(data.value);
break;
case 'zoneConfigArray':
case 'zoneArray':
case 'deviceArray':
case 'programArray':
mytype = 'object';
for (const i in data.value) {
data.value[i].name = Buffer.from(data.value[i].name, 'base64');
}
value = JSON.stringify(data.value);
break;
case 'addressInfo':
mytype = 'object';
for (const i in data.value) {
for (const [key, value] of Object.entries(data.value[i])) {
data.value[i][key] = Buffer.from(value, 'base64').toString();
}
}
value = JSON.stringify(data.value);
break;
case 'dhwProgram':
case 'energyRecordings':
case 'eventArray':
case 'notificationStruct':
case 'boostShortcutStruct':
case 'boostZoneStruct':
case 'errorList':
case 'dayProgram':
case 'weekProgram':
case 'autoAwayArray':
mytype = 'object';
value = JSON.stringify(data.value);
break;
}
if (mytype) {
if (this.initializing) {
const obj = await this.getObjectAsync(name);
if (obj) {
await this.onObjectChange(obj._id, obj);
}
else {
let common;
if (mytype === 'string') {
common = {
name: data.id.split('/').slice(-1)[0],
type: 'string',
read: true,
write: Boolean(data.writeable),
role: 'value',
unit: data.unitOfMeasure,
};
}
else if (mytype === 'number') {
common = {
name: data.id.split('/').slice(-1)[0],
type: 'number',
read: true,
write: Boolean(data.writeable),
role: 'value',
min: Number(data.minValue),
max: Number(data.maxValue),
step: Number(data.stepSize),
unit: data.unitOfMeasure,
};
}
else {
common = {
name: data.id.split('/').slice(-1)[0],
type: 'string',
read: true,
write: Boolean(data.writeable),
role: 'value',
unit: data.unitOfMeasure,
};
}
this.log.info('creating new object with: ' + JSON.stringify(common));
await this.setObjectAsync(name, {
type: 'state', common: common, native: {}
});
}
}
if (name.endsWith('.name')) {
const parent_name = name.split('.').slice(0, -1).join('.');
const parent = await this.getObjectAsync(parent_name);
if (parent) {
parent.common.name = value;
await this.setObjectAsync(parent_name, parent);
this.log.debug('updated name of ' + parent_name + ' to: ' + JSON.stringify(parent));
}
}
this.log.debug('updating ' + name + ' to ' + value);
await this.setStateAsync(name, value, true);
}
else {
this.log.warn(
'got unknown data with id:' + data.id + ' type:' + data.type + ' writeable:' + data.writeable +
' recordable:' + data.recordable + ' value:' + JSON.stringify(data.value) + ' used: ' + data.used +
' unitOfMeasure:' + data.unitOfMeasure + ' minValue:' + data.minValue + ' maxValue: ' + data.maxValue +
' stepSize:' + data.stepSize);
}
}
async startalltimers() {
for (const [key, value] of Object.entries(this.starttimers)) {
this.log.debug('calling starttimer for ' + key + ' interval ' + value);
await this.starttimer(key, value);
}
}
/**
* @param {string} name
* @param {number} interval
*/
async starttimer(name, interval) {
if (name.endsWith('.info.connection')) {
return;
}
await this.stoptimer(name);
const nslice = name.split('.');
const path = '/' + nslice.slice(2).join('/');
this.timers[name] = setIntervalAsync(this.processurl.bind(this, path), interval * 1000);
this.log.debug('started update timer for ' + name + ' with interval ' + interval + 's');
}
/**
* @param {string} name
*/
async stoptimer(name) {
if (name in this.timers) {
this.log.debug('stopping timer for: ' + name);
await clearIntervalAsync(this.timers[name]);
delete this.timers[name];
this.log.debug('stopped update timer for ' + name);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
async onUnload(callback) {
try {
this.client.end();
for (const name in this.timers) {
await this.stoptimer(name);
}
this.setState('info.connection', false, true);
callback();
}
catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
async onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.debug(`object ${id} changed: ${JSON.stringify(obj)}`);
if (obj.common.custom && obj.common.custom[`${this.name}.${this.instance}`]) {
if (obj.common.custom[`${this.name}.${this.instance}`].enabled) {
if (this.initializing) {
this.log.debug('adding timer for start ' + id);
this.starttimers[id] = obj.common.custom[`${this.name}.${this.instance}`].refresh;
}
else {
this.log.debug('calling starttimer for ' + id);
await this.starttimer(id, obj.common.custom[`${this.name}.${this.instance}`].refresh);
}
} else {
this.log.debug('calling stoptimer 1 for ' + id);
await this.stoptimer(id);
}
} else {
this.log.debug('calling stoptimer 2 for ' + id);
await this.stoptimer(id);
}
} else {
// The object was deleted
this.log.debug(`object ${id} deleted`);
await this.stoptimer(id);
}
this.log.debug('running timers: ' + Object.keys(this.starttimers));
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state) {
// The state was changed
this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (state.ack === false) {
try {
const nslice = id.split('.');
const path = '/' + nslice.slice(2).join('/');
const ret = this.client.put(path, {'value':state.val}); // not tested
this.log.debug('set state returned: ' + ret);
await this.processurl(path);
}
catch (e) {
this.log.error(e.stack || e);
}
}
}
else {
// The state was deleted
this.log.debug(`state ${id} deleted`);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Boscheasycontrol(options);
}
else {
// otherwise start the instance directly
new Boscheasycontrol();
}