forked from Mubramaj/QcpuUsage
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
75 lines (62 loc) · 2.05 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
// Library to track cpuUsage
const os = require('os-utils');
// Library to send signal to Q keyboards
const q = require('daskeyboard-applet');
// Color associated to cpu activity from low (green) to high (red).
const colors = ['#00FF00', '#00FF00', '#00FF00', '#00FF00', '#FFFF00', '#FFFF00', '#FF0000',
'#FF0000', '#FF0000', '#FF0000'
];
const logger = q.logger;
class CpuUsage extends q.DesktopApp {
constructor() {
super();
// run every 3000 ms
this.pollingInterval = 3000;
logger.info("CPU Usage Meter ready to go!");
}
// call this function every pollingInterval
async run() {
return this.getCpuUsage().then(percent => {
return new q.Signal({
points: [this.generatePoints(percent)],
name: "CPU Usage",
message: Math.round(percent * 100) + "%",
isMuted: true, // don't flash the Q button on each signal
});
});
}
async getCpuUsage() {
return new Promise((resolve) => {
os.cpuUsage(v => {
resolve(v);
})
})
}
generatePoints(percent) {
const numberOfKeys = 10;
// multiply the cpu percentage by the number total of keys
const numberOfKeysToLight = Math.round(numberOfKeys * percent);
let points = [];
// create a list of points (zones) with a color). Each point
// correspond to an LED
for (let i = 0; i < numberOfKeys; i++) {
points.push(new q.Point(this.getColor(i, numberOfKeysToLight)));
}
return points;
}
/** get a color of a zone depending on it's index on the zone array */
getColor(zoneIndex, numberOfKeysToLight) {
if (zoneIndex >= numberOfKeysToLight) {
// if the zone is after the number max of keys to light. Set the light to the selected default or turn it off
// Black color = no light
return this.config.defaultColor || '#000000';
} else {
// turn on the zone with the proper color
return colors[zoneIndex];
}
}
}
module.exports = {
CpuUsage: CpuUsage
};
const cpuUsage = new CpuUsage();