Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import {HueApi, lightState} from 'node-hue-api';
- import creds from './constants/credentials'; // JSON object containing Hue Bridge ip-address and user credentials
- class Api {
- constructor() {
- this.api = new HueApi(creds.hue.hostname, creds.user.username);
- this.lights = [];
- }
- _onError(err) {
- console.error(err);
- }
- getConfig(cb) {
- this.api.config((err, config) => {
- if (err) this._onError(err);
- else cb(config);
- });
- }
- getLights(cb) {
- this.api.getLights((err, {lights}) => {
- if (err) this._onError(err);
- else this.setLights(lights), cb(lights);
- });
- }
- setLights(lights) {
- this.lights = lights;
- }
- getLightStatus(lightId, cb) {
- this.api.lightStatusWithRGB(lightId, ((err, {state}) => {
- if (err) this._onError(err);
- else cb(state);
- }));
- }
- setLightState(lightId, state) {
- this.api.setLightState(lightId, state);
- }
- createLS(cmd, args) {
- return lightState.create()[cmd](args);
- }
- setColor(color) {
- // Hardcoding lightId for reasons
- this.setLightState(3, lightState.create().rgb(this.hexToRGB(color)));
- this.setLightState(4, lightState.create().rgb(this.hexToRGB(color)));
- }
- hexToRGB(hex) {
- return hex.split(/([a-f0-9]{2})/).filter(c => c && !isNaN(parseInt(c, 16))).map(c => parseInt(c, 16));
- }
- rgbToHex(rgb) {
- return '#' + rgb.map(c => ('0' + c.toString(16)).substr(-2));
- }
- }
- export default Api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement