2021-02-01 21:24:27 +01:00
|
|
|
const Logger = require('./Logger');
|
2021-01-22 15:49:02 +01:00
|
|
|
const Scene = require('./Scene');
|
|
|
|
|
|
2021-02-01 21:24:27 +01:00
|
|
|
const logger = Logger.getLogger('scene-manager');
|
2021-02-20 15:33:06 +01:00
|
|
|
class SceneManager {
|
2021-03-31 20:04:45 +02:00
|
|
|
/** @private @type {import('./DeviceRegistry')} */
|
2021-02-01 21:24:27 +01:00
|
|
|
deviceRegistry;
|
2021-03-31 20:04:45 +02:00
|
|
|
/** @private @type {import('./PlejdDeviceCommunication')} */
|
|
|
|
|
plejdDeviceCommunication;
|
|
|
|
|
/** @private @type {Object.<number,Scene>} */
|
2021-02-01 21:24:27 +01:00
|
|
|
scenes;
|
2021-01-22 15:49:02 +01:00
|
|
|
|
2021-03-31 20:04:45 +02:00
|
|
|
constructor(deviceRegistry, plejdDeviceCommunication) {
|
2021-02-01 21:24:27 +01:00
|
|
|
this.deviceRegistry = deviceRegistry;
|
2021-03-31 20:04:45 +02:00
|
|
|
this.plejdDeviceCommunication = plejdDeviceCommunication;
|
2021-02-01 21:24:27 +01:00
|
|
|
this.scenes = {};
|
2021-01-22 15:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {
|
2021-03-31 20:04:45 +02:00
|
|
|
const scenes = this.deviceRegistry
|
|
|
|
|
.getApiSite()
|
|
|
|
|
.scenes.filter((x) => x.hiddenFromSceneList === false);
|
2021-02-01 21:24:27 +01:00
|
|
|
|
|
|
|
|
this.scenes = {};
|
|
|
|
|
scenes.forEach((scene) => {
|
2021-03-31 20:04:45 +02:00
|
|
|
const idx = this.deviceRegistry.getApiSite().sceneIndex[scene.sceneId];
|
|
|
|
|
this.scenes[idx] = new Scene(this.deviceRegistry, idx, scene);
|
2021-02-01 21:24:27 +01:00
|
|
|
});
|
2021-01-22 15:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-08 19:55:18 +01:00
|
|
|
executeScene(sceneId) {
|
|
|
|
|
const scene = this.scenes[sceneId];
|
2021-01-22 15:49:02 +01:00
|
|
|
if (!scene) {
|
2021-02-08 19:55:18 +01:00
|
|
|
logger.info(`Scene with id ${sceneId} not found`);
|
2021-02-01 21:24:27 +01:00
|
|
|
logger.verbose(`Scenes: ${JSON.stringify(this.scenes, null, 2)}`);
|
2021-01-22 15:49:02 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 21:24:27 +01:00
|
|
|
scene.steps.forEach((step) => {
|
2021-03-31 20:04:45 +02:00
|
|
|
const uniqueId = this.deviceRegistry.getUniqueOutputId(step.deviceId, step.output);
|
|
|
|
|
const device = this.deviceRegistry.getOutputDevice(uniqueId);
|
2021-01-22 15:49:02 +01:00
|
|
|
if (device) {
|
|
|
|
|
if (device.dimmable && step.state) {
|
2021-03-31 20:04:45 +02:00
|
|
|
this.plejdDeviceCommunication.turnOn(uniqueId, { brightness: step.brightness });
|
2021-01-22 15:49:02 +01:00
|
|
|
} else if (!device.dimmable && step.state) {
|
2021-03-31 20:04:45 +02:00
|
|
|
this.plejdDeviceCommunication.turnOn(uniqueId, {});
|
2021-01-22 15:49:02 +01:00
|
|
|
} else if (!step.state) {
|
2021-03-31 20:04:45 +02:00
|
|
|
this.plejdDeviceCommunication.turnOff(uniqueId, {});
|
2021-01-22 15:49:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-01 21:24:27 +01:00
|
|
|
});
|
2021-01-22 15:49:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = SceneManager;
|