'use strict'; module.exports = { init: init, getConfig: getGeneric('getConfig'), getFullState: getGeneric('getFullState'), getLights: getGeneric('lights'), setLight: setLight }; var hue = require('node-hue-api'), HueApi = hue.HueApi, lightState = hue.lightState, Color = require('color'); 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 t' + name); 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.state !== 'undefined') { if (req.body.state) state.on(); else state.off(); } if (req.body.color) { var color = new Color(req.body.color); state.rgb(color.rgb().array()); } api.setLightState(req.params.id, state, function (error, result) { if (error) return res.send(500, error); res.send(200, {}); }); }