routes.js 1.1 KB

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