Add physical devices to DeviceRegistry to allow for serial number lookup during BLE startup
This commit is contained in:
parent
d6b9d9104a
commit
ef718cf1db
1 changed files with 71 additions and 17 deletions
|
|
@ -5,18 +5,25 @@ class DeviceRegistry {
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
cryptoKey = null;
|
cryptoKey = null;
|
||||||
|
|
||||||
outputDeviceIdByRoomId = {};
|
/** @private @type {Object.<string, import('types/ApiSite').Device>} */
|
||||||
outputDeviceIdByBLEIndex = {};
|
devices = {};
|
||||||
|
/** @private */
|
||||||
|
outputDeviceUniqueIdsByRoomId = {};
|
||||||
|
/** @private */
|
||||||
|
outputUniqueIdByBleOutputAddress = {};
|
||||||
|
|
||||||
|
/** @private @type {import('./types/ApiSite').ApiSite} */
|
||||||
|
apiSite;
|
||||||
|
|
||||||
// Dictionaries of [id]: device per type
|
// Dictionaries of [id]: device per type
|
||||||
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
/** @private @type {import('types/DeviceRegistry').OutputDevices} */
|
||||||
outputDevices = {};
|
outputDevices = {};
|
||||||
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
/** @private @type {import('types/DeviceRegistry').OutputDevices} */
|
||||||
sceneDevices = {};
|
sceneDevices = {};
|
||||||
|
|
||||||
// eslint-disable-next-line class-methods-use-this
|
/** @param device {import('./types/ApiSite').Device} */
|
||||||
getUniqueOutputId(deviceId, outputIndex) {
|
addPhysicalDevice(device) {
|
||||||
return `${deviceId}_${outputIndex}`;
|
this.devices[device.deviceId] = device;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
||||||
|
|
@ -32,19 +39,19 @@ class DeviceRegistry {
|
||||||
} output devices in total.`,
|
} output devices in total.`,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.outputDeviceIdByBLEIndex[outputDevice.bleDeviceIndex] = outputDevice.uniqueId;
|
this.outputUniqueIdByBleOutputAddress[outputDevice.bleOutputAddress] = outputDevice.uniqueId;
|
||||||
|
|
||||||
if (!this.outputDeviceIdByRoomId[outputDevice.roomId]) {
|
if (!this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId]) {
|
||||||
this.outputDeviceIdByRoomId[outputDevice.roomId] = [];
|
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = [];
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
outputDevice.roomId !== outputDevice.uniqueId
|
outputDevice.roomId !== outputDevice.uniqueId &&
|
||||||
&& !this.outputDeviceIdByRoomId[outputDevice.roomId].includes(outputDevice.roomId)
|
!this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].includes(outputDevice.uniqueId)
|
||||||
) {
|
) {
|
||||||
this.outputDeviceIdByRoomId[outputDevice.roomId].push(outputDevice.roomId);
|
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].push(outputDevice.uniqueId);
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Added device to room ${outputDevice.roomId}: ${JSON.stringify(
|
`Added device to room ${outputDevice.roomId}: ${JSON.stringify(
|
||||||
this.outputDeviceIdByRoomId[outputDevice.roomId],
|
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId],
|
||||||
)}`,
|
)}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -70,28 +77,64 @@ class DeviceRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
clearPlejdDevices() {
|
clearPlejdDevices() {
|
||||||
|
this.devices = {};
|
||||||
this.outputDevices = {};
|
this.outputDevices = {};
|
||||||
this.outputDeviceIdByRoomId = {};
|
this.outputDeviceUniqueIdsByRoomId = {};
|
||||||
this.deviceIdsBySerial = {};
|
this.outputUniqueIdByBleOutputAddress = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
clearSceneDevices() {
|
clearSceneDevices() {
|
||||||
this.sceneDevices = {};
|
this.sceneDevices = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {import('./types/DeviceRegistry').OutputDevice[]}
|
||||||
|
*/
|
||||||
|
getAllOutputDevices() {
|
||||||
|
return Object.values(this.outputDevices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {import('./types/DeviceRegistry').OutputDevice[]}
|
||||||
|
*/
|
||||||
|
getAllSceneDevices() {
|
||||||
|
return Object.values(this.sceneDevices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @returns {import('./types/ApiSite').ApiSite} */
|
||||||
|
getApiSite() {
|
||||||
|
return this.apiSite;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} uniqueOutputId
|
||||||
|
*/
|
||||||
getOutputDevice(uniqueOutputId) {
|
getOutputDevice(uniqueOutputId) {
|
||||||
return this.outputDevices[uniqueOutputId];
|
return this.outputDevices[uniqueOutputId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {import('./types/DeviceRegistry').OutputDevice} */
|
||||||
|
getOutputDeviceByBleOutputAddress(bleOutputAddress) {
|
||||||
|
return this.outputDevices[this.outputUniqueIdByBleOutputAddress[bleOutputAddress]];
|
||||||
|
}
|
||||||
|
|
||||||
/** @returns {string[]} */
|
/** @returns {string[]} */
|
||||||
getOutputDeviceIdsByRoomId(roomId) {
|
getOutputDeviceIdsByRoomId(roomId) {
|
||||||
return this.outputDeviceIdByRoomId[roomId];
|
return this.outputDeviceUniqueIdsByRoomId[roomId];
|
||||||
}
|
}
|
||||||
|
|
||||||
getOutputDeviceName(uniqueOutputId) {
|
getOutputDeviceName(uniqueOutputId) {
|
||||||
return (this.outputDevices[uniqueOutputId] || {}).name;
|
return (this.outputDevices[uniqueOutputId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string } deviceId The physical device serial number
|
||||||
|
* @return {import('./types/ApiSite').Device}
|
||||||
|
*/
|
||||||
|
getPhysicalDevice(deviceId) {
|
||||||
|
return this.devices[deviceId];
|
||||||
|
}
|
||||||
|
|
||||||
getScene(sceneId) {
|
getScene(sceneId) {
|
||||||
return this.sceneDevices[sceneId];
|
return this.sceneDevices[sceneId];
|
||||||
}
|
}
|
||||||
|
|
@ -100,6 +143,17 @@ class DeviceRegistry {
|
||||||
return (this.sceneDevices[sceneId] || {}).name;
|
return (this.sceneDevices[sceneId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line class-methods-use-this
|
||||||
|
getUniqueOutputId(deviceId, outputIndex) {
|
||||||
|
return `${deviceId}_${outputIndex}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param apiSite {import('./types/ApiSite').ApiSite} */
|
||||||
|
setApiSite(apiSite) {
|
||||||
|
this.apiSite = apiSite;
|
||||||
|
this.cryptoKey = apiSite.plejdMesh.cryptoKey;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} uniqueOutputId
|
* @param {string} uniqueOutputId
|
||||||
* @param {boolean} state
|
* @param {boolean} state
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue