iobroker.zigbee 1.5.6 → 1.6.8

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 (64) hide show
  1. package/.devcontainer/devcontainer.json +35 -35
  2. package/.devcontainer/docker-compose.yml +51 -51
  3. package/.devcontainer/iobroker/Dockerfile +1 -1
  4. package/.devcontainer/nginx/nginx.conf +32 -32
  5. package/.devcontainer/parcel/Dockerfile +8 -8
  6. package/.devcontainer/parcel/run.sh +6 -6
  7. package/.eslintignore +1 -1
  8. package/.eslintrc.json +36 -36
  9. package/.github/FUNDING.yml +3 -3
  10. package/.github/stale.yml +13 -13
  11. package/.github/workflows/test-and-release.yml +151 -151
  12. package/.travis/wiki.sh +27 -27
  13. package/LICENSE +21 -21
  14. package/README.md +385 -353
  15. package/admin/adapter-settings.js +244 -208
  16. package/admin/admin.js +2704 -2714
  17. package/admin/img/R7060.png +0 -0
  18. package/admin/img/WHD02.png +0 -0
  19. package/admin/img/ikea_E1812.png +0 -0
  20. package/admin/img/philips_hue_lom001.png +0 -0
  21. package/admin/img/tuya_rb280.png +0 -0
  22. package/admin/index.html +159 -159
  23. package/admin/index_m.html +1055 -965
  24. package/admin/moment.min.js +1 -1
  25. package/admin/shuffle.min.js +2 -2
  26. package/admin/tab_m.html +1025 -934
  27. package/admin/vis-network.min.js +26 -26
  28. package/admin/words.js +106 -106
  29. package/docs/de/readme.md +27 -27
  30. package/docs/en/readme.md +30 -30
  31. package/docs/flashing_via_arduino_(en).md +110 -110
  32. package/docs/ru/readme.md +28 -28
  33. package/docs/tutorial/groups-1.png +0 -0
  34. package/docs/tutorial/groups-2.png +0 -0
  35. package/docs/tutorial/tab-dev-1.png +0 -0
  36. package/docs/tutorial/zigbee.png +0 -0
  37. package/io-package.json +317 -290
  38. package/lib/backup.js +132 -132
  39. package/lib/binding.js +325 -325
  40. package/lib/colors.js +460 -460
  41. package/lib/commands.js +435 -434
  42. package/lib/developer.js +148 -144
  43. package/lib/devices.js +3119 -3109
  44. package/lib/exclude.js +168 -168
  45. package/lib/exposes.js +204 -51
  46. package/lib/groups.js +316 -316
  47. package/lib/json.js +60 -60
  48. package/lib/networkmap.js +56 -56
  49. package/lib/ota.js +153 -153
  50. package/lib/rgb.js +225 -225
  51. package/lib/seriallist.js +37 -37
  52. package/lib/states.js +6381 -6322
  53. package/lib/statescontroller.js +502 -495
  54. package/lib/tools.js +54 -54
  55. package/lib/utils.js +151 -132
  56. package/lib/zbBaseExtension.js +31 -27
  57. package/lib/zbDelayedAction.js +151 -146
  58. package/lib/zbDeviceAvailability.js +306 -304
  59. package/lib/zbDeviceConfigure.js +148 -143
  60. package/lib/zbDeviceEvent.js +43 -43
  61. package/lib/zigbeecontroller.js +856 -822
  62. package/main.js +113 -39
  63. package/package.json +74 -73
  64. 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;