-
Notifications
You must be signed in to change notification settings - Fork 0
/
hueWrapper.js
100 lines (79 loc) · 2.66 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
var _ = require('underscore'),
util = require('util'),
hue = require("node-hue-api"),
HueApi = hue.HueApi,
lightState = hue.lightState,
logger = require('./logger');
(function(context) {
var HUE_USER_NAME = "lalaautomationserver";
var hueBridgeInfo,
hueBridgeDetailedInfo,
hueApi,
lightArray,
lightNamesToIds;
function Light(lightName) {
this.lightId = lightNamesToIds[lightName];
this.on = function() {
hueApi
.setLightState(this.lightId, lightState.create().on())
.done();
};
this.off = function() {
hueApi
.setLightState(this.lightId, lightState.create().off())
.done();
};
}
function doHueBridgeInfoReceived(hueBridges) {
logger.i('Bridge Info Received!');
logger.i(util.format('%d Hue Bridges Found: %s', hueBridges.length, util.inspect(hueBridges)));
hueBridgeInfo = hueBridges[0];
logger.i('Connecting to the HueApi...');
hueApi = new HueApi(hueBridgeInfo.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(doHueBridgeInfoReceived)
.done();
}
exports.init = init;
exports.turnOnAllLights = turnOnAllLights;
exports.getLightByName = getLightByName;
})(exports);