forked from stenehall/homebridge-ikea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
202 lines (167 loc) · 5.73 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var execSync = require('child_process').execSync
const coap = (method, config, payload = "{}") => `${config.coapClient} -u "Client_identity" -k "${config.psk}" -e '${payload}' -m ${method} coaps://${config.ip}/15001/`
const put = (config, id, payload) => coap("put", config, payload) + id
const get = (config, id="") => coap("get", config) + id
const kelvinToProcent = kelvin => (kelvin - 2200) / 18 // 4000
const procentToKelvin = procent => 2200 + (18 * procent) // 4000
const colorX = procent => Math.round(33135 - (82.05 * procent)) // 24930
const colorY = procent => Math.round(27211 - (25.17 * (100 - procent))) // 24694
const getKelvin = colorX => procentToKelvin(Math.round((33135 - colorX) / 82.05))
module.exports.getKelvin = getKelvin
module.exports.setBrightness = (config, id, brightness, callback) => {
const arguments = `{ "3311" : [{ "5851" : ${brightness}} ] }`
const cmd = put(config, id, arguments)
if (config.debug) {
config.log(`Setting brightness of ${brightness} for ${id}`)
config.log(cmd)
}
callback(execSync(cmd, {encoding: "utf8"}))
}
module.exports.setKelvin = (config, id, kelvin, callback) => {
const arguments = `{ "3311" : [{ "5709" : ${colorX(kelvinToProcent(kelvin))}, "5710": ${colorY(kelvinToProcent(kelvin))} }] }`
var cmd = put(config, id, arguments)
if (config.debug) {
config.log(cmd)
}
callback(execSync(cmd, {encoding: "utf8"}))
}
// Source: http://stackoverflow.com/a/9493060
const hslToRgb = (h, s, l) => {
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
} else {
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
const hexToRgb = (hex) => {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
const rgbToHsl = (r, g, b) => {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
// Source http://stackoverflow.com/a/36061908
const rgbToXy = (red,green,blue) => {
red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
var X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
var Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
var Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
var fx = X / (X + Y + Z);
var fy = Y / (X + Y + Z);
return [fx.toPrecision(4),fy.toPrecision(4)];
}
module.exports.convertRGBToHSL = (hex) => {
var c = hexToRgb(hex)
return rgbToHsl(c.r, c.g, c.b);
}
module.exports.setColor = (config, id, color, callback) => {
// First we convert hue and saturation
// to RGB, with 75% lighntess
const rgb = hslToRgb(color.hue, color.saturation, 0.75);
// Then we convert the rgb values to
// CIE L*a*b XY values
const cie = rgbToXy(...rgb).map(item => {
// we need to scale the values
return Math.floor(100000 * item);
});
const arguments = `{ "3311" : [{ "5709" : ${cie[0]}, "5710": ${cie[1]} }] }`
const cmd = put(config, id, arguments)
if (config.debug) {
config.log(cmd)
}
callback(execSync(cmd, {encoding: "utf8"}))
}
// @TODO: Figure out if the gateway actually don't support this
module.exports.setOnOff = (config, id, state, callback) => {
const arguments = `{ "3311" : [{ "5580" : ${state}} ] }`
var cmd = put(config, id, arguments)
if (config.debug) {
config.log(cmd)
}
callback(execSync(cmd, {encoding: "utf8"}))
}
const parseDeviceList = str => {
const split = str.trim().split("\n")
return split.pop().slice(1,-1).split(",")
}
module.exports.getDevices = config => new Promise((resolve, reject) => {
var cmd = get(config)
if (config.debug) {
config.log(cmd)
}
resolve(parseDeviceList(execSync(cmd, {encoding: "utf8"})))
})
const parseDevice = str => {
const split = str.trim().split("\n")
const json = JSON.parse(split.pop())
return {
name: json["9001"],
type: json["5750"],
createdAt: json["9002"],
instanceId: json["9003"],
details: json["3"],
reachabilityState: json["9019"],
lastSeen: json["9020"],
otaUpdateState: json["9054"],
switch: json["15009"],
light: json["3311"]
}
/*
light: {
{
onoff: json["3311"]["5580"],
dimmer: json["3311"]["5851"],
color_x: json["3311"]["5709"],
color_y: json["3311"]["5710"],
color: json["3311"]["5706"],
instance_id: json["3311"]["9003"],
"5707":0,
"5708":0,
"5711":0,
}
}
*/
}
module.exports.getDevice = (config, id) => new Promise((resolve, reject) => {
var cmd = get(config, id)
if (config.debug) {
config.log(`Get device information for: ${id}`)
config.log(cmd)
}
resolve(parseDevice(execSync(cmd, {encoding: "utf8"})))
})