-
Notifications
You must be signed in to change notification settings - Fork 15
/
Device.js
162 lines (147 loc) · 4.35 KB
/
Device.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
const noble = require("@abandonware/noble");
function 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)];
}
function log(message) {
console.log(`[@lyliya/homebridge-ledstrip-ble]:`, message);
}
module.exports = class Device {
constructor(uuid) {
this.uuid = uuid;
this.connected = false;
this.power = false;
this.brightness = 100;
this.hue = 0;
this.saturation = 0;
this.l = 0.5;
this.peripheral = undefined;
noble.on("stateChange", (state) => {
if (state == "poweredOn") {
noble.startScanningAsync();
} else {
if (this.peripheral) this.peripheral.disconnect();
this.connected = false;
}
});
noble.on("discover", async (peripheral) => {
console.log(
"[@lyliya/homebridge-ledstrip-ble]:",
peripheral.uuid,
peripheral.advertisement.localName
);
if (peripheral.uuid == this.uuid) {
this.peripheral = peripheral;
noble.stopScanning();
}
});
}
async connectAndGetWriteCharacteristics() {
if (!this.peripheral) {
noble.startScanningAsync();
return;
}
log(`Connecting to ${this.peripheral.uuid}...`);
await this.peripheral.connectAsync();
log(`Connected`);
this.connected = true;
const { characteristics } =
await this.peripheral.discoverSomeServicesAndCharacteristicsAsync(
["fff0"],
["fff3"]
);
this.write = characteristics[0];
}
async debounceDisconnect() {
let timer;
return () => {
clearTimeout(timer);
timer = setTimeout(async () => {
if (this.peripheral) {
log("Deconnecting...");
await this.peripheral.disconnectAsync();
log("Deconnected");
this.connected = false;
}
}, 5000);
};
}
async set_power(status) {
if (!this.connected) await this.connectAndGetWriteCharacteristics();
if (this.write) {
const buffer = Buffer.from(
`7e0004${status ? "01" : "00"}00000000ef`,
"hex"
);
log(buffer);
this.write.write(buffer, true, (err) => {
if (err) console.log("Error:", err);
this.power = status;
this.debounceDisconnect();
});
}
}
async set_brightness(level) {
if (level > 100 || level < 0) return;
if (!this.connected) await this.connectAndGetWriteCharacteristics();
if (this.write) {
const level_hex = ("0" + level.toString(16)).slice(-2);
const buffer = Buffer.from(`7e0001${level_hex}00000000ef`, "hex");
log(buffer);
this.write.write(buffer, true, (err) => {
if (err) console.log("Error:", err);
this.brightness = level;
this.debounceDisconnect();
});
}
}
async set_rgb(r, g, b) {
if (!this.connected) await this.connectAndGetWriteCharacteristics();
if (this.write) {
const rhex = ("0" + r.toString(16)).slice(-2);
const ghex = ("0" + g.toString(16)).slice(-2);
const bhex = ("0" + b.toString(16)).slice(-2);
const buffer = Buffer.from(`7e000503${rhex}${ghex}${bhex}00ef`, "hex");
log(buffer);
this.write.write(buffer, true, (err) => {
if (err) console.log("Error:", err);
this.debounceDisconnect();
});
}
}
async set_hue(hue) {
if (!this.connected) await this.connectAndGetWriteCharacteristics();
if (this.write) {
this.hue = hue;
const rgb = hslToRgb(hue / 360, this.saturation / 100, this.l);
this.set_rgb(rgb[0], rgb[1], rgb[2]);
this.debounceDisconnect();
}
}
async set_saturation(saturation) {
if (!this.connected) await this.connectAndGetWriteCharacteristics();
if (this.write) {
this.saturation = saturation;
const rgb = hslToRgb(this.hue / 360, saturation / 100, this.l);
this.set_rgb(rgb[0], rgb[1], rgb[2]);
this.debounceDisconnect();
}
}
};