iobroker.zigbee 1.8.9 → 1.8.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +413 -387
  3. package/admin/adapter-settings.js +244 -244
  4. package/admin/admin.js +2981 -2961
  5. package/admin/i18n/de/translations.json +108 -108
  6. package/admin/i18n/en/translations.json +108 -108
  7. package/admin/i18n/es/translations.json +102 -102
  8. package/admin/i18n/fr/translations.json +108 -108
  9. package/admin/i18n/it/translations.json +102 -102
  10. package/admin/i18n/nl/translations.json +108 -108
  11. package/admin/i18n/pl/translations.json +108 -108
  12. package/admin/i18n/pt/translations.json +102 -102
  13. package/admin/i18n/ru/translations.json +108 -108
  14. package/admin/i18n/uk/translations.json +108 -108
  15. package/admin/i18n/zh-cn/translations.json +102 -102
  16. package/admin/img/philips_hue_lom001.png +0 -0
  17. package/admin/index.html +159 -159
  18. package/admin/index_m.html +1356 -1331
  19. package/admin/moment.min.js +1 -1
  20. package/admin/shuffle.min.js +2 -2
  21. package/admin/tab_m.html +1009 -986
  22. package/admin/vis-network.min.css +1 -1
  23. package/admin/vis-network.min.js +27 -27
  24. package/admin/words.js +110 -110
  25. package/docs/de/basedocu.md +19 -0
  26. package/docs/de/img/Bild10.png +0 -0
  27. package/docs/de/img/Bild12.png +0 -0
  28. package/docs/de/img/Bild13.png +0 -0
  29. package/docs/de/img/Bild14.png +0 -0
  30. package/docs/de/img/Bild15.png +0 -0
  31. package/docs/de/img/Bild16.png +0 -0
  32. package/docs/de/img/Bild17.png +0 -0
  33. package/docs/de/img/Bild18.png +0 -0
  34. package/docs/de/img/Bild19.png +0 -0
  35. package/docs/de/img/Bild2.png +0 -0
  36. package/docs/de/img/Bild20.png +0 -0
  37. package/docs/de/img/Bild21.png +0 -0
  38. package/docs/de/img/Bild22.png +0 -0
  39. package/docs/de/img/Bild23.png +0 -0
  40. package/docs/de/img/Bild24.png +0 -0
  41. package/docs/de/img/Bild25.png +0 -0
  42. package/docs/de/img/Bild26.png +0 -0
  43. package/docs/de/img/Bild28.png +0 -0
  44. package/docs/de/img/Bild3.png +0 -0
  45. package/docs/de/img/Bild30.png +0 -0
  46. package/docs/de/img/Bild31.png +0 -0
  47. package/docs/de/img/Bild32.png +0 -0
  48. package/docs/de/img/Bild33.png +0 -0
  49. package/docs/de/img/Bild34.png +0 -0
  50. package/docs/de/img/Bild35.png +0 -0
  51. package/docs/de/img/Bild36.png +0 -0
  52. package/docs/de/img/Bild37.png +0 -0
  53. package/docs/de/img/Bild4.png +0 -0
  54. package/docs/de/img/Bild5.jpg +0 -0
  55. package/docs/de/img/Bild6.png +0 -0
  56. package/docs/de/img/Bild7.png +0 -0
  57. package/docs/de/img/Bild8.png +0 -0
  58. package/docs/de/img/Bild9.png +0 -0
  59. package/docs/de/img/software1.jpg +0 -0
  60. package/docs/de/img/sonoff.png +0 -0
  61. package/docs/de/readme.md +126 -27
  62. package/docs/en/img/Bild13.png +0 -0
  63. package/docs/en/img/Bild18.png +0 -0
  64. package/docs/en/img/Bild23.png +0 -0
  65. package/docs/en/img/Bild25.png +0 -0
  66. package/docs/en/img/Bild26.png +0 -0
  67. package/docs/en/img/Bild4.png +0 -0
  68. package/docs/en/img/Bild9.png +0 -0
  69. package/docs/en/img/software1.jpg +0 -0
  70. package/docs/en/readme.md +128 -30
  71. package/docs/flashing_via_arduino_(en).md +110 -110
  72. package/docs/ru/readme.md +28 -28
  73. package/docs/tutorial/groups-1.png +0 -0
  74. package/docs/tutorial/groups-2.png +0 -0
  75. package/docs/tutorial/tab-dev-1.png +0 -0
  76. package/io-package.json +13 -30
  77. package/lib/backup.js +171 -171
  78. package/lib/binding.js +319 -320
  79. package/lib/colors.js +465 -465
  80. package/lib/commands.js +534 -510
  81. package/lib/developer.js +145 -145
  82. package/lib/devices.js +3135 -3135
  83. package/lib/exclude.js +162 -162
  84. package/lib/exposes.js +873 -830
  85. package/lib/groups.js +345 -340
  86. package/lib/json.js +59 -59
  87. package/lib/networkmap.js +55 -55
  88. package/lib/ota.js +198 -195
  89. package/lib/rgb.js +297 -297
  90. package/lib/seriallist.js +48 -48
  91. package/lib/states.js +6420 -6420
  92. package/lib/statescontroller.js +693 -693
  93. package/lib/tools.js +54 -54
  94. package/lib/utils.js +163 -157
  95. package/lib/zbBaseExtension.js +36 -36
  96. package/lib/zbDelayedAction.js +144 -144
  97. package/lib/zbDeviceAvailability.js +319 -319
  98. package/lib/zbDeviceConfigure.js +147 -147
  99. package/lib/zbDeviceEvent.js +48 -48
  100. package/lib/zigbeecontroller.js +989 -956
  101. package/main.js +70 -5
  102. package/package.json +11 -11
  103. package/support/docgen.js +93 -93
  104. /package/admin/img/{paumann_spot.png → paulmann_spot.png} +0 -0
package/lib/commands.js CHANGED
@@ -1,510 +1,534 @@
1
- 'use strict';
2
-
3
- const getZbId = require('./utils').getZbId;
4
- const statesMapping = require('./devices');
5
- const disallowedDashStates = [
6
- 'link_quality', 'available', 'battery', 'groups', 'device_query',
7
- 'hue_move', 'color_temp_move', 'satuation_move', 'brightness_move', 'brightness_step', 'hue_calibration',
8
- 'msg_from_zigbee', 'send_payload',
9
- ];
10
-
11
- class Commands {
12
- constructor(adapter) {
13
- this.adapter = adapter;
14
- this.adapter.on('message', obj => this.onMessage(obj));
15
- }
16
-
17
- start(zbController, stController) {
18
- this.zbController = zbController;
19
- this.stController = stController;
20
- }
21
-
22
- stop() {
23
- delete this.zbController;
24
- delete this.stController;
25
- }
26
-
27
- info(msg) {
28
- this.adapter.log.info(msg);
29
- }
30
-
31
- error(msg) {
32
- this.adapter.log.error(msg);
33
- }
34
-
35
- debug(msg) {
36
- this.adapter.log.debug(msg);
37
- }
38
-
39
- warn(msg) {
40
- this.adapter.log.warn(msg);
41
- }
42
-
43
- /**
44
- * @param {ioBroker.Message} obj
45
- */
46
- onMessage(obj) {
47
- if (typeof obj === 'object' && obj.command) {
48
- switch (obj.command) {
49
- case 'letsPairing':
50
- if (obj && obj.message && typeof obj.message === 'object') {
51
- this.letsPairing(obj.from, obj.command, obj.message, obj.callback);
52
- }
53
- break;
54
- case 'touchlinkReset':
55
- if (obj && obj.message && typeof obj.message === 'object') {
56
- this.touchlinkReset(obj.from, obj.command, obj.message, obj.callback);
57
- }
58
- break;
59
- case 'getDevices':
60
- if (obj && obj.message && typeof obj.message === 'object') {
61
- this.getDevices(obj.from, obj.command, null, obj.callback);
62
- }
63
- break;
64
- case 'renameDevice':
65
- if (obj && obj.message && typeof obj.message === 'object') {
66
- this.renameDevice(obj.from, obj.command, obj.message, obj.callback);
67
- }
68
- break;
69
- case 'deleteDevice':
70
- if (obj && obj.message && typeof obj.message === 'object') {
71
- this.deleteDevice(obj.from, obj.command, obj.message, obj.callback);
72
- }
73
- break;
74
- case 'getChannels':
75
- if (obj && obj.message && typeof obj.message === 'object') {
76
- this.getChannels(obj.from, obj.command, obj.message, obj.callback);
77
- }
78
- break;
79
- case 'getCoordinatorInfo':
80
- if (obj && obj.message && typeof obj.message === 'object') {
81
- this.getCoordinatorInfo(obj.from, obj.command, obj.callback);
82
- }
83
- break;
84
- case 'cleanDeviceStates':
85
- if (obj && obj.message && typeof obj.message === 'object') {
86
- this.cleanDeviceStates(obj.from, obj.command, obj.message, obj.callback);
87
- }
88
- break;
89
- case 'setState':
90
- if (obj && obj.message && typeof obj.message === 'object' && obj.message.id) {
91
- // this.adapter.setState(obj.message.id, obj.message.val, false, obj.callback);
92
- this.stController.setState_typed(obj.message.id, obj.message.val, false, undefined, obj.callback);
93
- }
94
- break;
95
- case 'getDevice':
96
- if (obj && obj.message && typeof obj.message === 'object' && obj.message.id) {
97
- this.getDevices(obj.from, obj.command, obj.message.id, obj.callback);
98
- }
99
- break;
100
- case 'reconfigure':
101
- if (obj && obj.message && typeof obj.message === 'object') {
102
- this.reconfigure(obj.from, obj.command, obj.message, obj.callback);
103
- }
104
- break;
105
- case 'setDeviceActivated':
106
- if (obj && obj.message && typeof obj.message === 'object') {
107
- this.setDeviceActivated(obj.from, obj.command, obj.message, obj.callback);
108
- }
109
- break;
110
-
111
- }
112
- }
113
- }
114
-
115
- letsPairing(from, command, message, callback) {
116
- if (this.zbController) {
117
- let devId = '';
118
- if (message && message.id) {
119
- devId = getZbId(message.id);
120
- }
121
- // allow devices to join the network within 60 secs
122
- this.adapter.logToPairing('Pairing started ' + devId, true);
123
-
124
- let cTimer = Number(this.adapter.config.countDown);
125
- if (!this.adapter.config.countDown || !cTimer) {
126
- cTimer = 60;
127
- }
128
-
129
- this.zbController.permitJoin(cTimer, devId, err => {
130
- if (!err) {
131
- // set pairing mode on
132
- this.adapter.setState('info.pairingMode', true);
133
- }
134
- });
135
- this.adapter.sendTo(from, command, 'Start pairing!', callback);
136
- } else {
137
- this.adapter.sendTo(
138
- from, command,
139
- {error: 'You need to setup serial port and start the adapter before pairing!'},
140
- callback
141
- );
142
- }
143
- }
144
-
145
- touchlinkReset(from, command, message, callback) {
146
- if (this.zbController) {
147
- // allow devices to join the network within 60 secs
148
- this.adapter.logToPairing('Touchlink reset started ', true);
149
-
150
- let cTimer = Number(this.adapter.config.countDown);
151
- if (!this.adapter.config.countDown || !cTimer) {
152
- cTimer = 60;
153
- }
154
-
155
- this.zbController.touchlinkReset(cTimer);
156
- this.adapter.sendTo(from, command, 'Start touchlink reset and pairing!', callback);
157
- } else {
158
- this.adapter.sendTo(
159
- from, command,
160
- {error: 'You need to setup serial port and start the adapter before pairing!'},
161
- callback
162
- );
163
- }
164
- }
165
-
166
- async getDevices(from, command, id, callback) {
167
- if (this.zbController) {
168
- const pairedDevices = await this.zbController.getClients(true);
169
- const groups = {};
170
- let rooms;
171
- this.adapter.getEnumsAsync('enum.rooms')
172
- .then(enums => {
173
- // rooms
174
- rooms = enums['enum.rooms'];
175
- })
176
- // get all adapter devices
177
- .then(() => this.adapter.getDevicesAsync())
178
- .then(async result => {
179
- const alls = id ? await this.adapter.getStatesAsync(id + '.*') : await this.adapter.getStatesAsync('*');
180
- const allst = id ? await this.adapter.getStatesOfAsync(id) : await this.adapter.getStatesOfAsync();
181
- result = result.filter(item => !id || id === item._id);
182
- // get device states and groups
183
- result.forEach(async devInfo => {
184
- if (devInfo._id) {
185
- // groups
186
- // const grState = alls[`${devInfo._id}.groups`];
187
- // if (grState && grState.val) {
188
- // groups[devInfo._id] = JSON.parse(grState.val);
189
- // }
190
- // battery and link_quality
191
- const lqState = alls[`${devInfo._id}.link_quality`];
192
- devInfo.link_quality = lqState ? lqState.val : undefined;
193
- devInfo.link_quality_lc = lqState ? lqState.lc : undefined;
194
- const batState = alls[`${devInfo._id}.battery`];
195
- devInfo.battery = batState ? batState.val : undefined;
196
- // devInfo.states = states || {};
197
-
198
- const states = allst.filter(item => item._id.startsWith(devInfo._id));
199
-
200
- // put only allowed states
201
- devInfo.statesDef = (states || []).filter(stateDef => {
202
- const sid = stateDef._id;
203
- const name = sid.split('.').pop();
204
- return !disallowedDashStates.includes(name);
205
-
206
- }).map(stateDef => {
207
- const name = stateDef.common.name;
208
- const devname = devInfo.common.name;
209
- // replace state
210
- return {
211
- id: stateDef._id,
212
- name: typeof name === 'string' ? name.replace(devname, '') : name,
213
- type: stateDef.common.type,
214
- read: stateDef.common.read,
215
- write: stateDef.common.write,
216
- val: alls[stateDef._id] ? alls[stateDef._id].val : undefined,
217
- role: stateDef.common.role,
218
- unit: stateDef.common.unit,
219
- states: stateDef.common.states,
220
- };
221
- });
222
- }
223
- });
224
- return result;
225
- })
226
- .then(async result => {
227
- // combine info
228
- const devices = [];
229
- for (const devInfo of result) {
230
- if (devInfo._id.indexOf('group') > 0) {
231
- devInfo.icon = 'img/group.png';
232
- devInfo.vendor = 'ioBroker';
233
- // get group members and store them
234
- const match = /zigbee.\d.group_([0-9]+)/.exec(devInfo._id);
235
- if (match && match.length > 1) {
236
- const groupmembers = await this.zbController.getGroupMembersFromController(match[1]);
237
- this.debug(`group members: ${JSON.stringify(groupmembers)}`);
238
- if (groupmembers && groupmembers.length > 0) {
239
- const memberinfo = [];
240
- for (const member of groupmembers) {
241
- if (groups) {
242
- const grouparray = groups[member.ieee];
243
- if (grouparray) {
244
- if (!grouparray.includes(match[1])) {
245
- groups[member.ieee].push(match[1]);
246
- }
247
- } else {
248
- groups[member.ieee] = [match[1]];
249
- }
250
- }
251
- const device = await this.adapter.getObjectAsync(`${this.adapter.namespace}.${member.ieee.substr(2)}`);
252
- if (device) {
253
- member.device = device.common.name;
254
- } else {
255
- member.device = 'unknown';
256
- }
257
- memberinfo.push(member);
258
- }
259
- devInfo.memberinfo = memberinfo;
260
- this.debug(`memberinfo: ${JSON.stringify(devInfo.memberinfo)}`);
261
- }
262
- }
263
- } else {
264
- const modelDesc = statesMapping.findModel(devInfo.common.type);
265
- devInfo.icon = (modelDesc && modelDesc.icon) ? modelDesc.icon : 'img/unknown.png';
266
- devInfo.vendor = modelDesc ? modelDesc.vendor : '';
267
- }
268
-
269
- const id = getZbId(devInfo._id);
270
- devInfo.info = await this.zbController.resolveEntity(id);
271
-
272
- devInfo.rooms = [];
273
- for (const room in rooms) {
274
- if (!rooms.hasOwnProperty(room) ||
275
- !rooms[room] ||
276
- !rooms[room].common ||
277
- !rooms[room].common.members
278
- ) {
279
- continue;
280
- }
281
- if (rooms[room].common.members.includes(devInfo._id)) {
282
- devInfo.rooms.push(rooms[room].common.name);
283
- }
284
- }
285
- devInfo.paired = !!devInfo.info;
286
- // devInfo.groups = groups[devInfo._id];
287
- devices.push(devInfo);
288
- }
289
- return devices;
290
- })
291
- .then(async (devices) => {
292
- // fill group info
293
- for (const groupdev in groups) {
294
- this.debug(`GetDevices scanning group ${groupdev} ${JSON.stringify(groups[groupdev])}`);
295
- const device = devices.find(dev => (groupdev === getZbId(dev._id)));
296
- if (device) {
297
- device.groups = groups[groupdev];
298
- this.debug(`adding group info to device ${groupdev}`);
299
- }
300
- }
301
- // append devices that paired but not created
302
- if (!id) {
303
- for (const d of pairedDevices) {
304
- const device = await this.zbController.resolveEntity(d.ieeeAddr);
305
- if (!device) {
306
- continue;
307
- }
308
- const exists = devices.find((dev) => (dev._id && device.device.ieeeAddr === getZbId(dev._id)));
309
- if (!exists) {
310
- devices.push({
311
- _id: device.device.ieeeAddr,
312
- icon: 'img/unknown.png',
313
- paired: true,
314
- info: device,
315
- common: {
316
- name: undefined,
317
- type: undefined,
318
- },
319
- native: {}
320
- });
321
- }
322
- }
323
- }
324
- return devices;
325
- })
326
- .then(devices => {
327
- this.debug(`getDevices result: ${JSON.stringify(devices)}`);
328
- this.adapter.sendTo(from, command, devices, callback);
329
- })
330
- .catch(err => this.error(`getDevices error: ${err.stack}`));
331
- } else {
332
- this.adapter.sendTo(from, command, {error: 'You need save and run adapter before pairing!'}, callback);
333
- }
334
- }
335
-
336
-
337
- async getCoordinatorInfo(from, command, callback) {
338
- if (this.zbController) {
339
- const coordinatorinfo = {
340
- installSource: 'IADefault_1',
341
- channel: '-1',
342
- port: 'Default_1',
343
- installedVersion: 'Default_1',
344
- type: 'Default_1',
345
- revision: 'Default_1',
346
- version: '9-9.9.9.9'
347
- };
348
-
349
- const coordinatorVersion = await this.adapter.zbController.herdsman.getCoordinatorVersion();
350
-
351
- await this.adapter.getForeignObject(`system.adapter.${this.adapter.namespace}`, (err, obj) => {
352
- if (!err && obj) {
353
- if (obj.common.installedFrom && obj.common.installedFrom.includes('://')) {
354
- const instFrom = obj.common.installedFrom;
355
- coordinatorinfo.installSource = instFrom.replace('tarball', 'commit');
356
- } else {
357
- coordinatorinfo.installSource = obj.common.installedFrom;
358
- }
359
- }
360
- try {
361
- coordinatorinfo.port = obj.native.port;
362
- coordinatorinfo.channel = obj.native.channel;
363
- coordinatorinfo.installedVersion = obj.native.version;
364
- if (coordinatorVersion && coordinatorVersion.type && coordinatorVersion.meta) {
365
- coordinatorinfo.type = coordinatorVersion.type;
366
- const meta = coordinatorVersion.meta;
367
- if (meta) {
368
- if (meta.hasOwnProperty('revision')) {
369
- coordinatorinfo.revision = meta.revision;
370
- }
371
- let vt = 'x-';
372
- if (meta.hasOwnProperty('transportrev')) {
373
- vt = meta.transportrev + '-';
374
- }
375
- if (meta.hasOwnProperty('product')) {
376
- vt = vt + meta.product + '.';
377
- } else {
378
- vt = vt + 'x.';
379
- }
380
- if (meta.hasOwnProperty('majorrel')) {
381
- vt = vt + meta.majorrel + '.';
382
- } else {
383
- vt = vt + 'x.';
384
- }
385
- if (meta.hasOwnProperty('minorrel')) {
386
- vt = vt + meta.minorrel + '.';
387
- } else {
388
- vt = vt + 'x.';
389
- }
390
- if (meta.hasOwnProperty('maintrel')) {
391
- vt = vt + meta.maintrel + '.';
392
- } else {
393
- vt = vt + 'x.';
394
- }
395
- coordinatorinfo.version = vt;
396
- }
397
- }
398
- } catch {
399
- this.warn('exception raised in getCoordinatorInfo');
400
- }
401
-
402
- this.debug(`getCoordinatorInfo result: ${JSON.stringify(coordinatorinfo)}`);
403
- this.adapter.sendTo(from, command, coordinatorinfo, callback);
404
- });
405
- } else {
406
- this.adapter.sendTo(from, command, {error: 'You need save and run adapter before pairing!'}, callback);
407
- }
408
- }
409
-
410
-
411
- renameDevice(from, command, msg, callback) {
412
- if (this.stController) {
413
- const id = msg.id;
414
- const newName = msg.name;
415
- this.stController.renameDevice(id, newName);
416
- this.adapter.sendTo(from, command, {}, callback);
417
- }
418
- }
419
-
420
- deleteDevice(from, command, msg, callback) {
421
- if (this.zbController && this.stController) {
422
- this.debug(`deleteDevice message: ${JSON.stringify(msg)}`);
423
- const id = msg.id;
424
- const force = msg.force;
425
- const sysid = id.replace(this.adapter.namespace + '.', '0x');
426
- const devId = id.replace(this.adapter.namespace + '.', '');
427
- this.debug(`deleteDevice sysid: ${sysid}`);
428
- const dev = this.zbController.getDevice(sysid);
429
- if (!dev) {
430
- this.debug('Not found!');
431
- this.debug(`Try delete dev ${devId} from iobroker.`);
432
- this.stController.deleteDeviceStates(devId, () =>
433
- this.adapter.sendTo(from, command, {}, callback));
434
- return;
435
- }
436
- this.zbController.remove(sysid, force, err => {
437
- if (!err) {
438
- this.stController.deleteDeviceStates(devId, () =>
439
- this.adapter.sendTo(from, command, {}, callback));
440
- } else {
441
- this.debug(`Error on remove! ${err}`);
442
- this.adapter.sendTo(from, command, {error: err}, callback);
443
- }
444
- });
445
- } else {
446
- this.adapter.sendTo(from, command, {error: 'You need to save and start the adapter!'}, callback);
447
- }
448
- }
449
-
450
- async cleanDeviceStates(from, command, msg, callback) {
451
- this.info(`State cleanup with ${JSON.stringify(msg)}`);
452
- const devicesFromDB = await this.zbController.getClients(false);
453
- for (const device of devicesFromDB) {
454
- const entity = await this.zbController.resolveEntity(device);
455
- if (entity) {
456
- const model = (entity.mapped) ? entity.mapped.model : entity.device.modelID;
457
- await this.stController.deleteOrphanedDeviceStates(device.ieeeAddr, model, msg.force);
458
- }
459
- }
460
- this.adapter.sendTo(from, command, {}, callback);
461
- }
462
-
463
- async getChannels(from, command, message, callback) {
464
- if (this.zbController) {
465
- const result = await this.zbController.getChannelsEnergy();
466
- this.debug(`getChannels result: ${JSON.stringify(result)}`);
467
- this.adapter.sendTo(from, command, result, callback);
468
- } else {
469
- this.adapter.sendTo(
470
- from, command,
471
- {error: 'You need to setup serial port and start the adapter before pairing!'},
472
- callback
473
- );
474
- }
475
- }
476
-
477
- async setDeviceActivated(from, command, msg, callback) {
478
- if (this.stController) {
479
- const id = msg.id;
480
- const targetstate = msg.deactivated;
481
- this.stController.setDeviceActivated(id, targetstate);
482
- this.adapter.sendTo(from, command, {}, callback);
483
- }
484
- }
485
-
486
- async reconfigure(from, command, msg, callback) {
487
- if (this.zbController) {
488
- const devid = getZbId(msg.id);
489
- this.debug(`Reconfigure ${devid}`);
490
- const entity = await this.zbController.resolveEntity(devid);
491
- if (entity) {
492
- try {
493
- await this.zbController.callExtensionMethod(
494
- 'doConfigure',
495
- [entity.device, entity.mapped],
496
- );
497
- this.adapter.sendTo(from, command, {}, callback);
498
- } catch (error) {
499
- const errmsg = `Reconfigure failed ${entity.device.ieeeAddr} ${entity.device.modelID}, (${error.stack})`;
500
- this.error(errmsg);
501
- this.adapter.sendTo(from, command, {error: errmsg}, callback);
502
- }
503
- } else {
504
- this.adapter.sendTo(from, command, {error: 'No device'}, callback);
505
- }
506
- }
507
- }
508
- }
509
-
510
- module.exports = Commands;
1
+ 'use strict';
2
+
3
+ const getZbId = require('./utils').getZbId;
4
+ const statesMapping = require('./devices');
5
+ const disallowedDashStates = [
6
+ 'link_quality', 'available', 'battery', 'groups', 'device_query',
7
+ 'hue_move', 'color_temp_move', 'satuation_move', 'brightness_move', 'brightness_step', 'hue_calibration',
8
+ 'msg_from_zigbee', 'send_payload',
9
+ ];
10
+
11
+ class Commands {
12
+ constructor(adapter) {
13
+ this.adapter = adapter;
14
+ this.adapter.on('message', obj => this.onMessage(obj));
15
+ }
16
+
17
+ start(zbController, stController) {
18
+ this.zbController = zbController;
19
+ this.stController = stController;
20
+ }
21
+
22
+ stop() {
23
+ delete this.zbController;
24
+ delete this.stController;
25
+ }
26
+
27
+ info(msg) {
28
+ this.adapter.log.info(msg);
29
+ }
30
+
31
+ error(msg) {
32
+ this.adapter.log.error(msg);
33
+ }
34
+
35
+ debug(msg) {
36
+ this.adapter.log.debug(msg);
37
+ }
38
+
39
+ warn(msg) {
40
+ this.adapter.log.warn(msg);
41
+ }
42
+
43
+ /**
44
+ * @param {ioBroker.Message} obj
45
+ */
46
+ onMessage(obj) {
47
+ if (typeof obj === 'object' && obj.command) {
48
+ switch (obj.command) {
49
+ case 'letsPairing':
50
+ if (obj && obj.message && typeof obj.message === 'object') {
51
+ this.letsPairing(obj.from, obj.command, obj.message, obj.callback);
52
+ }
53
+ break;
54
+ case 'touchlinkReset':
55
+ if (obj && obj.message && typeof obj.message === 'object') {
56
+ this.touchlinkReset(obj.from, obj.command, obj.message, obj.callback);
57
+ }
58
+ break;
59
+ case 'getDevices':
60
+ if (obj && obj.message && typeof obj.message === 'object') {
61
+ this.getDevices(obj.from, obj.command, null, obj.callback);
62
+ }
63
+ break;
64
+ case 'renameDevice':
65
+ if (obj && obj.message && typeof obj.message === 'object') {
66
+ this.renameDevice(obj.from, obj.command, obj.message, obj.callback);
67
+ }
68
+ break;
69
+ case 'deleteDevice':
70
+ if (obj && obj.message && typeof obj.message === 'object') {
71
+ this.deleteDevice(obj.from, obj.command, obj.message, obj.callback);
72
+ }
73
+ break;
74
+ case 'getChannels':
75
+ if (obj && obj.message && typeof obj.message === 'object') {
76
+ this.getChannels(obj.from, obj.command, obj.message, obj.callback);
77
+ }
78
+ break;
79
+ case 'getCoordinatorInfo':
80
+ if (obj && obj.message && typeof obj.message === 'object') {
81
+ this.getCoordinatorInfo(obj.from, obj.command, obj.callback);
82
+ }
83
+ break;
84
+ case 'cleanDeviceStates':
85
+ if (obj && obj.message && typeof obj.message === 'object') {
86
+ this.cleanDeviceStates(obj.from, obj.command, obj.message, obj.callback);
87
+ }
88
+ break;
89
+ case 'setState':
90
+ if (obj && obj.message && typeof obj.message === 'object' && obj.message.id) {
91
+ // this.adapter.setState(obj.message.id, obj.message.val, false, obj.callback);
92
+ this.stController.setState_typed(obj.message.id, obj.message.val, false, undefined, obj.callback);
93
+ }
94
+ break;
95
+ case 'getDevice':
96
+ if (obj && obj.message && typeof obj.message === 'object' && obj.message.id) {
97
+ this.getDevices(obj.from, obj.command, obj.message.id, obj.callback);
98
+ }
99
+ break;
100
+ case 'reconfigure':
101
+ if (obj && obj.message && typeof obj.message === 'object') {
102
+ this.reconfigure(obj.from, obj.command, obj.message, obj.callback);
103
+ }
104
+ break;
105
+ case 'setDeviceActivated':
106
+ if (obj && obj.message && typeof obj.message === 'object') {
107
+ this.setDeviceActivated(obj.from, obj.command, obj.message, obj.callback);
108
+ }
109
+ break;
110
+
111
+ }
112
+ }
113
+ }
114
+
115
+ async letsPairing(from, command, message, callback) {
116
+ if (this.zbController) {
117
+ let devId = '';
118
+ if (message) {
119
+ if (message.id) devId = getZbId(message.id);
120
+ if (message.code) {
121
+ try {
122
+ this.debug(`letsPairing called with code ${message.code}`);
123
+ const success = await this.zbController.addPairingCode(message.code)
124
+ if (!success) {
125
+ this.adapter.sendTo(
126
+ from, command,
127
+ {error: 'Pairing code rejected by Coordinator!'},
128
+ callback
129
+ );
130
+ return;
131
+ };
132
+ }
133
+ catch (e) {
134
+ this.error(JSON.stringify(e))
135
+ this.adapter.sendTo(
136
+ from, command,
137
+ {error: 'Exception when trying to add QR code'},
138
+ callback
139
+ );
140
+ return;
141
+
142
+ }
143
+ }
144
+ }
145
+ // allow devices to join the network within 60 secs
146
+ this.adapter.logToPairing('Pairing started ' + devId, true);
147
+
148
+ let cTimer = Number(this.adapter.config.countDown);
149
+ if (!this.adapter.config.countDown || !cTimer) {
150
+ cTimer = 60;
151
+ }
152
+
153
+ this.zbController.permitJoin(cTimer, devId, err => {
154
+ if (!err) {
155
+ // set pairing mode on
156
+ this.adapter.setState('info.pairingMode', true);
157
+ }
158
+ });
159
+ this.adapter.sendTo(from, command, 'Start pairing!', callback);
160
+ } else {
161
+ this.adapter.sendTo(
162
+ from, command,
163
+ {error: 'You need to setup serial port and start the adapter before pairing!'},
164
+ callback
165
+ );
166
+ }
167
+ }
168
+
169
+ touchlinkReset(from, command, message, callback) {
170
+ if (this.zbController) {
171
+ // allow devices to join the network within 60 secs
172
+ this.adapter.logToPairing('Touchlink reset started ', true);
173
+
174
+ let cTimer = Number(this.adapter.config.countDown);
175
+ if (!this.adapter.config.countDown || !cTimer) {
176
+ cTimer = 60;
177
+ }
178
+
179
+ this.zbController.touchlinkReset(cTimer);
180
+ this.adapter.sendTo(from, command, 'Start touchlink reset and pairing!', callback);
181
+ } else {
182
+ this.adapter.sendTo(
183
+ from, command,
184
+ {error: 'You need to setup serial port and start the adapter before pairing!'},
185
+ callback
186
+ );
187
+ }
188
+ }
189
+
190
+ async getDevices(from, command, id, callback) {
191
+ if (this.zbController) {
192
+ const pairedDevices = await this.zbController.getClients(true);
193
+ const groups = {};
194
+ let rooms;
195
+ this.adapter.getEnumsAsync('enum.rooms')
196
+ .then(enums => {
197
+ // rooms
198
+ rooms = enums['enum.rooms'];
199
+ })
200
+ // get all adapter devices
201
+ .then(() => this.adapter.getDevicesAsync())
202
+ .then(async result => {
203
+ const alls = id ? await this.adapter.getStatesAsync(id + '.*') : await this.adapter.getStatesAsync('*');
204
+ const allst = id ? await this.adapter.getStatesOfAsync(id) : await this.adapter.getStatesOfAsync();
205
+ result = result.filter(item => !id || id === item._id);
206
+ // get device states and groups
207
+ result.forEach(async devInfo => {
208
+ if (devInfo._id) {
209
+ // groups
210
+ // const grState = alls[`${devInfo._id}.groups`];
211
+ // if (grState && grState.val) {
212
+ // groups[devInfo._id] = JSON.parse(grState.val);
213
+ // }
214
+ // battery and link_quality
215
+ const lqState = alls[`${devInfo._id}.link_quality`];
216
+ devInfo.link_quality = lqState ? lqState.val : undefined;
217
+ devInfo.link_quality_lc = lqState ? lqState.lc : undefined;
218
+ const batState = alls[`${devInfo._id}.battery`];
219
+ devInfo.battery = batState ? batState.val : undefined;
220
+ // devInfo.states = states || {};
221
+
222
+ const states = allst.filter(item => item._id.startsWith(devInfo._id));
223
+
224
+ // put only allowed states
225
+ devInfo.statesDef = (states || []).filter(stateDef => {
226
+ const sid = stateDef._id;
227
+ const name = sid.split('.').pop();
228
+ return !disallowedDashStates.includes(name);
229
+
230
+ }).map(stateDef => {
231
+ const name = stateDef.common.name;
232
+ const devname = devInfo.common.name;
233
+ // replace state
234
+ return {
235
+ id: stateDef._id,
236
+ name: typeof name === 'string' ? name.replace(devname, '') : name,
237
+ type: stateDef.common.type,
238
+ read: stateDef.common.read,
239
+ write: stateDef.common.write,
240
+ val: alls[stateDef._id] ? alls[stateDef._id].val : undefined,
241
+ role: stateDef.common.role,
242
+ unit: stateDef.common.unit,
243
+ states: stateDef.common.states,
244
+ };
245
+ });
246
+ }
247
+ });
248
+ return result;
249
+ })
250
+ .then(async result => {
251
+ // combine info
252
+ const devices = [];
253
+ for (const devInfo of result) {
254
+ if (devInfo._id.indexOf('group') > 0) {
255
+ devInfo.icon = 'img/group.png';
256
+ devInfo.vendor = 'ioBroker';
257
+ // get group members and store them
258
+ const match = /zigbee.\d.group_([0-9]+)/.exec(devInfo._id);
259
+ if (match && match.length > 1) {
260
+ const groupmembers = await this.zbController.getGroupMembersFromController(match[1]);
261
+ this.debug(`group members: ${JSON.stringify(groupmembers)}`);
262
+ if (groupmembers && groupmembers.length > 0) {
263
+ const memberinfo = [];
264
+ for (const member of groupmembers) {
265
+ if (groups) {
266
+ const grouparray = groups[member.ieee];
267
+ if (grouparray) {
268
+ if (!grouparray.includes(match[1])) {
269
+ groups[member.ieee].push(match[1]);
270
+ }
271
+ } else {
272
+ groups[member.ieee] = [match[1]];
273
+ }
274
+ }
275
+ const device = await this.adapter.getObjectAsync(`${this.adapter.namespace}.${member.ieee.substr(2)}`);
276
+ if (device) {
277
+ member.device = device.common.name;
278
+ } else {
279
+ member.device = 'unknown';
280
+ }
281
+ memberinfo.push(member);
282
+ }
283
+ devInfo.memberinfo = memberinfo;
284
+ this.debug(`memberinfo for ${match[1]}: ${JSON.stringify(devInfo.memberinfo)}`);
285
+ }
286
+ }
287
+ } else {
288
+ const modelDesc = statesMapping.findModel(devInfo.common.type);
289
+ devInfo.icon = (modelDesc && modelDesc.icon) ? modelDesc.icon : 'img/unknown.png';
290
+ devInfo.vendor = modelDesc ? modelDesc.vendor : '';
291
+ }
292
+
293
+ const id = getZbId(devInfo._id);
294
+ devInfo.info = await this.zbController.resolveEntity(id);
295
+
296
+ devInfo.rooms = [];
297
+ for (const room in rooms) {
298
+ if (!rooms.hasOwnProperty(room) ||
299
+ !rooms[room] ||
300
+ !rooms[room].common ||
301
+ !rooms[room].common.members
302
+ ) {
303
+ continue;
304
+ }
305
+ if (rooms[room].common.members.includes(devInfo._id)) {
306
+ devInfo.rooms.push(rooms[room].common.name);
307
+ }
308
+ }
309
+ devInfo.paired = !!devInfo.info;
310
+ // devInfo.groups = groups[devInfo._id];
311
+ devices.push(devInfo);
312
+ }
313
+ return devices;
314
+ })
315
+ .then(async (devices) => {
316
+ // fill group info
317
+ for (const groupdev in groups) {
318
+ //this.debug(`GetDevices scanning group ${groupdev} ${JSON.stringify(groups[groupdev])}`);
319
+ const device = devices.find(dev => (groupdev === getZbId(dev._id)));
320
+ if (device) {
321
+ device.groups = groups[groupdev];
322
+ //this.debug(`adding group info to device ${groupdev}`);
323
+ }
324
+ }
325
+ // append devices that paired but not created
326
+ if (!id) {
327
+ for (const d of pairedDevices) {
328
+ const device = await this.zbController.resolveEntity(d.ieeeAddr);
329
+ if (!device) {
330
+ continue;
331
+ }
332
+ const exists = devices.find((dev) => (dev._id && device.device.ieeeAddr === getZbId(dev._id)));
333
+ if (!exists) {
334
+ devices.push({
335
+ _id: device.device.ieeeAddr,
336
+ icon: 'img/unknown.png',
337
+ paired: true,
338
+ info: device,
339
+ common: {
340
+ name: undefined,
341
+ type: undefined,
342
+ },
343
+ native: {}
344
+ });
345
+ }
346
+ }
347
+ }
348
+ return devices;
349
+ })
350
+ .then(devices => {
351
+ this.debug(`getDevices result: ${JSON.stringify(devices)}`);
352
+ this.adapter.sendTo(from, command, devices, callback);
353
+ })
354
+ .catch(err => this.error(`getDevices error: ${err.stack}`));
355
+ } else {
356
+ this.adapter.sendTo(from, command, {error: 'You need save and run adapter before pairing!'}, callback);
357
+ }
358
+ }
359
+
360
+
361
+ async getCoordinatorInfo(from, command, callback) {
362
+ if (this.zbController) {
363
+ const coordinatorinfo = {
364
+ installSource: 'IADefault_1',
365
+ channel: '-1',
366
+ port: 'Default_1',
367
+ installedVersion: 'Default_1',
368
+ type: 'Default_1',
369
+ revision: 'Default_1',
370
+ version: '9-9.9.9.9'
371
+ };
372
+
373
+ const coordinatorVersion = await this.adapter.zbController.herdsman.getCoordinatorVersion();
374
+
375
+ await this.adapter.getForeignObject(`system.adapter.${this.adapter.namespace}`, (err, obj) => {
376
+ if (!err && obj) {
377
+ if (obj.common.installedFrom && obj.common.installedFrom.includes('://')) {
378
+ const instFrom = obj.common.installedFrom;
379
+ coordinatorinfo.installSource = instFrom.replace('tarball', 'commit');
380
+ } else {
381
+ coordinatorinfo.installSource = obj.common.installedFrom;
382
+ }
383
+ }
384
+ try {
385
+ coordinatorinfo.port = obj.native.port;
386
+ coordinatorinfo.channel = obj.native.channel;
387
+ coordinatorinfo.installedVersion = obj.native.version;
388
+ if (coordinatorVersion && coordinatorVersion.type && coordinatorVersion.meta) {
389
+ coordinatorinfo.type = coordinatorVersion.type;
390
+ const meta = coordinatorVersion.meta;
391
+ if (meta) {
392
+ if (meta.hasOwnProperty('revision')) {
393
+ coordinatorinfo.revision = meta.revision;
394
+ }
395
+ let vt = 'x-';
396
+ if (meta.hasOwnProperty('transportrev')) {
397
+ vt = meta.transportrev + '-';
398
+ }
399
+ if (meta.hasOwnProperty('product')) {
400
+ vt = vt + meta.product + '.';
401
+ } else {
402
+ vt = vt + 'x.';
403
+ }
404
+ if (meta.hasOwnProperty('majorrel')) {
405
+ vt = vt + meta.majorrel + '.';
406
+ } else {
407
+ vt = vt + 'x.';
408
+ }
409
+ if (meta.hasOwnProperty('minorrel')) {
410
+ vt = vt + meta.minorrel + '.';
411
+ } else {
412
+ vt = vt + 'x.';
413
+ }
414
+ if (meta.hasOwnProperty('maintrel')) {
415
+ vt = vt + meta.maintrel + '.';
416
+ } else {
417
+ vt = vt + 'x.';
418
+ }
419
+ coordinatorinfo.version = vt;
420
+ }
421
+ }
422
+ } catch {
423
+ this.warn('exception raised in getCoordinatorInfo');
424
+ }
425
+
426
+ this.debug(`getCoordinatorInfo result: ${JSON.stringify(coordinatorinfo)}`);
427
+ this.adapter.sendTo(from, command, coordinatorinfo, callback);
428
+ });
429
+ } else {
430
+ this.adapter.sendTo(from, command, {error: 'You need save and run adapter before pairing!'}, callback);
431
+ }
432
+ }
433
+
434
+
435
+ renameDevice(from, command, msg, callback) {
436
+ if (this.stController) {
437
+ const id = msg.id;
438
+ const newName = msg.name;
439
+ this.stController.renameDevice(id, newName);
440
+ this.adapter.sendTo(from, command, {}, callback);
441
+ }
442
+ }
443
+
444
+ deleteDevice(from, command, msg, callback) {
445
+ if (this.zbController && this.stController) {
446
+ this.debug(`deleteDevice message: ${JSON.stringify(msg)}`);
447
+ const id = msg.id;
448
+ const force = msg.force;
449
+ const sysid = id.replace(this.adapter.namespace + '.', '0x');
450
+ const devId = id.replace(this.adapter.namespace + '.', '');
451
+ this.debug(`deleteDevice sysid: ${sysid}`);
452
+ const dev = this.zbController.getDevice(sysid);
453
+ if (!dev) {
454
+ this.debug('Not found!');
455
+ this.debug(`Try delete dev ${devId} from iobroker.`);
456
+ this.stController.deleteDeviceStates(devId, () =>
457
+ this.adapter.sendTo(from, command, {}, callback));
458
+ return;
459
+ }
460
+ this.zbController.remove(sysid, force, err => {
461
+ if (!err) {
462
+ this.stController.deleteDeviceStates(devId, () =>
463
+ this.adapter.sendTo(from, command, {}, callback));
464
+ } else {
465
+ this.debug(`Error on remove! ${err}`);
466
+ this.adapter.sendTo(from, command, {error: err}, callback);
467
+ }
468
+ });
469
+ } else {
470
+ this.adapter.sendTo(from, command, {error: 'You need to save and start the adapter!'}, callback);
471
+ }
472
+ }
473
+
474
+ async cleanDeviceStates(from, command, msg, callback) {
475
+ this.info(`State cleanup with ${JSON.stringify(msg)}`);
476
+ const devicesFromDB = await this.zbController.getClients(false);
477
+ for (const device of devicesFromDB) {
478
+ const entity = await this.zbController.resolveEntity(device);
479
+ if (entity) {
480
+ const model = (entity.mapped) ? entity.mapped.model : entity.device.modelID;
481
+ await this.stController.deleteOrphanedDeviceStates(device.ieeeAddr, model, msg.force);
482
+ }
483
+ }
484
+ this.adapter.sendTo(from, command, {}, callback);
485
+ }
486
+
487
+ async getChannels(from, command, message, callback) {
488
+ if (this.zbController) {
489
+ const result = await this.zbController.getChannelsEnergy();
490
+ this.debug(`getChannels result: ${JSON.stringify(result)}`);
491
+ this.adapter.sendTo(from, command, result, callback);
492
+ } else {
493
+ this.adapter.sendTo(
494
+ from, command,
495
+ {error: 'You need to setup serial port and start the adapter before pairing!'},
496
+ callback
497
+ );
498
+ }
499
+ }
500
+
501
+ async setDeviceActivated(from, command, msg, callback) {
502
+ if (this.stController) {
503
+ const id = msg.id;
504
+ const targetstate = msg.deactivated;
505
+ this.stController.setDeviceActivated(id, targetstate);
506
+ this.adapter.sendTo(from, command, {}, callback);
507
+ }
508
+ }
509
+
510
+ async reconfigure(from, command, msg, callback) {
511
+ if (this.zbController) {
512
+ const devid = getZbId(msg.id);
513
+ this.debug(`Reconfigure ${devid}`);
514
+ const entity = await this.zbController.resolveEntity(devid);
515
+ if (entity) {
516
+ try {
517
+ await this.zbController.callExtensionMethod(
518
+ 'doConfigure',
519
+ [entity.device, entity.mapped],
520
+ );
521
+ this.adapter.sendTo(from, command, {}, callback);
522
+ } catch (error) {
523
+ const errmsg = `Reconfigure failed ${entity.device.ieeeAddr} ${entity.device.modelID}, (${error.stack})`;
524
+ this.error(errmsg);
525
+ this.adapter.sendTo(from, command, {error: errmsg}, callback);
526
+ }
527
+ } else {
528
+ this.adapter.sendTo(from, command, {error: 'No device'}, callback);
529
+ }
530
+ }
531
+ }
532
+ }
533
+
534
+ module.exports = Commands;