This repository was archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
525 lines (435 loc) · 13 KB
/
Copy pathserver.js
File metadata and controls
525 lines (435 loc) · 13 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// DO NOT EDIT THIS FILE, unless you know what you are doing.
// Load config
var config = require('./config');
// Load web app
var express = require('express');
var path = require('path');
var app = express();
var http = require('http').Server(app);
// Load milight and others
var Milight;
var commands;
var light;
var zone = 0;
// ejs
console.log("Setting up EJS...");
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
// Keeping state of lights
var moodMode = false;
var moodCounter = 50;
var moodInterval = null;
var moodIncrement = false;
var MOOD_MAX = 50;
var MOOD_MIN = 0;
var MOOD_INTERVAL_MS = 1000;
var MOOD_CHANGE_PER_INT = 1; // MUST BE 1, because user can choose hue of any multiple of 1!
// Updates
var UPDATE_CHECK_INTERVAL_MS = 1 * 60 * 60 * 1000; // Default: every hour
var updateAvailable = false;
// Keeps last CCT value
// TODO array of CCT per zone
var cct = 0;
// Keep lightsOn state by zone inside an array
var lightsOn = [];
for(i = 0; i < 5; i++)
{
lightsOn[i] = false;
}
var lastHue = -1;
// Don't force check updates cache as it will make page load slower
app.get('/', function(req, res) {
res.render('index.html', { zones: config.zones,
hasNewVersion: hasNewVersion(false) });
});
// TODO
app.get('/update', function(req, res) {
var lastCommit = getLastCommitId();
var latestVersion = getLatestVersionId();
res.render('update.html', { version: lastCommit,
latestVersion: latestVersion,
hasNewVersion: hasNewVersion(true) });
});
// Force update cache on about page, it's fine for it to be slower
app.get('/api', function(req, res) {
var lastCommit = getLastCommitId();
var latestVersion = getLatestVersionId();
res.render('about.html', { version: lastCommit,
latestVersion: latestVersion,
hasNewVersion: hasNewVersion(true) });
});
app.get('/disco', function(req, res) {
res.render('disco.html', { zones: config.zones });
});
app.get('/mood', function(req, res) {
res.render('mood.html');
});
app.get('/alarm', function(req, res) {
res.render('alarm.html', { zones: config.zones });
});
///////////////////
// API FUNCTIONS //
///////////////////
// mood mode
app.get('/api/mood', function(req, res) {
console.log("Mood mode");
// Set min and max hues
MOOD_MIN = parseInt(req.query.start);
MOOD_MAX = parseInt(req.query.end);
moodCounter = MOOD_MIN;
moodIncrement = true;
console.log("Mood min: " + MOOD_MIN + " - max: " + MOOD_MAX);
if(moodMode)
{
console.log("Turn off mood mode");
if(moodInterval != null)
{
clearInterval(moodInterval);
// Reset to white colour
light.sendCommands(commands.fullColor.whiteTemperature(zone, cct), commands.rgbw.whiteMode(zone));
}
moodMode = false;
}
else {
console.log("Turn on mood mode");
light.sendCommands(commands.fullColor.on(zone), commands.rgbw.on(zone));
moodInterval = setInterval(doMood, MOOD_INTERVAL_MS);
moodMode = true;
}
res.send();
});
function doMood() {
if(moodIncrement)
{
moodCounter += MOOD_CHANGE_PER_INT;
}
else
{
moodCounter -= MOOD_CHANGE_PER_INT;
}
// Set hue direction
if(moodCounter == MOOD_MIN)
{
moodIncrement = true;
}
else if(moodCounter == MOOD_MAX)
{
moodIncrement = false;
}
console.log("domood(): set hue to " + moodCounter + " (D) Increment next time? " + moodIncrement);
light.sendCommands(commands.fullColor.hue(zone, moodCounter), commands.rgbw.hue(zone, moodCounter));
}
app.get('/api/toggle', function(req, res) {
if(lightsOn[zone] == true)
{
console.log("Turn off (toggle)");
light.sendCommands(commands.fullColor.off(zone), commands.rgbw.off(zone), commands.white.off(zone));
}
else {
console.log("Turn on (toggle)");
light.sendCommands(commands.fullColor.on(zone), commands.rgbw.on(zone), commands.white.on(zone));
}
lightsOn[zone] = !lightsOn[zone];
res.send();
});
app.get('/api/start_alarm', function(req, res) {
alarmAt = req.query.wakeUpAt;
minutesToAlarm = req.query.startAt;
startAlarm();
});
app.get('/api/test_alarm', function(req, res) {
console.log("Test alarm");
testAlarm();
res.send();
});
// TODO
app.get('/api/last_hue', function(req, res) {
console.log("GET Last hue");
//light.sendCommands(commands.fullColor.effectModeNext(zone));
//light.sendCommands(commands.rgbw.effectModeNext(zone));
res.send();
});
app.get('/api/mode', function(req, res) {
console.log("Next mode");
light.sendCommands(commands.fullColor.effectModeNext(zone));
light.sendCommands(commands.rgbw.effectModeNext(zone));
res.send();
});
app.get('/api/mode_slow', function(req, res) {
console.log("Mode slower");
for(i = 0; i < 50; i++)
{
light.sendCommands(commands.fullColor.effectSpeedDown(zone));
light.sendCommands(commands.rgbw.effectSpeedDown(zone));
}
res.send();
});
app.get('/api/mode_fast', function(req, res) {
console.log("Mode faster");
for(i = 0; i < 50; i++)
{
light.sendCommands(commands.fullColor.effectSpeedUp(zone));
light.sendCommands(commands.rgbw.effectSpeedUp(zone));
}
res.send();
});
app.get('/api/turn_on', function(req, res) {
console.log('Turn on');
light.sendCommands(commands.fullColor.on(zone), commands.rgbw.on(zone), commands.white.on(zone));
lightsOn[zone] = true;
res.send();
});
app.get('/api/turn_off', function(req, res) {
console.log('Turn off');
light.sendCommands(commands.fullColor.off(zone), commands.rgbw.off(zone), commands.white.off(zone));
lightsOn[zone] = false;
res.send();
});
app.get('/api/night', function(req, res) {
console.log('Night mode');
light.sendCommands(commands.fullColor.nightMode(zone));
res.send();
});
app.get('/api/zone', function(req, res) {
zone = req.query.value;
console.log('Set zone to: ' + zone);
res.send();
});
app.get('/api/alarm_zone', function(req, res) {
alarmZone = req.query.value;
console.log('Set alarm zone to: ' + alarmZone);
res.send();
});
app.get('/api/cct', function(req, res) {
cct = req.query.value;
console.log('Set temperature (CCT) to: ' + cct);
light.sendCommands(commands.fullColor.whiteTemperature(zone, cct), commands.rgbw.whiteMode(zone));
res.send();
});
app.get('/api/brightness', function(req, res) {
var brightness = req.query.value;
console.log('Set brightness to: ' + brightness + '%');
light.sendCommands(commands.fullColor.brightness(zone, brightness), commands.rgbw.brightness(zone, brightness)); //, commands.white.brightness(zone, brightness));
res.send();
});
app.get('/api/saturation', function(req, res) {
var saturation = req.query.value;
console.log('Set saturation to: ' + saturation + '%');
light.sendCommands(commands.fullColor.saturation(zone, saturation));
res.send();
});
app.get('/api/hue', function(req, res) {
var hue = req.query.value;
console.log('Set hue to: ' + hue);
light.sendCommands(commands.fullColor.hue(zone, hue), commands.rgbw.hue(zone, hue));
res.send();
});
app.get('/api/rand_hue', function(req, res) {
var hue = Math.floor((Math.random() * 255) + 1); // Between 1 and 255
console.log('Set RANDOM hue (' + hue + ')');
light.sendCommands(commands.fullColor.hue(zone, hue));
res.send();
});
var static_dir = path.join(__dirname, 'bootstrap');
app.use('/bootstrap', express.static(static_dir));
static_dir = path.join(__dirname, 'js');
app.use('/js', express.static(static_dir));
http.listen(config.nodejs_port, function() {
console.log('Initialising Milight bridge connection (version ' + config.bridge_version + ')');
initIBox();
console.log('Setting up port ' + config.nodejs_port);
console.log('milights-bridge is ready, please open your browser at http://' + getCurrentIP() + ':' + config.nodejs_port);
});
function getCurrentIP()
{
var ip = require("ip");
return ip.address();
}
function initIBox() {
Milight = require('./node_modules/node-milight-promise/src/index').MilightController;
commands = require('./node_modules/node-milight-promise/src/index').commandsV6;
light = new Milight({
ip: config.bridge_ip, //"255.255.255.255",
type: config.bridge_version
});
// Check for updates on startup
// As well as every X ms (see interval variable)
refreshUpdateCache();
setInterval(refreshUpdateCache, UPDATE_CHECK_INTERVAL_MS);
}
/********* ALARM FUNCTIONS ********/
// -- EXTERNAL --
var alarmAt = 0;
var minutesToAlarm = 30;
var alarmZone = 1;
// -- INTERNAL --
// -- Depends on alarm setting, do not touch --
// There are 100 events -- so
// 30 min / 100 events => x MIN there is an event (brightnes++ and colour change)
var msBetweenTimeout = 0;
// In order to cancel timeout if needed
var alarmTimeout = null;
var alarmBrightness = 0; // Percent
var alarmSet = false;
var testMode = false;
function testAlarm() {
testMode = true;
alarmAt = new Date().getTime() + minsToMs(1);
minutesToAlarm = 1;
startAlarm();
testMode = false;
}
function startAlarm() {
if(alarmSet)
{
console.log("FAIL. Alarm already set.");
return;
}
alarmSet = true;
console.log('alarmAt: ' + alarmAt + ' minutesToAlarm: ' + minutesToAlarm + ' alarmZone: ' + alarmZone + ' Server time: ' + new Date());
alarmBrightness = 0;
dimAndTurnOn();
// Calculate the timeout between updates
// To be used in alarmCallback();
var timeout = 10;
if(!testMode)
{
calcMsTimeout();
timeout = getAlarmDelayFromNow();
}
console.log("Timeout to start alarm brightness cycles: " + timeout);
setTimeout(alarmCallback, timeout); ///60000);
}
function dimAndTurnOn() {
// Make sure the light turns on with 0 brightness and in white mode
light.sendCommands(commands.fullColor.whiteTemperature(alarmZone, 20));
light.sendCommands(commands.fullColor.brightness(alarmZone, 0));
light.sendCommands(commands.fullColor.on(alarmZone));
light.sendCommands(commands.fullColor.whiteTemperature(alarmZone, 20));
light.sendCommands(commands.fullColor.brightness(alarmZone, 0));
// Repeat for any other lights
light.sendCommands(commands.white.on(alarmZone));
for(i = 0; i < 10; i++) // White lights have only ten steps and no setTo(X) command
{
light.sendCommands(commands.white.warmer(alarmZone));
light.sendCommands(commands.white.brightDown(alarmZone));
}
// Repeat for any other lights
light.sendCommands(commands.rgbw.brightness(alarmZone, 0));
light.sendCommands(commands.rgbw.on(alarmZone));
light.sendCommands(commands.rgbw.brightness(alarmZone, 0));
}
function alarmCallback() {
console.log("AlarmCallback()");
alarmBrightness++;
light.sendCommands(commands.fullColor.brightness(alarmZone, alarmBrightness),
commands.rgbw.brightness(alarmZone, alarmBrightness));
// White lights have only ten steps and no setTo(X) command
// So instead, for every 10%, make it brighter.
if((alarmBrightness % 10) == 0)
{
light.sendCommands(commands.white.brightUp(alarmZone));
}
if(alarmBrightness < 101)
{
console.log("Incremented brightness to: " + alarmBrightness);
setTimeout(alarmCallback, msBetweenTimeout);
}
else
{
console.log("Alarm finished.");
alarmSet = false;
}
}
function calcMsTimeout() {
// Calculate timeout in mins
msBetweenTimeout = minutesToAlarm / 100;
// Mins to ms
msBetweenTimeout = minsToMs(msBetweenTimeout);
console.log("msBetweenTimeout: " + msBetweenTimeout);
}
function getAlarmToMs() {
alarmAt = alarmAt + '';
var hour = alarmAt.substring(0, 2);
var mins = alarmAt.substring(3, 5);
return minsToMs(hour*60) + minsToMs(mins);
}
function getAlarmStartTimeMs() {
// Alarm finished ms - (minsToAlarm to MS)
return getAlarmToMs() - minsToMs(minutesToAlarm);
}
function getAlarmStartInUnix() {
console.log("test");
var startMs = getAlarmStartTimeMs();
// Today at midnight + start time (ms)
var d = new Date();
d.setHours(0,0,0,0);
var startUnix = d.getTime() + startMs;
if(startUnix < new Date().getTime())
{
// The alarm is meant to be set for tomorrow!
d = new Date();
d.setDate(d.getDate() + 1); // set to tomorrow
d.setHours(0,0,0,0);
startUnix = d.getTime() + startMs;
}
console.log("Alarm start unix detected: " + startUnix);
return startUnix;
}
function getAlarmDelayFromNow() {
// From date timestamp
// To delay in ms
var delay = getAlarmStartInUnix();
delay -= new Date().getTime();
return delay;
}
function minsToMs(mins) {
return mins * 60000;
}
// About page FUNCTIONS
function getLastCommitId() {
var child_process = require('child_process');
if (!child_process.execSync) {
"Error cannot query Github - to be fixed in next release."
}
else {
return child_process
.execSync('git rev-parse HEAD')
.toString().trim();
}
}
function getLatestVersionId() {
var child_process = require('child_process');
if (!child_process.execSync) {
"Error cannot query Github - to be fixed in next release."
}
else {
return child_process
.execSync('git ls-remote https://github.com/KevinVR/milights-bridge.git HEAD')
.toString().trim()
.split(" ")[0];
}
}
function refreshUpdateCache()
{
console.log("Checking for updates...");
var local = getLastCommitId();
var remote = getLatestVersionId();
// Add to cache
updateAvailable = (local != remote);
}
function hasNewVersion(forceCache)
{
if(forceCache)
{
refreshUpdateCache();
}
return updateAvailable;
}
// mood FUNCTIONS
// function playmoodMusic()
// {
// require('child_process')
// .exec("castnow ");
// }