-
Notifications
You must be signed in to change notification settings - Fork 14
/
sunrise.js
54 lines (51 loc) · 1.4 KB
/
sunrise.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
// Original by kevincoleman with modifications by stockmopar
// requires & definitions
var TCPConnected = require('./index.js');
Sunrise = new TCPConnected("192.168.1.137");
//user vars
var room = 'Living Room';
var fadeDuration = 1;
var step = 4;
var sunrise;
// script
var updateFadeLevel = function() {
Sunrise.GetState(function(error,system){
Sunrise.GetRoomStateByName(room, function(error,state,level){
if(state == 0){
Sunrise.TurnOnRoomWithLevelByName(room, 1, function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
});
}else{
if (level >= 100) {
console.log('Lights are all the way on.');
clearInterval(sunrise);
Sunrise.GWEnd();
} else {
Sunrise.SetRoomLevelByName(room, Math.min(100,level+step), function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
} );
}
}
});
});
}
// run the script
Sunrise.Init(function(error){
if(!error){
Sunrise.GetState(function(error,system){
Sunrise.GetRoomStateByName(room, function(error,state,level){
console.log(state);
if(state == 1){
console.log("Light is on. Turn it off first.");
Sunrise.TurnOffRoomByName(room, function(){
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
});
}else{
sunrise = setTimeout(updateFadeLevel, (fadeDuration * 60));
}
});
});
}else{
console.log("There was an issue initializing the token");
}
});