routes.js 1.1 KB

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