iobroker.zigbee 1.8.22 → 1.8.23
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.
- package/LICENSE +21 -21
- package/README.md +417 -414
- package/admin/adapter-settings.js +244 -244
- package/admin/admin.js +2981 -2981
- package/admin/i18n/de/translations.json +108 -108
- package/admin/i18n/en/translations.json +108 -108
- package/admin/i18n/es/translations.json +102 -102
- package/admin/i18n/fr/translations.json +108 -108
- package/admin/i18n/it/translations.json +102 -102
- package/admin/i18n/nl/translations.json +108 -108
- package/admin/i18n/pl/translations.json +108 -108
- package/admin/i18n/pt/translations.json +102 -102
- package/admin/i18n/ru/translations.json +108 -108
- package/admin/i18n/uk/translations.json +108 -108
- package/admin/i18n/zh-cn/translations.json +102 -102
- package/admin/img/philips_hue_lom001.png +0 -0
- package/admin/index.html +159 -159
- package/admin/index_m.html +1356 -1356
- package/admin/moment.min.js +1 -1
- package/admin/shuffle.min.js +2 -2
- package/admin/tab_m.html +1009 -1009
- package/admin/vis-network.min.css +1 -1
- package/admin/vis-network.min.js +27 -27
- package/admin/words.js +110 -110
- package/docs/de/basedocu.md +19 -19
- package/docs/de/readme.md +126 -126
- package/docs/en/readme.md +128 -128
- package/docs/flashing_via_arduino_(en).md +110 -110
- package/docs/ru/readme.md +28 -28
- package/docs/tutorial/groups-1.png +0 -0
- package/docs/tutorial/groups-2.png +0 -0
- package/docs/tutorial/tab-dev-1.png +0 -0
- package/io-package.json +14 -14
- package/lib/backup.js +171 -171
- package/lib/binding.js +319 -319
- package/lib/colors.js +465 -465
- package/lib/commands.js +534 -534
- package/lib/developer.js +145 -145
- package/lib/devices.js +3135 -3135
- package/lib/exclude.js +162 -162
- package/lib/exposes.js +913 -913
- package/lib/groups.js +345 -345
- package/lib/json.js +59 -59
- package/lib/networkmap.js +55 -55
- package/lib/ota.js +198 -198
- package/lib/rgb.js +297 -297
- package/lib/seriallist.js +48 -48
- package/lib/states.js +6420 -6420
- package/lib/statescontroller.js +672 -672
- package/lib/tools.js +54 -54
- package/lib/utils.js +165 -165
- package/lib/zbBaseExtension.js +36 -36
- package/lib/zbDelayedAction.js +144 -144
- package/lib/zbDeviceAvailability.js +319 -319
- package/lib/zbDeviceConfigure.js +147 -147
- package/lib/zbDeviceEvent.js +48 -48
- package/lib/zigbeecontroller.js +989 -989
- package/main.js +94 -75
- package/package.json +2 -2
- package/support/docgen.js +93 -93
package/lib/statescontroller.js
CHANGED
|
@@ -1,672 +1,672 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EventEmitter = require('events').EventEmitter;
|
|
4
|
-
const statesMapping = require('./devices');
|
|
5
|
-
const getAdId = require('./utils').getAdId;
|
|
6
|
-
const getZbId = require('./utils').getZbId;
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const request = require('request');
|
|
9
|
-
|
|
10
|
-
let savedDeviceNames = {};
|
|
11
|
-
let knownUndefinedDevices = {};
|
|
12
|
-
|
|
13
|
-
class StatesController extends EventEmitter {
|
|
14
|
-
constructor(adapter) {
|
|
15
|
-
super();
|
|
16
|
-
this.adapter = adapter;
|
|
17
|
-
this.adapter.on('stateChange', this.onStateChange.bind(this));
|
|
18
|
-
this.query_device_block = [];
|
|
19
|
-
this.debugDevices = undefined;
|
|
20
|
-
const fn = adapter.expandFileName('dev_names.json');
|
|
21
|
-
this.dev_names_fn = fn.replace('.', '_');
|
|
22
|
-
this.retTimeoutHandle = null;
|
|
23
|
-
fs.readFile(this.dev_names_fn, (err, data) => {
|
|
24
|
-
if (!err) {
|
|
25
|
-
try {
|
|
26
|
-
savedDeviceNames = JSON.parse(data);
|
|
27
|
-
} catch {
|
|
28
|
-
savedDeviceNames = {};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
info(message, data) {
|
|
35
|
-
this.emit('log', 'info', message, data);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
error(message, data) {
|
|
39
|
-
this.emit('log', 'error', message, data);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
debug(message, data) {
|
|
43
|
-
this.emit('log', 'debug', message, data);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
warn(message, data) {
|
|
47
|
-
this.emit('log', 'warn', message, data);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
sendError(error, message) {
|
|
51
|
-
this.adapter.sendError(error, message);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
retainDeviceNames() {
|
|
55
|
-
clearTimeout(this.retTimeoutHandle);
|
|
56
|
-
this.retTimeoutHanlde = setTimeout(() => {
|
|
57
|
-
fs.writeFile(this.dev_names_fn, JSON.stringify(savedDeviceNames, null, 2), err => {
|
|
58
|
-
if (err) {
|
|
59
|
-
this.error(`error saving device names: ${JSON.stringify(err)}`);
|
|
60
|
-
} else {
|
|
61
|
-
this.debug('saved device names');
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}, 5000);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
getDebugDevices() {
|
|
68
|
-
this.debugDevices = [];
|
|
69
|
-
this.adapter.getState(`${this.adapter.namespace}.info.debugmessages`, (err, state) => {
|
|
70
|
-
if (state) {
|
|
71
|
-
if (typeof state.val === 'string' && state.val.length > 2) {
|
|
72
|
-
this.debugDevices = state.val.split(';');
|
|
73
|
-
}
|
|
74
|
-
this.info(`debug devices set to ${JSON.stringify(this.debugDevices)}`);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
this.adapter.setStateAsync(`info.undefinedDevices`, JSON.stringify(knownUndefinedDevices), true);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
onStateChange(id, state) {
|
|
82
|
-
if (!this.adapter.zbController || !this.adapter.zbController.connected()) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (this.debugDevices === undefined) {
|
|
86
|
-
this.getDebugDevices();
|
|
87
|
-
}
|
|
88
|
-
if (state && !state.ack) {
|
|
89
|
-
if (id.endsWith('pairingCountdown') || id.endsWith('pairingMessage') || id.endsWith('connection')) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (id.endsWith('debugmessages')) {
|
|
93
|
-
if (typeof state.val === 'string' && state.val.length > 2) {
|
|
94
|
-
this.debugDevices = state.val.split(';');
|
|
95
|
-
|
|
96
|
-
} else {
|
|
97
|
-
this.debugDevices = [];
|
|
98
|
-
}
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
for (const addressPart of this.debugDevices) {
|
|
102
|
-
if (typeof id === 'string' && id.includes(addressPart)) {
|
|
103
|
-
this.warn(`ELEVATED: User stateChange ${id} ${JSON.stringify(state)}`);
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
this.debug(`User stateChange ${id} ${JSON.stringify(state)}`);
|
|
108
|
-
const devId = getAdId(this.adapter, id); // iobroker device id
|
|
109
|
-
let deviceId = getZbId(id); // zigbee device id
|
|
110
|
-
// const stateKey = id.split('.')[3];
|
|
111
|
-
const arr = /zigbee.[0-9].[^.]+.(\S+)/gm.exec(id);
|
|
112
|
-
if (arr[1] === undefined) {
|
|
113
|
-
this.warn(`unable to extract id from state ${id}`);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const stateKey = arr[1];
|
|
117
|
-
this.adapter.getObject(devId, (err, obj) => {
|
|
118
|
-
if (obj) {
|
|
119
|
-
const model = obj.common.type;
|
|
120
|
-
if (!model) {
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (obj.common.deactivated) {
|
|
124
|
-
this.debug('State Change detected on deactivated Device - ignored');
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (model === 'group') {
|
|
128
|
-
deviceId = parseInt(deviceId.replace('0xgroup_', ''));
|
|
129
|
-
}
|
|
130
|
-
this.collectOptions(id.split('.')[2], model, options =>
|
|
131
|
-
this.publishFromState(deviceId, model, stateKey, state, options));
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async collectOptions(devId, model, callback) {
|
|
138
|
-
const result = {};
|
|
139
|
-
// find model states for options and get it values
|
|
140
|
-
const devStates = await this.getDevStates('0x' + devId, model);
|
|
141
|
-
if (devStates == null || devStates == undefined) {
|
|
142
|
-
callback(result);
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const states = devStates.states.filter(statedesc => statedesc.isOption || statedesc.inOptions);
|
|
147
|
-
if (states == null || states == undefined) {
|
|
148
|
-
callback(result);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
let cnt = 0;
|
|
152
|
-
try {
|
|
153
|
-
const len = states.length;
|
|
154
|
-
states.forEach(statedesc => {
|
|
155
|
-
const id = `${this.adapter.namespace}.${devId}.${statedesc.id}`;
|
|
156
|
-
this.adapter.getState(id, (err, state) => {
|
|
157
|
-
cnt = cnt + 1;
|
|
158
|
-
if (!err && state) {
|
|
159
|
-
result[statedesc.id] = state.val;
|
|
160
|
-
}
|
|
161
|
-
if (cnt === len) {
|
|
162
|
-
callback(result);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
if (!len) {
|
|
167
|
-
callback(result);
|
|
168
|
-
}
|
|
169
|
-
} catch (error) {
|
|
170
|
-
this.sendError(error);
|
|
171
|
-
this.error(`Error collectOptions for ${devId}. Error: ${error.stack}`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async getDevStates(deviceId, model) {
|
|
176
|
-
try {
|
|
177
|
-
let states;
|
|
178
|
-
let stateModel;
|
|
179
|
-
if (model === 'group') {
|
|
180
|
-
states = statesMapping.groupStates;
|
|
181
|
-
} else {
|
|
182
|
-
stateModel = statesMapping.findModel(model);
|
|
183
|
-
if (!stateModel) {
|
|
184
|
-
if (knownUndefinedDevices[deviceId]) {
|
|
185
|
-
knownUndefinedDevices[deviceId]++;
|
|
186
|
-
} else {
|
|
187
|
-
knownUndefinedDevices[deviceId] = 1;
|
|
188
|
-
this.error(`Device ${deviceId} "${model}" not described in statesMapping.`);
|
|
189
|
-
}
|
|
190
|
-
this.adapter.setStateAsync(`info.undefinedDevices`, JSON.stringify(knownUndefinedDevices), true);
|
|
191
|
-
states = statesMapping.commonStates;
|
|
192
|
-
} else {
|
|
193
|
-
states = stateModel.states;
|
|
194
|
-
}
|
|
195
|
-
if (typeof states === 'function' && !states.prototype) {
|
|
196
|
-
const entity = await this.adapter.zbController.resolveEntity(deviceId);
|
|
197
|
-
if (entity) {
|
|
198
|
-
states = states(entity);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return {states, stateModel};
|
|
203
|
-
} catch (error) {
|
|
204
|
-
this.sendError(error);
|
|
205
|
-
this.error(`Error getDevStates for ${deviceId}. Error: ${error.stack}`);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async publishFromState(deviceId, model, stateKey, state, options) {
|
|
210
|
-
if (this.debugDevices === undefined) this.getDebugDevices();
|
|
211
|
-
this.debug(`Change state '${stateKey}' at device ${deviceId} type '${model}'`);
|
|
212
|
-
for (const addressPart of this.debugDevices) {
|
|
213
|
-
if (typeof deviceId === 'string' && deviceId.includes(addressPart)) {
|
|
214
|
-
this.warn(`ELEVATED Change state '${stateKey}' at device ${deviceId} type '${model}'`);
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
const devStates = await this.getDevStates(deviceId, model);
|
|
219
|
-
if (!devStates) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const commonStates = statesMapping.commonStates.find(statedesc => stateKey === statedesc.id);
|
|
223
|
-
const stateDesc = (commonStates === undefined ? devStates.states.find(statedesc => stateKey === statedesc.id) : commonStates);
|
|
224
|
-
const stateModel = devStates.stateModel;
|
|
225
|
-
if (!stateDesc) {
|
|
226
|
-
this.error(`No state available for '${model}' with key '${stateKey}'`);
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const value = state.val;
|
|
231
|
-
if (value === undefined || value === '') {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
let stateList = [{stateDesc: stateDesc, value: value, index: 0, timeout: 0}];
|
|
235
|
-
if (stateModel && stateModel.linkedStates) {
|
|
236
|
-
stateModel.linkedStates.forEach(linkedFunct => {
|
|
237
|
-
try {
|
|
238
|
-
if (typeof linkedFunct === 'function') {
|
|
239
|
-
const res = linkedFunct(stateDesc, value, options, this.adapter.config.disableQueue);
|
|
240
|
-
if (res) {
|
|
241
|
-
stateList = stateList.concat(res);
|
|
242
|
-
}
|
|
243
|
-
} else {
|
|
244
|
-
this.warn(`publish from State - LinkedState is not a function ${JSON.stringify(linkedFunct)}`);
|
|
245
|
-
}
|
|
246
|
-
} catch (e) {
|
|
247
|
-
this.sendError(e);
|
|
248
|
-
this.error('Exception caught in publishfromstate');
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
});
|
|
252
|
-
// sort by index
|
|
253
|
-
stateList.sort((a, b) => a.index - b.index);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// holds the states for read after write requests
|
|
257
|
-
let readAfterWriteStates = [];
|
|
258
|
-
if (stateModel && stateModel.readAfterWriteStates) {
|
|
259
|
-
stateModel.readAfterWriteStates.forEach((readAfterWriteStateDesc) =>
|
|
260
|
-
readAfterWriteStates = readAfterWriteStates.concat(readAfterWriteStateDesc.id));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
this.emit('changed', deviceId, model, stateModel, stateList, options);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
renameDevice(id, newName) {
|
|
267
|
-
this.storeDeviceName(id, newName);
|
|
268
|
-
this.adapter.extendObject(id, {common: {name: newName}});
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
setDeviceActivated(id, active) {
|
|
272
|
-
this.adapter.extendObject(id, {common: {deactivated: active}});
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
storeDeviceName(id, name) {
|
|
276
|
-
savedDeviceNames[id.replace(`${this.adapter.namespace}.`, '')] = name;
|
|
277
|
-
this.retainDeviceNames();
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
verifyDeviceName(id, name) {
|
|
281
|
-
const savedId = id.replace(`${this.adapter.namespace}.`, '');
|
|
282
|
-
if (!savedDeviceNames.hasOwnProperty(savedId)) {
|
|
283
|
-
savedDeviceNames[savedId] = name;
|
|
284
|
-
this.retainDeviceNames();
|
|
285
|
-
}
|
|
286
|
-
return savedDeviceNames[savedId];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
deleteDeviceStates(devId, callback) {
|
|
290
|
-
this.adapter.getStatesOf(devId, (err, states) => {
|
|
291
|
-
if (!err && states) {
|
|
292
|
-
states.forEach(state =>
|
|
293
|
-
this.adapter.deleteState(devId, null, state._id));
|
|
294
|
-
}
|
|
295
|
-
this.adapter.deleteDevice(devId, () =>
|
|
296
|
-
callback && callback());
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
async deleteDeviceStatesAsync(devId) {
|
|
301
|
-
const states = await this.adapter.getStatesOf(devId);
|
|
302
|
-
if (states) {
|
|
303
|
-
for (const state of states) {
|
|
304
|
-
await this.adapter.deleteState(devId, null, state._id);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
await this.adapter.deleteDevice(devId);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// eslint-disable-next-line no-unused-vars
|
|
311
|
-
async deleteOrphanedDeviceStates(ieeeAddr, model, force, callback) {
|
|
312
|
-
const devStates = await this.getDevStates(ieeeAddr, model);
|
|
313
|
-
const commonStates = statesMapping.commonStates;
|
|
314
|
-
const devId = ieeeAddr.substr(2);
|
|
315
|
-
this.adapter.getStatesOf(devId, (err, states) => {
|
|
316
|
-
if (!err && states) {
|
|
317
|
-
states.forEach((state) => {
|
|
318
|
-
let statename = state._id;
|
|
319
|
-
const arr = /zigbee.[0-9].[^.]+.(\S+)/gm.exec(statename);
|
|
320
|
-
if (arr[1] === undefined) {
|
|
321
|
-
this.warn(`unable to extract id from state ${statename}`);
|
|
322
|
-
const idx = statename.lastIndexOf('.');
|
|
323
|
-
if (idx > -1) {
|
|
324
|
-
statename = statename.slice(idx + 1);
|
|
325
|
-
}
|
|
326
|
-
} else {
|
|
327
|
-
statename = arr[1];
|
|
328
|
-
}
|
|
329
|
-
if (commonStates.find(statedesc => statename === statedesc.id) === undefined &&
|
|
330
|
-
devStates.states.find(statedesc => statename === statedesc.id) === undefined
|
|
331
|
-
) {
|
|
332
|
-
if (state.common.hasOwnProperty('custom') && !force) {
|
|
333
|
-
this.info(`keeping disconnected state ${JSON.stringify(statename)} of ${devId} `);
|
|
334
|
-
} else {
|
|
335
|
-
this.info(`deleting disconnected state ${JSON.stringify(statename)} of ${devId} `);
|
|
336
|
-
this.adapter.deleteState(devId, null, state._id);
|
|
337
|
-
}
|
|
338
|
-
} else {
|
|
339
|
-
this.debug(`keeping connecte state ${JSON.stringify(statename)} of ${devId} `);
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
updateStateWithTimeout(dev_id, name, value, common, timeout, outValue) {
|
|
347
|
-
this.updateState(dev_id, name, value, common);
|
|
348
|
-
setTimeout(() => this.updateState(dev_id, name, outValue, common), timeout);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
updateState(devId, name, value, common) {
|
|
352
|
-
this.adapter.getObject(devId, (err, obj) => {
|
|
353
|
-
if (obj) {
|
|
354
|
-
if (!obj.common.deactivated) {
|
|
355
|
-
const new_common = {name: name};
|
|
356
|
-
const id = devId + '.' + name;
|
|
357
|
-
const new_name = obj.common.name;
|
|
358
|
-
if (common) {
|
|
359
|
-
if (common.name !== undefined) {
|
|
360
|
-
new_common.name = common.name;
|
|
361
|
-
}
|
|
362
|
-
if (common.type !== undefined) {
|
|
363
|
-
new_common.type = common.type;
|
|
364
|
-
}
|
|
365
|
-
if (common.unit !== undefined) {
|
|
366
|
-
new_common.unit = common.unit;
|
|
367
|
-
}
|
|
368
|
-
if (common.states !== undefined) {
|
|
369
|
-
new_common.states = common.states;
|
|
370
|
-
}
|
|
371
|
-
if (common.read !== undefined) {
|
|
372
|
-
new_common.read = common.read;
|
|
373
|
-
}
|
|
374
|
-
if (common.write !== undefined) {
|
|
375
|
-
new_common.write = common.write;
|
|
376
|
-
}
|
|
377
|
-
if (common.role !== undefined) {
|
|
378
|
-
new_common.role = common.role;
|
|
379
|
-
}
|
|
380
|
-
if (common.min !== undefined) {
|
|
381
|
-
new_common.min = common.min;
|
|
382
|
-
}
|
|
383
|
-
if (common.max !== undefined) {
|
|
384
|
-
new_common.max = common.max;
|
|
385
|
-
}
|
|
386
|
-
if (common.icon !== undefined) {
|
|
387
|
-
new_common.icon = common.icon;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
// check if state exist
|
|
391
|
-
this.adapter.getObject(id, (err, stobj) => {
|
|
392
|
-
let hasChanges = false;
|
|
393
|
-
if (stobj) {
|
|
394
|
-
// update state - not change name and role (user can it changed)
|
|
395
|
-
if (stobj.common.name) {
|
|
396
|
-
delete new_common.name;
|
|
397
|
-
} else {
|
|
398
|
-
new_common.name = `${new_name} ${new_common.name}`;
|
|
399
|
-
}
|
|
400
|
-
delete new_common.role;
|
|
401
|
-
|
|
402
|
-
// check whether any common property is different
|
|
403
|
-
if (stobj.common) {
|
|
404
|
-
for (const property in new_common) {
|
|
405
|
-
if (stobj.common.hasOwnProperty(property)) {
|
|
406
|
-
if (stobj.common[property] === new_common[property]) {
|
|
407
|
-
delete new_common[property];
|
|
408
|
-
} else {
|
|
409
|
-
hasChanges = true;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
} else {
|
|
415
|
-
hasChanges = true;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// only change object when any common property has changed
|
|
419
|
-
if (hasChanges) {
|
|
420
|
-
this.adapter.extendObject(id, {type: 'state', common: new_common, native: {}}, () =>
|
|
421
|
-
value !== undefined && this.setState_typed(id, value, true, stobj ? stobj.common.type : new_common.type));
|
|
422
|
-
} else if (value !== undefined) {
|
|
423
|
-
this.setState_typed(id, value, true, stobj.common.type);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
});
|
|
427
|
-
} else {
|
|
428
|
-
this.debug(`UpdateState: Device is deactivated ${devId} ${JSON.stringify(obj)}`);
|
|
429
|
-
}
|
|
430
|
-
} else {
|
|
431
|
-
this.debug(`UpdateState: missing device ${devId} ${JSON.stringify(obj)}`);
|
|
432
|
-
}
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
setState_typed(id, value, ack, type, callback) {
|
|
437
|
-
// never set a null or undefined value
|
|
438
|
-
if (value === null || value === undefined) return;
|
|
439
|
-
if (!type) {
|
|
440
|
-
this.debug('SetState_typed called without type');
|
|
441
|
-
// identify datatype, recursively call this function with set datatype
|
|
442
|
-
this.adapter.getObject(id, (err, obj) => {
|
|
443
|
-
if (obj && obj.common) {
|
|
444
|
-
this.setState_typed(id, value, ack, obj.common.type, callback);
|
|
445
|
-
} else {
|
|
446
|
-
this.setState_typed(id, value, ack, 'noobj', callback);
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
if (typeof value !== type) {
|
|
452
|
-
this.debug(`SetState_typed : converting ${JSON.stringify(value)} for ${id} from ${typeof value} to ${type}`);
|
|
453
|
-
switch (type) {
|
|
454
|
-
case 'number':
|
|
455
|
-
value = parseFloat(value);
|
|
456
|
-
if (isNaN(value)) {
|
|
457
|
-
value = 0;
|
|
458
|
-
}
|
|
459
|
-
break;
|
|
460
|
-
case 'string':
|
|
461
|
-
case 'text':
|
|
462
|
-
value = JSON.stringify(value);
|
|
463
|
-
break;
|
|
464
|
-
case 'boolean': {
|
|
465
|
-
if (typeof value == 'number') {
|
|
466
|
-
value = value !== 0;
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
const sval = JSON.stringify(value).toLowerCase().trim();
|
|
470
|
-
value = sval === 'true' || sval === 'yes' || sval === 'on';
|
|
471
|
-
}
|
|
472
|
-
break;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
this.adapter.setState(id, value, ack, callback);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
updateDev(dev_id, dev_name, model, callback) {
|
|
479
|
-
const __dev_name = this.verifyDeviceName(dev_id, dev_name);
|
|
480
|
-
const id = '' + dev_id;
|
|
481
|
-
const modelDesc = statesMapping.findModel(model);
|
|
482
|
-
let icon = (modelDesc && modelDesc.icon) ? modelDesc.icon : 'img/unknown.png';
|
|
483
|
-
|
|
484
|
-
// download icon if it external and not undef
|
|
485
|
-
if (model === undefined) {
|
|
486
|
-
this.warn(`download icon ${__dev_name} for undefined Device not available. Check your devices.`);
|
|
487
|
-
} else {
|
|
488
|
-
const model_modif = model.replace(/\//g, '-');
|
|
489
|
-
const pathToIcon = `${this.adapter.adapterDir}/admin/img/${model_modif}.png`;
|
|
490
|
-
|
|
491
|
-
if (icon.startsWith('http')) {
|
|
492
|
-
try {
|
|
493
|
-
if (!fs.existsSync(pathToIcon)) {
|
|
494
|
-
this.warn(`download icon from ${icon} saved into ${pathToIcon}`);
|
|
495
|
-
this.downloadIcon(icon, pathToIcon);
|
|
496
|
-
}
|
|
497
|
-
icon = `img/${model_modif}.png`;
|
|
498
|
-
} catch (e) {
|
|
499
|
-
this.debug(`ERROR : icon not found from ${icon} saved into ${pathToIcon}`);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
this.adapter.setObjectNotExists(id, {
|
|
505
|
-
type: 'device',
|
|
506
|
-
// actually this is an error, so device.common has no attribute type. It must be in native part
|
|
507
|
-
common: {
|
|
508
|
-
name: __dev_name,
|
|
509
|
-
type: model,
|
|
510
|
-
icon,
|
|
511
|
-
color: null,
|
|
512
|
-
statusStates: {onlineId: `${this.adapter.namespace}.${dev_id}.available`}
|
|
513
|
-
},
|
|
514
|
-
native: {id: dev_id}
|
|
515
|
-
}, () => {
|
|
516
|
-
// update type and icon
|
|
517
|
-
this.adapter.extendObject(id, {
|
|
518
|
-
common: {
|
|
519
|
-
type: model,
|
|
520
|
-
icon,
|
|
521
|
-
color: null,
|
|
522
|
-
statusStates: {onlineId: `${this.adapter.namespace}.${dev_id}.available`}
|
|
523
|
-
}
|
|
524
|
-
}, callback);
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
async downloadIcon(url, image_path) {
|
|
529
|
-
if (!fs.existsSync(image_path)) {
|
|
530
|
-
return new Promise((resolve, reject) => {
|
|
531
|
-
request.head(url, (err, res, body) => {
|
|
532
|
-
if (err) {
|
|
533
|
-
return reject(err + ' ' + res + ' ' + body);
|
|
534
|
-
}
|
|
535
|
-
const stream = request(url);
|
|
536
|
-
stream.pipe(
|
|
537
|
-
fs.createWriteStream(image_path)
|
|
538
|
-
.on('error', err => reject(err)))
|
|
539
|
-
.on('close', () => resolve());
|
|
540
|
-
});
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
async syncDevStates(dev, model) {
|
|
546
|
-
const devId = dev.ieeeAddr.substr(2);
|
|
547
|
-
// devId - iobroker device id
|
|
548
|
-
const devStates = await this.getDevStates(dev.ieeeAddr, model);
|
|
549
|
-
if (!devStates) {
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
const states = statesMapping.commonStates.concat(devStates.states);
|
|
553
|
-
|
|
554
|
-
for (const stateInd in states) {
|
|
555
|
-
if (!states.hasOwnProperty(stateInd)) {
|
|
556
|
-
continue;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
const statedesc = states[stateInd];
|
|
560
|
-
if (statedesc === undefined) {
|
|
561
|
-
this.error(`syncDevStates: Illegal state in ${JSON.stringify(dev)} - ${JSON.stringify(stateInd)}`);
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
// Filter out non routers or devices that are battery driven for the availability flag
|
|
565
|
-
if (statedesc.id === 'available') {
|
|
566
|
-
if (!(dev.type === 'Router') || dev.powerSource === 'Battery') {
|
|
567
|
-
continue;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
// lazy states
|
|
571
|
-
if (statedesc.lazy) {
|
|
572
|
-
continue;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
const common = {
|
|
576
|
-
name: statedesc.name,
|
|
577
|
-
type: statedesc.type,
|
|
578
|
-
unit: statedesc.unit,
|
|
579
|
-
read: statedesc.read,
|
|
580
|
-
write: statedesc.write,
|
|
581
|
-
icon: statedesc.icon,
|
|
582
|
-
role: statedesc.role,
|
|
583
|
-
min: statedesc.min,
|
|
584
|
-
max: statedesc.max,
|
|
585
|
-
states: statedesc.states,
|
|
586
|
-
};
|
|
587
|
-
this.updateState(devId, statedesc.id, undefined, common);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
async getExcludeExposes(allExcludesObj) {
|
|
592
|
-
statesMapping.fillStatesWithExposes(allExcludesObj);
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
async publishToState(devId, model, payload) {
|
|
596
|
-
const devStates = await this.getDevStates(`0x${devId}`, model);
|
|
597
|
-
let has_debug = false;
|
|
598
|
-
if (this.debugDevices === undefined) this.getDebugDevices();
|
|
599
|
-
for (const addressPart of this.debugDevices) {
|
|
600
|
-
if (typeof devId === 'string' && devId.includes(addressPart)) {
|
|
601
|
-
if (payload.hasOwnProperty('msg_from_zigbee')) break;
|
|
602
|
-
this.warn(`ELEVATED publishToState: message received '${JSON.stringify(payload)}' from device ${devId} type '${model}'`);
|
|
603
|
-
has_debug = true;
|
|
604
|
-
break;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
if (!devStates) {
|
|
608
|
-
return;
|
|
609
|
-
}
|
|
610
|
-
// find states for payload
|
|
611
|
-
if (devStates.states !== undefined) {
|
|
612
|
-
const states = statesMapping.commonStates.concat(
|
|
613
|
-
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id)));
|
|
614
|
-
|
|
615
|
-
for (const stateInd in states) {
|
|
616
|
-
const statedesc = states[stateInd];
|
|
617
|
-
let value;
|
|
618
|
-
if (statedesc.getter) {
|
|
619
|
-
value = statedesc.getter(payload);
|
|
620
|
-
} else {
|
|
621
|
-
value = payload[statedesc.prop || statedesc.id];
|
|
622
|
-
}
|
|
623
|
-
// checking value
|
|
624
|
-
if (value === undefined || value === null) {
|
|
625
|
-
continue;
|
|
626
|
-
}
|
|
627
|
-
let stateID = statedesc.id;
|
|
628
|
-
|
|
629
|
-
if (has_debug && statedesc.id !== 'msg_from_zigbee') {
|
|
630
|
-
this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
const common = {
|
|
634
|
-
name: statedesc.name,
|
|
635
|
-
type: statedesc.type,
|
|
636
|
-
unit: statedesc.unit,
|
|
637
|
-
read: statedesc.read,
|
|
638
|
-
write: statedesc.write,
|
|
639
|
-
icon: statedesc.icon,
|
|
640
|
-
role: statedesc.role,
|
|
641
|
-
min: statedesc.min,
|
|
642
|
-
max: statedesc.max,
|
|
643
|
-
};
|
|
644
|
-
|
|
645
|
-
if (typeof value === 'object' && value.hasOwnProperty('stateid')) {
|
|
646
|
-
stateID = `${stateID}.${value.stateid}`;
|
|
647
|
-
if (value.hasOwnProperty('unit')) {
|
|
648
|
-
common.unit = value.unit;
|
|
649
|
-
}
|
|
650
|
-
common.name = value.name ? value.name : value.stateid;
|
|
651
|
-
common.role = value.role ? `value.${value.role}` : 'number';
|
|
652
|
-
value = value.value;
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
// if needs to return value to back after timeout
|
|
656
|
-
if (statedesc.isEvent) {
|
|
657
|
-
this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value);
|
|
658
|
-
} else {
|
|
659
|
-
if (statedesc.prepublish) {
|
|
660
|
-
this.collectOptions(devId, model, options =>
|
|
661
|
-
statedesc.prepublish(devId, value, newvalue =>
|
|
662
|
-
this.updateState(devId, stateID, newvalue, common), options));
|
|
663
|
-
} else {
|
|
664
|
-
this.updateState(devId, stateID, value, common);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
module.exports = StatesController;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const EventEmitter = require('events').EventEmitter;
|
|
4
|
+
const statesMapping = require('./devices');
|
|
5
|
+
const getAdId = require('./utils').getAdId;
|
|
6
|
+
const getZbId = require('./utils').getZbId;
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const request = require('request');
|
|
9
|
+
|
|
10
|
+
let savedDeviceNames = {};
|
|
11
|
+
let knownUndefinedDevices = {};
|
|
12
|
+
|
|
13
|
+
class StatesController extends EventEmitter {
|
|
14
|
+
constructor(adapter) {
|
|
15
|
+
super();
|
|
16
|
+
this.adapter = adapter;
|
|
17
|
+
this.adapter.on('stateChange', this.onStateChange.bind(this));
|
|
18
|
+
this.query_device_block = [];
|
|
19
|
+
this.debugDevices = undefined;
|
|
20
|
+
const fn = adapter.expandFileName('dev_names.json');
|
|
21
|
+
this.dev_names_fn = fn.replace('.', '_');
|
|
22
|
+
this.retTimeoutHandle = null;
|
|
23
|
+
fs.readFile(this.dev_names_fn, (err, data) => {
|
|
24
|
+
if (!err) {
|
|
25
|
+
try {
|
|
26
|
+
savedDeviceNames = JSON.parse(data);
|
|
27
|
+
} catch {
|
|
28
|
+
savedDeviceNames = {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
info(message, data) {
|
|
35
|
+
this.emit('log', 'info', message, data);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
error(message, data) {
|
|
39
|
+
this.emit('log', 'error', message, data);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
debug(message, data) {
|
|
43
|
+
this.emit('log', 'debug', message, data);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
warn(message, data) {
|
|
47
|
+
this.emit('log', 'warn', message, data);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
sendError(error, message) {
|
|
51
|
+
this.adapter.sendError(error, message);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
retainDeviceNames() {
|
|
55
|
+
clearTimeout(this.retTimeoutHandle);
|
|
56
|
+
this.retTimeoutHanlde = setTimeout(() => {
|
|
57
|
+
fs.writeFile(this.dev_names_fn, JSON.stringify(savedDeviceNames, null, 2), err => {
|
|
58
|
+
if (err) {
|
|
59
|
+
this.error(`error saving device names: ${JSON.stringify(err)}`);
|
|
60
|
+
} else {
|
|
61
|
+
this.debug('saved device names');
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}, 5000);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getDebugDevices() {
|
|
68
|
+
this.debugDevices = [];
|
|
69
|
+
this.adapter.getState(`${this.adapter.namespace}.info.debugmessages`, (err, state) => {
|
|
70
|
+
if (state) {
|
|
71
|
+
if (typeof state.val === 'string' && state.val.length > 2) {
|
|
72
|
+
this.debugDevices = state.val.split(';');
|
|
73
|
+
}
|
|
74
|
+
this.info(`debug devices set to ${JSON.stringify(this.debugDevices)}`);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
this.adapter.setStateAsync(`info.undefinedDevices`, JSON.stringify(knownUndefinedDevices), true);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
onStateChange(id, state) {
|
|
82
|
+
if (!this.adapter.zbController || !this.adapter.zbController.connected()) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (this.debugDevices === undefined) {
|
|
86
|
+
this.getDebugDevices();
|
|
87
|
+
}
|
|
88
|
+
if (state && !state.ack) {
|
|
89
|
+
if (id.endsWith('pairingCountdown') || id.endsWith('pairingMessage') || id.endsWith('connection')) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (id.endsWith('debugmessages')) {
|
|
93
|
+
if (typeof state.val === 'string' && state.val.length > 2) {
|
|
94
|
+
this.debugDevices = state.val.split(';');
|
|
95
|
+
|
|
96
|
+
} else {
|
|
97
|
+
this.debugDevices = [];
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
for (const addressPart of this.debugDevices) {
|
|
102
|
+
if (typeof id === 'string' && id.includes(addressPart)) {
|
|
103
|
+
this.warn(`ELEVATED: User stateChange ${id} ${JSON.stringify(state)}`);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
this.debug(`User stateChange ${id} ${JSON.stringify(state)}`);
|
|
108
|
+
const devId = getAdId(this.adapter, id); // iobroker device id
|
|
109
|
+
let deviceId = getZbId(id); // zigbee device id
|
|
110
|
+
// const stateKey = id.split('.')[3];
|
|
111
|
+
const arr = /zigbee.[0-9].[^.]+.(\S+)/gm.exec(id);
|
|
112
|
+
if (arr[1] === undefined) {
|
|
113
|
+
this.warn(`unable to extract id from state ${id}`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const stateKey = arr[1];
|
|
117
|
+
this.adapter.getObject(devId, (err, obj) => {
|
|
118
|
+
if (obj) {
|
|
119
|
+
const model = obj.common.type;
|
|
120
|
+
if (!model) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (obj.common.deactivated) {
|
|
124
|
+
this.debug('State Change detected on deactivated Device - ignored');
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (model === 'group') {
|
|
128
|
+
deviceId = parseInt(deviceId.replace('0xgroup_', ''));
|
|
129
|
+
}
|
|
130
|
+
this.collectOptions(id.split('.')[2], model, options =>
|
|
131
|
+
this.publishFromState(deviceId, model, stateKey, state, options));
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async collectOptions(devId, model, callback) {
|
|
138
|
+
const result = {};
|
|
139
|
+
// find model states for options and get it values
|
|
140
|
+
const devStates = await this.getDevStates('0x' + devId, model);
|
|
141
|
+
if (devStates == null || devStates == undefined) {
|
|
142
|
+
callback(result);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const states = devStates.states.filter(statedesc => statedesc.isOption || statedesc.inOptions);
|
|
147
|
+
if (states == null || states == undefined) {
|
|
148
|
+
callback(result);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
let cnt = 0;
|
|
152
|
+
try {
|
|
153
|
+
const len = states.length;
|
|
154
|
+
states.forEach(statedesc => {
|
|
155
|
+
const id = `${this.adapter.namespace}.${devId}.${statedesc.id}`;
|
|
156
|
+
this.adapter.getState(id, (err, state) => {
|
|
157
|
+
cnt = cnt + 1;
|
|
158
|
+
if (!err && state) {
|
|
159
|
+
result[statedesc.id] = state.val;
|
|
160
|
+
}
|
|
161
|
+
if (cnt === len) {
|
|
162
|
+
callback(result);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
if (!len) {
|
|
167
|
+
callback(result);
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
this.sendError(error);
|
|
171
|
+
this.error(`Error collectOptions for ${devId}. Error: ${error.stack}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async getDevStates(deviceId, model) {
|
|
176
|
+
try {
|
|
177
|
+
let states;
|
|
178
|
+
let stateModel;
|
|
179
|
+
if (model === 'group') {
|
|
180
|
+
states = statesMapping.groupStates;
|
|
181
|
+
} else {
|
|
182
|
+
stateModel = statesMapping.findModel(model);
|
|
183
|
+
if (!stateModel) {
|
|
184
|
+
if (knownUndefinedDevices[deviceId]) {
|
|
185
|
+
knownUndefinedDevices[deviceId]++;
|
|
186
|
+
} else {
|
|
187
|
+
knownUndefinedDevices[deviceId] = 1;
|
|
188
|
+
this.error(`Device ${deviceId} "${model}" not described in statesMapping.`);
|
|
189
|
+
}
|
|
190
|
+
this.adapter.setStateAsync(`info.undefinedDevices`, JSON.stringify(knownUndefinedDevices), true);
|
|
191
|
+
states = statesMapping.commonStates;
|
|
192
|
+
} else {
|
|
193
|
+
states = stateModel.states;
|
|
194
|
+
}
|
|
195
|
+
if (typeof states === 'function' && !states.prototype) {
|
|
196
|
+
const entity = await this.adapter.zbController.resolveEntity(deviceId);
|
|
197
|
+
if (entity) {
|
|
198
|
+
states = states(entity);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return {states, stateModel};
|
|
203
|
+
} catch (error) {
|
|
204
|
+
this.sendError(error);
|
|
205
|
+
this.error(`Error getDevStates for ${deviceId}. Error: ${error.stack}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async publishFromState(deviceId, model, stateKey, state, options) {
|
|
210
|
+
if (this.debugDevices === undefined) this.getDebugDevices();
|
|
211
|
+
this.debug(`Change state '${stateKey}' at device ${deviceId} type '${model}'`);
|
|
212
|
+
for (const addressPart of this.debugDevices) {
|
|
213
|
+
if (typeof deviceId === 'string' && deviceId.includes(addressPart)) {
|
|
214
|
+
this.warn(`ELEVATED Change state '${stateKey}' at device ${deviceId} type '${model}'`);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const devStates = await this.getDevStates(deviceId, model);
|
|
219
|
+
if (!devStates) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const commonStates = statesMapping.commonStates.find(statedesc => stateKey === statedesc.id);
|
|
223
|
+
const stateDesc = (commonStates === undefined ? devStates.states.find(statedesc => stateKey === statedesc.id) : commonStates);
|
|
224
|
+
const stateModel = devStates.stateModel;
|
|
225
|
+
if (!stateDesc) {
|
|
226
|
+
this.error(`No state available for '${model}' with key '${stateKey}'`);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const value = state.val;
|
|
231
|
+
if (value === undefined || value === '') {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
let stateList = [{stateDesc: stateDesc, value: value, index: 0, timeout: 0}];
|
|
235
|
+
if (stateModel && stateModel.linkedStates) {
|
|
236
|
+
stateModel.linkedStates.forEach(linkedFunct => {
|
|
237
|
+
try {
|
|
238
|
+
if (typeof linkedFunct === 'function') {
|
|
239
|
+
const res = linkedFunct(stateDesc, value, options, this.adapter.config.disableQueue);
|
|
240
|
+
if (res) {
|
|
241
|
+
stateList = stateList.concat(res);
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
this.warn(`publish from State - LinkedState is not a function ${JSON.stringify(linkedFunct)}`);
|
|
245
|
+
}
|
|
246
|
+
} catch (e) {
|
|
247
|
+
this.sendError(e);
|
|
248
|
+
this.error('Exception caught in publishfromstate');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
});
|
|
252
|
+
// sort by index
|
|
253
|
+
stateList.sort((a, b) => a.index - b.index);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// holds the states for read after write requests
|
|
257
|
+
let readAfterWriteStates = [];
|
|
258
|
+
if (stateModel && stateModel.readAfterWriteStates) {
|
|
259
|
+
stateModel.readAfterWriteStates.forEach((readAfterWriteStateDesc) =>
|
|
260
|
+
readAfterWriteStates = readAfterWriteStates.concat(readAfterWriteStateDesc.id));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.emit('changed', deviceId, model, stateModel, stateList, options);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
renameDevice(id, newName) {
|
|
267
|
+
this.storeDeviceName(id, newName);
|
|
268
|
+
this.adapter.extendObject(id, {common: {name: newName}});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
setDeviceActivated(id, active) {
|
|
272
|
+
this.adapter.extendObject(id, {common: {deactivated: active}});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
storeDeviceName(id, name) {
|
|
276
|
+
savedDeviceNames[id.replace(`${this.adapter.namespace}.`, '')] = name;
|
|
277
|
+
this.retainDeviceNames();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
verifyDeviceName(id, name) {
|
|
281
|
+
const savedId = id.replace(`${this.adapter.namespace}.`, '');
|
|
282
|
+
if (!savedDeviceNames.hasOwnProperty(savedId)) {
|
|
283
|
+
savedDeviceNames[savedId] = name;
|
|
284
|
+
this.retainDeviceNames();
|
|
285
|
+
}
|
|
286
|
+
return savedDeviceNames[savedId];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
deleteDeviceStates(devId, callback) {
|
|
290
|
+
this.adapter.getStatesOf(devId, (err, states) => {
|
|
291
|
+
if (!err && states) {
|
|
292
|
+
states.forEach(state =>
|
|
293
|
+
this.adapter.deleteState(devId, null, state._id));
|
|
294
|
+
}
|
|
295
|
+
this.adapter.deleteDevice(devId, () =>
|
|
296
|
+
callback && callback());
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async deleteDeviceStatesAsync(devId) {
|
|
301
|
+
const states = await this.adapter.getStatesOf(devId);
|
|
302
|
+
if (states) {
|
|
303
|
+
for (const state of states) {
|
|
304
|
+
await this.adapter.deleteState(devId, null, state._id);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
await this.adapter.deleteDevice(devId);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// eslint-disable-next-line no-unused-vars
|
|
311
|
+
async deleteOrphanedDeviceStates(ieeeAddr, model, force, callback) {
|
|
312
|
+
const devStates = await this.getDevStates(ieeeAddr, model);
|
|
313
|
+
const commonStates = statesMapping.commonStates;
|
|
314
|
+
const devId = ieeeAddr.substr(2);
|
|
315
|
+
this.adapter.getStatesOf(devId, (err, states) => {
|
|
316
|
+
if (!err && states) {
|
|
317
|
+
states.forEach((state) => {
|
|
318
|
+
let statename = state._id;
|
|
319
|
+
const arr = /zigbee.[0-9].[^.]+.(\S+)/gm.exec(statename);
|
|
320
|
+
if (arr[1] === undefined) {
|
|
321
|
+
this.warn(`unable to extract id from state ${statename}`);
|
|
322
|
+
const idx = statename.lastIndexOf('.');
|
|
323
|
+
if (idx > -1) {
|
|
324
|
+
statename = statename.slice(idx + 1);
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
statename = arr[1];
|
|
328
|
+
}
|
|
329
|
+
if (commonStates.find(statedesc => statename === statedesc.id) === undefined &&
|
|
330
|
+
devStates.states.find(statedesc => statename === statedesc.id) === undefined
|
|
331
|
+
) {
|
|
332
|
+
if (state.common.hasOwnProperty('custom') && !force) {
|
|
333
|
+
this.info(`keeping disconnected state ${JSON.stringify(statename)} of ${devId} `);
|
|
334
|
+
} else {
|
|
335
|
+
this.info(`deleting disconnected state ${JSON.stringify(statename)} of ${devId} `);
|
|
336
|
+
this.adapter.deleteState(devId, null, state._id);
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
this.debug(`keeping connecte state ${JSON.stringify(statename)} of ${devId} `);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
updateStateWithTimeout(dev_id, name, value, common, timeout, outValue) {
|
|
347
|
+
this.updateState(dev_id, name, value, common);
|
|
348
|
+
setTimeout(() => this.updateState(dev_id, name, outValue, common), timeout);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
updateState(devId, name, value, common) {
|
|
352
|
+
this.adapter.getObject(devId, (err, obj) => {
|
|
353
|
+
if (obj) {
|
|
354
|
+
if (!obj.common.deactivated) {
|
|
355
|
+
const new_common = {name: name};
|
|
356
|
+
const id = devId + '.' + name;
|
|
357
|
+
const new_name = obj.common.name;
|
|
358
|
+
if (common) {
|
|
359
|
+
if (common.name !== undefined) {
|
|
360
|
+
new_common.name = common.name;
|
|
361
|
+
}
|
|
362
|
+
if (common.type !== undefined) {
|
|
363
|
+
new_common.type = common.type;
|
|
364
|
+
}
|
|
365
|
+
if (common.unit !== undefined) {
|
|
366
|
+
new_common.unit = common.unit;
|
|
367
|
+
}
|
|
368
|
+
if (common.states !== undefined) {
|
|
369
|
+
new_common.states = common.states;
|
|
370
|
+
}
|
|
371
|
+
if (common.read !== undefined) {
|
|
372
|
+
new_common.read = common.read;
|
|
373
|
+
}
|
|
374
|
+
if (common.write !== undefined) {
|
|
375
|
+
new_common.write = common.write;
|
|
376
|
+
}
|
|
377
|
+
if (common.role !== undefined) {
|
|
378
|
+
new_common.role = common.role;
|
|
379
|
+
}
|
|
380
|
+
if (common.min !== undefined) {
|
|
381
|
+
new_common.min = common.min;
|
|
382
|
+
}
|
|
383
|
+
if (common.max !== undefined) {
|
|
384
|
+
new_common.max = common.max;
|
|
385
|
+
}
|
|
386
|
+
if (common.icon !== undefined) {
|
|
387
|
+
new_common.icon = common.icon;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// check if state exist
|
|
391
|
+
this.adapter.getObject(id, (err, stobj) => {
|
|
392
|
+
let hasChanges = false;
|
|
393
|
+
if (stobj) {
|
|
394
|
+
// update state - not change name and role (user can it changed)
|
|
395
|
+
if (stobj.common.name) {
|
|
396
|
+
delete new_common.name;
|
|
397
|
+
} else {
|
|
398
|
+
new_common.name = `${new_name} ${new_common.name}`;
|
|
399
|
+
}
|
|
400
|
+
delete new_common.role;
|
|
401
|
+
|
|
402
|
+
// check whether any common property is different
|
|
403
|
+
if (stobj.common) {
|
|
404
|
+
for (const property in new_common) {
|
|
405
|
+
if (stobj.common.hasOwnProperty(property)) {
|
|
406
|
+
if (stobj.common[property] === new_common[property]) {
|
|
407
|
+
delete new_common[property];
|
|
408
|
+
} else {
|
|
409
|
+
hasChanges = true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
} else {
|
|
415
|
+
hasChanges = true;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// only change object when any common property has changed
|
|
419
|
+
if (hasChanges) {
|
|
420
|
+
this.adapter.extendObject(id, {type: 'state', common: new_common, native: {}}, () =>
|
|
421
|
+
value !== undefined && this.setState_typed(id, value, true, stobj ? stobj.common.type : new_common.type));
|
|
422
|
+
} else if (value !== undefined) {
|
|
423
|
+
this.setState_typed(id, value, true, stobj.common.type);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
});
|
|
427
|
+
} else {
|
|
428
|
+
this.debug(`UpdateState: Device is deactivated ${devId} ${JSON.stringify(obj)}`);
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
this.debug(`UpdateState: missing device ${devId} ${JSON.stringify(obj)}`);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
setState_typed(id, value, ack, type, callback) {
|
|
437
|
+
// never set a null or undefined value
|
|
438
|
+
if (value === null || value === undefined) return;
|
|
439
|
+
if (!type) {
|
|
440
|
+
this.debug('SetState_typed called without type');
|
|
441
|
+
// identify datatype, recursively call this function with set datatype
|
|
442
|
+
this.adapter.getObject(id, (err, obj) => {
|
|
443
|
+
if (obj && obj.common) {
|
|
444
|
+
this.setState_typed(id, value, ack, obj.common.type, callback);
|
|
445
|
+
} else {
|
|
446
|
+
this.setState_typed(id, value, ack, 'noobj', callback);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (typeof value !== type) {
|
|
452
|
+
this.debug(`SetState_typed : converting ${JSON.stringify(value)} for ${id} from ${typeof value} to ${type}`);
|
|
453
|
+
switch (type) {
|
|
454
|
+
case 'number':
|
|
455
|
+
value = parseFloat(value);
|
|
456
|
+
if (isNaN(value)) {
|
|
457
|
+
value = 0;
|
|
458
|
+
}
|
|
459
|
+
break;
|
|
460
|
+
case 'string':
|
|
461
|
+
case 'text':
|
|
462
|
+
value = JSON.stringify(value);
|
|
463
|
+
break;
|
|
464
|
+
case 'boolean': {
|
|
465
|
+
if (typeof value == 'number') {
|
|
466
|
+
value = value !== 0;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
const sval = JSON.stringify(value).toLowerCase().trim();
|
|
470
|
+
value = sval === 'true' || sval === 'yes' || sval === 'on';
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
this.adapter.setState(id, value, ack, callback);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
updateDev(dev_id, dev_name, model, callback) {
|
|
479
|
+
const __dev_name = this.verifyDeviceName(dev_id, dev_name);
|
|
480
|
+
const id = '' + dev_id;
|
|
481
|
+
const modelDesc = statesMapping.findModel(model);
|
|
482
|
+
let icon = (modelDesc && modelDesc.icon) ? modelDesc.icon : 'img/unknown.png';
|
|
483
|
+
|
|
484
|
+
// download icon if it external and not undef
|
|
485
|
+
if (model === undefined) {
|
|
486
|
+
this.warn(`download icon ${__dev_name} for undefined Device not available. Check your devices.`);
|
|
487
|
+
} else {
|
|
488
|
+
const model_modif = model.replace(/\//g, '-');
|
|
489
|
+
const pathToIcon = `${this.adapter.adapterDir}/admin/img/${model_modif}.png`;
|
|
490
|
+
|
|
491
|
+
if (icon.startsWith('http')) {
|
|
492
|
+
try {
|
|
493
|
+
if (!fs.existsSync(pathToIcon)) {
|
|
494
|
+
this.warn(`download icon from ${icon} saved into ${pathToIcon}`);
|
|
495
|
+
this.downloadIcon(icon, pathToIcon);
|
|
496
|
+
}
|
|
497
|
+
icon = `img/${model_modif}.png`;
|
|
498
|
+
} catch (e) {
|
|
499
|
+
this.debug(`ERROR : icon not found from ${icon} saved into ${pathToIcon}`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
this.adapter.setObjectNotExists(id, {
|
|
505
|
+
type: 'device',
|
|
506
|
+
// actually this is an error, so device.common has no attribute type. It must be in native part
|
|
507
|
+
common: {
|
|
508
|
+
name: __dev_name,
|
|
509
|
+
type: model,
|
|
510
|
+
icon,
|
|
511
|
+
color: null,
|
|
512
|
+
statusStates: {onlineId: `${this.adapter.namespace}.${dev_id}.available`}
|
|
513
|
+
},
|
|
514
|
+
native: {id: dev_id}
|
|
515
|
+
}, () => {
|
|
516
|
+
// update type and icon
|
|
517
|
+
this.adapter.extendObject(id, {
|
|
518
|
+
common: {
|
|
519
|
+
type: model,
|
|
520
|
+
icon,
|
|
521
|
+
color: null,
|
|
522
|
+
statusStates: {onlineId: `${this.adapter.namespace}.${dev_id}.available`}
|
|
523
|
+
}
|
|
524
|
+
}, callback);
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
async downloadIcon(url, image_path) {
|
|
529
|
+
if (!fs.existsSync(image_path)) {
|
|
530
|
+
return new Promise((resolve, reject) => {
|
|
531
|
+
request.head(url, (err, res, body) => {
|
|
532
|
+
if (err) {
|
|
533
|
+
return reject(err + ' ' + res + ' ' + body);
|
|
534
|
+
}
|
|
535
|
+
const stream = request(url);
|
|
536
|
+
stream.pipe(
|
|
537
|
+
fs.createWriteStream(image_path)
|
|
538
|
+
.on('error', err => reject(err)))
|
|
539
|
+
.on('close', () => resolve());
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
async syncDevStates(dev, model) {
|
|
546
|
+
const devId = dev.ieeeAddr.substr(2);
|
|
547
|
+
// devId - iobroker device id
|
|
548
|
+
const devStates = await this.getDevStates(dev.ieeeAddr, model);
|
|
549
|
+
if (!devStates) {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const states = statesMapping.commonStates.concat(devStates.states);
|
|
553
|
+
|
|
554
|
+
for (const stateInd in states) {
|
|
555
|
+
if (!states.hasOwnProperty(stateInd)) {
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
const statedesc = states[stateInd];
|
|
560
|
+
if (statedesc === undefined) {
|
|
561
|
+
this.error(`syncDevStates: Illegal state in ${JSON.stringify(dev)} - ${JSON.stringify(stateInd)}`);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
// Filter out non routers or devices that are battery driven for the availability flag
|
|
565
|
+
if (statedesc.id === 'available') {
|
|
566
|
+
if (!(dev.type === 'Router') || dev.powerSource === 'Battery') {
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
// lazy states
|
|
571
|
+
if (statedesc.lazy) {
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const common = {
|
|
576
|
+
name: statedesc.name,
|
|
577
|
+
type: statedesc.type,
|
|
578
|
+
unit: statedesc.unit,
|
|
579
|
+
read: statedesc.read,
|
|
580
|
+
write: statedesc.write,
|
|
581
|
+
icon: statedesc.icon,
|
|
582
|
+
role: statedesc.role,
|
|
583
|
+
min: statedesc.min,
|
|
584
|
+
max: statedesc.max,
|
|
585
|
+
states: statedesc.states,
|
|
586
|
+
};
|
|
587
|
+
this.updateState(devId, statedesc.id, undefined, common);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
async getExcludeExposes(allExcludesObj) {
|
|
592
|
+
statesMapping.fillStatesWithExposes(allExcludesObj);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async publishToState(devId, model, payload) {
|
|
596
|
+
const devStates = await this.getDevStates(`0x${devId}`, model);
|
|
597
|
+
let has_debug = false;
|
|
598
|
+
if (this.debugDevices === undefined) this.getDebugDevices();
|
|
599
|
+
for (const addressPart of this.debugDevices) {
|
|
600
|
+
if (typeof devId === 'string' && devId.includes(addressPart)) {
|
|
601
|
+
if (payload.hasOwnProperty('msg_from_zigbee')) break;
|
|
602
|
+
this.warn(`ELEVATED publishToState: message received '${JSON.stringify(payload)}' from device ${devId} type '${model}'`);
|
|
603
|
+
has_debug = true;
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (!devStates) {
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
// find states for payload
|
|
611
|
+
if (devStates.states !== undefined) {
|
|
612
|
+
const states = statesMapping.commonStates.concat(
|
|
613
|
+
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id)));
|
|
614
|
+
|
|
615
|
+
for (const stateInd in states) {
|
|
616
|
+
const statedesc = states[stateInd];
|
|
617
|
+
let value;
|
|
618
|
+
if (statedesc.getter) {
|
|
619
|
+
value = statedesc.getter(payload);
|
|
620
|
+
} else {
|
|
621
|
+
value = payload[statedesc.prop || statedesc.id];
|
|
622
|
+
}
|
|
623
|
+
// checking value
|
|
624
|
+
if (value === undefined || value === null) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
let stateID = statedesc.id;
|
|
628
|
+
|
|
629
|
+
if (has_debug && statedesc.id !== 'msg_from_zigbee') {
|
|
630
|
+
this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const common = {
|
|
634
|
+
name: statedesc.name,
|
|
635
|
+
type: statedesc.type,
|
|
636
|
+
unit: statedesc.unit,
|
|
637
|
+
read: statedesc.read,
|
|
638
|
+
write: statedesc.write,
|
|
639
|
+
icon: statedesc.icon,
|
|
640
|
+
role: statedesc.role,
|
|
641
|
+
min: statedesc.min,
|
|
642
|
+
max: statedesc.max,
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
if (typeof value === 'object' && value.hasOwnProperty('stateid')) {
|
|
646
|
+
stateID = `${stateID}.${value.stateid}`;
|
|
647
|
+
if (value.hasOwnProperty('unit')) {
|
|
648
|
+
common.unit = value.unit;
|
|
649
|
+
}
|
|
650
|
+
common.name = value.name ? value.name : value.stateid;
|
|
651
|
+
common.role = value.role ? `value.${value.role}` : 'number';
|
|
652
|
+
value = value.value;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// if needs to return value to back after timeout
|
|
656
|
+
if (statedesc.isEvent) {
|
|
657
|
+
this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value);
|
|
658
|
+
} else {
|
|
659
|
+
if (statedesc.prepublish) {
|
|
660
|
+
this.collectOptions(devId, model, options =>
|
|
661
|
+
statedesc.prepublish(devId, value, newvalue =>
|
|
662
|
+
this.updateState(devId, stateID, newvalue, common), options));
|
|
663
|
+
} else {
|
|
664
|
+
this.updateState(devId, stateID, value, common);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
module.exports = StatesController;
|