forked from joeledwards/sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmon.js
170 lines (139 loc) · 3.9 KB
/
sysmon.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require('log-a-log');
console.log("log-a-log setup")
const fs = require('fs');
console.log("system modules loaded")
const P = require('bluebird');
const cogs = require('cogs-sdk');
const async = require('async');
const moment = require('moment');
const durations = require('durations');
console.log("3rd party modules loaded")
const pi = require('./pi');
console.log("local modules loaded")
const CPU_TEMP_PATH = '/sys/class/thermal/thermal_zone0/temp';
const GREEN_PIN = 16;
const RED_PIN = 37;
const red = {
on: () => pi.on(RED_PIN),
off: () => pi.off(RED_PIN),
};
const green = {
on: () => pi.on(GREEN_PIN),
off: () => pi.off(GREEN_PIN),
};
const readFile = P.promisify(fs.readFile);
function fileExists(fileName) {
return new P(resolve => fs.exists(fileName, resolve));
}
// Read the CPU's temperature.
function readCpuTemp() {
return readFile(CPU_TEMP_PATH)
.then(temperature => ({ cpuTemp: parseFloat(temperature) }));
}
// Schedules the next reading at the beginning
// of the next second.
function scheduleNextReading(data) {
let next = moment.utc().add(1, 'seconds');
next.milliseconds(0);
let now = moment.utc();
let offset = next.valueOf() - now.valueOf();
setTimeout(() => takeReading(data), offset);
}
// Take a readings from the system.
const firstReading = true;
const readingDelay = durations.stopwatch();
function takeReading() {
const timestamp = moment.utc().toISOString();
if (readingDelay.duration().millis() >= 1500) {
console.log("Reading too slowly!");
}
readingDelay.reset().start();
red.on()
.then(() => readCpuTemp())
.then(({cpuTemp}) => {
let cpuTempC = parseFloat((cpuTemp / 1000).toFixed(3));
let cpuTempF = parseFloat(((cpuTemp / 1000) * 1.8 + 32).toFixed(3));
publishData({
timestamp,
cpu_temp_c: cpuTempC,
cpu_temp_f: cpuTempF,
});
console.log(`${cpuTempC} C (${cpuTempF} F)`);
scheduleNextReading();
})
.catch(error => {
console.error("Error reading sensor data:", error);
})
.finally(() => red.off());
}
// Fetch the Cogs config file.
const cogsConfigFile = process.env.COGS_CONFIG_FILE;
let cogsConfig;
function getCogsConfig() {
if (cogsConfig == null) {
if (cogsConfigFile == null) {
console.log("Cogs config file not supplied.");
cogsConfig = P.resolve({});
} else {
cogsConfig = readFile(cogsConfigFile)
.then(JSON.parse)
.catch(error => {
console.error("Could not read cogs config file:", error)
throw error;
});
}
}
return cogsConfig;
}
// Fetch the Cogs Pub/Sub client.
let cogsClient;
function getCogsClient() {
if (cogsClient == null) {
cogsClient = getCogsConfig()
.then(cfg => {
if (cfg.pubsub) {
let {keys, options} = cfg.pubsub;
return cogs.pubsub.connect(keys, options);
} else {
return P.resolve({publish: () => {}});
}
})
.catch(error => {
// If we initiate the cogs handle when a config file was supplied,
// then shutdown the connection.
logger.error("Terminating due to error creating cogs client:", error);
return process.exit(1);
});
}
return cogsClient;
}
const publishChannel = 'groot-sysmon-1hz';
function publishData(data) {
const message = JSON.stringify(data);
/*
green.on()
.then(() => console.log(`Publishing data: ${message}`))
.finally(() => green.off());
//*/
//*
const watch = durations.stopwatch().start();
green.on()
.then(() => getCogsClient())
.then(client => client.publishWithAck(publishChannel, message))
.then(() => {
if (watch.stop().duration().millis() > 100) {
console.log(`Publish took a long time: ${watch}`);
}
})
.catch(error => console.error('Error publishing data:', error))
.finally(() => green.off());
//*/
}
function run() {
scheduleNextReading();
}
// Handle shutdown signal.
process.on('SIGINT', () => {
pi.shutdown().then(() => process.exit(0));
});
run();