-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·81 lines (74 loc) · 2.57 KB
/
app.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
var hue = require('node-hue-api'),
// Converter = require('./converter'),
HueApi = hue.HueApi,
lightState = hue.lightState,
bridgeIP, username;
var argCount = process.argv.length;
for (var i = 0; i < argCount; i++) {
var arg = process.argv[i];
if (arg === '-b' && i + 1 < argCount) {
bridgeIP = process.argv[i + 1];
} else if (arg === '-u' && i + 1 < argCount) {
username = process.argv[i + 1];
}
}
if (typeof bridgeIP === 'undefined') {
console.error('Provide a Hue bridge IP address with -b flag.');
console.log('Example: node app.js -b 192.168.1.100');
process.exit(1);
}
if (typeof username === 'undefined') {
console.error('Provide a Hue bridge user with -u flag.');
console.log('Example: node app.js -u 123abc');
process.exit(1);
}
console.log('Connecting to bridge ' + bridgeIP + ' as user ' + username);
var api = new HueApi(bridgeIP, username);
var displayError = function(err) {
console.error(err);
};
var displayLight = function(light) {
var state = light.state;
console.log("\t" + light.name + ' model ' + light.modelid);
if (state.on) {
var x = state.xy[0];
var y = state.xy[1];
console.log("\tx: " + x + ', y: ' + y + ', brightness: ' + state.bri);
var rgb = Converter.CIE1931ToRGB(x, y, state.bri);
console.log("\trgb(" + rgb[0] + ', ' + rgb[1] + ', ' + rgb[2] + ')');
var hex = Converter.CIE1931ToHex(x, y, state.bri);
console.log("\t#" + hex);
} else {
console.log('\toff');
}
};
var getLightStatus = function(lightID) {
api.lightStatus(lightID).
then(function(result) {
console.log("\nLight #" + lightID);
displayLight(result);
}).fail(displayError).done();
};
var displayGroup = function(group) {
console.log("\t# lights: " + group.lights.length);
for (var i = 0; i < group.lights.length; i++) {
getLightStatus(group.lights[i]);
}
};
var displayBridge = function(bridge) {
console.log('Bridge "' + bridge.name + '"');
console.log("\tTime: " + bridge.localtime);
console.log("\tAPI version: " + bridge.apiversion);
api.getGroup('0').then(displayGroup).done();
};
// api.config().then(displayBridge).done();
var displayBridges = function(bridge) {
console.log("Hue Bridges Found: " + JSON.stringify(bridge));
};
hue.upnpSearch(2000).then(displayBridges).done();
// // var redX = 0.6417, redY = 0.304;
// // var blueX = 0.168, blueY = 0.041;
// // // var state = lightState.create().on().xy(redX, redY);
// // var state = lightState.create().on().effect('none');
// // api.setLightState(lightID, state).
// // then(displayLightState).fail(displayError).done();