2021-02-20 12:34:30 +01:00
|
|
|
const Logger = require('./Logger');
|
|
|
|
|
|
|
|
|
|
const logger = Logger.getLogger('device-registry');
|
2021-02-01 21:18:42 +01:00
|
|
|
class DeviceRegistry {
|
2021-03-29 12:51:48 +02:00
|
|
|
/** @type {string} */
|
2021-02-01 21:18:42 +01:00
|
|
|
cryptoKey = null;
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
outputDeviceIdByRoomId = {};
|
|
|
|
|
outputDeviceIdByBLEIndex = {};
|
2021-02-01 21:18:42 +01:00
|
|
|
|
|
|
|
|
// Dictionaries of [id]: device per type
|
2021-03-29 12:51:48 +02:00
|
|
|
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
|
|
|
|
outputDevices = {};
|
|
|
|
|
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
2021-02-01 21:18:42 +01:00
|
|
|
sceneDevices = {};
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
|
getUniqueOutputId(deviceId, outputIndex) {
|
|
|
|
|
return `${deviceId}_${outputIndex}`;
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
|
|
|
|
addOutputDevice(outputDevice) {
|
|
|
|
|
this.outputDevices = {
|
|
|
|
|
...this.outputDevices,
|
|
|
|
|
[outputDevice.uniqueId]: outputDevice,
|
2021-02-20 12:34:30 +01:00
|
|
|
};
|
|
|
|
|
|
2021-02-20 15:33:06 +01:00
|
|
|
logger.verbose(
|
2021-03-29 12:51:48 +02:00
|
|
|
`Added/updated output device: ${JSON.stringify(outputDevice)}. ${
|
|
|
|
|
Object.keys(this.outputDevices).length
|
|
|
|
|
} output devices in total.`,
|
2021-02-20 15:33:06 +01:00
|
|
|
);
|
2021-02-20 12:34:30 +01:00
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
this.outputDeviceIdByBLEIndex[outputDevice.bleDeviceIndex] = outputDevice.uniqueId;
|
|
|
|
|
|
|
|
|
|
if (!this.outputDeviceIdByRoomId[outputDevice.roomId]) {
|
|
|
|
|
this.outputDeviceIdByRoomId[outputDevice.roomId] = [];
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
outputDevice.roomId !== outputDevice.uniqueId
|
|
|
|
|
&& !this.outputDeviceIdByRoomId[outputDevice.roomId].includes(outputDevice.roomId)
|
|
|
|
|
) {
|
|
|
|
|
this.outputDeviceIdByRoomId[outputDevice.roomId].push(outputDevice.roomId);
|
2021-02-20 15:33:06 +01:00
|
|
|
logger.verbose(
|
2021-03-29 12:51:48 +02:00
|
|
|
`Added device to room ${outputDevice.roomId}: ${JSON.stringify(
|
|
|
|
|
this.outputDeviceIdByRoomId[outputDevice.roomId],
|
2021-03-25 16:39:56 +01:00
|
|
|
)}`,
|
2021-02-20 15:33:06 +01:00
|
|
|
);
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
2021-02-20 12:34:30 +01:00
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
if (outputDevice.hiddenFromIntegrations || outputDevice.hiddenFromRoomList) {
|
|
|
|
|
logger.verbose(`Device is hidden and should possibly not be included.
|
|
|
|
|
Hidden from room list: ${outputDevice.hiddenFromRoomList}
|
|
|
|
|
Hidden from integrations: ${outputDevice.hiddenFromIntegrations}`);
|
|
|
|
|
}
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
/** @param scene {import('types/DeviceRegistry').OutputDevice} */
|
2021-02-20 12:34:30 +01:00
|
|
|
addScene(scene) {
|
|
|
|
|
this.sceneDevices = {
|
|
|
|
|
...this.sceneDevices,
|
2021-03-29 12:51:48 +02:00
|
|
|
[scene.uniqueId]: scene,
|
2021-02-20 12:34:30 +01:00
|
|
|
};
|
2021-02-20 15:33:06 +01:00
|
|
|
logger.verbose(
|
2021-03-29 12:51:48 +02:00
|
|
|
`Added/updated scene: ${JSON.stringify(scene)}. ${
|
2021-02-20 15:33:06 +01:00
|
|
|
Object.keys(this.sceneDevices).length
|
|
|
|
|
} scenes in total.`,
|
|
|
|
|
);
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearPlejdDevices() {
|
2021-03-29 12:51:48 +02:00
|
|
|
this.outputDevices = {};
|
|
|
|
|
this.outputDeviceIdByRoomId = {};
|
2021-02-01 21:18:42 +01:00
|
|
|
this.deviceIdsBySerial = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearSceneDevices() {
|
|
|
|
|
this.sceneDevices = {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
getOutputDevice(uniqueOutputId) {
|
|
|
|
|
return this.outputDevices[uniqueOutputId];
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
/** @returns {string[]} */
|
|
|
|
|
getOutputDeviceIdsByRoomId(roomId) {
|
|
|
|
|
return this.outputDeviceIdByRoomId[roomId];
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
getOutputDeviceName(uniqueOutputId) {
|
|
|
|
|
return (this.outputDevices[uniqueOutputId] || {}).name;
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-10 10:10:28 +01:00
|
|
|
getScene(sceneId) {
|
|
|
|
|
return this.sceneDevices[sceneId];
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 21:18:42 +01:00
|
|
|
getSceneName(sceneId) {
|
|
|
|
|
return (this.sceneDevices[sceneId] || {}).name;
|
|
|
|
|
}
|
2021-02-20 12:34:30 +01:00
|
|
|
|
2021-03-29 12:51:48 +02:00
|
|
|
/**
|
|
|
|
|
* @param {string} uniqueOutputId
|
|
|
|
|
* @param {boolean} state
|
|
|
|
|
* @param {number?} [dim]
|
|
|
|
|
*/
|
|
|
|
|
setOutputState(uniqueOutputId, state, dim) {
|
|
|
|
|
const device = this.getOutputDevice(uniqueOutputId);
|
|
|
|
|
if (!device) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
`Trying to set state for ${uniqueOutputId} which is not in the list of known outputs.`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
2021-02-20 12:34:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-20 15:33:06 +01:00
|
|
|
device.state = state;
|
2021-02-20 12:34:30 +01:00
|
|
|
if (dim && device.dimmable) {
|
|
|
|
|
device.dim = dim;
|
|
|
|
|
}
|
|
|
|
|
if (Logger.shouldLog('silly')) {
|
|
|
|
|
logger.silly(`Updated state: ${JSON.stringify(device)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-01 21:18:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = DeviceRegistry;
|