-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
317 lines (266 loc) · 8.14 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const request = require('request-promise-native');
const hue = require('node-hue-api');
const utils = require('./src/utils');
const fs = require('fs');
const argv = process.argv;
const cmd = argv[2];
const configFile = argv.indexOf('--config') < 0 ? undefined : argv[argv.indexOf('--config') + 1];
if (!configFile) {
console.warn('Please provide config via: --config config.json');
return;
}
const callbacks = {};
const isInit = 'init' === cmd;
const config = isInit ? null : require(configFile);
const hueApi = isInit ? null : new hue.HueApi(config.hueBridge.host, config.hueBridge.username);
const findBridge = async () => {
const bridge = await hue.upnpSearch();
return bridge;
}
const triggerLamp = async (inParams) => {
const params = inParams || {};
console.log('triggerLamp: ', params);
if (!!params.delay) {
await utils.delay(params.delay);
}
if (!!params.state) {
await hueApi.setLightState(params.bulb, params.state)
} else {
const offTransition = 400;
const offState = hue.lightState.create().on(false).transition(offTransition);
const retry = params.retry || { count: 1, delay: 500 } ;
const rgb = params.rgb;
const on = params.on || !!rgb;
const onTransition = 200;
let state = hue.lightState.create().on(on).transition(onTransition);
if (on) {
state = state.rgb(rgb.r, rgb.g, rgb.b)
.brightness(params.brightness || 100)
}
for (let i = 0; i < retry.count; i++) {
if (i > 0) {
await hueApi.setLightState(params.bulb, offState);
await utils.delay(retry.delay + offTransition);
}
await hueApi.setLightState(params.bulb, state);
await utils.delay(onTransition);
}
}
};
callbacks['triggerLamp'] = triggerLamp;
const triggerSequence = async (params) => {
const sequence = params.sequence;
for (const idx in sequence) {
const item = sequence[idx]
const cb = item.callback;
const cbParams = item.params;
await callbacks[cb](cbParams);
}
};
callbacks['triggerSequence'] = triggerSequence;
const pingLamp = async (params) => {
const onTransition = 100;
const offTransition = 800;
const on = hue.lightState.create().on().rgb(20, 20, 255).brightness(80).transition(onTransition);
const off = hue.lightState.create().on(false).transition(offTransition);
try {
const originalStatus = await hueApi.lightStatus(params.bulb)
for (let i = 0; i < 4; i++) {
await hueApi.setLightState(params.bulb, on)
await utils.delay(onTransition);
await hueApi.setLightState(params.bulb, off)
await utils.delay(offTransition);
}
if (originalStatus.state.on) {
await hueApi.setLightState(params.bulb, originalStatus.state)
}
} catch (err) {
if (-1 === err.message.search('not available')) {
throw err;
}
const lights = (await hueApi.lights()).lights.map((l) => l.id);
console.log('The light is ' + params.bulb + ' not available.');
console.log('Available lights: ' + lights.join(', '));
}
};
const pingAll = async () => {
const lights = (await hueApi.lights()).lights.map((l) => l.id);
for (const idx in lights) {
const lightID = lights[idx];
console.log('Ping light#' + lightID);
await pingLamp({ bulb: lightID });
}
}
const party = async () => {
const lights = await hueApi.lights()
const states = [
hue.lightState.create().on().rgb(255, 0, 0), // red
hue.lightState.create().on().rgb(0, 255, 0), // green
hue.lightState.create().on().rgb(0, 0, 255), // blue
hue.lightState.create().on().rgb(255, 100, 0),
hue.lightState.create().on().rgb(255, 0, 100),
hue.lightState.create().on().rgb(0, 255, 100),
];
for (let round = 10; round > 0; round--) {
console.log('Rounds left ' + round);
for (let idx in lights.lights) {
const l = lights.lights[idx];
const rndIdx = Math.round(Math.random() * 1000) % states.length;
const state = states[rndIdx];
await hueApi.setLightState(l.id, state);
await utils.delay(150);
}
}
}
const runState = async (entry, stateName) => {
const states = config.states;
let state = entry.states[stateName];
if (!state) {
state = entry.states['*'];
}
state = utils.deepOverride(states[state.parent], state);
await callbacks[state.callback](state.params);
}
const run = async (params) => {
console.log('Run with params: ', params)
Object.keys(config.checkers).forEach(async (entryKey) => {
if (undefined !== params.check && entryKey !== params.check) {
return;
}
const entry = config.checkers[entryKey];
console.log('Processing #' + entryKey + ': ' + entry.name)
if (!!params.state) {
await runState(entry, params.state);
return;
}
const module = require('./src/modules/' + entry.module);
const moduleConfig = utils.deepOverride(config.configs[entry.config.parent], entry.config);
const result = await module(moduleConfig);
if (params.moduleOnly) {
console.log('Module only result');
console.log(result);
return;
}
const resultState = result.state;
await runState(entry, result.state);
});
}
const init = async (configFilename) => {
if(fs.existsSync(configFilename)) {
console.error('Config file ' + configFilename + ' exists. Please provide another name');
return;
}
const newConfig = {
hueBridge: {
host: undefined,
username: undefined,
},
states: {
fatal: {
callback: 'triggerLamp',
params: {
rgb: { r: 255, g: 0, b: 0},
brightness: 100,
retry: { count: 12, delay: 100 },
},
},
error: {
callback: 'triggerLamp',
params: {
rgb: { r: 255, g: 0, b: 0},
brightness: 50,
}
},
'*': {
callback: 'triggerLamp',
}
},
configs: {
sentry: {
token: '...',
user: '...',
timeout: 2,
}
},
checkers: {
ios: {
name: 'Sentry ios',
module: 'sentry',
config: {
'parent': 'sentry',
'project': '...'
},
'states': {
fatal: {
parent: 'fatal',
params: { bulb: 8 }
},
error: {
parent: 'error',
params: { bulb: 8 }
},
'*': {
parent: '*',
params: { bulb: 8 }
},
test: {
callback: 'triggerSequence',
params: {
sequence: [
{
// will turn on green for light 8
callback: 'triggerLamp',
params: {
on: true,
rgb: { r: 0, g: 255, b: 0 },
brightness: 30,
bulb: 8
}
},
{
// will turn off the 8 light after 5 seconds
callback: 'triggerLamp',
params: {
delay: 5000,
bulb: 8
}
},
]
}
}
}
}
}
};
const bridge = await findBridge();
newConfig.hueBridge.host = bridge[0].ipaddress;
const api = new hue.HueApi();
for (let i = 0; i < 12; i++) {
await utils.delay(2000);
try {
const registration = await api.registerUser(newConfig.hueBridge.host, 'light_notifier/1');
newConfig.hueBridge.username = registration;
break;
} catch (err) {
console.log('Please, press the button on the bridge');
}
}
const json = JSON.stringify(newConfig, null, 2);
fs.writeFileSync(configFilename, json);
console.log('All done, please modify the config file (' + configFilename + ') and enjoy your new light notifications');
};
if ('ping' === cmd) {
pingLamp({ bulb: argv[3] })
} else if ('pingAll' === cmd) {
pingAll();
} else if ('party' === cmd) {
party();
} else if ('init' === cmd) {
init(configFile);
} else {
run({
check: argv.indexOf('--check') < 0 ? undefined : argv[argv.indexOf('--check') + 1],
moduleOnly: argv.indexOf('--module-only') > -1 ? true : false,
state: argv.indexOf('--state') < 0 ? undefined : argv[argv.indexOf('--state') + 1],
});
}