routes.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. module.exports = {
  3. init: init,
  4. getConfig: getGeneric('getConfig'),
  5. getFullState: getGeneric('getFullState'),
  6. getLights: getGeneric('lights'),
  7. setLight: setLight
  8. };
  9. var hue = require('node-hue-api'),
  10. HueApi = hue.HueApi,
  11. lightState = hue.lightState,
  12. Color = require('color');
  13. var api = null;
  14. function init(hueConfig, callback) {
  15. api = new HueApi(hueConfig.host, hueConfig.username);
  16. callback();
  17. }
  18. function getGeneric(name) {
  19. return function (req, res) {
  20. api[name](function (error, result) {
  21. if (error) return res.send(500, 'Unable to get t' + name);
  22. res.send(200, result);
  23. });
  24. };
  25. }
  26. function setLight(req, res) {
  27. console.log('set light: %s to color %s', req.params.id, req.body.color);
  28. var color = new Color(req.body.color);
  29. var state = lightState.create().rgb(color.rgb().array());
  30. api.setLightState(req.params.id, state, function (error, result) {
  31. if (error) return res.send(500, error);
  32. res.send(200, {});
  33. });
  34. }