iobroker.zigbee 1.7.6 → 1.8.0

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 (97) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +368 -381
  3. package/admin/admin.js +2951 -2927
  4. package/admin/i18n/de/translations.json +108 -0
  5. package/admin/i18n/en/translations.json +108 -0
  6. package/admin/i18n/es/translations.json +102 -0
  7. package/admin/i18n/fr/translations.json +108 -0
  8. package/admin/i18n/it/translations.json +102 -0
  9. package/admin/i18n/nl/translations.json +108 -0
  10. package/admin/i18n/pl/translations.json +108 -0
  11. package/admin/i18n/pt/translations.json +102 -0
  12. package/admin/i18n/ru/translations.json +108 -0
  13. package/admin/i18n/uk/translations.json +108 -0
  14. package/admin/i18n/zh-cn/translations.json +102 -0
  15. package/admin/img/philips_hue_lom001.png +0 -0
  16. package/admin/index.html +159 -159
  17. package/admin/index_m.html +1161 -1161
  18. package/admin/moment.min.js +1 -1
  19. package/admin/shuffle.min.js +2 -2
  20. package/admin/tab_m.html +985 -944
  21. package/admin/vis-network.min.css +1 -1
  22. package/admin/vis-network.min.js +27 -27
  23. package/admin/words.js +110 -110
  24. package/docs/de/img/CC2531.png +0 -0
  25. package/docs/de/img/CC2538_CC2592_PA.PNG +0 -0
  26. package/docs/de/img/CC2591.png +0 -0
  27. package/docs/de/img/boards.jpg +0 -0
  28. package/docs/de/img/cc26x2r.PNG +0 -0
  29. package/docs/de/img/results.jpg +0 -0
  30. package/docs/de/img/sku_429478_2.png +0 -0
  31. package/docs/de/img/sku_429601_2.png +0 -0
  32. package/docs/de/readme.md +27 -0
  33. package/docs/en/img/CC2531.png +0 -0
  34. package/docs/en/img/CC2591.png +0 -0
  35. package/docs/en/img/deconz.png +0 -0
  36. package/docs/en/img/sku_429478_2.png +0 -0
  37. package/docs/en/img/sku_429601_2.png +0 -0
  38. package/docs/en/readme.md +30 -0
  39. package/docs/flashing_via_arduino_(en).md +110 -0
  40. package/docs/ru/img/CC2531.png +0 -0
  41. package/docs/ru/img/CC2591.png +0 -0
  42. package/docs/ru/img/sku_429478_2.png +0 -0
  43. package/docs/ru/img/sku_429601_2.png +0 -0
  44. package/docs/ru/readme.md +28 -0
  45. package/docs/tutorial/CC2530_20190425.zip +0 -0
  46. package/docs/tutorial/CC2530_CC2591_20190515.zip +0 -0
  47. package/docs/tutorial/CC2530_CC2592_20190515.zip +0 -0
  48. package/docs/tutorial/CC2531_20190425.zip +0 -0
  49. package/docs/tutorial/adm5_1.PNG +0 -0
  50. package/docs/tutorial/adm5_2.PNG +0 -0
  51. package/docs/tutorial/cat.PNG +0 -0
  52. package/docs/tutorial/groups-1.png +0 -0
  53. package/docs/tutorial/groups-2.png +0 -0
  54. package/docs/tutorial/inst.PNG +0 -0
  55. package/docs/tutorial/reflash-finish.PNG +0 -0
  56. package/docs/tutorial/reflash-step0.png +0 -0
  57. package/docs/tutorial/reflash-step1.PNG +0 -0
  58. package/docs/tutorial/reflash-step2.PNG +0 -0
  59. package/docs/tutorial/settings.png +0 -0
  60. package/docs/tutorial/tab-dev-1.png +0 -0
  61. package/docs/tutorial/zigbee.png +0 -0
  62. package/docs/tutorial/zigbee15.png +0 -0
  63. package/io-package.json +335 -370
  64. package/lib/backup.js +171 -171
  65. package/lib/binding.js +324 -325
  66. package/lib/colors.js +462 -460
  67. package/lib/commands.js +508 -501
  68. package/lib/developer.js +147 -148
  69. package/lib/devices.js +3136 -3145
  70. package/lib/exclude.js +162 -168
  71. package/lib/exposes.js +799 -804
  72. package/lib/groups.js +341 -342
  73. package/lib/json.js +59 -60
  74. package/lib/networkmap.js +56 -56
  75. package/lib/ota.js +187 -179
  76. package/lib/rgb.js +258 -255
  77. package/lib/seriallist.js +48 -37
  78. package/lib/states.js +6406 -6416
  79. package/lib/statescontroller.js +676 -670
  80. package/lib/tools.js +54 -54
  81. package/lib/utils.js +152 -151
  82. package/lib/zbBaseExtension.js +36 -36
  83. package/lib/zbDelayedAction.js +144 -152
  84. package/lib/zbDeviceAvailability.js +318 -315
  85. package/lib/zbDeviceConfigure.js +147 -159
  86. package/lib/zbDeviceEvent.js +48 -49
  87. package/lib/zigbeecontroller.js +943 -951
  88. package/main.js +149 -133
  89. package/package.json +33 -19
  90. package/support/docgen.js +93 -93
  91. package/.eslintignore +0 -2
  92. package/.eslintrc.json +0 -37
  93. package/.github/FUNDING.yml +0 -3
  94. package/.github/stale.yml +0 -13
  95. package/.github/workflows/test-and-release.yml +0 -151
  96. package/.travis/wiki.sh +0 -28
  97. package/admin/adapter-settings.js +0 -244
package/lib/binding.js CHANGED
@@ -1,325 +1,324 @@
1
- 'use strict';
2
-
3
- const safeJsonStringify = require('./json');
4
-
5
- // 5 - genScenes, 6 - genOnOff, 8 - genLevelCtrl, 768 - lightingColorCtrl
6
- const allowClusters = [5, 6, 8, 768];
7
- const allowClustersName = {5: 'genScenes', 6: 'genOnOff', 8: 'genLevelCtrl', 768: 'lightingColorCtrl'};
8
-
9
-
10
- class Binding {
11
- constructor(adapter) {
12
- this.adapter = adapter;
13
- this.adapter.on('message', this.onMessage.bind(this));
14
- }
15
-
16
- start(zbController, stController) {
17
- this.zbController = zbController;
18
- this.stController = stController;
19
- }
20
-
21
- stop() {
22
- delete this.zbController;
23
- delete this.stController;
24
- }
25
-
26
- info(msg) {
27
- this.adapter.log.info(msg);
28
- }
29
-
30
- error(msg) {
31
- this.adapter.log.error(msg);
32
- }
33
-
34
- debug(msg) {
35
- this.adapter.log.debug(msg);
36
- }
37
-
38
- warn(msg) {
39
- this.adapter.log.warn(msg);
40
- }
41
-
42
- /**
43
- * @param {ioBroker.Message} obj
44
- */
45
- onMessage(obj) {
46
- if (typeof obj === 'object' && obj.command) {
47
- switch (obj.command) {
48
- case 'addBinding':
49
- if (obj && obj.message && typeof obj.message === 'object') {
50
- this.addBinding(obj.from, obj.command, obj.message, (err)=>{
51
- this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
52
- });
53
- }
54
- break;
55
- case 'editBinding':
56
- if (obj && obj.message && typeof obj.message === 'object') {
57
- this.editBinding(obj.from, obj.command, obj.message, (err)=>{
58
- this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
59
- });
60
- }
61
- break;
62
- case 'getBinding':
63
- if (obj && obj.message && typeof obj.message === 'object') {
64
- this.getBinding((binding)=>{
65
- this.adapter.sendTo(obj.from, obj.command, binding, obj.callback);
66
- });
67
- }
68
- break;
69
- case 'delBinding':
70
- if (obj && obj.message) {
71
- this.delBinding(obj.from, obj.command, obj.message, (err)=>{
72
- this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
73
- });
74
- }
75
- break;
76
- }
77
- }
78
- }
79
-
80
- extractBindId(stateId) {
81
- return stateId.replace(`${this.adapter.namespace}.info.`, '');
82
- }
83
-
84
- getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep) {
85
- return `bind_${this.extractDeviceId(bind_source)}_${bind_source_ep}_${this.extractDeviceId(bind_target)}_${bind_target_ep}`;
86
- }
87
-
88
- extractDeviceId(stateId) {
89
- if (stateId)
90
- return stateId.replace(`${this.adapter.namespace}.`, '');
91
- return '';
92
- }
93
-
94
- getBindEp(ep) {
95
- if (ep)
96
- return parseInt(ep.split('_')[0]);
97
-
98
- this.warn(`getBindEp called with illegal ep: ${safeJsonStringify(ep)}`);
99
- return 0;
100
- }
101
-
102
- getBindCl(ep) {
103
- return (ep.indexOf('_') > 0) ? ep.split('_')[1] : null;
104
- }
105
-
106
- async doBindUnbind(type, bind_source, bind_source_ep, bind_target, bind_target_ep, callback) {
107
- try {
108
- const id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
109
-
110
- const source = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_source)}`, this.getBindEp(bind_source_ep));
111
- this.debug(`source: ${safeJsonStringify(source)}`);
112
- let target = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_target)}`, this.getBindEp(bind_target_ep));
113
- this.debug(`target: ${safeJsonStringify(target)}`);
114
- if (!target) {
115
- if (bind_target === 'coordinator') {
116
- target = await this.zbController.resolveEntity(bind_target);
117
- this.debug(`Coordinator target: ${safeJsonStringify(target)}`);
118
- } else {
119
- target = await this.zbController.resolveEntity(parseInt(bind_target));
120
- this.debug(`Group target: ${safeJsonStringify(target)}`);
121
- }
122
- }
123
-
124
- if (!source || !target) {
125
- this.error('Devices not found');
126
- if (callback) callback('Devices not found');
127
- return;
128
- }
129
- const sourceName = source.name;
130
- const targetName = target.name;
131
- let found = false;
132
- const bindCluster = this.getBindCl(bind_source_ep);
133
- const clusters = bindCluster ? [bindCluster] : allowClusters;
134
- // Find which clusters are supported by both the source and target.
135
- // Groups are assumed to support all clusters.
136
- for (const clID of clusters) {
137
- const cluster = allowClustersName[clID];
138
- const targetValid = target.type === 'group' ||
139
- target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
140
-
141
- if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
142
- found = true;
143
- }
144
- }
145
- if (!found) {
146
- this.debug(`No bind clusters`);
147
- if (callback) callback(`No bind clusters`);
148
- return;
149
- } else {
150
- let ok = true;
151
- for (const clID of clusters) {
152
- const cluster = allowClustersName[clID];
153
- const targetValid = target.type === 'group' ||
154
- target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
155
-
156
- if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
157
- this.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
158
- try {
159
- const bindTarget = target.type === 'group' ? target.group : target.endpoint;
160
- if (type === 'bind') {
161
- await source.endpoint.bind(cluster, bindTarget);
162
- } else {
163
- await source.endpoint.unbind(cluster, bindTarget);
164
- }
165
- this.info(
166
- `Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
167
- `'${sourceName}' to '${targetName}'`,
168
- );
169
- } catch (error) {
170
- this.error(
171
- `Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
172
- `'${targetName}' (${error})`,
173
- );
174
- if (callback) callback(`Failed to ${type} cluster '${cluster}' from '${sourceName}' to '${targetName}' (${error})`);
175
- ok = false;
176
- break;
177
- }
178
- }
179
- }
180
- if (ok && callback) callback(undefined, id);
181
- }
182
- } catch (error) {
183
- this.error(`Failed to doBindUnbind ${error.stack}`);
184
- if (callback) callback(`Failed to doBindUnbind ${error.stack}`);
185
- }
186
- }
187
-
188
- async addBinding(from, command, params, callback) {
189
- try {
190
- this.debug('addBinding message: ' + JSON.stringify(params));
191
- const bind_source = params.bind_source,
192
- bind_source_ep = params.bind_source_ep,
193
- bind_target = params.bind_target,
194
- bind_target_ep = params.bind_target_ep;
195
-
196
- if (params.unbind_from_coordinator) {
197
- await this.doBindUnbind('unbind', bind_source, bind_source_ep, 'coordinator', '1');
198
- }
199
-
200
- await this.doBindUnbind('bind', bind_source, bind_source_ep, bind_target, bind_target_ep, (err, id) => {
201
- if (err) {
202
- callback({error: err});
203
- } else {
204
- const stateId = `info.${id}`;
205
- // now set state
206
- this.adapter.setObjectNotExists(stateId, {
207
- type: 'state',
208
- common: {name: id},
209
- }, () => {
210
- this.adapter.setState(stateId, JSON.stringify(params), true, () => {
211
- callback();
212
- });
213
- });
214
- }
215
- });
216
- } catch (error) {
217
- this.error(`Failed to addBinding ${error.stack}`);
218
- throw new Error(`Failed to addBinding ${error.stack}`);
219
- }
220
- }
221
-
222
- async editBinding(from, command, params, callback) {
223
- try {
224
- this.debug('editBinding message: ' + JSON.stringify(params));
225
- const old_id = params.id,
226
- bind_source = params.bind_source,
227
- bind_source_ep = params.bind_source_ep,
228
- bind_target = params.bind_target,
229
- bind_target_ep = params.bind_target_ep,
230
- id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
231
- if (old_id !== id) {
232
- await this.delBinding(from, command, old_id, async (err)=>{
233
- if (err) {
234
- callback(err);
235
- } else {
236
- await this.addBinding(from, command, params, callback);
237
- }
238
- });
239
- } else {
240
- const type = params.unbind_from_coordinator ? 'unbind' : 'bind';
241
- try {
242
- await this.doBindUnbind(type , bind_source, bind_source_ep, 'coordinator', '1');
243
- this.debug('Successfully ' + (type === 'bind' ? 'bound' : 'unbound') + ' Coordinator from ' + bind_source);
244
- } catch (e) {
245
- this.error('Could not ' + type + ' Coordinator from ' + bind_source + ': ' + JSON.stringify(e));
246
- }
247
- }
248
- } catch (error) {
249
- this.error(`Failed to editBinding ${error.stack}`);
250
- }
251
- }
252
-
253
- async delBinding(from, command, bind_id, callback) {
254
- try {
255
- this.debug('delBinding message: ' + JSON.stringify(bind_id));
256
- const stateId = `info.${bind_id}`;
257
- this.adapter.getStateAsync(stateId)
258
- .then(async (stateV) => {
259
- this.debug('found state: ' + JSON.stringify(stateV));
260
- const params = JSON.parse(stateV.val);
261
- const bind_source = params.bind_source,
262
- bind_source_ep = params.bind_source_ep,
263
- bind_target = params.bind_target,
264
- bind_target_ep = params.bind_target_ep;
265
- await this.doBindUnbind('unbind', bind_source, bind_source_ep, bind_target, bind_target_ep, async (err) => {
266
- if (err) {
267
- callback({error: err});
268
- } else {
269
- this.adapter.deleteState(null, 'info', bind_id, async () => {
270
- // if (err) {
271
- // callback({error: err});
272
- // } else {
273
- if (params.unbind_from_coordinator) {
274
- await this.doBindUnbind('bind', bind_source, bind_source_ep, 'coordinator', '1', callback);
275
- } else {
276
- callback();
277
- }
278
- //}
279
- });
280
- }
281
- });
282
- });
283
- } catch (error) {
284
- this.error(`Failed to delBinding ${error.stack}`);
285
- throw new Error(`Failed to delBinding ${error.stack}`);
286
- }
287
- }
288
-
289
- getBinding(callback) {
290
- try {
291
- const binding = [];
292
- this.adapter.getStatesOf('info', (err, states) => {
293
- if (!err && states) {
294
- const chain = [];
295
- states.forEach(state => {
296
- if (state._id.startsWith(`${this.adapter.namespace}.info.bind_`)) {
297
- chain.push(new Promise(resolve => {
298
- return this.adapter.getStateAsync(state._id)
299
- .then(stateV => {
300
- if (stateV !== null) {
301
- const val = JSON.parse(stateV.val);
302
- val.id = this.extractBindId(state._id);
303
- binding.push(val);
304
- }
305
- resolve();
306
- });
307
- }));
308
- }
309
- });
310
- return Promise.all(chain).then(() => {
311
- this.debug('getBinding result: ' + JSON.stringify(binding));
312
- callback(binding);
313
- });
314
- } else {
315
- this.debug('getBinding result: ' + JSON.stringify(binding));
316
- callback(binding);
317
- }
318
- });
319
- } catch (error) {
320
- this.error(`Failed to getBinding ${error.stack}`);
321
- }
322
- }
323
- }
324
-
325
- module.exports = Binding;
1
+ 'use strict';
2
+
3
+ const safeJsonStringify = require('./json');
4
+
5
+ // 5 - genScenes, 6 - genOnOff, 8 - genLevelCtrl, 768 - lightingColorCtrl
6
+ const allowClusters = [5, 6, 8, 768];
7
+ const allowClustersName = {5: 'genScenes', 6: 'genOnOff', 8: 'genLevelCtrl', 768: 'lightingColorCtrl'};
8
+
9
+
10
+ class Binding {
11
+ constructor(adapter) {
12
+ this.adapter = adapter;
13
+ this.adapter.on('message', this.onMessage.bind(this));
14
+ }
15
+
16
+ start(zbController, stController) {
17
+ this.zbController = zbController;
18
+ this.stController = stController;
19
+ }
20
+
21
+ stop() {
22
+ delete this.zbController;
23
+ delete this.stController;
24
+ }
25
+
26
+ info(msg) {
27
+ this.adapter.log.info(msg);
28
+ }
29
+
30
+ error(msg) {
31
+ this.adapter.log.error(msg);
32
+ }
33
+
34
+ debug(msg) {
35
+ this.adapter.log.debug(msg);
36
+ }
37
+
38
+ warn(msg) {
39
+ this.adapter.log.warn(msg);
40
+ }
41
+
42
+ /**
43
+ * @param {ioBroker.Message} obj
44
+ */
45
+ onMessage(obj) {
46
+ if (typeof obj === 'object' && obj.command) {
47
+ switch (obj.command) {
48
+ case 'addBinding':
49
+ if (obj && obj.message && typeof obj.message === 'object') {
50
+ this.addBinding(obj.from, obj.command, obj.message, (err)=>{
51
+ this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
52
+ });
53
+ }
54
+ break;
55
+ case 'editBinding':
56
+ if (obj && obj.message && typeof obj.message === 'object') {
57
+ this.editBinding(obj.from, obj.command, obj.message, (err)=>{
58
+ this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
59
+ });
60
+ }
61
+ break;
62
+ case 'getBinding':
63
+ if (obj && obj.message && typeof obj.message === 'object') {
64
+ this.getBinding((binding)=>{
65
+ this.adapter.sendTo(obj.from, obj.command, binding, obj.callback);
66
+ });
67
+ }
68
+ break;
69
+ case 'delBinding':
70
+ if (obj && obj.message) {
71
+ this.delBinding(obj.from, obj.command, obj.message, (err)=>{
72
+ this.adapter.sendTo(obj.from, obj.command, err, obj.callback);
73
+ });
74
+ }
75
+ break;
76
+ }
77
+ }
78
+ }
79
+
80
+ extractBindId(stateId) {
81
+ return stateId.replace(`${this.adapter.namespace}.info.`, '');
82
+ }
83
+
84
+ getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep) {
85
+ return `bind_${this.extractDeviceId(bind_source)}_${bind_source_ep}_${this.extractDeviceId(bind_target)}_${bind_target_ep}`;
86
+ }
87
+
88
+ extractDeviceId(stateId) {
89
+ if (stateId) {
90
+ return stateId.replace(`${this.adapter.namespace}.`, '');
91
+ }
92
+ return '';
93
+ }
94
+
95
+ getBindEp(ep) {
96
+ if (ep) {
97
+ return parseInt(ep.split('_')[0]);
98
+ }
99
+
100
+ this.warn(`getBindEp called with illegal ep: ${safeJsonStringify(ep)}`);
101
+ return 0;
102
+ }
103
+
104
+ getBindCl(ep) {
105
+ return ep.indexOf('_') > 0 ? ep.split('_')[1] : null;
106
+ }
107
+
108
+ async doBindUnbind(type, bind_source, bind_source_ep, bind_target, bind_target_ep, callback) {
109
+ try {
110
+ const id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
111
+
112
+ const source = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_source)}`, this.getBindEp(bind_source_ep));
113
+ this.debug(`source: ${safeJsonStringify(source)}`);
114
+ let target = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_target)}`, this.getBindEp(bind_target_ep));
115
+ this.debug(`target: ${safeJsonStringify(target)}`);
116
+ if (!target) {
117
+ if (bind_target === 'coordinator') {
118
+ target = await this.zbController.resolveEntity(bind_target);
119
+ this.debug(`Coordinator target: ${safeJsonStringify(target)}`);
120
+ } else {
121
+ target = await this.zbController.resolveEntity(parseInt(bind_target));
122
+ this.debug(`Group target: ${safeJsonStringify(target)}`);
123
+ }
124
+ }
125
+
126
+ if (!source || !target) {
127
+ this.error('Devices not found');
128
+ return callback && callback('Devices not found');
129
+ }
130
+ const sourceName = source.name;
131
+ const targetName = target.name;
132
+ let found = false;
133
+ const bindCluster = this.getBindCl(bind_source_ep);
134
+ const clusters = bindCluster ? [bindCluster] : allowClusters;
135
+ // Find which clusters are supported by both the source and target.
136
+ // Groups are assumed to support all clusters.
137
+ for (const clID of clusters) {
138
+ const cluster = allowClustersName[clID];
139
+ const targetValid = target.type === 'group' ||
140
+ target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
141
+
142
+ if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
143
+ found = true;
144
+ }
145
+ }
146
+ if (!found) {
147
+ this.debug(`No bind clusters`);
148
+ return callback && callback(`No bind clusters`);
149
+ } else {
150
+ let ok = true;
151
+ for (const clID of clusters) {
152
+ const cluster = allowClustersName[clID];
153
+ const targetValid = target.type === 'group' ||
154
+ target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
155
+
156
+ if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
157
+ this.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
158
+ try {
159
+ const bindTarget = target.type === 'group' ? target.group : target.endpoint;
160
+ if (type === 'bind') {
161
+ await source.endpoint.bind(cluster, bindTarget);
162
+ } else {
163
+ await source.endpoint.unbind(cluster, bindTarget);
164
+ }
165
+ this.info(
166
+ `Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
167
+ `'${sourceName}' to '${targetName}'`,
168
+ );
169
+ } catch (error) {
170
+ this.error(
171
+ `Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
172
+ `'${targetName}' (${error})`,
173
+ );
174
+ callback && callback(`Failed to ${type} cluster '${cluster}' from '${sourceName}' to '${targetName}' (${error})`);
175
+ ok = false;
176
+ break;
177
+ }
178
+ }
179
+ }
180
+ ok && callback && callback(undefined, id);
181
+ }
182
+ } catch (error) {
183
+ this.error(`Failed to doBindUnbind ${error.stack}`);
184
+ callback && callback(`Failed to doBindUnbind ${error.stack}`);
185
+ }
186
+ }
187
+
188
+ async addBinding(from, command, params, callback) {
189
+ try {
190
+ this.debug(`addBinding message: ${JSON.stringify(params)}`);
191
+ const bind_source = params.bind_source,
192
+ bind_source_ep = params.bind_source_ep,
193
+ bind_target = params.bind_target,
194
+ bind_target_ep = params.bind_target_ep;
195
+
196
+ if (params.unbind_from_coordinator) {
197
+ await this.doBindUnbind('unbind', bind_source, bind_source_ep, 'coordinator', '1');
198
+ }
199
+
200
+ await this.doBindUnbind('bind', bind_source, bind_source_ep, bind_target, bind_target_ep, (err, id) => {
201
+ if (err) {
202
+ callback({error: err});
203
+ } else {
204
+ const stateId = `info.${id}`;
205
+ // now set state
206
+ this.adapter.setObjectNotExists(stateId, {
207
+ type: 'state',
208
+ common: {name: id},
209
+ }, () => {
210
+ this.adapter.setState(stateId, JSON.stringify(params), true, () =>
211
+ callback());
212
+ });
213
+ }
214
+ });
215
+ } catch (error) {
216
+ this.error(`Failed to addBinding ${error.stack}`);
217
+ throw new Error(`Failed to addBinding ${error.stack}`);
218
+ }
219
+ }
220
+
221
+ async editBinding(from, command, params, callback) {
222
+ try {
223
+ this.debug(`editBinding message: ${JSON.stringify(params)}`);
224
+ const old_id = params.id,
225
+ bind_source = params.bind_source,
226
+ bind_source_ep = params.bind_source_ep,
227
+ bind_target = params.bind_target,
228
+ bind_target_ep = params.bind_target_ep,
229
+ id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
230
+ if (old_id !== id) {
231
+ await this.delBinding(from, command, old_id, async err => {
232
+ if (err) {
233
+ callback(err);
234
+ } else {
235
+ await this.addBinding(from, command, params, callback);
236
+ }
237
+ });
238
+ } else {
239
+ const type = params.unbind_from_coordinator ? 'unbind' : 'bind';
240
+ try {
241
+ await this.doBindUnbind(type , bind_source, bind_source_ep, 'coordinator', '1');
242
+ this.debug('Successfully ' + (type === 'bind' ? 'bound' : 'unbound') + ' Coordinator from ' + bind_source);
243
+ } catch (e) {
244
+ this.error(`Could not ${type} Coordinator from ${bind_source}: ${JSON.stringify(e)}`);
245
+ }
246
+ }
247
+ } catch (error) {
248
+ this.error(`Failed to editBinding ${error.stack}`);
249
+ }
250
+ }
251
+
252
+ async delBinding(from, command, bind_id, callback) {
253
+ try {
254
+ this.debug(`delBinding message: ${JSON.stringify(bind_id)}`);
255
+ const stateId = `info.${bind_id}`;
256
+ this.adapter.getStateAsync(stateId)
257
+ .then(async stateV => {
258
+ this.debug(`found state: ${JSON.stringify(stateV)}`);
259
+ const params = JSON.parse(stateV.val);
260
+ const bind_source = params.bind_source,
261
+ bind_source_ep = params.bind_source_ep,
262
+ bind_target = params.bind_target,
263
+ bind_target_ep = params.bind_target_ep;
264
+ await this.doBindUnbind('unbind', bind_source, bind_source_ep, bind_target, bind_target_ep, async err => {
265
+ if (err) {
266
+ callback({error: err});
267
+ } else {
268
+ this.adapter.deleteState(null, 'info', bind_id, async () => {
269
+ // if (err) {
270
+ // callback({error: err});
271
+ // } else {
272
+ if (params.unbind_from_coordinator) {
273
+ await this.doBindUnbind('bind', bind_source, bind_source_ep, 'coordinator', '1', callback);
274
+ } else {
275
+ callback();
276
+ }
277
+ //}
278
+ });
279
+ }
280
+ });
281
+ });
282
+ } catch (error) {
283
+ this.error(`Failed to delBinding ${error.stack}`);
284
+ throw new Error(`Failed to delBinding ${error.stack}`);
285
+ }
286
+ }
287
+
288
+ getBinding(callback) {
289
+ try {
290
+ const binding = [];
291
+ this.adapter.getStatesOf('info', (err, states) => {
292
+ if (!err && states) {
293
+ const chain = [];
294
+ states.forEach(state => {
295
+ if (state._id.startsWith(`${this.adapter.namespace}.info.bind_`)) {
296
+ chain.push(new Promise(resolve => {
297
+ return this.adapter.getStateAsync(state._id)
298
+ .then(stateV => {
299
+ if (stateV !== null) {
300
+ const val = JSON.parse(stateV.val);
301
+ val.id = this.extractBindId(state._id);
302
+ binding.push(val);
303
+ }
304
+ resolve();
305
+ });
306
+ }));
307
+ }
308
+ });
309
+ return Promise.all(chain).then(() => {
310
+ this.debug(`getBinding result: ${JSON.stringify(binding)}`);
311
+ callback(binding);
312
+ });
313
+ } else {
314
+ this.debug(`getBinding result: ${JSON.stringify(binding)}`);
315
+ callback(binding);
316
+ }
317
+ });
318
+ } catch (error) {
319
+ this.error(`Failed to getBinding ${error.stack}`);
320
+ }
321
+ }
322
+ }
323
+
324
+ module.exports = Binding;