Improve SceneManager structure and use central DeviceRegistry

This commit is contained in:
Victor Hagelbäck 2021-02-01 21:24:27 +01:00
parent 377c6a75db
commit c5ee71d503

View file

@ -1,48 +1,54 @@
/* eslint-disable max-classes-per-file */
const EventEmitter = require('events'); const EventEmitter = require('events');
const Logger = require('./Logger');
const Scene = require('./Scene'); const Scene = require('./Scene');
const logger = Logger.getLogger('scene-manager');
class SceneManager extends EventEmitter { class SceneManager extends EventEmitter {
constructor(site, devices) { deviceRegistry;
plejdBle;
scenes;
constructor(deviceRegistry, plejdBle) {
super(); super();
this.site = site; this.deviceRegistry = deviceRegistry;
this.scenes = []; this.plejdBle = plejdBle;
this.devices = devices; this.scenes = {};
this.init();
} }
init() { init() {
const scenes = this.site.scenes.filter((x) => x.hiddenFromSceneList === false); const scenes = this.deviceRegistry.apiSite.scenes.filter(
// eslint-disable-next-line no-restricted-syntax (x) => x.hiddenFromSceneList === false,
for (const scene of scenes) { );
const idx = this.site.sceneIndex[scene.sceneId];
this.scenes.push(new Scene(idx, scene, this.site.sceneSteps)); this.scenes = {};
} scenes.forEach((scene) => {
const idx = this.deviceRegistry.apiSite.sceneIndex[scene.sceneId];
this.scenes[scene.sceneId] = new Scene(idx, scene, this.deviceRegistry.apiSite.sceneSteps);
});
} }
executeScene(sceneIndex, ble) { executeScene(sceneIndex) {
const scene = this.scenes.find((x) => x.id === sceneIndex); const scene = this.scenes[sceneIndex];
if (!scene) { if (!scene) {
logger.info(`Scene with id ${sceneIndex} not found`);
logger.verbose(`Scenes: ${JSON.stringify(this.scenes, null, 2)}`);
return; return;
} }
// eslint-disable-next-line no-restricted-syntax scene.steps.forEach((step) => {
for (const step of scene.steps) { const device = this.deviceRegistry.getDeviceBySerialNumber(step.deviceId);
const device = this.devices.find((x) => x.serialNumber === step.deviceId);
if (device) { if (device) {
if (device.dimmable && step.state) { if (device.dimmable && step.state) {
ble.turnOn(device.id, { brightness: step.brightness }); this.plejdBle.turnOn(device.id, { brightness: step.brightness });
} else if (!device.dimmable && step.state) { } else if (!device.dimmable && step.state) {
ble.turnOn(device.id, {}); this.plejdBle.turnOn(device.id, {});
} else if (!step.state) { } else if (!step.state) {
ble.turnOff(device.id, {}); this.plejdBle.turnOff(device.id, {});
} }
} }
} });
} }
} }
module.exports = SceneManager; module.exports = SceneManager;
/* eslint-disable */