Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import {HueApi, lightState} from 'node-hue-api';
  4. import creds from './constants/credentials'; // JSON object containing Hue Bridge ip-address and user credentials
  5.  
  6. class Api {
  7. constructor() {
  8. this.api = new HueApi(creds.hue.hostname, creds.user.username);
  9. this.lights = [];
  10. }
  11.  
  12. _onError(err) {
  13. console.error(err);
  14. }
  15.  
  16. getConfig(cb) {
  17. this.api.config((err, config) => {
  18. if (err) this._onError(err);
  19. else cb(config);
  20. });
  21. }
  22.  
  23. getLights(cb) {
  24. this.api.getLights((err, {lights}) => {
  25. if (err) this._onError(err);
  26. else this.setLights(lights), cb(lights);
  27. });
  28. }
  29.  
  30. setLights(lights) {
  31. this.lights = lights;
  32. }
  33.  
  34. getLightStatus(lightId, cb) {
  35. this.api.lightStatusWithRGB(lightId, ((err, {state}) => {
  36. if (err) this._onError(err);
  37. else cb(state);
  38. }));
  39. }
  40.  
  41. setLightState(lightId, state) {
  42. this.api.setLightState(lightId, state);
  43. }
  44.  
  45. createLS(cmd, args) {
  46. return lightState.create()[cmd](args);
  47. }
  48.  
  49. setColor(color) {
  50. // Hardcoding lightId for reasons
  51. this.setLightState(3, lightState.create().rgb(this.hexToRGB(color)));
  52. this.setLightState(4, lightState.create().rgb(this.hexToRGB(color)));
  53. }
  54.  
  55. hexToRGB(hex) {
  56. return hex.split(/([a-f0-9]{2})/).filter(c => c && !isNaN(parseInt(c, 16))).map(c => parseInt(c, 16));
  57. }
  58.  
  59. rgbToHex(rgb) {
  60. return '#' + rgb.map(c => ('0' + c.toString(16)).substr(-2));
  61. }
  62. }
  63.  
  64. export default Api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement