-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
58 lines (51 loc) · 1.4 KB
/
functions.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
var fs = require('fs');
var exec = require('child_process').exec;
var moment = require('moment');
var config = require('./config.json');
// Execute CLI cmds
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
// Parse Temperature
function parse_temp(data){
return data.substr(data.indexOf('=')+1, 4);
}
// Check if a file exists
function _fileExists(filePath){
try
{
return fs.statSync(filePath).isFile();
}
catch (err)
{
return false;
}
}
// Get Logs
function getLogs(){
var filePath = '/tmp/fan_output';
if (_fileExists(filePath))
return fs.readFileSync(filePath, "utf8");
else
return "No Logs in "+filePath;
}
// Function to replace all occurrencies in a given string
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
// Check if the fans can spin in the current timestamp
function allowedInterval(now){
if (!moment(now).isValid()) return false;
var start_at = moment(config.FORBIDDEN_INTERVAL.hour, 'h:mm');
var until = moment(start_at).add(config.FORBIDDEN_INTERVAL.interval, 'hours');
if (moment(now).isBetween(start_at, until))
return false;
else
return true;
}
// Exporting the functions
exports.execute = execute;
exports.parse_temp = parse_temp;
exports.getLogs = getLogs;
exports.replaceAll = replaceAll;
exports.allowedInterval = allowedInterval;