This repository has been archived by the owner on Aug 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (249 loc) · 8.35 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var Service, Characteristic;
var request = require('request');
var colorsys = require('colorsys');
var PAYLOAD_ON = 'ON';
var PAYLOAD_OFF = 'OFF';
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-ailight', 'AiLight', AiLightAccessory);
};
function AiLightAccessory(log, config) {
// Initialize various object members (config, etc.)
this.log = log;
this.hostname = config.hostname;
this.name = config.name;
this.apikey = config.apikey;
// Local caching of HSL color space for RGB callback
this.brightness = 0;
this.hue = 0;
this.saturation = 0;
/**
* Performs a RESTful API call to the configured device.
*
* @param {String} path The name of API route/path.
* @param {String} body The optional payload to be submitted to the device.
* @param {String} method The HTTP method (GET, PUT) to be used.
* @param {function} callback The callback that handles the response.
*/
this.httpRequest = function(path, body, method, callback) {
request({
url: 'http://' + this.hostname + '/api/' + path,
body: body,
method: method,
rejectUnauthorized: false,
headers: {
'content-type': 'application/json',
'api-key': this.apikey
}
}, function(error, response, body) {
callback(error, response, body);
});
};
/**
* Sets the RGB value of the device based on the cached HSL values.
*
* @param {function} callback The callback that handles the response.
*/
this.setRGB = function(callback) {
var rgb = colorsys.hsl2Rgb({
h: this.hue,
s: this.saturation,
l: this.brightness
});
this.log('Converting H:%s S:%s L:%s to RGB:(r: %s, g: %s, b: %s)...', this.hue, this.saturation, this.brightness, rgb.r, rgb.g, rgb.b);
var payload = {
'state': PAYLOAD_ON,
'color': {
'r': rgb.r,
'g': rgb.g,
'b': rgb.b
}
};
this.httpRequest('light', JSON.stringify(payload), 'PATCH', function(error, response, body) {
if (error) {
this.log("Error '%s' setting RGB values. Response: %s", error, body);
callback(error || new Error('Error setting RGB values.'));
} else {
this.log('RGB values successfully changed to RGB:(r: %s, g: %s, b: %s)', rgb.r, rgb.g, rgb.b);
callback();
}
}.bind(this));
};
}
AiLightAccessory.prototype = {
identify: function(callback) {
this.log('Identify requested!');
callback();
},
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Sacha Telgenhof')
.setCharacteristic(Characteristic.Model, 'AiLight')
.setCharacteristic(Characteristic.FirmwareRevision, '0.1.0')
.setCharacteristic(Characteristic.SerialNumber, 'AI001');
this.service = new Service.Lightbulb(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.service.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
this.service.addCharacteristic(new Characteristic.Hue())
.on('get', this.getHue.bind(this))
.on('set', this.setHue.bind(this));
this.service.addCharacteristic(new Characteristic.Saturation())
.on('get', this.getSaturation.bind(this))
.on('set', this.setSaturation.bind(this));
this.informationService = informationService;
return [this.informationService, this.service];
},
/**
* Gets the current state (On/Off) of this device.
*
* @param {function} callback The callback that handles the response.
*/
getState: function(callback) {
this.httpRequest('light', '', 'GET', function(error, response, body) {
if (error) {
this.log('Error getting State (statuscode %s): %s', response.statusCode, error);
callback(error);
} else {
var json = JSON.parse(body);
this.log('Power is %s', json.state);
callback(null, (json.state == PAYLOAD_ON));
}
}.bind(this));
},
/**
* Sets the state (On/Off) for this device.
*
* @param {bool} on The requested sate.
* @param {function} callback The callback that handles the response.
*/
setState: function(on, callback) {
var state = (on) ? PAYLOAD_ON : PAYLOAD_OFF;
var payload = {
'state': state
};
this.httpRequest('light', JSON.stringify(payload), 'PATCH', function(error, response, body) {
if (error) {
this.log("Error '%s' setting State. Response: %s", error, body);
callback(error || new Error('Error setting State.'));
} else {
this.log('State successfully changed to %s', state);
callback();
}
}.bind(this));
},
/**
* Gets the current Brightness level of this device.
*
* @param {function} callback The callback that handles the response.
*/
getBrightness: function(callback) {
this.httpRequest('light', '', 'GET', function(error, response, body) {
if (error) {
this.log('Error getting Brightness (statuscode %s): %s', response.statusCode, error);
callback(error);
} else {
var json = JSON.parse(body);
var brightness = Math.round((json.brightness / 255) * 100);
this.log('Brightness is set at %s (%s\%)', json.brightness, brightness);
this.brightness = brightness;
callback(null, brightness);
}
}.bind(this));
},
/**
* Sets the brightness level for this device.
*
* @param {integer} on The requested brightness level.
* @param {function} callback The callback that handles the response.
*/
setBrightness: function(level, callback) {
var brightnessLevel = Math.round((level * 255) / 100);
var payload = {
'state': PAYLOAD_ON,
'brightness': brightnessLevel
};
this.httpRequest('light', JSON.stringify(payload), 'PATCH', function(error, response, body) {
if (error) {
this.log("Error '%s' setting Brightness. Response: %s", error, body);
callback(error || new Error('Error setting Brightness.'));
} else {
this.log('Brightness changed from %s\% to %s\%', this.brightness, level);
this.brightness = level;
callback();
}
}.bind(this));
},
/**
* Gets the current Hue level of this device.
*
* @param {function} callback The callback that handles the response.
*/
getHue: function(callback) {
this.httpRequest('light', '', 'GET', function(error, response, body) {
if (error) {
this.log('Error getting Hue (statuscode %s): %s', response.statusCode, error);
callback(error);
} else {
var json = JSON.parse(body);
var hsl = colorsys.rgb2Hsl({
r: json.color.r,
g: json.color.g,
b: json.color.b
});
this.log('Hue is set at %s°', hsl.h);
this.hue = hsl.h;
callback(null, hsl.h);
}
}.bind(this));
},
/**
* Sets the Hue level for this device.
*
* @param {integer} hue The requested Hue level.
* @param {function} callback The callback that handles the response.
*/
setHue: function(hue, callback) {
this.hue = hue;
this.setRGB(callback);
},
/**
* Gets the current Saturation level of this device.
*
* @param {function} callback The callback that handles the response.
*/
getSaturation: function(callback) {
this.httpRequest('light', '', 'GET', function(error, response, body) {
if (error) {
this.log('Error getting Saturation (statuscode %s): %s', response.statusCode, error);
callback(error);
} else {
var json = JSON.parse(body);
var hsl = colorsys.rgb2Hsl({
r: json.color.r,
g: json.color.g,
b: json.color.b
});
this.log('Saturation is set at %s%', hsl.s);
this.saturation = hsl.s;
callback(null, hsl.s);
}
}.bind(this));
},
/**
* Sets the Saturation level for this device.
*
* @param {integer} on The requested saturation level.
* @param {function} callback The callback that handles the response.
*/
setSaturation: function(saturation, callback) {
this.saturation = saturation;
this.setRGB(callback);
},
};