-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue.js
71 lines (61 loc) · 1.57 KB
/
hue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const hueJay = require('huejay');
const convert = require('color-convert');
const USERNAME = "93a6cc168ba90f1bf7f2a410fae0a7";
const eventEmitter = require ('events').EventEmitter;
const event = new eventEmitter();
const rx = require('rx-lite');
// Discover Ip
exports.changeLights = function(color, initalColor, activeLights){
// Convert color to hue X/Y color input
let xyz = convert.keyword.xyz(color),
X = xyz[0],
Y = xyz[1],
Z = xyz[2];
let x = X / (X + Y + Z);
let y = Y / (X + Y + Z);
let xy = [x, y];
// Discover hue bridges, find lights and change color
hueJay.discover()
.then(function(bridges){
let firstIp = bridges[0]["ip"];
// console.log(`Detected first Ip: ${firstIp}`)
return firstIp;
})
.then(function(firstIp){
// Set up client
let client = new hueJay.Client({
"host": firstIp,
"username": USERNAME
});
// Test connection
client.bridge.ping()
.catch((error) => {
console.log('Could not connect');
})
client.lights.getAll()
.then(function(lights){
let lightsNameAndId = {};
for (let light of lights){
for (let activeLight of activeLights){
if (light.name == activeLight){
lightsNameAndId[light.name] = light.id;
}
}
}
return lightsNameAndId;
})
.then(function(lightIds){
for (let lightId in lightIds){
let i = lightIds[lightId];
client.lights.getById(i)
.then(function(light){
light.on = true;
light.xy = xy;
return client.lights.save(light);
})
}
})
})
console.log(`changed the lights to ${color}`);
};