-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (63 loc) · 2.06 KB
/
app.js
File metadata and controls
71 lines (63 loc) · 2.06 KB
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
const express = require('express')
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const HID = require('node-hid');
const devices = HID.devices();
const COLORS = {
on: [38, 255, 0, 248, 7],
off: [38, 255, 0, 249, 6],
stronger : [38, 255, 0, 255, 0],
lighter : [38, 255, 0, 253, 2],
flash : [38, 255, 0, 240, 15],
strobe : [38, 255, 0, 236, 19],
smooth : [38, 255, 0, 232, 23],
mode : [38, 255, 0, 228, 27],
white : [38, 255, 0, 252, 3],
red : [38, 255, 0, 247, 8],
orangered : [38, 255, 0, 243, 12],
darkorange : [38, 255, 0, 239, 16],
orange : [38, 255, 0, 235, 20],
gold : [38, 255, 0, 231, 24],
green : [38, 255, 0, 246, 9],
limegreen : [38, 255, 0, 242, 13],
lightseagreen: [38, 255, 0, 238, 17],
deepskyblue : [38, 255, 0, 234, 21],
dodgerblue : [38, 255, 0, 230, 25],
blue : [38, 255, 0, 245, 10],
lightskyblue : [38, 255, 0, 241, 14],
pink : [38, 255, 0, 237, 18],
hotpink : [38, 255, 0, 233, 22],
mediumvioletred : [38, 255, 0, 229, 26],
}
function getColorName(code){
for (let key in COLORS) {
if (COLORS[key].join() === code.join() ) return key
}
}
function getDevice(devices, deviceName, callback) {
devices.forEach(
device => {
if (device.product === deviceName) callback(new HID.HID(device.path));
});
}
// "IR Receiver"
function encodeIrData(data) {
return Buffer.from({"type": "Buffer", "data": data})
}
app.use(express.static('public'))
io.on('connection', (socket) => {
console.log('a user connected');
getDevice(devices, "IR Receiver", (device) => {
device.on('data', data => {
let decoded = JSON.stringify(data); //{"type":"Buffer","data":[38,255,0,242,13]}
let code = JSON.parse(decoded).data;
console.log(code);
socket.emit("change", getColorName(code));
});
device.on('error', err => console.log(err));
});
});
http.listen(3000, () => {
console.log('listening on *:3000');
});