-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (115 loc) · 3.79 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
'use strict';
var _ = require('underscore');
var HTTP = require('q-io/http');
var config = require('./config.js');
var beam = require('./example.json');
var math = require('mathjs');
var lights;
var isDynamic = function (state){
for(var property in state) {
if (property === 'bri'){
if (typeof state[property] === 'string') {
return true;
}
}
}
return false;
};
var calculateProperty = function(state, property, t){
if (typeof state[property] === 'string') {
var scope = {
t: t,
};
return math.eval(state[property], scope);
} else {
return state[property];
}
};
var calculateState = function(state, t){
console.log('t:', t);
var calculatedState = {};
var calculatables = ['bri'];
_.each(Object.keys(state), function(key){
// property saturation
// property color
if (_.contains(calculatables, key)){
calculatedState[key] = Math.round(calculateProperty(state, key, t));
} else {
calculatedState[key] = state[key];
}
});
return JSON.stringify(calculatedState);
};
var buildRequest = function(command, bulb, t, transistiontime){
var state = calculateState(command.state,t );
state[transistiontime] = transistiontime;
var body = [state];
console.log('building ' , body , ' to light ' , bulb);
return {
'host': config.host,
'path': '/api/' + config.username + '/lights/' + bulb + '/state',
'method': 'PUT',
'body': body
};
};
var runCommand = function(command, bulbs, t, transistiontime) {
_.each(bulbs, function(bulb){
HTTP.request(buildRequest(command, bulb, t, transistiontime));
});
};
var nextCommand = function(ray, current, currentStarted){
var command = ray.commands[current];
if (isDynamic(command.state)) {
console.log('dynamic state');
return function(){
if (command.for !== null && command.for !== 0){
if (new Date().getTime() >= (currentStarted.getTime() + command.for)){
console.log('moving on');
current = current +1;
currentStarted = new Date();
}
}
console.log('then dynamic tick');
nextTick(ray, current, currentStarted);
};
} else {
return function(){
console.log('command:', command);
setTimeout(function(){
console.log('then static tick, current:', current);
current = current +1;
currentStarted = new Date();
nextTick(ray, current, currentStarted);
}, command.for); //todo: it should be (started + for -now)
};
}
};
var nextTick = function (ray, current, currentStarted) {
console.log('next tick');
current = current % ray.commands.length;
// max 10 commands per second
// http://www.developers.meethue.com/documentation/core-concepts
var timeout = 100 * lights.length;
var transistiontime = timeout / 100;
var t = new Date() - currentStarted;
runCommand(ray.commands[current], lights, t, transistiontime);
var next = nextCommand(ray, current, currentStarted);
setTimeout(function(){
next();
}, timeout);
};
var selectBulbs = function(){
// "all",
// "groups" : ["living"],
// "requires" : ["color"],
return [1,2];
};
exports.run = function(ray, bridge) {
console.log('Running beam ', ray, ' on ', bridge);
console.log('With config ', config);
lights = selectBulbs(ray.lights, {});
nextTick(ray, 0, new Date());
};
exports.beam = beam;
exports.config = config;
exports.run(beam, config.host);