-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (82 loc) · 2.48 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
/* This is the legacy code done originally on the stream - No longer used.
App logic now lives in src/index.js
You can watch the original video here:
https://www.youtube.com/watch?v=sBglf1RggaM
*/
const axios = require("axios");
const Color = require("color");
const tmi = require("tmi.js");
const client = new tmi.Client({
connection: {
secure: true,
reconnect: true,
},
channels: ["xabadu"],
});
client.connect();
function hexToRGB(hex) {
// hex = string, ff0000, 2d2d2d, 0000ff
// 233456
// fff
// r -> = 0x23, g-> 0x34, b-> 0x56
if (hex.length < 3 || hex.length > 6) {
return;
}
const r = hex.length === 3 ? `0x${hex[0]}${hex[0]}` : `0x${hex[0]}${hex[1]}`;
const g = hex.length === 3 ? `0x${hex[1]}${hex[1]}` : `0x${hex[2]}${hex[3]}`;
const b = hex.length === 3 ? `0x${hex[2]}${hex[2]}` : `0x${hex[4]}${hex[5]}`;
// +r -> number
return `rgb(${+r},${+g},${+b})`;
}
client.on("message", (channel, tags, message, self) => {
// !color rgb(255, 255, 255)
// !color #ffffff #fff
// !loop
let parsedMessage;
if (message.startsWith("!color")) {
// comando para cambiar la luz
if (message.indexOf("rgb") !== -1) {
parsedMessage = message.split("!color ")[1];
}
if (message.indexOf("#") !== -1) {
parsedMessage = hexToRGB(message.split("!color #")[1]);
}
const rgbColor = Color.rgb(parsedMessage);
const [h, s, v] = rgbColor.hsv().color;
axios({
method: "put",
url:
"http://10.0.0.220/api/G-vbAfzffh30DzmkuLzpMc1YV74rWFCxIgpQcKeo/lights/6/state",
data: {
on: true,
hue: parseInt((65535 * h) / 360, 10),
sat: parseInt(254 * (s / 100), 10),
bri: parseInt(254 * (v / 100), 10),
transitiontime: 10,
},
})
.then((res) => console.log("res", res.status))
.catch((err) => console.log(err));
}
if (message.startsWith("!loop")) {
axios({
method: "put",
url:
"http://10.0.0.220/api/G-vbAfzffh30DzmkuLzpMc1YV74rWFCxIgpQcKeo/lights/6/state",
data: {
on: true,
effect: "colorloop",
},
})
.then((res) => console.log("res", res.status))
.catch((err) => console.log(err));
}
});
// const rgbColor = Color.rgb(3, 252, 11);
// const [h, s, v] = rgbColor.hsv().color;
// // HSV - HSB
// // HUE = DEGREES => INTEGERS (0, 65535)
// // SAT = PERCENTAGES => INTEGERS (0, 254)
// // BRIGHTNESS/VALUE = PERCENTAGES => INTEGERS (0, 254)
// // 360 => 65535
// // 121.92 => X X = 65535 * 121.92 / 360