forked from sandeepmistry/node-yeelight-blue
/
index.js
53 lines (37 loc) · 1.59 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
var util = require('util');
var NobleDevice = require('noble-device');
var SERVICE_UUID = 'fff0';
var CONTROL_UUID = 'fff1';
var EFFECT_UUID = 'fffc';
var YeelightBlue = function(peripheral) {
NobleDevice.call(this, peripheral);
};
YeelightBlue.SCAN_UUIDS = [SERVICE_UUID];
YeelightBlue.is = function(peripheral) {
var localName = peripheral.advertisement.localName;
return ((localName === undefined) || (localName === 'Yeelight Blu'));
};
NobleDevice.Util.inherits(YeelightBlue, NobleDevice);
YeelightBlue.prototype.writeServiceStringCharacteristic = function(uuid, string, callback) {
this.writeStringCharacteristic(SERVICE_UUID, uuid, string, callback);
};
YeelightBlue.prototype.writeControlCharateristic = function(red, green, blue, brightness, callback) {
var command = util.format('%d,%d,%d,%d', red, green, blue, brightness);
for (var i = command.length; i < 18; i++) {
command += ',';
}
this.writeServiceStringCharacteristic(CONTROL_UUID, command, callback);
};
YeelightBlue.prototype.turnOn = function(callback) {
this.writeControlCharateristic(255, 255, 255, 100, callback);
};
YeelightBlue.prototype.turnOff = function(callback) {
this.writeControlCharateristic(0, 0, 0, 0, callback);
};
YeelightBlue.prototype.setColorAndBrightness = function(red, green, blue, brightness, callback) {
this.writeControlCharateristic(red, green, blue, brightness, callback);
};
YeelightBlue.prototype.setGradualMode = function(on, callback) {
this.writeServiceStringCharacteristic(EFFECT_UUID, on ? 'TS' : 'TE', callback);
};
module.exports = YeelightBlue;