-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDeviceManager.js
277 lines (226 loc) · 8.67 KB
/
DeviceManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"use strict";
const normalize = require('./normalize');
const Log = require('./Log');
const EventHandler = require('./EventHandler');
class DeviceManager {
constructor({ api, settings }) {
this.api = api;
this.onAdd = new EventHandler('Device.add');
this.onRemove = new EventHandler('Device.remove');
this.onUpdate = new EventHandler('Device.update');
this.setEnabledDevices((settings || {}).devices);
}
getDeviceId(device) {
if (!device) return undefined;
if (device) {
if (typeof device === 'string') {
if (this.deviceIds && this.deviceIds.has(device))
return device;
if (this.deviceNames && this.deviceNames.has(device))
return this.deviceNames.get(device);
if (this.deviceTopics && this.deviceTopics.has(normalize(device)))
return this.deviceTopics.get(normalize(device));
} else if (typeof device === 'object') {
if (device.id)
return device.id;
if (device.name) {
if (this.deviceNames && this.deviceNames.has(device.name))
return this.deviceNames.get(device.name);
if (this.deviceTopics && this.deviceTopics.has(normalize(device.name)))
return this.deviceTopics.get(normalize(device.name));
}
}
}
Log.error("Device id not found");
Log.debug(device);
return undefined;
}
getDeviceName(device) {
// from device info
if (typeof device === 'object' && device.name) {
return normalize(device.name);
}
// from mapping
if (!this.deviceIds) return undefined;
const id = this.getDeviceId(device);
return this.deviceIds.get(id);
}
getDeviceTopic(device) {
if (!this.deviceTopicIds) return undefined;
const id = this.getDeviceId(device);
return this.deviceTopicIds.get(id);
}
async getDeviceById(deviceId) {
if (deviceId) {
const device = await this.api.devices.getDevice({ id: deviceId });
if (!device) {
Log.error("Device not found: " + deviceId);
}
return device;
}
Log.error("No device id provided");
return undefined;
}
// TODO: optimize
async getDeviceByName(device) {
const id = this.getDeviceId(device);
return await this.getDeviceById(id);
}
// TODO: optimize
async getDeviceByTopic(device) {
const id = this.getDeviceId(device);
return await this.getDeviceById(id);
}
async getDevice(device) {
const id = this.getDeviceId(device);
return await this.getDeviceById(id);
}
isRegistered(device) {
if (!this.deviceIds) return false;
const id = this.getDeviceId(device);
return id && this.deviceIds.has(id);
}
async register() {
// Subscribe to realtime events and set all devices global
this.api.devices.on('device.create', this._addDevice.bind(this));
this.api.devices.on('device.delete', this._removeDevice.bind(this));
this.api.devices.on('device.update', this._updateDevice.bind(this));
this.api.zones.on('zones.create', this._addZone.bind(this));
this.api.zones.on('zones.delete', this._removeZone.bind(this));
this.api.zones.on('zones.update', this._updateZone.bind(this));
this.devices = await this.api.devices.getDevices();
this.zones = await this.api.zones.getZones();
if (this.devices) {
for (let key in this.devices) {
if (Array.isArray(this.devices) || this.devices.hasOwnProperty(key)) {
const device = this.devices[key];
// inject zone
if (this.zones && device.zone) {
device.zone = this.zones[device.zone];
}
// register
await this.registerDevice(device);
}
}
}
}
async unregister() { }
async registerDevice(device) {
if (typeof device === 'object') {
if (!device.id) {
Log.error("[SKIP] No device id provided");
return;
}
const deviceName = (device.name || '').trim();
if (!deviceName) {
Log.error("[SKIP] No device name provided");
return;
}
if (this.isRegistered(device)) {
Log.debug('device already registered');
return;
}
this.deviceIds = this.deviceIds || new Map(); // id => name
this.deviceNames = this.deviceNames || new Map(); // name => id
this.deviceTopicIds = this.deviceTopicIds || new Map(); // id => topic
this.deviceTopics = this.deviceTopics || new Map(); // topic => id
const deviceTopic = normalize(deviceName);
Log.info(deviceName + ': ' + deviceTopic);
this.deviceIds.set(device.id, deviceName);
this.deviceNames.set(deviceName, device.id);
if (deviceTopic) {
this.deviceTopicIds.set(device.id, deviceTopic);
this.deviceTopics.set(deviceTopic, device.id);
}
}
}
async _addDevice(device) {
Log.info('New device found!');
if (device && device.id) {
this.devices = this.devices || {};
this.devices[device.id] = device;
await this.registerDevice(device);
await this.onAdd.emit(device);
}
}
async _removeDevice(id) {
const deviceName = this.getDeviceName(id);
const deviceTopic = this.getDeviceTopic(id);
if (this.deviceIds) this.deviceIds.delete(id);
if (this.deviceTopicIds) this.deviceIds.delete(id);
if (deviceName && this.deviceNames) this.deviceNames.delete(deviceName);
if (deviceTopic && this.deviceTopics) this.deviceTopics.delete(deviceTopic);
if (this.devices) delete this.devices[id];
await this.onRemove.emit(id);
}
async _updateDevice(id) {
await this.onUpdate.emit(id);
}
async _addZone(id) {
Log.info('New zone found!');
if (id) {
this.zones = this.zones || {};
const zone = await this.api.zones.getZone({ id });
if (zone) {
this.zones[id] = zone;
}
}
}
async _removeZone(id) {
if (id && this.zones && this.zones.hasOwnProperty(id)) {
delete this.zones[id];
}
}
async _updateZone(id) {
await this._addZone(id);
}
async getCapabilities(device) {
device = await this.getDevice(device);
return device ? device.capabilitiesObj || device.capabilities : undefined; // 1.5.13 vs 2.0
}
async getCapability(device, capabilityId) {
const capabilities = this.getCapabilities(device);
return capabilities ? capabilities[capabilityId] : undefined;
}
computeChanges(devices) {
let changes = {
enabled: [],
disabled: [],
untouched: []
};
if (devices) {
for (let id in devices) {
if (devices.hasOwnProperty(id)) {
const enabled = devices[id] !== false;
if (enabled !== this.isDeviceEnabled(id)) {
if (enabled) {
if (this.devices) {
Log.debug("Enabled device: " + (this.devices[id] || {}).name);
}
changes.enabled.push(id);
} else {
if (this.devices) {
Log.debug("Disabled device: " + (this.devices[id] || {}).name);
}
changes.disabled.push(id);
}
} else {
changes.untouched.push(id);
}
}
}
}
return changes;
}
setEnabledDevices(devices) {
this._enabledDevices = devices;
}
isDeviceEnabled(device) {
const enabledDevices = this._enabledDevices;
if (!enabledDevices) return true;
const deviceId = typeof device === 'object' ? device.id : device;
if (!deviceId) return false;
return enabledDevices.hasOwnProperty(deviceId) ? enabledDevices[deviceId] : true;
}
}
module.exports = DeviceManager;