Refactor DeviceRegistry to start use unique ids based on device serial and output index
This commit is contained in:
parent
764a3ca223
commit
d6b9d9104a
3 changed files with 214 additions and 164 deletions
|
|
@ -2,125 +2,94 @@ const Logger = require('./Logger');
|
||||||
|
|
||||||
const logger = Logger.getLogger('device-registry');
|
const logger = Logger.getLogger('device-registry');
|
||||||
class DeviceRegistry {
|
class DeviceRegistry {
|
||||||
apiSite;
|
/** @type {string} */
|
||||||
cryptoKey = null;
|
cryptoKey = null;
|
||||||
|
|
||||||
deviceIdsByRoom = {};
|
outputDeviceIdByRoomId = {};
|
||||||
deviceIdsBySerial = {};
|
outputDeviceIdByBLEIndex = {};
|
||||||
|
|
||||||
// Dictionaries of [id]: device per type
|
// Dictionaries of [id]: device per type
|
||||||
plejdDevices = {};
|
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
||||||
roomDevices = {};
|
outputDevices = {};
|
||||||
|
/** @type {import('types/DeviceRegistry').OutputDevices} */
|
||||||
sceneDevices = {};
|
sceneDevices = {};
|
||||||
|
|
||||||
get allDevices() {
|
// eslint-disable-next-line class-methods-use-this
|
||||||
return [
|
getUniqueOutputId(deviceId, outputIndex) {
|
||||||
...Object.values(this.plejdDevices),
|
return `${deviceId}_${outputIndex}`;
|
||||||
...Object.values(this.roomDevices),
|
|
||||||
...Object.values(this.sceneDevices),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addPlejdDevice(device) {
|
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
||||||
const added = {
|
addOutputDevice(outputDevice) {
|
||||||
...this.plejdDevices[device.id],
|
this.outputDevices = {
|
||||||
...device,
|
...this.outputDevices,
|
||||||
|
[outputDevice.uniqueId]: outputDevice,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.plejdDevices = {
|
|
||||||
...this.plejdDevices,
|
|
||||||
[added.id]: added,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.deviceIdsBySerial[added.serialNumber] = added.id;
|
|
||||||
|
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Added/updated device: ${JSON.stringify(added)}. ${
|
`Added/updated output device: ${JSON.stringify(outputDevice)}. ${
|
||||||
Object.keys(this.plejdDevices).length
|
Object.keys(this.outputDevices).length
|
||||||
} plejd devices in total.`,
|
} output devices in total.`,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (added.roomId) {
|
this.outputDeviceIdByBLEIndex[outputDevice.bleDeviceIndex] = outputDevice.uniqueId;
|
||||||
if (!this.deviceIdsByRoom[added.roomId]) {
|
|
||||||
this.deviceIdsByRoom[added.roomId] = [];
|
if (!this.outputDeviceIdByRoomId[outputDevice.roomId]) {
|
||||||
}
|
this.outputDeviceIdByRoomId[outputDevice.roomId] = [];
|
||||||
const room = this.deviceIdsByRoom[added.roomId];
|
}
|
||||||
if (!room.includes(added.id)) {
|
if (
|
||||||
this.deviceIdsByRoom[added.roomId] = [...room, added.id];
|
outputDevice.roomId !== outputDevice.uniqueId
|
||||||
}
|
&& !this.outputDeviceIdByRoomId[outputDevice.roomId].includes(outputDevice.roomId)
|
||||||
|
) {
|
||||||
|
this.outputDeviceIdByRoomId[outputDevice.roomId].push(outputDevice.roomId);
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Added device to room ${added.roomId}: ${JSON.stringify(
|
`Added device to room ${outputDevice.roomId}: ${JSON.stringify(
|
||||||
this.deviceIdsByRoom[added.roomId],
|
this.outputDeviceIdByRoomId[outputDevice.roomId],
|
||||||
)}`,
|
)}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return added;
|
if (outputDevice.hiddenFromIntegrations || outputDevice.hiddenFromRoomList) {
|
||||||
}
|
logger.verbose(`Device is hidden and should possibly not be included.
|
||||||
|
Hidden from room list: ${outputDevice.hiddenFromRoomList}
|
||||||
addRoomDevice(device) {
|
Hidden from integrations: ${outputDevice.hiddenFromIntegrations}`);
|
||||||
const added = {
|
}
|
||||||
...this.roomDevices[device.id],
|
|
||||||
...device,
|
|
||||||
};
|
|
||||||
this.roomDevices = {
|
|
||||||
...this.roomDevices,
|
|
||||||
[added.id]: added,
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.verbose(
|
|
||||||
`Added/updated room device: ${JSON.stringify(added)}. ${
|
|
||||||
Object.keys(this.roomDevices).length
|
|
||||||
} room devices total.`,
|
|
||||||
);
|
|
||||||
return added;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param scene {import('types/DeviceRegistry').OutputDevice} */
|
||||||
addScene(scene) {
|
addScene(scene) {
|
||||||
const added = {
|
|
||||||
...this.sceneDevices[scene.id],
|
|
||||||
...scene,
|
|
||||||
};
|
|
||||||
this.sceneDevices = {
|
this.sceneDevices = {
|
||||||
...this.sceneDevices,
|
...this.sceneDevices,
|
||||||
[added.id]: added,
|
[scene.uniqueId]: scene,
|
||||||
};
|
};
|
||||||
logger.verbose(
|
logger.verbose(
|
||||||
`Added/updated scene: ${JSON.stringify(added)}. ${
|
`Added/updated scene: ${JSON.stringify(scene)}. ${
|
||||||
Object.keys(this.sceneDevices).length
|
Object.keys(this.sceneDevices).length
|
||||||
} scenes in total.`,
|
} scenes in total.`,
|
||||||
);
|
);
|
||||||
return added;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clearPlejdDevices() {
|
clearPlejdDevices() {
|
||||||
this.plejdDevices = {};
|
this.outputDevices = {};
|
||||||
this.deviceIdsByRoom = {};
|
this.outputDeviceIdByRoomId = {};
|
||||||
this.deviceIdsBySerial = {};
|
this.deviceIdsBySerial = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
clearRoomDevices() {
|
|
||||||
this.roomDevices = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
clearSceneDevices() {
|
clearSceneDevices() {
|
||||||
this.sceneDevices = {};
|
this.sceneDevices = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
getDevice(deviceId) {
|
getOutputDevice(uniqueOutputId) {
|
||||||
return this.plejdDevices[deviceId] || this.roomDevices[deviceId];
|
return this.outputDevices[uniqueOutputId];
|
||||||
}
|
}
|
||||||
|
|
||||||
getDeviceIdsByRoom(roomId) {
|
/** @returns {string[]} */
|
||||||
return this.deviceIdsByRoom[roomId];
|
getOutputDeviceIdsByRoomId(roomId) {
|
||||||
|
return this.outputDeviceIdByRoomId[roomId];
|
||||||
}
|
}
|
||||||
|
|
||||||
getDeviceBySerialNumber(serialNumber) {
|
getOutputDeviceName(uniqueOutputId) {
|
||||||
return this.getDevice(this.deviceIdsBySerial[serialNumber]);
|
return (this.outputDevices[uniqueOutputId] || {}).name;
|
||||||
}
|
|
||||||
|
|
||||||
getDeviceName(deviceId) {
|
|
||||||
return (this.plejdDevices[deviceId] || {}).name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getScene(sceneId) {
|
getScene(sceneId) {
|
||||||
|
|
@ -131,25 +100,20 @@ class DeviceRegistry {
|
||||||
return (this.sceneDevices[sceneId] || {}).name;
|
return (this.sceneDevices[sceneId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
||||||
getState(deviceId) {
|
/**
|
||||||
const device = this.getDevice(deviceId) || {};
|
* @param {string} uniqueOutputId
|
||||||
if (device.dimmable) {
|
* @param {boolean} state
|
||||||
return {
|
* @param {number?} [dim]
|
||||||
state: device.state,
|
*/
|
||||||
dim: device.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;
|
||||||
}
|
}
|
||||||
return {
|
|
||||||
state: device.state,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
setApiSite(siteDetails) {
|
|
||||||
this.apiSite = siteDetails;
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(deviceId, state, dim) {
|
|
||||||
const device = this.getDevice(deviceId) || this.addPlejdDevice({ id: deviceId });
|
|
||||||
device.state = state;
|
device.state = state;
|
||||||
if (dim && device.dimmable) {
|
if (dim && device.dimmable) {
|
||||||
device.dim = dim;
|
device.dim = dim;
|
||||||
|
|
|
||||||
|
|
@ -10,15 +10,33 @@ const API_LOGIN_URL = 'login';
|
||||||
const API_SITE_LIST_URL = 'functions/getSiteList';
|
const API_SITE_LIST_URL = 'functions/getSiteList';
|
||||||
const API_SITE_DETAILS_URL = 'functions/getSiteById';
|
const API_SITE_DETAILS_URL = 'functions/getSiteById';
|
||||||
|
|
||||||
|
const TRAITS = {
|
||||||
|
NO_LOAD: 0,
|
||||||
|
NON_DIMMABLE: 9,
|
||||||
|
DIMMABLE: 11,
|
||||||
|
};
|
||||||
|
|
||||||
const logger = Logger.getLogger('plejd-api');
|
const logger = Logger.getLogger('plejd-api');
|
||||||
|
|
||||||
class PlejdApi {
|
class PlejdApi {
|
||||||
|
/** @private @type {import('types/Configuration').Options} */
|
||||||
config;
|
config;
|
||||||
|
|
||||||
|
/** @private @type {import('DeviceRegistry')} */
|
||||||
deviceRegistry;
|
deviceRegistry;
|
||||||
|
|
||||||
|
/** @private @type {string} */
|
||||||
sessionToken;
|
sessionToken;
|
||||||
|
|
||||||
|
/** @private @type {string} */
|
||||||
siteId;
|
siteId;
|
||||||
|
|
||||||
|
/** @private @type {import('types/ApiSite').ApiSite} */
|
||||||
siteDetails;
|
siteDetails;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import("./DeviceRegistry")} deviceRegistry
|
||||||
|
*/
|
||||||
constructor(deviceRegistry) {
|
constructor(deviceRegistry) {
|
||||||
this.config = Configuration.getOptions();
|
this.config = Configuration.getOptions();
|
||||||
this.deviceRegistry = deviceRegistry;
|
this.deviceRegistry = deviceRegistry;
|
||||||
|
|
@ -58,19 +76,19 @@ class PlejdApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.deviceRegistry.setApiSite(this.siteDetails);
|
|
||||||
this.deviceRegistry.cryptoKey = this.siteDetails.plejdMesh.cryptoKey;
|
this.deviceRegistry.cryptoKey = this.siteDetails.plejdMesh.cryptoKey;
|
||||||
|
|
||||||
this.getDevices();
|
this.getDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {Promise<import('types/ApiSite').CachedSite>} */
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
async getCachedCopy() {
|
async getCachedCopy() {
|
||||||
logger.info('Getting cached api response from disk');
|
logger.info('Getting cached api response from disk');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const rawData = await fs.promises.readFile('/data/cachedApiResponse.json');
|
const rawData = await fs.promises.readFile('/data/cachedApiResponse.json');
|
||||||
const cachedCopy = JSON.parse(rawData);
|
const cachedCopy = JSON.parse(rawData.toString());
|
||||||
|
|
||||||
return cachedCopy;
|
return cachedCopy;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
@ -82,12 +100,14 @@ class PlejdApi {
|
||||||
async saveCachedCopy() {
|
async saveCachedCopy() {
|
||||||
logger.info('Saving cached copy');
|
logger.info('Saving cached copy');
|
||||||
try {
|
try {
|
||||||
const rawData = JSON.stringify({
|
/** @type {import('types/ApiSite').CachedSite} */
|
||||||
|
const cachedSite = {
|
||||||
siteId: this.siteId,
|
siteId: this.siteId,
|
||||||
siteDetails: this.siteDetails,
|
siteDetails: this.siteDetails,
|
||||||
sessionToken: this.sessionToken,
|
sessionToken: this.sessionToken,
|
||||||
dtCache: new Date().toISOString(),
|
dtCache: new Date().toISOString(),
|
||||||
});
|
};
|
||||||
|
const rawData = JSON.stringify(cachedSite);
|
||||||
await fs.promises.writeFile('/data/cachedApiResponse.json', rawData);
|
await fs.promises.writeFile('/data/cachedApiResponse.json', rawData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('Failed to save cache of api response', err);
|
logger.error('Failed to save cache of api response', err);
|
||||||
|
|
@ -194,6 +214,14 @@ class PlejdApi {
|
||||||
getDevices() {
|
getDevices() {
|
||||||
logger.info('Getting devices from site details response...');
|
logger.info('Getting devices from site details response...');
|
||||||
|
|
||||||
|
if (this.siteDetails.gateways && this.siteDetails.gateways.length) {
|
||||||
|
this.siteDetails.gateways.forEach((gwy) => {
|
||||||
|
logger.info(`Plejd gateway '${gwy.title}' found on site`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.info('No Plejd gateway found on site');
|
||||||
|
}
|
||||||
|
|
||||||
this._getPlejdDevices();
|
this._getPlejdDevices();
|
||||||
this._getRoomDevices();
|
this._getRoomDevices();
|
||||||
this._getSceneDevices();
|
this._getSceneDevices();
|
||||||
|
|
@ -216,8 +244,11 @@ class PlejdApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
_getDeviceType(hardwareId) {
|
_getDeviceType(plejdDevice) {
|
||||||
switch (parseInt(hardwareId, 10)) {
|
// Type name is also sometimes available in device.hardware.name
|
||||||
|
// (maybe only when GWY-01 is present?)
|
||||||
|
|
||||||
|
switch (parseInt(plejdDevice.hardwareId, 10)) {
|
||||||
case 1:
|
case 1:
|
||||||
case 11:
|
case 11:
|
||||||
return { name: 'DIM-01', type: 'light', dimmable: true };
|
return { name: 'DIM-01', type: 'light', dimmable: true };
|
||||||
|
|
@ -259,69 +290,106 @@ class PlejdApi {
|
||||||
case 20:
|
case 20:
|
||||||
return { name: 'SPR-01', type: 'switch', dimmable: false };
|
return { name: 'SPR-01', type: 'switch', dimmable: false };
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown device type with id ${hardwareId}`);
|
throw new Error(`Unknown device type with id ${plejdDevice.hardwareId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plejd API properties parsed
|
||||||
|
*
|
||||||
|
* * `devices` - physical Plejd devices, duplicated for devices with multiple outputs
|
||||||
|
* devices: [{deviceId, title, objectId, ...}, {...}]
|
||||||
|
* * `deviceAddress` - BLE address of each physical device
|
||||||
|
* deviceAddress: {[deviceId]: bleDeviceId}
|
||||||
|
* * `outputSettings` - lots of info about load settings, also links devices to output index
|
||||||
|
* outputSettings: [{deviceId, output, deviceParseId, ...}] //deviceParseId === objectId above
|
||||||
|
* * `outputAddress`: BLE address of [0] main output and [n] other output (loads)
|
||||||
|
* outputAddress: {[deviceId]: {[output]: bleDeviceId}}
|
||||||
|
* * `inputSettings` - detailed settings for inputs (buttons, RTR-01, ...), scenes triggered, ...
|
||||||
|
* inputSettings: [{deviceId, input, ...}] //deviceParseId === objectId above
|
||||||
|
* * `inputAddress` - Links inputs to what BLE device they control, or 255 for unassigned/scene
|
||||||
|
* inputAddress: {[deviceId]: {[input]: bleDeviceId}}
|
||||||
|
*/
|
||||||
_getPlejdDevices() {
|
_getPlejdDevices() {
|
||||||
this.deviceRegistry.clearPlejdDevices();
|
this.deviceRegistry.clearPlejdDevices();
|
||||||
|
|
||||||
this.siteDetails.devices.forEach((device) => {
|
this.siteDetails.devices.forEach((device) => {
|
||||||
const { deviceId } = device;
|
const outputSettings = this.siteDetails.outputSettings.find(
|
||||||
|
|
||||||
const settings = this.siteDetails.outputSettings.find(
|
|
||||||
(x) => x.deviceParseId === device.objectId,
|
(x) => x.deviceParseId === device.objectId,
|
||||||
);
|
);
|
||||||
|
|
||||||
let deviceNum = this.siteDetails.deviceAddress[deviceId];
|
if (device.traits === TRAITS.NO_LOAD) {
|
||||||
|
logger.warn(
|
||||||
|
`Device ${device.title} (${device.deviceId}) has no load configured and will be excluded`,
|
||||||
|
);
|
||||||
|
} else if (outputSettings) {
|
||||||
|
const uniqueOutputId = this.deviceRegistry.getUniqueOutputId(
|
||||||
|
device.deviceId,
|
||||||
|
outputSettings.output,
|
||||||
|
);
|
||||||
|
|
||||||
if (settings) {
|
const bleDeviceIndex = this.siteDetails.outputAddress[device.deviceId][
|
||||||
const outputs = this.siteDetails.outputAddress[deviceId];
|
outputSettings.output
|
||||||
deviceNum = outputs[settings.output];
|
];
|
||||||
}
|
|
||||||
|
|
||||||
// check if device is dimmable
|
const plejdDevice = this.siteDetails.plejdDevices.find(
|
||||||
const plejdDevice = this.siteDetails.plejdDevices.find((x) => x.deviceId === deviceId);
|
(x) => x.deviceId === device.deviceId,
|
||||||
const deviceType = this._getDeviceType(plejdDevice.hardwareId);
|
);
|
||||||
const { name, type } = deviceType;
|
|
||||||
let { dimmable } = deviceType;
|
|
||||||
|
|
||||||
if (settings) {
|
const dimmable = device.traits === TRAITS.DIMMABLE;
|
||||||
dimmable = settings.dimCurve !== 'NonDimmable';
|
// dimmable = settings.dimCurve !== 'NonDimmable';
|
||||||
}
|
|
||||||
|
|
||||||
const newDevice = {
|
const { name: typeName, type } = this._getDeviceType(plejdDevice);
|
||||||
id: deviceNum,
|
|
||||||
name: device.title,
|
|
||||||
type,
|
|
||||||
typeName: name,
|
|
||||||
dimmable,
|
|
||||||
roomId: device.roomId,
|
|
||||||
version: plejdDevice.firmware.version,
|
|
||||||
serialNumber: plejdDevice.deviceId,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (newDevice.typeName === 'WPH-01') {
|
/** @type {import('types/DeviceRegistry').OutputDevice} */
|
||||||
// WPH-01 is special, it has two buttons which needs to be
|
const outputDevice = {
|
||||||
// registered separately.
|
bleDeviceIndex,
|
||||||
const inputs = this.siteDetails.inputAddress[deviceId];
|
deviceId: device.deviceId,
|
||||||
const first = inputs[0];
|
dimmable,
|
||||||
const second = inputs[1];
|
hiddenFromRoomList: device.hiddenFromRoomList,
|
||||||
|
hiddenFromIntegrations: device.hiddenFromIntegrations,
|
||||||
|
name: device.title,
|
||||||
|
output: outputSettings.output,
|
||||||
|
roomId: device.roomId,
|
||||||
|
state: undefined,
|
||||||
|
type,
|
||||||
|
typeName,
|
||||||
|
version: plejdDevice.firmware.version,
|
||||||
|
uniqueId: uniqueOutputId,
|
||||||
|
};
|
||||||
|
|
||||||
this.deviceRegistry.addPlejdDevice({
|
this.deviceRegistry.addOutputDevice(outputDevice);
|
||||||
...newDevice,
|
|
||||||
id: first,
|
|
||||||
name: `${device.title} left`,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.deviceRegistry.addPlejdDevice({
|
|
||||||
...newDevice,
|
|
||||||
id: second,
|
|
||||||
name: `${device.title} right`,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
this.deviceRegistry.addPlejdDevice(newDevice);
|
logger.warn(
|
||||||
|
`No outputSettings found for ${device.title} (${device.deviceId}), device will not be included`,
|
||||||
|
);
|
||||||
|
logger.verbose(
|
||||||
|
'Fallback cound potentially be implemented by assuming default deviceSettings[deviceId]',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// What should we do with inputs?!
|
||||||
|
// if (outputDevice.typeName === 'WPH-01') {
|
||||||
|
// // WPH-01 is special, it has two buttons which needs to be
|
||||||
|
// // registered separately.
|
||||||
|
// const inputs = this.siteDetails.inputAddress[deviceId];
|
||||||
|
// const first = inputs[0];
|
||||||
|
// const second = inputs[1];
|
||||||
|
|
||||||
|
// this.deviceRegistry.addPlejdDevice({
|
||||||
|
// ...outputDevice,
|
||||||
|
// id: first,
|
||||||
|
// name: `${device.title} left`,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// this.deviceRegistry.addPlejdDevice({
|
||||||
|
// ...outputDevice,
|
||||||
|
// id: second,
|
||||||
|
// name: `${device.title} right`,
|
||||||
|
// });
|
||||||
|
// } else {
|
||||||
|
// this.deviceRegistry.addPlejdDevice(outputDevice);
|
||||||
|
// }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -332,20 +400,31 @@ class PlejdApi {
|
||||||
const { roomId } = room;
|
const { roomId } = room;
|
||||||
const roomAddress = this.siteDetails.roomAddress[roomId];
|
const roomAddress = this.siteDetails.roomAddress[roomId];
|
||||||
|
|
||||||
const deviceIdsByRoom = this.deviceRegistry.getDeviceIdsByRoom(roomId);
|
const deviceIdsByRoom = this.deviceRegistry.getOutputDeviceIdsByRoomId(roomId);
|
||||||
|
|
||||||
const dimmable = deviceIdsByRoom
|
const dimmable = deviceIdsByRoom
|
||||||
&& deviceIdsByRoom.some((deviceId) => this.deviceRegistry.getDevice(deviceId).dimmable);
|
&& deviceIdsByRoom.some(
|
||||||
|
(deviceId) => this.deviceRegistry.getOutputDevice(deviceId).dimmable,
|
||||||
|
);
|
||||||
|
|
||||||
|
/** @type {import('types/DeviceRegistry').OutputDevice} */
|
||||||
const newDevice = {
|
const newDevice = {
|
||||||
id: roomAddress,
|
bleDeviceIndex: roomAddress,
|
||||||
|
deviceId: null,
|
||||||
|
dimmable,
|
||||||
|
hiddenFromRoomList: false,
|
||||||
|
hiddenFromIntegrations: false,
|
||||||
name: room.title,
|
name: room.title,
|
||||||
|
output: undefined,
|
||||||
|
roomId,
|
||||||
|
state: undefined,
|
||||||
type: 'light',
|
type: 'light',
|
||||||
typeName: 'Room',
|
typeName: 'Room',
|
||||||
dimmable,
|
uniqueId: roomId,
|
||||||
|
version: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.deviceRegistry.addRoomDevice(newDevice);
|
this.deviceRegistry.addOutputDevice(newDevice);
|
||||||
});
|
});
|
||||||
logger.debug('includeRoomsAsLights done.');
|
logger.debug('includeRoomsAsLights done.');
|
||||||
}
|
}
|
||||||
|
|
@ -357,14 +436,20 @@ class PlejdApi {
|
||||||
|
|
||||||
scenes.forEach((scene) => {
|
scenes.forEach((scene) => {
|
||||||
const sceneNum = this.siteDetails.sceneIndex[scene.sceneId];
|
const sceneNum = this.siteDetails.sceneIndex[scene.sceneId];
|
||||||
|
/** @type {import('types/DeviceRegistry').OutputDevice} */
|
||||||
const newScene = {
|
const newScene = {
|
||||||
id: sceneNum,
|
bleDeviceIndex: sceneNum,
|
||||||
|
deviceId: undefined,
|
||||||
|
dimmable: false,
|
||||||
|
hiddenFromSceneList: scene.hiddenFromSceneList,
|
||||||
name: scene.title,
|
name: scene.title,
|
||||||
|
output: undefined,
|
||||||
|
roomId: undefined,
|
||||||
|
state: false,
|
||||||
type: 'switch',
|
type: 'switch',
|
||||||
typeName: 'Scene',
|
typeName: 'Scene',
|
||||||
dimmable: false,
|
version: undefined,
|
||||||
version: '1.0',
|
uniqueId: scene.sceneId,
|
||||||
serialNumber: scene.objectId,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.deviceRegistry.addScene(newScene);
|
this.deviceRegistry.addScene(newScene);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const EventEmitter = require('events');
|
const { EventEmitter } = require('events');
|
||||||
const Configuration = require('./Configuration');
|
const Configuration = require('./Configuration');
|
||||||
const constants = require('./constants');
|
const constants = require('./constants');
|
||||||
const Logger = require('./Logger');
|
const Logger = require('./Logger');
|
||||||
|
|
@ -15,6 +15,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
bleDeviceTransitionTimers = {};
|
bleDeviceTransitionTimers = {};
|
||||||
plejdBleHandler;
|
plejdBleHandler;
|
||||||
config;
|
config;
|
||||||
|
/** @type {import('./DeviceRegistry')} */
|
||||||
deviceRegistry;
|
deviceRegistry;
|
||||||
writeQueue = [];
|
writeQueue = [];
|
||||||
writeQueueRef = null;
|
writeQueueRef = null;
|
||||||
|
|
@ -71,7 +72,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
turnOn(deviceId, command) {
|
turnOn(deviceId, command) {
|
||||||
const deviceName = this.deviceRegistry.getDeviceName(deviceId);
|
const deviceName = this.deviceRegistry.getOutputDeviceName(deviceId);
|
||||||
logger.info(
|
logger.info(
|
||||||
`Plejd got turn on command for ${deviceName} (${deviceId}), brightness ${command.brightness}${
|
`Plejd got turn on command for ${deviceName} (${deviceId}), brightness ${command.brightness}${
|
||||||
command.transition ? `, transition: ${command.transition}` : ''
|
command.transition ? `, transition: ${command.transition}` : ''
|
||||||
|
|
@ -81,7 +82,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
turnOff(deviceId, command) {
|
turnOff(deviceId, command) {
|
||||||
const deviceName = this.deviceRegistry.getDeviceName(deviceId);
|
const deviceName = this.deviceRegistry.getOutputDeviceName(deviceId);
|
||||||
logger.info(
|
logger.info(
|
||||||
`Plejd got turn off command for ${deviceName} (${deviceId})${
|
`Plejd got turn off command for ${deviceName} (${deviceId})${
|
||||||
command.transition ? `, transition: ${command.transition}` : ''
|
command.transition ? `, transition: ${command.transition}` : ''
|
||||||
|
|
@ -93,18 +94,18 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
_bleCommandReceived(deviceId, command, data) {
|
_bleCommandReceived(deviceId, command, data) {
|
||||||
try {
|
try {
|
||||||
if (command === COMMANDS.DIM) {
|
if (command === COMMANDS.DIM) {
|
||||||
this.deviceRegistry.setState(deviceId, data.state, data.dim);
|
this.deviceRegistry.setOutputState(deviceId, data.state, data.dim);
|
||||||
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
||||||
state: data.state,
|
state: !!data.state,
|
||||||
brightness: data.dim,
|
brightness: data.dim,
|
||||||
});
|
});
|
||||||
} else if (command === COMMANDS.TURN_ON) {
|
} else if (command === COMMANDS.TURN_ON) {
|
||||||
this.deviceRegistry.setState(deviceId, 1);
|
this.deviceRegistry.setOutputState(deviceId, true);
|
||||||
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
||||||
state: 1,
|
state: 1,
|
||||||
});
|
});
|
||||||
} else if (command === COMMANDS.TURN_OFF) {
|
} else if (command === COMMANDS.TURN_OFF) {
|
||||||
this.deviceRegistry.setState(deviceId, 0);
|
this.deviceRegistry.setOutputState(deviceId, false);
|
||||||
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
this.emit(PlejdDeviceCommunication.EVENTS.stateChanged, deviceId, {
|
||||||
state: 0,
|
state: 0,
|
||||||
});
|
});
|
||||||
|
|
@ -125,11 +126,11 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
_transitionTo(deviceId, targetBrightness, transition, deviceName) {
|
_transitionTo(deviceId, targetBrightness, transition, deviceName) {
|
||||||
const device = this.deviceRegistry.getDevice(deviceId);
|
const device = this.deviceRegistry.getOutputDevice(deviceId);
|
||||||
const initialBrightness = device ? device.state && device.dim : null;
|
const initialBrightness = device ? device.state && device.dim : null;
|
||||||
this._clearDeviceTransitionTimer(deviceId);
|
this._clearDeviceTransitionTimer(deviceId);
|
||||||
|
|
||||||
const isDimmable = this.deviceRegistry.getDevice(deviceId).dimmable;
|
const isDimmable = this.deviceRegistry.getOutputDevice(deviceId).dimmable;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
transition > 1
|
transition > 1
|
||||||
|
|
@ -249,7 +250,7 @@ class PlejdDeviceCommunication extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const queueItem = this.writeQueue.pop();
|
const queueItem = this.writeQueue.pop();
|
||||||
const deviceName = this.deviceRegistry.getDeviceName(queueItem.deviceId);
|
const deviceName = this.deviceRegistry.getOutputDeviceName(queueItem.deviceId);
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Write queue: Processing ${deviceName} (${queueItem.deviceId}). Command ${
|
`Write queue: Processing ${deviceName} (${queueItem.deviceId}). Command ${
|
||||||
queueItem.command
|
queueItem.command
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue