-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue-service.ts
79 lines (65 loc) · 1.99 KB
/
hue-service.ts
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
72
73
74
75
76
77
78
79
import * as log from 'fancy-log';
import * as colorstring from 'color-string';
import * as jsonfile from 'jsonfile';
import * as hue from 'node-hue-api';
let HueApi = hue.HueApi,
lightState = hue.lightState;
let config = jsonfile.readFileSync('./config.json');
let hueAPI = new HueApi(config.hueConfig.hostname, config.hueConfig.username);
let disco: Boolean = false;
export interface Color {
h: number,
s: number,
b: number
}
class TreeStatus {
color: Color;
on: boolean;
effect: string;
}
export function setLightToColor(lightId: number, color: Color) {
let state = lightState.create().on().hsb(color.h, color.s, color.b);
hueAPI.setLightState(lightId, state)
.then(displaySetLightStateResult)
.catch(function (e) {
log(e);
})
.done();
}
export function getLightColor(lightId: number) {
let treeStatus: TreeStatus = new TreeStatus();
return new Promise(function (resolve, reject) {
hueAPI.lightStatus(lightId)
.then((lightStatus) => {
treeStatus.color = { h: getHue(lightStatus), s: getSaturation(lightStatus), b: getBrightness(lightStatus) };
treeStatus.on = lightStatus.state.on;
treeStatus.effect = lightStatus.state.effect;
resolve(treeStatus);
})
.done();
});
}
export function getLightStatus() {
hueAPI.sensors()
.then(displayResult)
.done();
}
function getHue(philipsHue) {
return Math.round((philipsHue.state.hue / ((256 * 256) - 1)) * 360);
}
function getSaturation(philipsHue) {
return Math.round((philipsHue.state.sat / 255) * 100);
}
function getBrightness(philipsHue) {
return Math.round((philipsHue.state.bri / 255) * 100);
}
let displaySetLightStateResult = function (result) {
if (result == true) {
log('Light succesfully changed state');
} else {
log(result);
}
};
let displayResult = function(result) {
log(JSON.stringify(result, null, 2));
};