-
Notifications
You must be signed in to change notification settings - Fork 1
/
hueWrapper.js
232 lines (201 loc) · 7.08 KB
/
hueWrapper.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
var _ = require('underscore'),
util = require('util'),
hue = require("node-hue-api"),
HueApi = hue.HueApi,
LightState = hue.lightState,
logger = require('./logger'),
state = require('./state');
(function(context) {
var HUE_USER_NAME = "lalaautomationserver";
var FALLBACK_HUE_BRIDGE_IP = "10.0.0.30";
var hueBridgeInfo,
hueBridgeDetailedInfo,
hueApi,
lightArray,
lightNamesToIds;
function Light(lightName) {
this.name = lightName;
}
Light.prototype.act = function(commands) {
if (!_.isArray(commands)) {
commands = [commands];
}
var lightId = lightNamesToIds[this.name];
_.each(commands, function(command) {
command.execute(lightId);
});
}
function LightGroup(groupName, lightNames) {
this.name = groupName;
this.lightNames = lightNames;
}
LightGroup.prototype.act = function(commands) {
if (!_.isArray(commands)) {
commands = [commands];
}
logger.i("running commands on light group: " + this.name);
_.each(this.lightNames, function(lightName) {
logger.i("light: " + lightName);
var lightId = lightNamesToIds[lightName];
_.each(commands, function(command) {
command.execute(lightId);
});
});
}
function LightCommand() {}
LightCommand.prototype.execute = function(lightId) {
/** override */
}
function OnLightCommand(transition) {
LightCommand.call(this);
this.transition = transition || 1;
}
OnLightCommand.prototype = new LightCommand();
OnLightCommand.prototype.execute = function(lightId) {
LightCommand.prototype.execute(lightId);
hueApi
.setLightState(lightId, LightState.create()
.on()
.transition(this.transition))
.done();
}
function OffLightCommand(transition) {
LightCommand.call(this);
this.transition = transition || 1;
}
OffLightCommand.prototype = new LightCommand();
OffLightCommand.prototype.execute = function(lightId) {
LightCommand.prototype.execute(lightId);
hueApi
.setLightState(lightId, LightState.create()
.off()
.transition(this.transition))
.done();
}
function ColorLightCommand(color, transition) {
LightCommand.call(this);
this.color = color;
this.transition = transition || 1;
}
ColorLightCommand.prototype = new LightCommand();
ColorLightCommand.prototype.execute = function(lightId) {
LightCommand.prototype.execute(lightId);
hueApi
.setLightState(this.lightId, LightState.create()
.on()
.rgb(this.color.r, this.color.g, this.color.b)
.transition(this.transition))
.done();
}
/** Brightness 0-100. */
function BrightnessLightCommand(brightness, transition) {
LightCommand.call(this);
this.brightness = brightness;
this.transition = transition || 1;
}
BrightnessLightCommand.prototype = new LightCommand();
BrightnessLightCommand.prototype.execute = function(lightId) {
LightCommand.prototype.execute(lightId);
hueApi
.setLightState(lightId, LightState.create()
.on()
.brightness(this.brightness)
.transition(this.transition))
.done();
}
function ColorBrightnessLightCommand(color, brightness, transition) {
LightCommand.call(this);
this.color = color;
this.brightness = brightness;
this.transition = transition || 1;
}
ColorBrightnessLightCommand.prototype = new LightCommand();
ColorBrightnessLightCommand.prototype.execute = function(lightId) {
var brightness = this.brightness;
if (_.isString(brightness)) {
brightness = state.getComputedState(brightness);
}
LightCommand.prototype.execute(lightId);
hueApi
.setLightState(lightId, LightState.create()
.on()
.rgb(this.color.r, this.color.g, this.color.b)
.brightness(brightness)
.transition(this.transition))
.done();
}
function hueBridgeInfoReceived(hueBridges) {
logger.i('Bridge Info Received!');
logger.i(util.format('%d Hue Bridges Found: %s', hueBridges.length, util.inspect(hueBridges)));
var ipaddress = FALLBACK_HUE_BRIDGE_IP;
if (hueBridges.length > 0) {
hueBridgeInfo = hueBridges[0];
ipaddress = hueBridgeInfo.ipaddress;
}
logger.i('Connecting to the HueApi...');
hueApi = new HueApi(ipaddress, HUE_USER_NAME);
hueApi
.connect()
.then(hueApiConnectionSucess)
.done();
};
function hueApiConnectionSucess(_hueBridgeDetailedInfo) {
logger.i('hueApiConnectionSucess!');
hueBridgeDetailedInfo = _hueBridgeDetailedInfo;
logger.i('Fetching Hue light data...');
hueApi
.lights()
.then(hueApiLightDataReceived)
.done();
}
function hueApiLightDataReceived(_hueLightData) {
lightArray = _hueLightData.lights;
logger.i(util.format('Light data received. Found %d lights.', lightArray.length));
populateLightNameHash();
}
function populateLightNameHash() {
lightNamesToIds = {};
_.each(lightArray, function(lightData) {
lightNamesToIds[lightData.name] = lightData.id;
});
}
function turnOnAllLights() {
logger.i(util.format('Turning on all %d lights.', lightArray.length));
for (var i = 1; i <= lightArray.length; i++) {
logger.i('turning on ' + lightArray[i-1].name);
hueApi
.setLightState(i, LightState.create().on())
.done();
}
}
function getLightByName(lightName) {
return new Light(lightName);
}
function init() {
logger.i('Fetching Bridge Info!');
hue.locateBridges()
.then(hueBridgeInfoReceived)
.done();
}
exports.init = init;
exports.turnOnAllLights = turnOnAllLights;
exports.getLightByName = getLightByName;
exports.Light = Light;
exports.LightGroup = LightGroup;
exports.OnLightCommand = OnLightCommand;
exports.OffLightCommand = OffLightCommand;
exports.ColorLightCommand = ColorLightCommand;
exports.BrightnessLightCommand = BrightnessLightCommand;
exports.ColorBrightnessLightCommand = ColorBrightnessLightCommand;
exports.AllLights = new LightGroup("allLights",
["Stairs Bottom",
"Stairs Top",
"Hallway",
"Bedroom Main",
"Bedroom Window",
"Bedroom Spotlight",
"Bathtub",
"Toilet"]);
exports.EntranceGroup = new LightGroup("entranceLights", ["Stairs Bottom", "Stairs Top"]);
exports.BathroomGroup = new LightGroup("bathroomLights", ["Toilet", "Bathtub"]);
})(exports);