iobroker.zigbee 1.6.16 → 1.6.17

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 (53) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.json +36 -36
  3. package/.github/FUNDING.yml +3 -3
  4. package/.github/stale.yml +13 -13
  5. package/.github/workflows/test-and-release.yml +151 -151
  6. package/.travis/wiki.sh +27 -27
  7. package/LICENSE +21 -21
  8. package/README.md +425 -424
  9. package/admin/adapter-settings.js +244 -244
  10. package/admin/admin.js +2926 -2926
  11. package/admin/img/philips_hue_lom001.png +0 -0
  12. package/admin/index.html +159 -159
  13. package/admin/index_m.html +1161 -1158
  14. package/admin/moment.min.js +1 -1
  15. package/admin/shuffle.min.js +2 -2
  16. package/admin/tab_m.html +944 -944
  17. package/admin/vis-network.min.js +26 -26
  18. package/admin/words.js +109 -108
  19. package/docs/de/readme.md +27 -27
  20. package/docs/en/readme.md +30 -30
  21. package/docs/flashing_via_arduino_(en).md +110 -110
  22. package/docs/ru/readme.md +28 -28
  23. package/docs/tutorial/groups-1.png +0 -0
  24. package/docs/tutorial/groups-2.png +0 -0
  25. package/docs/tutorial/tab-dev-1.png +0 -0
  26. package/io-package.json +367 -355
  27. package/lib/backup.js +132 -132
  28. package/lib/binding.js +325 -325
  29. package/lib/colors.js +460 -460
  30. package/lib/commands.js +501 -501
  31. package/lib/developer.js +148 -148
  32. package/lib/devices.js +3145 -3144
  33. package/lib/exclude.js +168 -168
  34. package/lib/exposes.js +803 -795
  35. package/lib/groups.js +342 -342
  36. package/lib/json.js +60 -60
  37. package/lib/networkmap.js +56 -56
  38. package/lib/ota.js +179 -179
  39. package/lib/rgb.js +255 -255
  40. package/lib/seriallist.js +37 -37
  41. package/lib/states.js +6416 -6407
  42. package/lib/statescontroller.js +627 -627
  43. package/lib/tools.js +54 -54
  44. package/lib/utils.js +151 -151
  45. package/lib/zbBaseExtension.js +36 -32
  46. package/lib/zbDelayedAction.js +152 -152
  47. package/lib/zbDeviceAvailability.js +321 -318
  48. package/lib/zbDeviceConfigure.js +152 -152
  49. package/lib/zbDeviceEvent.js +49 -49
  50. package/lib/zigbeecontroller.js +946 -946
  51. package/main.js +42 -3
  52. package/package.json +77 -77
  53. package/support/docgen.js +93 -93
package/lib/binding.js CHANGED
@@ -1,325 +1,325 @@
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
+ 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;