-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblynk.js
82 lines (68 loc) · 2.13 KB
/
blynk.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
const BlynkLib = require('blynk-library');
const ResourceCollector = require('./resource-collector');
class RaspbettoBlynk {
constructor() {
this.blynk = new BlynkLib.Blynk(process.env.BLYNK_KEY);
this.refreshInterval = 5000;
this.resources = new ResourceCollector();
/** Refresh rate */
this.v0 = new this.blynk.VirtualPin(0);
/** Used RAM Percent*/
this.v1 = new this.blynk.VirtualPin(1);
/** CPU Usage Percent */
this.v2 = new this.blynk.VirtualPin(2);
/** CPU Temp */
this.v3 = new this.blynk.VirtualPin(3);
/** CPU Freq */
this.v4 = new this.blynk.VirtualPin(4);
/** Disk usage percent */
this.v5 = new this.blynk.VirtualPin(5);
/** CPU load one */
this.v6 = new this.blynk.VirtualPin(6);
/** CPU load five */
this.v7 = new this.blynk.VirtualPin(7);
/** CPU load fifteen */
this.v8 = new this.blynk.VirtualPin(8);
this.setupEvents();
this.setupVirtualWrites()
}
setupVirtualWrites() {
this.v0.on('write', param => {
this.refreshInterval = param * 1000;
this.resources.initWorkers(this.refreshInterval);
this.clearInterval();
this.setupInterval();
});
}
syncPins() {
console.log('Syncing Virtual Pins');
this.blynk.syncVirtual(0);
}
sendData() {
this.v1.write(this.resources.ramUsagePercent);
this.v2.write(this.resources.cpuPercent);
this.v3.write(this.resources.cpuTemp);
this.v4.write(this.resources.cpuFreq);
this.v5.write(this.resources.diskUsagePercent);
this.v6.write(this.resources.loadOne);
this.v7.write(this.resources.loadFive);
this.v8.write(this.resources.loadFifteen);
}
setupEvents() {
this.blynk.on('connect', () => {
console.log("Blynk ready.");
this.syncPins();
});
this.blynk.on('disconnect', () => {
console.log("DISCONNECT");
});
}
setupInterval() {
console.log('Setting up sendData interval to', this.refreshInterval, 'ms');
this.sendDataInterval = setInterval(this.sendData.bind(this), this.refreshInterval);
}
clearInterval() {
clearInterval(this.sendDataInterval);
}
}
module.exports = RaspbettoBlynk;