iobroker.zigbee 1.8.10 → 1.8.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +411 -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 +9 -17
  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 +873 -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 +693 -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 +1 -1
  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/json.js CHANGED
@@ -1,60 +1,60 @@
1
- 'use strict';
2
-
3
- const hasProp = Object.prototype.hasOwnProperty;
4
-
5
- function throwsMessage(err) {
6
- return `[Throws: ${err ? err.message : '?'}]`;
7
- }
8
-
9
- function safeGetValueFromPropertyOnObject(obj, property) {
10
- if (hasProp.call(obj, property)) {
11
- try {
12
- return obj[property];
13
- } catch (err) {
14
- return throwsMessage(err);
15
- }
16
- }
17
-
18
- return obj[property];
19
- }
20
-
21
- function ensureProperties(obj) {
22
- const seen = []; // store references to objects we have seen before
23
-
24
- function visit(obj) {
25
- if (obj === null || typeof obj !== 'object') {
26
- return obj;
27
- }
28
-
29
- if (seen.includes(obj)) {
30
- return '[Circular]';
31
- }
32
- seen.push(obj);
33
-
34
- if (typeof obj.toJSON === 'function') {
35
- try {
36
- return visit(obj.toJSON());
37
- } catch (err) {
38
- return throwsMessage(err);
39
- }
40
- }
41
-
42
- if (Array.isArray(obj)) {
43
- return obj.map(visit);
44
- }
45
-
46
- return Object.keys(obj).reduce((result, prop) => {
47
- // prevent faulty defined getter properties
48
- result[prop] = visit(safeGetValueFromPropertyOnObject(obj, prop));
49
- return result;
50
- }, {});
51
- }
52
-
53
- return visit(obj);
54
- }
55
-
56
- module.exports = function (data, replacer, space) {
57
- return JSON.stringify(ensureProperties(data), replacer, space);
58
- };
59
-
1
+ 'use strict';
2
+
3
+ const hasProp = Object.prototype.hasOwnProperty;
4
+
5
+ function throwsMessage(err) {
6
+ return `[Throws: ${err ? err.message : '?'}]`;
7
+ }
8
+
9
+ function safeGetValueFromPropertyOnObject(obj, property) {
10
+ if (hasProp.call(obj, property)) {
11
+ try {
12
+ return obj[property];
13
+ } catch (err) {
14
+ return throwsMessage(err);
15
+ }
16
+ }
17
+
18
+ return obj[property];
19
+ }
20
+
21
+ function ensureProperties(obj) {
22
+ const seen = []; // store references to objects we have seen before
23
+
24
+ function visit(obj) {
25
+ if (obj === null || typeof obj !== 'object') {
26
+ return obj;
27
+ }
28
+
29
+ if (seen.includes(obj)) {
30
+ return '[Circular]';
31
+ }
32
+ seen.push(obj);
33
+
34
+ if (typeof obj.toJSON === 'function') {
35
+ try {
36
+ return visit(obj.toJSON());
37
+ } catch (err) {
38
+ return throwsMessage(err);
39
+ }
40
+ }
41
+
42
+ if (Array.isArray(obj)) {
43
+ return obj.map(visit);
44
+ }
45
+
46
+ return Object.keys(obj).reduce((result, prop) => {
47
+ // prevent faulty defined getter properties
48
+ result[prop] = visit(safeGetValueFromPropertyOnObject(obj, prop));
49
+ return result;
50
+ }, {});
51
+ }
52
+
53
+ return visit(obj);
54
+ }
55
+
56
+ module.exports = function (data, replacer, space) {
57
+ return JSON.stringify(ensureProperties(data), replacer, space);
58
+ };
59
+
60
60
  module.exports.ensureProperties = ensureProperties;
package/lib/networkmap.js CHANGED
@@ -1,56 +1,56 @@
1
- 'use strict';
2
-
3
- class NetworkMap {
4
- constructor(adapter) {
5
- this.adapter = adapter;
6
- this.adapter.on('message', this.onMessage.bind(this));
7
- }
8
-
9
- start(zbController, stController) {
10
- this.zbController = zbController;
11
- this.stController = stController;
12
- }
13
-
14
- stop() {
15
- delete this.zbController;
16
- delete this.stController;
17
- }
18
-
19
- info(msg) {
20
- this.adapter.log.info(msg);
21
- }
22
-
23
- error(msg) {
24
- this.adapter.log.error(msg);
25
- }
26
-
27
- debug(msg) {
28
- this.adapter.log.debug(msg);
29
- }
30
-
31
- /**
32
- * @param {ioBroker.Message} obj
33
- */
34
- onMessage(obj) {
35
- if (typeof obj === 'object' && obj.command) {
36
- switch (obj.command) {
37
- case 'getMap':
38
- if (obj && obj.message && typeof obj.message === 'object') {
39
- this.getMap(obj.from, obj.command, obj.callback);
40
- }
41
- break;
42
- }
43
- }
44
- }
45
-
46
- getMap(from, command, callback) {
47
- if (this.zbController) {
48
- this.zbController.getMap(networkmap => {
49
- this.adapter.log.debug(`getMap result: ${JSON.stringify(networkmap)}`);
50
- this.adapter.sendTo(from, command, networkmap, callback);
51
- });
52
- }
53
- }
54
- }
55
-
1
+ 'use strict';
2
+
3
+ class NetworkMap {
4
+ constructor(adapter) {
5
+ this.adapter = adapter;
6
+ this.adapter.on('message', this.onMessage.bind(this));
7
+ }
8
+
9
+ start(zbController, stController) {
10
+ this.zbController = zbController;
11
+ this.stController = stController;
12
+ }
13
+
14
+ stop() {
15
+ delete this.zbController;
16
+ delete this.stController;
17
+ }
18
+
19
+ info(msg) {
20
+ this.adapter.log.info(msg);
21
+ }
22
+
23
+ error(msg) {
24
+ this.adapter.log.error(msg);
25
+ }
26
+
27
+ debug(msg) {
28
+ this.adapter.log.debug(msg);
29
+ }
30
+
31
+ /**
32
+ * @param {ioBroker.Message} obj
33
+ */
34
+ onMessage(obj) {
35
+ if (typeof obj === 'object' && obj.command) {
36
+ switch (obj.command) {
37
+ case 'getMap':
38
+ if (obj && obj.message && typeof obj.message === 'object') {
39
+ this.getMap(obj.from, obj.command, obj.callback);
40
+ }
41
+ break;
42
+ }
43
+ }
44
+ }
45
+
46
+ getMap(from, command, callback) {
47
+ if (this.zbController) {
48
+ this.zbController.getMap(networkmap => {
49
+ this.adapter.log.debug(`getMap result: ${JSON.stringify(networkmap)}`);
50
+ this.adapter.sendTo(from, command, networkmap, callback);
51
+ });
52
+ }
53
+ }
54
+ }
55
+
56
56
  module.exports = NetworkMap;
package/lib/ota.js CHANGED
@@ -1,195 +1,198 @@
1
- 'use strict';
2
-
3
- const getZbId = require('./utils').getZbId;
4
-
5
- class Ota {
6
- constructor(adapter) {
7
- this.adapter = adapter;
8
- this.adapter.on('message', this.onMessage.bind(this));
9
- this.inProgress = new Set();
10
- }
11
-
12
- start(zbController, stController) {
13
- this.zbController = zbController;
14
- this.stController = stController;
15
- }
16
-
17
- stop() {
18
- delete this.zbController;
19
- delete this.stController;
20
- }
21
-
22
- info(msg) {
23
- this.adapter.log.info(msg);
24
- }
25
-
26
- warn(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
- /**
39
- * @param {ioBroker.Message} obj
40
- */
41
- onMessage(obj) {
42
- if (typeof obj === 'object' && obj.command) {
43
- switch (obj.command) {
44
- case 'checkOtaAvail':
45
- this.checkOtaAvail(obj);
46
- break;
47
- case 'startOta':
48
- this.startOta(obj);
49
- break;
50
- }
51
- }
52
- }
53
-
54
- async checkOtaAvail(obj) {
55
- const device = await this.getDeviceForMessage(obj);
56
- if (!device) {
57
- this.debug(`Device ${obj.message.devId} is unavailable`);
58
- this.adapter.sendTo(obj.from, obj.command, {
59
- status: 'fail',
60
- device: getZbId(obj.message.devId),
61
- msg: 'Device is unavailable'
62
- }, obj.callback);
63
- return;
64
- }
65
- if (this.inProgress.has(device.device.ieeeAddr)) {
66
- this.warn(`Update or check already in progress for '${device.name}', skipping...`);
67
- return;
68
- }
69
- // do not attempt update for a device which has been deactivated or is unavailable
70
- const stateObj = await this.adapter.getObjectAsync(obj.message.devId);
71
- if (stateObj && stateObj.common && stateObj.common.deactivated) {
72
- this.warn(`Device ${obj.message.devId} is deactivated, skipping...`);
73
- this.adapter.sendTo(obj.from, obj.command, {
74
- status: 'fail',
75
- device: getZbId(obj.message.devId),
76
- msg: 'Device is deactivated'
77
- }, obj.callback);
78
- return;
79
- }
80
- const availablestate = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.available`);
81
- const lqi = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.link_quality`);
82
- if ((availablestate && (!availablestate.val)) || (lqi && lqi.val < 1)) {
83
- this.warn(`Device ${obj.message.devId} is marked unavailable, skipping...`);
84
- this.adapter.sendTo(obj.from, obj.command, {
85
- status: 'fail',
86
- device: getZbId(obj.message.devId),
87
- msg: 'Device is marked unavailable'
88
- }, obj.callback);
89
- return;
90
- }
91
- this.inProgress.add(device.device.ieeeAddr);
92
- const result = {status: 'unknown', device: device ? device.name : null};
93
- try {
94
- this.debug(`Checking if firmware update is available for ${device.name}`);
95
-
96
- if (device && device.mapped.ota) {
97
- const available = await device.mapped.ota.isUpdateAvailable(device.device, this);
98
- result.status = available ? 'available' : 'not_available';
99
- } else {
100
- result.status = 'not_supported';
101
- }
102
- this.debug(`Firmware update for ${device.name} is ${result.status}`);
103
- this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
104
- } catch (error) {
105
- const message = `Failed to check if update available for '${device.name}' (${error.message})`;
106
- result.status = 'fail';
107
- result.msg = message;
108
- this.warn(message);
109
- this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
110
- }
111
- this.inProgress.delete(device.device.ieeeAddr);
112
- }
113
-
114
- async startOta(obj) {
115
- const device = await this.getDeviceForMessage(obj);
116
- if (!device) {
117
- this.debug(`Device ${obj.message.devId} is unavailable`);
118
- this.adapter.sendTo(obj.from, obj.command, {
119
- status: 'fail',
120
- device: getZbId(obj.message.devId),
121
- msg: 'Device is unavailable'
122
- }, obj.callback);
123
- return;
124
- }
125
- if (this.inProgress.has(device.device.ieeeAddr)) {
126
- this.error(`Update or check already in progress for '${device.name}', skipping...`);
127
- return;
128
- }
129
- // do not attempt update for a device which has been deactivated or is unavailable
130
- const stateObj = await this.adapter.getObjectAsync(obj.message.devId);
131
- if (stateObj && stateObj.common && stateObj.common.deactivated) {
132
- this.warn(`Device ${obj.message.devId} is deactivated, skipping...`);
133
- return;
134
- }
135
- const availablestate = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.available`);
136
- const lqi = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.link_quality`);
137
- if ((availablestate && (!availablestate.val)) || (lqi && lqi.val < 1)) {
138
- this.warn(`Device ${obj.message.devId} is marked unavailable, skipping...`);
139
- return;
140
- }
141
- this.inProgress.add(device.device.ieeeAddr);
142
- const result = {status: 'unknown', device: device ? device.name : null};
143
- try {
144
- this.info('Start firmware update for ' + device.name);
145
-
146
- const onProgress = (progress, remaining) => {
147
- let message = `Update of '${device.name}' at ${progress}%`;
148
- if (remaining) {
149
- message += `, +- ${Math.round(remaining / 60)} minutes remaining`;
150
- }
151
- this.info(message);
152
- };
153
-
154
- const from_ = await this.readSoftwareBuildIDAndDateCode(device.device, false);
155
- await device.mapped.ota.updateToLatest(device.device, this, onProgress);
156
- const to = await this.readSoftwareBuildIDAndDateCode(device.device, true);
157
- const [fromS, toS] = [JSON.stringify(from_), JSON.stringify(to)];
158
- result.status = 'success';
159
- result.msg = `Finished update of '${device.name}'${to ? `, from '${fromS}' to '${toS}'` : ``}`;
160
- this.info(result.msg);
161
- this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
162
- } catch (error) {
163
- const message = `Update of '${device.name}' failed (${error.message})`;
164
- result.status = 'fail';
165
- result.msg = message;
166
- this.error(message);
167
- this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
168
- }
169
- this.inProgress.delete(device.device.ieeeAddr);
170
- }
171
-
172
- async readSoftwareBuildIDAndDateCode(device, update) {
173
- try {
174
- const endpoint = device.endpoints.find((e) => e.supportsInputCluster('genBasic'));
175
- const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId']);
176
-
177
- if (update) {
178
- device.softwareBuildID = result.swBuildId;
179
- device.dateCode = result.dateCode;
180
- device.save();
181
- }
182
-
183
- return {softwareBuildID: result.swBuildId, dateCode: result.dateCode};
184
- } catch (e) {
185
- return null;
186
- }
187
- }
188
-
189
- async getDeviceForMessage(obj) {
190
- const devId = getZbId(obj.message.devId);
191
- return this.zbController.resolveEntity(devId);
192
- }
193
- }
194
-
195
- module.exports = Ota;
1
+ 'use strict';
2
+
3
+ const getZbId = require('./utils').getZbId;
4
+
5
+ class Ota {
6
+ constructor(adapter) {
7
+ this.adapter = adapter;
8
+ this.adapter.on('message', this.onMessage.bind(this));
9
+ this.inProgress = new Set();
10
+ }
11
+
12
+ start(zbController, stController) {
13
+ this.zbController = zbController;
14
+ this.stController = stController;
15
+ }
16
+
17
+ stop() {
18
+ delete this.zbController;
19
+ delete this.stController;
20
+ }
21
+
22
+ info(msg) {
23
+ this.adapter.log.info(msg);
24
+ }
25
+
26
+ warn(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
+ /**
39
+ * @param {ioBroker.Message} obj
40
+ */
41
+ onMessage(obj) {
42
+ if (typeof obj === 'object' && obj.command) {
43
+ switch (obj.command) {
44
+ case 'checkOtaAvail':
45
+ this.checkOtaAvail(obj);
46
+ break;
47
+ case 'startOta':
48
+ this.startOta(obj);
49
+ break;
50
+ }
51
+ }
52
+ }
53
+
54
+ async checkOtaAvail(obj) {
55
+ const device = await this.getDeviceForMessage(obj);
56
+ if (!device) {
57
+ this.debug(`Device ${obj.message.devId} is unavailable`);
58
+ this.adapter.sendTo(obj.from, obj.command, {
59
+ status: 'fail',
60
+ device: getZbId(obj.message.devId),
61
+ msg: 'Device is unavailable'
62
+ }, obj.callback);
63
+ return;
64
+ }
65
+ if (this.inProgress.has(device.device.ieeeAddr)) {
66
+ this.warn(`Update or check already in progress for '${device.name}', skipping...`);
67
+ return;
68
+ }
69
+ // do not attempt update for a device which has been deactivated or is unavailable
70
+ const stateObj = await this.adapter.getObjectAsync(obj.message.devId);
71
+ if (stateObj && stateObj.common && stateObj.common.deactivated) {
72
+ this.warn(`Device ${obj.message.devId} is deactivated, skipping...`);
73
+ this.adapter.sendTo(obj.from, obj.command, {
74
+ status: 'fail',
75
+ device: getZbId(obj.message.devId),
76
+ msg: 'Device is deactivated'
77
+ }, obj.callback);
78
+ return;
79
+ }
80
+ const availablestate = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.available`);
81
+ const lqi = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.link_quality`);
82
+ if ((availablestate && (!availablestate.val)) || (lqi && lqi.val < 1)) {
83
+ this.warn(`Device ${obj.message.devId} is marked unavailable, skipping...`);
84
+ this.adapter.sendTo(obj.from, obj.command, {
85
+ status: 'fail',
86
+ device: getZbId(obj.message.devId),
87
+ msg: 'Device is marked unavailable'
88
+ }, obj.callback);
89
+ return;
90
+ }
91
+ this.inProgress.add(device.device.ieeeAddr);
92
+ const result = {status: 'unknown', device: device ? device.name : null};
93
+ try {
94
+ this.debug(`Checking if firmware update is available for ${device.name}`);
95
+
96
+ if (device && device.mapped.ota) {
97
+ const available = await device.mapped.ota.isUpdateAvailable(device.device, this);
98
+ result.status = available.available ? 'available' : 'not_available';
99
+ if (available.currentFileVersion !== available.otaFileVersion) {
100
+ this.warning(`current Firmware for ${device.name} is ${available.currentFileVersion} new is ${available.otaFileVersion}`);
101
+ }
102
+ } else {
103
+ result.status = 'not_supported';
104
+ }
105
+ this.debug(`Firmware update for ${device.name} is ${result.status}`);
106
+ this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
107
+ } catch (error) {
108
+ const message = `Failed to check if update available for '${device.name}' (${error.message})`;
109
+ result.status = 'fail';
110
+ result.msg = message;
111
+ this.warn(message);
112
+ this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
113
+ }
114
+ this.inProgress.delete(device.device.ieeeAddr);
115
+ }
116
+
117
+ async startOta(obj) {
118
+ const device = await this.getDeviceForMessage(obj);
119
+ if (!device) {
120
+ this.debug(`Device ${obj.message.devId} is unavailable`);
121
+ this.adapter.sendTo(obj.from, obj.command, {
122
+ status: 'fail',
123
+ device: getZbId(obj.message.devId),
124
+ msg: 'Device is unavailable'
125
+ }, obj.callback);
126
+ return;
127
+ }
128
+ if (this.inProgress.has(device.device.ieeeAddr)) {
129
+ this.error(`Update or check already in progress for '${device.name}', skipping...`);
130
+ return;
131
+ }
132
+ // do not attempt update for a device which has been deactivated or is unavailable
133
+ const stateObj = await this.adapter.getObjectAsync(obj.message.devId);
134
+ if (stateObj && stateObj.common && stateObj.common.deactivated) {
135
+ this.warn(`Device ${obj.message.devId} is deactivated, skipping...`);
136
+ return;
137
+ }
138
+ const availablestate = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.available`);
139
+ const lqi = await this.adapter.getStateAsync(`${obj.message.devId.replace(this.namespace + '.', '')}.link_quality`);
140
+ if ((availablestate && (!availablestate.val)) || (lqi && lqi.val < 1)) {
141
+ this.warn(`Device ${obj.message.devId} is marked unavailable, skipping...`);
142
+ return;
143
+ }
144
+ this.inProgress.add(device.device.ieeeAddr);
145
+ const result = {status: 'unknown', device: device ? device.name : null};
146
+ try {
147
+ this.info('Start firmware update for ' + device.name);
148
+
149
+ const onProgress = (progress, remaining) => {
150
+ let message = `Update of '${device.name}' at ${progress}%`;
151
+ if (remaining) {
152
+ message += `, +- ${Math.round(remaining / 60)} minutes remaining`;
153
+ }
154
+ this.info(message);
155
+ };
156
+
157
+ const from_ = await this.readSoftwareBuildIDAndDateCode(device.device, false);
158
+ await device.mapped.ota.updateToLatest(device.device, this, onProgress);
159
+ const to = await this.readSoftwareBuildIDAndDateCode(device.device, true);
160
+ const [fromS, toS] = [JSON.stringify(from_), JSON.stringify(to)];
161
+ result.status = 'success';
162
+ result.msg = `Finished update of '${device.name}'${to ? `, from '${fromS}' to '${toS}'` : ``}`;
163
+ this.info(result.msg);
164
+ this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
165
+ } catch (error) {
166
+ const message = `Update of '${device.name}' failed (${error.message})`;
167
+ result.status = 'fail';
168
+ result.msg = message;
169
+ this.error(message);
170
+ this.adapter.sendTo(obj.from, obj.command, result, obj.callback);
171
+ }
172
+ this.inProgress.delete(device.device.ieeeAddr);
173
+ }
174
+
175
+ async readSoftwareBuildIDAndDateCode(device, update) {
176
+ try {
177
+ const endpoint = device.endpoints.find((e) => e.supportsInputCluster('genBasic'));
178
+ const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId']);
179
+
180
+ if (update) {
181
+ device.softwareBuildID = result.swBuildId;
182
+ device.dateCode = result.dateCode;
183
+ device.save();
184
+ }
185
+
186
+ return {softwareBuildID: result.swBuildId, dateCode: result.dateCode};
187
+ } catch (e) {
188
+ return null;
189
+ }
190
+ }
191
+
192
+ async getDeviceForMessage(obj) {
193
+ const devId = getZbId(obj.message.devId);
194
+ return this.zbController.resolveEntity(devId);
195
+ }
196
+ }
197
+
198
+ module.exports = Ota;