| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 'use strict';
- module.exports = {
- init: init,
- getConfig: getGeneric('getConfig'),
- getFullState: getGeneric('getFullState'),
- getLights: getGeneric('lights'),
- getLight: getLight,
- setLight: setLight
- };
- var hue = require('node-hue-api'),
- HueApi = hue.HueApi,
- lightState = hue.lightState;
- var api = null;
- function init(hueConfig, callback) {
- api = new HueApi(hueConfig.host, hueConfig.username);
- callback();
- }
- function getGeneric(name) {
- return function (req, res) {
- api[name](function (error, result) {
- if (error) return res.send(500, 'Unable to get ' + name);
- res.send(200, result);
- });
- };
- }
- function getLight(req, res) {
- api.lightStatusWithRGB(req.params.id, function (error, result) {
- if (error) return res.send(500, 'Unable to get state of light ' + req.params.id);
- // id is not part of the light status api
- result.id = req.params.id;
- res.send(200, result);
- });
- }
- function setLight(req, res) {
- console.log('set light: %s', req.params.id, req.body);
- var state = lightState.create();
- if (req.body.reset) state.reset();
- if (req.body.colorLoop) state.colorLoop();
- if (typeof req.body.on !== 'undefined') {
- if (req.body.on) state.on();
- else state.off();
- }
- if (req.body.rgb) {
- state.rgb(req.body.rgb);
- }
- if (req.body.bri) {
- state.bri(req.body.bri);
- }
- api.setLightState(req.params.id, state, function (error, result) {
- if (error) return res.send(500, error);
- res.send(200, {});
- });
- }
|