-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.js
161 lines (131 loc) · 4.36 KB
/
config.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// TODO: separate all classes
const sleep = require('sleep-promise');
const { DateTime } = require('luxon');
const Condition = require('./condition');
const Device = require('./device');
const config = require(`${process.cwd()}/config.json`);
class Mode {
constructor(name, config) {
this.name = name;
this.features = config;
this.interval = config.interval || 1000;
this.devices = [];
this._dependents = undefined;
}
get dependents() {
if (this._dependents !== undefined) {
return this._dependents;
}
let depends = Object.keys(this.features)
.reduce((conds, key) => {
const current = this.features[key];
if (current.conditions === undefined) {
return conds;
}
const keys = current.conditions.map(Object.keys);
conds = conds.concat(...keys, key);
return conds;
}, [])
.filter(feature => Device.FEATURES.includes(feature));
depends = Array.from(new Set(depends));
this._dependents = depends;
return depends;
}
addDevices(...devices) {
const { dependents } = this;
devices.forEach(device => {
device.setParentMode(this);
device.setPollingInterval(this.interval);
device.subscribe(...dependents);
});
this.devices = this.devices.concat(devices);
}
get actions() {
const actions = Object.keys(this.features)
.reduce((acts, feature) => {
if (this.features[feature].conditions === undefined) {
return acts;
}
const { action, conditions } = this.features[feature];
const conds = conditions
.map(cond => ({ action: cond.action, testers: Condition.fromConfig(cond), feature }));
acts.push({
feature,
action,
conditions: conds,
});
return acts;
}, []);
return actions;
}
get defaults() {
return Object.keys(this.features).reduce((defaults, current) => {
if (this.dependents.includes(current)) {
defaults[current] = [this.features[current].default] || null;
}
return defaults;
}, {});
}
async loop() {
const { actions, defaults, dependents } = this;
const form = dependents
.filter(item => Device.CHANGABLES.includes(item))
.reduce((previous, current) => ({ ...previous, [current]: null }), {});
for (;;) {
// better ideas?
// load all device information from depdents
const time = DateTime.local();
const devices = await Promise.all(this.devices.map(async (device) => ({
device,
data: {
...device.stats,
time,
},
})));
// try to match
for (const info of devices) {
const { device, data } = info;
const changes = { ...form };
for (const action of actions) {
const { conditions, feature } = action;
for (const condition of conditions) {
const tests = condition.testers.map(tester => tester.test(data));
const yet = tests.some(result => result === null);
if (yet === true) {
delete changes[feature];
continue;
}
const passed = tests.every(result => result === true);
if (passed === true) {
changes[feature] = condition;
}
}
}
// apply changes
const promises = [];
for (const [feature, condition] of Object.entries(changes)) {
const next = condition !== null ? condition.action : null;
if (next === null && defaults[feature] !== null && data[feature] !== defaults[feature]) {
promises.push(device.update(feature, ...defaults[feature]));
} else {
promises.push(device.update(feature, ...next));
}
}
await Promise.all(promises);
}
await sleep(this.interval);
}
}
}
class Config {
constructor(config) {
this.devices = config.devices;
this.discover = config.discover;
this.modes = Object.keys(config.modes).reduce((modes, current) => {
const mode = new Mode(current, config.modes[current]);
modes.push(mode);
return modes;
}, []);
}
}
module.exports = Object.freeze(new Config(config));