iobroker.zigbee 1.8.10 → 1.8.13

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 +417 -400
  3. package/admin/adapter-settings.js +244 -244
  4. package/admin/admin.js +2981 -2981
  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 -1356
  19. package/admin/moment.min.js +1 -1
  20. package/admin/shuffle.min.js +2 -2
  21. package/admin/tab_m.html +1009 -1009
  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 -19
  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 +32 -21
  77. package/lib/backup.js +171 -171
  78. package/lib/binding.js +319 -319
  79. package/lib/colors.js +465 -465
  80. package/lib/commands.js +534 -534
  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 +913 -830
  85. package/lib/groups.js +345 -345
  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 +672 -693
  93. package/lib/tools.js +54 -54
  94. package/lib/utils.js +163 -163
  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 -989
  101. package/main.js +133 -123
  102. package/package.json +10 -10
  103. package/support/docgen.js +93 -93
  104. /package/admin/img/{paumann_spot.png → paulmann_spot.png} +0 -0
package/lib/binding.js CHANGED
@@ -1,319 +1,319 @@
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
- break;
54
- case 'editBinding':
55
- if (obj && obj.message && typeof obj.message === 'object') {
56
- this.editBinding(obj.from, obj.command, obj.message, err =>
57
- this.adapter.sendTo(obj.from, obj.command, err, obj.callback));
58
- }
59
- break;
60
- case 'getBinding':
61
- if (obj && obj.message && typeof obj.message === 'object') {
62
- this.getBinding(binding =>
63
- this.adapter.sendTo(obj.from, obj.command, binding, obj.callback));
64
- }
65
- break;
66
- case 'delBinding':
67
- if (obj && obj.message) {
68
- this.delBinding(obj.from, obj.command, obj.message, err =>
69
- this.adapter.sendTo(obj.from, obj.command, err, obj.callback));
70
- }
71
- break;
72
- }
73
- }
74
- }
75
-
76
- extractBindId(stateId) {
77
- return stateId.replace(`${this.adapter.namespace}.info.`, '');
78
- }
79
-
80
- getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep) {
81
- return `bind_${this.extractDeviceId(bind_source)}_${bind_source_ep}_${this.extractDeviceId(bind_target)}_${bind_target_ep}`;
82
- }
83
-
84
- extractDeviceId(stateId) {
85
- if (stateId) {
86
- return stateId.replace(`${this.adapter.namespace}.`, '');
87
- }
88
- return '';
89
- }
90
-
91
- getBindEp(ep) {
92
- if (ep) {
93
- return parseInt(ep.split('_')[0]);
94
- }
95
-
96
- this.warn(`getBindEp called with illegal ep: ${safeJsonStringify(ep)}`);
97
- return 0;
98
- }
99
-
100
- getBindCl(ep) {
101
- return ep.indexOf('_') > 0 ? ep.split('_')[1] : null;
102
- }
103
-
104
- async doBindUnbind(type, bind_source, bind_source_ep, bind_target, bind_target_ep, callback) {
105
- try {
106
- const id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
107
- const source = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_source)}`, this.getBindEp(bind_source_ep));
108
- this.debug(`source: ${safeJsonStringify(source)}`);
109
- let target = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_target)}`, this.getBindEp(bind_target_ep));
110
- this.debug(`target: ${safeJsonStringify(target)}`);
111
- if (!target) {
112
- if (bind_target === 'coordinator') {
113
- target = await this.zbController.resolveEntity(bind_target);
114
- this.debug(`Coordinator target: ${safeJsonStringify(target)}`);
115
- } else {
116
- target = await this.zbController.resolveEntity(parseInt(bind_target));
117
- this.debug(`Group target: ${safeJsonStringify(target)}`);
118
- }
119
- }
120
-
121
- if (!source || !target) {
122
- this.error('Devices not found');
123
- return callback && callback('Devices not found');
124
- }
125
- const sourceName = source.name;
126
- const targetName = target.name;
127
- let found = false;
128
- const bindCluster = this.getBindCl(bind_source_ep);
129
- const clusters = bindCluster ? [bindCluster] : allowClusters;
130
- // Find which clusters are supported by both the source and target.
131
- // Groups are assumed to support all clusters.
132
- for (const clID of clusters) {
133
- const cluster = allowClustersName[clID];
134
- const targetValid = target.type === 'group' ||
135
- target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
136
-
137
- if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
138
- found = true;
139
- }
140
- }
141
- if (!found) {
142
- this.debug(`No bind clusters`);
143
- return callback && callback(`No bind clusters`);
144
- } else {
145
- let ok = true;
146
- for (const clID of clusters) {
147
- const cluster = allowClustersName[clID];
148
- const targetValid = target.type === 'group' ||
149
- target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
150
-
151
- if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
152
- this.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
153
- try {
154
- const bindTarget = target.type === 'group' ? target.group : target.endpoint;
155
- if (type === 'bind') {
156
- await source.endpoint.bind(cluster, bindTarget);
157
- } else {
158
- await source.endpoint.unbind(cluster, bindTarget);
159
- }
160
- this.info(
161
- `Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
162
- `'${sourceName}' to '${targetName}'`,
163
- );
164
- } catch (error) {
165
- this.error(
166
- `Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
167
- `'${targetName}' (${error})`,
168
- );
169
- callback && callback(`Failed to ${type} cluster '${cluster}' from '${sourceName}' to '${targetName}' (${error})`);
170
- ok = false;
171
- break;
172
- }
173
- }
174
- }
175
- ok && callback && callback(undefined, id);
176
- }
177
- } catch (error) {
178
- this.error(`Failed to doBindUnbind ${error.stack}`);
179
- callback && callback(`Failed to doBindUnbind ${error.stack}`);
180
- }
181
- }
182
-
183
- async addBinding(from, command, params, callback) {
184
- try {
185
- this.debug(`addBinding message: ${JSON.stringify(params)}`);
186
- const bind_source = params.bind_source,
187
- bind_source_ep = params.bind_source_ep,
188
- bind_target = params.bind_target,
189
- bind_target_ep = params.bind_target_ep;
190
-
191
- if (params.unbind_from_coordinator) {
192
- await this.doBindUnbind('unbind', bind_source, bind_source_ep, 'coordinator', '1');
193
- }
194
-
195
- await this.doBindUnbind('bind', bind_source, bind_source_ep, bind_target, bind_target_ep, (err, id) => {
196
- if (err) {
197
- callback({error: err});
198
- } else {
199
- const stateId = `info.${id}`;
200
- // now set state
201
- this.adapter.setObjectNotExists(stateId, {
202
- type: 'state',
203
- common: {name: id},
204
- }, () => {
205
- this.adapter.setState(stateId, JSON.stringify(params), true, () =>
206
- callback());
207
- });
208
- }
209
- });
210
- } catch (error) {
211
- this.error(`Failed to addBinding ${error.stack}`);
212
- throw new Error(`Failed to addBinding ${error.stack}`);
213
- }
214
- }
215
-
216
- async editBinding(from, command, params, callback) {
217
- try {
218
- this.debug(`editBinding message: ${JSON.stringify(params)}`);
219
- const old_id = params.id,
220
- bind_source = params.bind_source,
221
- bind_source_ep = params.bind_source_ep,
222
- bind_target = params.bind_target,
223
- bind_target_ep = params.bind_target_ep,
224
- id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
225
- if (old_id !== id) {
226
- await this.delBinding(from, command, old_id, async err => {
227
- if (err) {
228
- callback(err);
229
- } else {
230
- await this.addBinding(from, command, params, callback);
231
- }
232
- });
233
- } else {
234
- const type = params.unbind_from_coordinator ? 'unbind' : 'bind';
235
- try {
236
- await this.doBindUnbind(type, bind_source, bind_source_ep, 'coordinator', '1');
237
- this.debug('Successfully ' + (type === 'bind' ? 'bound' : 'unbound') + ' Coordinator from ' + bind_source);
238
- } catch (e) {
239
- this.error(`Could not ${type} Coordinator from ${bind_source}: ${JSON.stringify(e)}`);
240
- }
241
- }
242
- } catch (error) {
243
- this.error(`Failed to editBinding ${error.stack}`);
244
- }
245
- }
246
-
247
- async delBinding(from, command, bind_id, callback) {
248
- try {
249
- this.debug(`delBinding message: ${JSON.stringify(bind_id)}`);
250
- const stateId = `info.${bind_id}`;
251
- this.adapter.getStateAsync(stateId)
252
- .then(async stateV => {
253
- this.debug(`found state: ${JSON.stringify(stateV)}`);
254
- const params = JSON.parse(stateV.val);
255
- const bind_source = params.bind_source,
256
- bind_source_ep = params.bind_source_ep,
257
- bind_target = params.bind_target,
258
- bind_target_ep = params.bind_target_ep;
259
- await this.doBindUnbind('unbind', bind_source, bind_source_ep, bind_target, bind_target_ep, async err => {
260
- if (err) {
261
- callback({error: err});
262
- } else {
263
- this.adapter.deleteState(null, 'info', bind_id, async () => {
264
- // if (err) {
265
- // callback({error: err});
266
- // } else {
267
- if (params.unbind_from_coordinator) {
268
- await this.doBindUnbind('bind', bind_source, bind_source_ep, 'coordinator', '1', callback);
269
- } else {
270
- callback();
271
- }
272
- //}
273
- });
274
- }
275
- });
276
- });
277
- } catch (error) {
278
- this.error(`Failed to delBinding ${error.stack}`);
279
- throw new Error(`Failed to delBinding ${error.stack}`);
280
- }
281
- }
282
-
283
- getBinding(callback) {
284
- try {
285
- const binding = [];
286
- this.adapter.getStatesOf('info', (err, states) => {
287
- if (!err && states) {
288
- const chain = [];
289
- states.forEach(state => {
290
- if (state._id.startsWith(`${this.adapter.namespace}.info.bind_`)) {
291
- chain.push(new Promise(resolve => {
292
- return this.adapter.getStateAsync(state._id)
293
- .then(stateV => {
294
- if (stateV !== null) {
295
- const val = JSON.parse(stateV.val);
296
- val.id = this.extractBindId(state._id);
297
- binding.push(val);
298
- }
299
- resolve();
300
- });
301
- }));
302
- }
303
- });
304
- return Promise.all(chain).then(() => {
305
- this.debug(`getBinding result: ${JSON.stringify(binding)}`);
306
- callback(binding);
307
- });
308
- } else {
309
- this.debug(`getBinding result: ${JSON.stringify(binding)}`);
310
- callback(binding);
311
- }
312
- });
313
- } catch (error) {
314
- this.error(`Failed to getBinding ${error.stack}`);
315
- }
316
- }
317
- }
318
-
319
- 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
+ break;
54
+ case 'editBinding':
55
+ if (obj && obj.message && typeof obj.message === 'object') {
56
+ this.editBinding(obj.from, obj.command, obj.message, err =>
57
+ this.adapter.sendTo(obj.from, obj.command, err, obj.callback));
58
+ }
59
+ break;
60
+ case 'getBinding':
61
+ if (obj && obj.message && typeof obj.message === 'object') {
62
+ this.getBinding(binding =>
63
+ this.adapter.sendTo(obj.from, obj.command, binding, obj.callback));
64
+ }
65
+ break;
66
+ case 'delBinding':
67
+ if (obj && obj.message) {
68
+ this.delBinding(obj.from, obj.command, obj.message, err =>
69
+ this.adapter.sendTo(obj.from, obj.command, err, obj.callback));
70
+ }
71
+ break;
72
+ }
73
+ }
74
+ }
75
+
76
+ extractBindId(stateId) {
77
+ return stateId.replace(`${this.adapter.namespace}.info.`, '');
78
+ }
79
+
80
+ getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep) {
81
+ return `bind_${this.extractDeviceId(bind_source)}_${bind_source_ep}_${this.extractDeviceId(bind_target)}_${bind_target_ep}`;
82
+ }
83
+
84
+ extractDeviceId(stateId) {
85
+ if (stateId) {
86
+ return stateId.replace(`${this.adapter.namespace}.`, '');
87
+ }
88
+ return '';
89
+ }
90
+
91
+ getBindEp(ep) {
92
+ if (ep) {
93
+ return parseInt(ep.split('_')[0]);
94
+ }
95
+
96
+ this.warn(`getBindEp called with illegal ep: ${safeJsonStringify(ep)}`);
97
+ return 0;
98
+ }
99
+
100
+ getBindCl(ep) {
101
+ return ep.indexOf('_') > 0 ? ep.split('_')[1] : null;
102
+ }
103
+
104
+ async doBindUnbind(type, bind_source, bind_source_ep, bind_target, bind_target_ep, callback) {
105
+ try {
106
+ const id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
107
+ const source = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_source)}`, this.getBindEp(bind_source_ep));
108
+ this.debug(`source: ${safeJsonStringify(source)}`);
109
+ let target = await this.zbController.resolveEntity(`0x${this.extractDeviceId(bind_target)}`, this.getBindEp(bind_target_ep));
110
+ this.debug(`target: ${safeJsonStringify(target)}`);
111
+ if (!target) {
112
+ if (bind_target === 'coordinator') {
113
+ target = await this.zbController.resolveEntity(bind_target);
114
+ this.debug(`Coordinator target: ${safeJsonStringify(target)}`);
115
+ } else {
116
+ target = await this.zbController.resolveEntity(parseInt(bind_target));
117
+ this.debug(`Group target: ${safeJsonStringify(target)}`);
118
+ }
119
+ }
120
+
121
+ if (!source || !target) {
122
+ this.error('Devices not found');
123
+ return callback && callback('Devices not found');
124
+ }
125
+ const sourceName = source.name;
126
+ const targetName = target.name;
127
+ let found = false;
128
+ const bindCluster = this.getBindCl(bind_source_ep);
129
+ const clusters = bindCluster ? [bindCluster] : allowClusters;
130
+ // Find which clusters are supported by both the source and target.
131
+ // Groups are assumed to support all clusters.
132
+ for (const clID of clusters) {
133
+ const cluster = allowClustersName[clID];
134
+ const targetValid = target.type === 'group' ||
135
+ target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
136
+
137
+ if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
138
+ found = true;
139
+ }
140
+ }
141
+ if (!found) {
142
+ this.debug(`No bind clusters`);
143
+ return callback && callback(`No bind clusters`);
144
+ } else {
145
+ let ok = true;
146
+ for (const clID of clusters) {
147
+ const cluster = allowClustersName[clID];
148
+ const targetValid = target.type === 'group' ||
149
+ target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
150
+
151
+ if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
152
+ this.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
153
+ try {
154
+ const bindTarget = target.type === 'group' ? target.group : target.endpoint;
155
+ if (type === 'bind') {
156
+ await source.endpoint.bind(cluster, bindTarget);
157
+ } else {
158
+ await source.endpoint.unbind(cluster, bindTarget);
159
+ }
160
+ this.info(
161
+ `Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
162
+ `'${sourceName}' to '${targetName}'`,
163
+ );
164
+ } catch (error) {
165
+ this.error(
166
+ `Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
167
+ `'${targetName}' (${error})`,
168
+ );
169
+ callback && callback(`Failed to ${type} cluster '${cluster}' from '${sourceName}' to '${targetName}' (${error})`);
170
+ ok = false;
171
+ break;
172
+ }
173
+ }
174
+ }
175
+ ok && callback && callback(undefined, id);
176
+ }
177
+ } catch (error) {
178
+ this.error(`Failed to doBindUnbind ${error.stack}`);
179
+ callback && callback(`Failed to doBindUnbind ${error.stack}`);
180
+ }
181
+ }
182
+
183
+ async addBinding(from, command, params, callback) {
184
+ try {
185
+ this.debug(`addBinding message: ${JSON.stringify(params)}`);
186
+ const bind_source = params.bind_source,
187
+ bind_source_ep = params.bind_source_ep,
188
+ bind_target = params.bind_target,
189
+ bind_target_ep = params.bind_target_ep;
190
+
191
+ if (params.unbind_from_coordinator) {
192
+ await this.doBindUnbind('unbind', bind_source, bind_source_ep, 'coordinator', '1');
193
+ }
194
+
195
+ await this.doBindUnbind('bind', bind_source, bind_source_ep, bind_target, bind_target_ep, (err, id) => {
196
+ if (err) {
197
+ callback({error: err});
198
+ } else {
199
+ const stateId = `info.${id}`;
200
+ // now set state
201
+ this.adapter.setObjectNotExists(stateId, {
202
+ type: 'state',
203
+ common: {name: id},
204
+ }, () => {
205
+ this.adapter.setState(stateId, JSON.stringify(params), true, () =>
206
+ callback());
207
+ });
208
+ }
209
+ });
210
+ } catch (error) {
211
+ this.error(`Failed to addBinding ${error.stack}`);
212
+ throw new Error(`Failed to addBinding ${error.stack}`);
213
+ }
214
+ }
215
+
216
+ async editBinding(from, command, params, callback) {
217
+ try {
218
+ this.debug(`editBinding message: ${JSON.stringify(params)}`);
219
+ const old_id = params.id,
220
+ bind_source = params.bind_source,
221
+ bind_source_ep = params.bind_source_ep,
222
+ bind_target = params.bind_target,
223
+ bind_target_ep = params.bind_target_ep,
224
+ id = this.getBindingId(bind_source, bind_source_ep, bind_target, bind_target_ep);
225
+ if (old_id !== id) {
226
+ await this.delBinding(from, command, old_id, async err => {
227
+ if (err) {
228
+ callback(err);
229
+ } else {
230
+ await this.addBinding(from, command, params, callback);
231
+ }
232
+ });
233
+ } else {
234
+ const type = params.unbind_from_coordinator ? 'unbind' : 'bind';
235
+ try {
236
+ await this.doBindUnbind(type, bind_source, bind_source_ep, 'coordinator', '1');
237
+ this.debug('Successfully ' + (type === 'bind' ? 'bound' : 'unbound') + ' Coordinator from ' + bind_source);
238
+ } catch (e) {
239
+ this.error(`Could not ${type} Coordinator from ${bind_source}: ${JSON.stringify(e)}`);
240
+ }
241
+ }
242
+ } catch (error) {
243
+ this.error(`Failed to editBinding ${error.stack}`);
244
+ }
245
+ }
246
+
247
+ async delBinding(from, command, bind_id, callback) {
248
+ try {
249
+ this.debug(`delBinding message: ${JSON.stringify(bind_id)}`);
250
+ const stateId = `info.${bind_id}`;
251
+ this.adapter.getStateAsync(stateId)
252
+ .then(async stateV => {
253
+ this.debug(`found state: ${JSON.stringify(stateV)}`);
254
+ const params = JSON.parse(stateV.val);
255
+ const bind_source = params.bind_source,
256
+ bind_source_ep = params.bind_source_ep,
257
+ bind_target = params.bind_target,
258
+ bind_target_ep = params.bind_target_ep;
259
+ await this.doBindUnbind('unbind', bind_source, bind_source_ep, bind_target, bind_target_ep, async err => {
260
+ if (err) {
261
+ callback({error: err});
262
+ } else {
263
+ this.adapter.deleteState(null, 'info', bind_id, async () => {
264
+ // if (err) {
265
+ // callback({error: err});
266
+ // } else {
267
+ if (params.unbind_from_coordinator) {
268
+ await this.doBindUnbind('bind', bind_source, bind_source_ep, 'coordinator', '1', callback);
269
+ } else {
270
+ callback();
271
+ }
272
+ //}
273
+ });
274
+ }
275
+ });
276
+ });
277
+ } catch (error) {
278
+ this.error(`Failed to delBinding ${error.stack}`);
279
+ throw new Error(`Failed to delBinding ${error.stack}`);
280
+ }
281
+ }
282
+
283
+ getBinding(callback) {
284
+ try {
285
+ const binding = [];
286
+ this.adapter.getStatesOf('info', (err, states) => {
287
+ if (!err && states) {
288
+ const chain = [];
289
+ states.forEach(state => {
290
+ if (state._id.startsWith(`${this.adapter.namespace}.info.bind_`)) {
291
+ chain.push(new Promise(resolve => {
292
+ return this.adapter.getStateAsync(state._id)
293
+ .then(stateV => {
294
+ if (stateV !== null) {
295
+ const val = JSON.parse(stateV.val);
296
+ val.id = this.extractBindId(state._id);
297
+ binding.push(val);
298
+ }
299
+ resolve();
300
+ });
301
+ }));
302
+ }
303
+ });
304
+ return Promise.all(chain).then(() => {
305
+ this.debug(`getBinding result: ${JSON.stringify(binding)}`);
306
+ callback(binding);
307
+ });
308
+ } else {
309
+ this.debug(`getBinding result: ${JSON.stringify(binding)}`);
310
+ callback(binding);
311
+ }
312
+ });
313
+ } catch (error) {
314
+ this.error(`Failed to getBinding ${error.stack}`);
315
+ }
316
+ }
317
+ }
318
+
319
+ module.exports = Binding;