-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
537 lines (477 loc) · 18.5 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
'use strict';
const utils = require('@iobroker/adapter-core');
const wolfsmartset = require('./lib/wss');
const timeoutHandler = [];
let device = {};
const ValList = [];
let ParamObjList = [];
//const objects = {};
class WolfSmartset extends utils.Adapter {
/**
* @param [options]
*/
constructor(options) {
super({
...options,
name: 'wolf-smartset',
});
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));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.onlinePoll = 4;
this.emptyCount = 0;
try {
device = JSON.parse(this.config.devices);
//parseWebFormat
if (typeof device.Id !== 'undefined') {
device.SystemId = device.Id;
}
if (
this.config.user &&
this.config.password &&
this.config.user !== '' &&
this.config.password !== '' &&
typeof device.GatewayId !== 'undefined' &&
typeof device.SystemId !== 'undefined'
) {
this.wss = new wolfsmartset(this.config.user, this.config.password, this);
await this.wss.openIdInit();
await this.main();
} else {
this.wss = new wolfsmartset('', '', this);
this.log.warn('Please configure user, password and device in config');
}
} catch (error) {
this.wss = new wolfsmartset('', '', this);
this.log.error('Please configure user, password and device in config');
this.log.error(error.stack);
}
}
async main() {
this.config.pingInterval = parseInt(this.config.pingInterval, 10) || 15;
// Abfrageintervall mindestens 15 sec.
if (this.config.pingInterval < 15) {
this.config.pingInterval = 15;
}
await this.wss.init();
try {
const GUIdesk = await this.wss.getGUIDescription(device.GatewayId, device.SystemId);
if (GUIdesk) {
ParamObjList = (await getParamsWebGui(GUIdesk)) || [];
}
if (ParamObjList) {
await this.CreateParams(ParamObjList);
}
this.objects = await this.getForeignObjectsAsync(`${this.namespace}.*`);
this.log.debug(JSON.stringify(this.objects));
await this.PollValueList();
} catch (error) {
this.log.warn(error);
this.log.warn('Try again in 60 sek.');
if (timeoutHandler['restartTimeout']) {
clearTimeout(timeoutHandler['restartTimeout']);
}
timeoutHandler['restartTimeout'] = setTimeout(async () => {
this.main();
}, 60000);
}
// //find Parameter for App Objects
// async function getParams(guiData) {
// if (guiData == null) {
// return;
// }
// const param = [];
// guiData.UserSystemOverviewData.forEach(UserSystemOverviewData => {
// const tabName = UserSystemOverviewData.TabName;
// UserSystemOverviewData.ParameterDescriptors.forEach(ParameterDescriptors => {
// const paramInfo = ParameterDescriptors;
// //search duplicate
// const find = param.find(element => element.ParameterId === paramInfo.ParameterId);
// if (find) {
// //this.log.debug('find double: ' + paramInfo.Name)
// } else {
// paramInfo.TabName = tabName;
// param.push(paramInfo);
// }
// });
// });
// return param;
// }
async function getParamsWebGui(guiData) {
if (guiData == null) {
return;
}
const param = [];
guiData.MenuItems.forEach(MenuItems => {
const tabName = MenuItems.Name;
MenuItems.TabViews.forEach(TabViews => {
const tabName2 = `${tabName}.${TabViews.TabName}`;
TabViews.ParameterDescriptors.forEach(ParameterDescriptors => {
const paramInfo = ParameterDescriptors;
//search duplicate
const find = param.find(element => element.ParameterId === paramInfo.ParameterId);
if (!find) {
paramInfo.TabName = tabName2.replace(' ', '_');
param.push(paramInfo);
}
});
});
//Fachmannebene
MenuItems.SubMenuEntries.forEach(SubMenuEntry => {
const tab = SubMenuEntry.Name;
SubMenuEntry.TabViews.forEach(TabViews => {
const tabName2 = `${tabName}.${tab}.${TabViews.TabName}`;
TabViews.ParameterDescriptors.forEach(ParameterDescriptors => {
const paramInfo = ParameterDescriptors;
//search duplicate
const find = param.find(element => element.ParameterId === paramInfo.ParameterId);
if (!find) {
paramInfo.TabName = tabName2.replace(' ', '_');
param.push(paramInfo);
}
});
});
});
});
return param;
}
this.subscribeStates('*');
}
async PollValueList() {
this.onlinePoll++;
if (timeoutHandler['pollTimeout']) {
clearTimeout(timeoutHandler['pollTimeout']);
}
try {
const recValList = await this.wss.getValList(device.GatewayId, device.SystemId, ValList);
if (recValList) {
await this.SetStatesArray(recValList);
}
if (this.onlinePoll > 4) {
this.onlinePoll = 0;
const systemStatus = await this.wss.getSystemState(parseInt(device.SystemId));
if (systemStatus && typeof systemStatus.IsOnline !== 'undefined') {
await this.setStateAsync('info.connection', {
val: systemStatus.IsOnline,
ack: true,
});
} else {
await this.setStateAsync('info.connection', {
val: false,
ack: true,
});
}
}
} catch (error) {
this.log.warn(error);
}
timeoutHandler['pollTimeout'] = setTimeout(() => {
this.PollValueList();
}, this.config.pingInterval * 1000);
}
async SetStatesArray(array) {
if (array.Values.length === 0) {
this.emptyCount++;
} else {
this.emptyCount = 0;
}
if (this.emptyCount >= 10) {
// no data for long time try a restart
this.emptyCount = 0;
this.main();
return;
}
array.Values.forEach(recVal => {
//this.log.debug("search:" + JSON.stringify(recVal));
//find ParamId for ValueId
const findParamObj = ParamObjList.find(element => element.ValueId === recVal.ValueId);
if (findParamObj) {
for (const key in this.objects) {
if (this.objects[key].native && this.objects[key].native.ParameterId === findParamObj.ParameterId) {
this.setStatesWithDiffTypes(this.objects[key].native.ControlType, key, recVal.Value);
}
}
}
});
}
async setStatesWithDiffTypes(type, id, value) {
if (type == null || id == null || value == null) {
return;
}
// Wolf ControlTypes:
// 0: Unknown
// 1: Enum w/ ListItems (simple)
// 5: Bool
// 6: Number; 'Decimals' = decimal places (accuracy)
// 9: Date
// 10: Time
// 13: list of time programs (1, 2 or 3)
// 14: list of time ranges
// 19: time program (Mon - Sun)
// 20: Name, SerialNo, MacAddr, SW-Version, HW-Version
// 21: IPv4 addr or netmask
// 31: Number range
// 35: Enum w/ ListItems (w/ Image, Decription, ...)
switch (type) {
case 5:
this.setStateAsync(id, {
val: value === 'True' ? true : false,
ack: true,
});
break;
case 20:
case 21:
case 10:
case 9:
this.setStateAsync(id, {
val: value.toString(),
ack: true,
});
break;
default:
this.setStateAsync(id, {
val: parseFloat(value),
ack: true,
});
break;
}
}
async CreateParams(paramArry) {
const collectedChannels = {};
for (const WolfObj of paramArry) {
let group = '';
let id = '';
if (WolfObj.Group) {
group = `${WolfObj.Group.replace(' ', '_')}.`;
}
collectedChannels[`${WolfObj.TabName}.${group}`] = true;
id = `${WolfObj.TabName}.${group}${WolfObj.ParameterId}`;
const common = {
name:
typeof WolfObj.NamePrefix !== 'undefined' ? `${WolfObj.NamePrefix}: ${WolfObj.Name}` : WolfObj.Name,
type: 'number',
role: 'value',
read: true,
write: !WolfObj.IsReadOnly,
};
// Wolf ControlTypes:
// 0: Unknown
// 1: Enum w/ ListItems (simple)
// 5: Bool
// 6: Number; 'Decimals' = decimal places (accuracy)
// 9: Date
// 10: Time
// 13: list of time programs (1, 2 or 3)
// 14: list of time ranges
// 19: time program (Mon - Sun)
// 20: Name, SerialNo, MacAddr, SW-Version, HW-Version
// 21: IPv4 addr or netmask
// 31: Number range
// 35: Enum w/ ListItems (w/ Image, Decription, ...)
if (WolfObj.ControlType === 5) {
//Boolean text
common.type = 'boolean';
common.role = WolfObj.IsReadOnly ? 'indicator' : 'switch';
} else if (
WolfObj.ControlType === 20 ||
WolfObj.ControlType === 9 ||
WolfObj.ControlType === 10 ||
WolfObj.ControlType === 21
) {
common.type = 'string';
common.role = 'text';
} else {
if (typeof WolfObj.Unit !== 'undefined') {
common.unit = WolfObj.Unit;
}
// thresholds min/max : use Min/MaxValueCondition if available, otherwise use MinValue/MaxValue
// Min/MaxValue might be wrong in case of floats, whereas Min/MaxValueCondition seem to be always correct
if (typeof WolfObj.MinValue !== 'undefined') {
common.min = WolfObj.MinValue;
}
if (typeof WolfObj.MinValueCondition !== 'undefined') {
common.min = parseFloat(WolfObj.MinValueCondition);
}
if (typeof WolfObj.MaxValue !== 'undefined') {
common.max = WolfObj.MaxValue;
}
if (typeof WolfObj.MaxValueCondition !== 'undefined') {
common.max = parseFloat(WolfObj.MaxValueCondition);
}
if (typeof WolfObj.StepWidth !== 'undefined') {
common.step = WolfObj.StepWidth;
}
if (typeof WolfObj.ListItems !== 'undefined') {
const states = {};
WolfObj.ListItems.forEach(ListItems => {
states[ListItems.Value] = ListItems.DisplayText;
});
common.states = states;
}
}
// gereate ValueList for Polling
ValList.push(WolfObj.ValueId);
this.log.debug(`WolfObj ${JSON.stringify(WolfObj)} --> ioBrokerObj.common ${JSON.stringify(common)}`);
await this.extendObjectAsync(id, {
type: 'state',
common,
native: {
ValueId: WolfObj.ValueId,
ParameterId: WolfObj.ParameterId,
ControlType: WolfObj.ControlType,
},
});
await this.setStatesWithDiffTypes(WolfObj.ControlType, id, WolfObj.Value);
}
const createdObjects = {};
for (let channel of Object.keys(collectedChannels)) {
channel = channel.substring(0, channel.length - 1);
const name = channel.split('.').pop();
await this.extendObjectAsync(channel, {
type: 'channel',
common: {
name,
},
native: {},
});
createdObjects[channel] = true;
this.log.debug(`Create channel ${channel}`);
const channelParts = channel.split('.');
let id = channelParts.shift() || '';
let folderName = id;
while (id && channelParts.length > 0) {
if (!createdObjects[id]) {
await this.extendObjectAsync(id, {
type: 'folder',
common: {
name: folderName,
},
native: {},
});
this.log.debug(`Create folder ${id}`);
createdObjects[id] = true;
}
folderName = channelParts.shift() || '';
if (!folderName) {
break;
}
id += `.${folderName}`;
}
}
this.log.debug('create states DONE');
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*
* @param callback
*/
onUnload(callback) {
try {
if (timeoutHandler['pollTimeout']) {
clearTimeout(timeoutHandler['pollTimeout']);
}
if (timeoutHandler['startTimeout']) {
clearTimeout(timeoutHandler['startTimeout']);
}
if (timeoutHandler['restartTimeout']) {
clearTimeout(timeoutHandler['restartTimeout']);
}
this.wss.stop();
callback();
} catch {
callback();
}
}
/**
* Is called if a subscribed state changes
*
* @param id
* @param state
*/
async onStateChange(id, state) {
if (state && !state.ack) {
//const ParamId = id.split('.').pop();
const obj = await this.getObjectAsync(id);
if (obj) {
const findParamObj = ParamObjList.find(element => element.ParameterId === obj.native.ParameterId);
this.log.info(`Change value for: ${obj.common.name}: ${JSON.stringify(state)}`);
try {
const answer = await this.wss.setParameter(device.GatewayId, device.SystemId, [
{
ValueId: findParamObj.ValueId,
ParameterId: obj.native.ParameterId,
Value: String(state.val),
ParameterName: obj.common.name,
},
]);
if (typeof answer.Values !== 'undefined') {
await this.setStateAsync(id, {
val: state.val,
ack: true,
});
await this.SetStatesArray(answer);
}
} catch (err) {
this.log.error(err);
}
}
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * 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
// * @param {ioBroker.Message} obj
// */
async onMessage(obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
this.log.info('send command');
// Send response in callback if required
if (obj.callback) {
this.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
if (obj.command === 'getDeviceList') {
this.log.info('getDeviceList');
let devicelist;
try {
devicelist = await this.wss.adminGetDevicelist(obj.message.username, obj.message.password);
if (obj.callback) {
this.sendTo(obj.from, obj.command, devicelist, obj.callback);
}
} catch (error) {
if (obj.callback) {
this.sendTo(
obj.from,
obj.command,
{
error: error,
},
obj.callback,
);
}
}
}
}
}
}
// @ts-expect-error parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param [options]
*/
module.exports = options => new WolfSmartset(options);
} else {
// otherwise start the instance directly
new WolfSmartset();
}