-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
152 lines (148 loc) · 4.72 KB
/
app.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
const HID = require('node-hid'),
SysTray = require('systray2').default,
path = require('path'),
DATA_REQ = [0xC9, 0x64],
CORSAIR_VID = 0x1B1C,
KNOWN_PIDS = {
0x0A38: "HS70 Wireless",
0x0A4F: "HS70 PRO Wireless",
0x1B27: "VOID Wireless",
0x0A2B: "VOID Wireless",
0x0A14: "VOID PRO Wireless",
0x0A16: "VOID PRO Wireless",
0x0A1A: "VOID PRO Wireless",
0x0A55: "VOID ELITE Wireless",
0x0A51: "VOID ELITE Wireless",
0x0A3E: "Virtuoso RGB Wireless",
0x0A40: "Virtuoso RGB Wireless",
0x0A42: "Virtuoso RGB Wireless",
0x0A44: "Virtuoso RGB Wireless",
0x0A5C: "Virtuoso RGB Wireless",
0x0A64: "Virtuoso RGB Wireless",
},
DEVICE_STATES = {
0: "Disconnected",
1: "Connected",
2: "Low battery",
4: "Fully charged",
5: "Charging"
},
TRAY_ICONS = {
default: path.join(__dirname, "icons/headphones.ico"),
charging: path.join(__dirname, "icons/battery-charging.ico"),
10: path.join(__dirname, "icons/battery-wireless.ico"),
9: path.join(__dirname, "icons/battery-wireless-90.ico"),
8: path.join(__dirname, "icons/battery-wireless-80.ico"),
7: path.join(__dirname, "icons/battery-wireless-70.ico"),
6: path.join(__dirname, "icons/battery-wireless-60.ico"),
5: path.join(__dirname, "icons/battery-wireless-50.ico"),
4: path.join(__dirname, "icons/battery-wireless-40.ico"),
3: path.join(__dirname, "icons/battery-wireless-30.ico"),
2: path.join(__dirname, "icons/battery-wireless-20.ico"),
1: path.join(__dirname, "icons/battery-wireless-10.ico"),
0: path.join(__dirname, "icons/battery-wireless-0.ico"),
},
MENU_ITEMS = [
{
title: "Refresh device",
tooltip: "Refresh device",
checked: false,
enabled: true,
click: init_device
},
{
title: "Exit",
tooltip: "Exit",
checked: false,
enabled: true,
click: () => {
tray.kill(false);
process.exit(0);
}
}
],
TRAY_OPTIONS = {
menu: {
icon: TRAY_ICONS["default"],
title: "Corsair battery level",
tooltip: "No device found",
items: MENU_ITEMS
},
debug: false,
copyDir: true
},
VOID_BATTERY_MICUP = 128,
tray = new SysTray(TRAY_OPTIONS);
let device_info = device_hid = null;
process.on('exit', () => { tray.kill(false); });
tray.onClick(event => { event.item.click(); });
tray.ready().then(init_device);
function init_device() {
[device_hid, device_info] = get_HID();
if (!device_hid) {
reset_tray();
return;
}
device_info.full_name = `${device_info.manufacturer} ${KNOWN_PIDS[device_info.productId]}`;
device_hid.setNonBlocking(1);
device_hid.on('data', update_tray);
device_hid.on('error', () => {
reset_tray();
device_info = device_hid = null;
});
device_hid.resume();
}
function get_HID() {
let dList = HID.devices(), hidDevice, infoObj;
for (let deviceObj of dList) {
if (deviceObj.vendorId !== CORSAIR_VID || KNOWN_PIDS[deviceObj.productId] === undefined)
continue;
try {
hidDevice = new HID.HID(deviceObj.path);
hidDevice.write(DATA_REQ);
hidDevice.pause();
infoObj = deviceObj;
break;
} catch {
hidDevice = infoObj = null;
continue;
}
}
return [hidDevice, infoObj];
}
function update_tray([, , battery, , state]) {
if (battery > VOID_BATTERY_MICUP) {
battery = battery - VOID_BATTERY_MICUP;
}
let icon, tooltip;
if (state === 0 || DEVICE_STATES[state] === undefined) { //disconnected
icon = TRAY_ICONS["default"];
tooltip = `${device_info.full_name}: ${DEVICE_STATES[0]}`;
} else if (state === 5) { // charging (not full)
icon = TRAY_ICONS["charging"];
tooltip = `${device_info.full_name}: ${DEVICE_STATES[state]}`;
} else {
icon = TRAY_ICONS[Math.floor(battery / 10)];
tooltip = `${device_info.full_name}: ${DEVICE_STATES[state]} (${battery}%)`;
}
tray.sendAction({
type: 'update-menu',
menu: {
icon,
tooltip,
title: tooltip,
items: MENU_ITEMS
}
});
}
function reset_tray() {
tray.sendAction({
type: 'update-menu',
menu: {
icon: TRAY_ICONS["default"],
title: "Corsair battery level",
tooltip: "No device found",
items: MENU_ITEMS
}
});
}