Skip to content

Instantly share code, notes, and snippets.

@paulmand3l
Created May 16, 2014 08:46
Show Gist options
  • Save paulmand3l/737acf67919267fc0ca8 to your computer and use it in GitHub Desktop.
Save paulmand3l/737acf67919267fc0ca8 to your computer and use it in GitHub Desktop.
(function() {
var fs, getBridge, getUser, getUsernameForBridge, hslLights, hue, keypress, mod, q, startKeypress, toggleWarmWhite, _;
fs = require('fs');
_ = require('underscore');
hue = require("node-hue-api");
q = require("q");
keypress = require('keypress');
keypress(process.stdin);
mod = function(n, m) {
return ((n % m) + m) % m;
};
getUsernameForBridge = function(id) {
if (fs.existsSync('usernames')) {
return JSON.parse(fs.readFileSync('usernames', {
'encoding': 'utf8'
}))[id];
}
};
getBridge = function() {
return hue.locateBridges().then(function(bridges) {
if (!(bridges.length > 0)) {
throw new Error('No bridges detected.');
} else {
return bridges[0];
}
});
};
getUser = function(bridge) {
var username;
username = getUsernameForBridge(bridge.id);
if (username) {
console.log('Found existing user:', username);
return q(username);
} else {
return (new hue.HueApi()).registerUser(bridge.ipaddress).then(function(username) {
var bridgeToUsername;
bridgeToUsername = fs.existsSync('usernames') ? JSON.parse(fs.readFileSync('usernames', {
'encoding': 'utf8'
})) : {};
bridgeToUsername[bridge.id] = username;
fs.writeFileSync('usernames', JSON.stringify(bridgeToUsername));
console.log('Registered new user:', username);
return username;
});
}
};
hslLights = function(api, lights, h, s, l) {
var light, _i, _len, _results;
_results = [];
for (_i = 0, _len = lights.length; _i < _len; _i++) {
light = lights[_i];
console.log('Setting', light.name, 'to', h, s, l);
_results.push(api.setLightState(light.id, hue.lightState.create().hsl(h, s, l).transition(0.2).on()));
}
return _results;
};
toggleWarmWhite = function(api, lights) {
console.log('Toggling lights');
return api.lightStatus(lights[0].id).then(function(status) {
var light, lightsOn, _i, _len, _results;
lightsOn = status.state.on;
_results = [];
for (_i = 0, _len = lights.length; _i < _len; _i++) {
light = lights[_i];
if (lightsOn) {
console.log('Lights going off');
_results.push(api.setLightState(light.id, hue.lightState.create().off()));
} else {
console.log('Lights going on');
_results.push(api.setLightState(light.id, hue.lightState.create().on().white(360, 80)));
}
}
return _results;
});
};
startKeypress = function(api, lights) {
process.stdin.on('keypress', function(ch, key) {
console.log('Keypress received', ch, key);
if (key && key.name && key.name === 'w') {
toggleWarmWhite(api, lights);
}
if (key && key.ctrl && key.name === 'c') {
return process.stdin.pause();
}
});
process.stdin.setRawMode(true);
return process.stdin.resume();
};
getBridge().then(function(bridge) {
return getUser(bridge).then(function(username) {
var api;
api = new hue.HueApi(bridge.ipaddress, username);
return api.searchForNewLights().then(function() {
return api.lights().then(function(lights) {
console.log('Found lights', lights);
return startKeypress(api, lights.lights);
});
});
});
}).done();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment