forked from thomasloven/plejd2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.manager.js
72 lines (60 loc) · 1.83 KB
/
scene.manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const EventEmitter = require('events');
const _ = require('lodash');
class SceneManager extends EventEmitter {
constructor(site, devices) {
super();
this.site = site;
this.scenes = [];
this.devices = devices;
this.init();
}
init() {
const scenes = this.site.scenes.filter(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));
}
}
executeScene(sceneIndex, ble) {
const scene = this.scenes.find(x => x.id === sceneIndex);
if (!scene) {
return;
}
for (const step of scene.steps) {
const device = this.devices.find(x => x.serialNumber === step.deviceId);
if (!device) {
continue;
}
if (device.dimmable && step.state) {
ble.turnOn(device.id, { brightness: step.brightness });
}
else if (!device.dimmable && step.state) {
ble.turnOn(device.id, {});
}
else if (!step.state) {
ble.turnOff(device.id, {});
}
}
}
}
class Scene {
constructor(idx, scene, steps) {
this.id = idx;
this.title = scene.title;
this.sceneId = scene.sceneId;
const sceneSteps = steps.filter(x => x.sceneId === scene.sceneId);
this.steps = [];
for (const step of sceneSteps) {
this.steps.push(new SceneStep(step));
}
}
}
class SceneStep {
constructor(step) {
this.sceneId = step.sceneId;
this.deviceId = step.deviceId;
this.state = step.state === 'On' ? 1 : 0;
this.brightness = step.value;
}
}
module.exports = SceneManager;