-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (48 loc) · 2.07 KB
/
index.js
File metadata and controls
57 lines (48 loc) · 2.07 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
'use strict';
const config = require('./config');
const SensorMonitor = require('./sensor-monitor.js');
let mqtt = require('mqtt');
let options = {
port: config.mqtt.port,
clientId: config.mqtt.clientId.trim().length ? config.mqtt.clientId : 'mqttjs_' + Math.random().toString(16).substr(2, 8),
username: config.mqtt.username,
password: config.mqtt.password,
};
let client = mqtt.connect(config.mqtt.host, options);
client.on('connect', () => {
console.log('Connected to broker on ' + config.mqtt.host + ':' + config.mqtt.port);
let oldCpuTemperatures = [];
let oldGraphiCardTempeatures = [];
let intervalCpuTemps = setInterval(async () => {
try {
let cpuTemps = await SensorMonitor.getProcessorTemps();
cpuTemps.forEach((temperature, index) => {
if (!oldCpuTemperatures[index] || oldCpuTemperatures[index] !== temperature) {
if (config.log) {
console.log('CPU Thread ' + index + ': ' + temperature)
}
oldCpuTemperatures[index] = temperature;
client.publish('computer/temperatures/cpu/' + index, "" + temperature);
}
});
let graphicTemperatures = await SensorMonitor.getGraphicTemperatures();
graphicTemperatures.forEach((temperature, index) => {
if (!oldGraphiCardTempeatures[index] || oldGraphiCardTempeatures[index] !== temperature) {
if (config.log) {
console.log('Graphic card ' + index + ': ' + temperature);
}
client.publish('computer/temperatures/graphic-card/' + index, temperature);
oldGraphiCardTempeatures[index] = temperature;
}
});
} catch (error) {
console.error(error);
}
}, config.interval);
});
client.on('close', () => {
console.error('Unable to connect with broker');
});
client.on('error', (error) => {
console.error('The following error has occurred ' + error);
});