-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
82 lines (67 loc) · 2.45 KB
/
main.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
'use strict';
// CONFIG - edit these ########################################################################
const config = {
battery: 1, // which port on the MoPi is the backup battery connected to
mainPower: 2, // which port on the MoPi is the main power connected to
timer: 11, // how long should we wait before deciding the main power is gone for good
mainLowVoltage: 10, // when the main battery drops below this, start timer
pollingInterval: 2, // how often should we check if voltage has dropped, in s
shutdownDelay: 35 // how long to wait until power is cut, in seconds(min 30s)
}
// ############################################################################################
// DO NOT EDIT BEYOND THIS POINT
const mopiapi = require('./mopiapi');
const exec = require('child_process').exec;
const mopi = new mopiapi.mopiapi();
mopi.connect(function(err) {
if (err) return console.log(err);
function isMainVoltageGood() {
return new Promise((resolve) => {
mopi.getVoltage(config.mainPower, (err, voltage) => {
if (err) {
console.log('checkVoltage error', err);
resolve(false);
}
console.log('voltage: ' + voltage);
if (voltage < config.mainLowVoltage * 1000) resolve(false);
resolve(true);
});
});
}
const timerCounter = Math.ceil(config.timer / config.pollingInterval);
let shutdownCounter = 0;
function shouldShutdown() {
isMainVoltageGood().then(value => {
if (value === false) {
shutdownCounter++;
console.log(`counter: ${shutdownCounter}`);
if (shutdownCounter >= timerCounter) {
doShutdown();
}
} else {
shutdownCounter = 0;
}
});
mopi.getStatus((err, status) => {
if (err) return console.log("Error", err);
status = new mopiapi.status(status);
if (status.ForcedShutdown()) {
doShutdown();
}
});
}
function doShutdown() {
clearInterval(shutdownInterval);
console.log('SHUTTING DOWN!');
let delay = config.shutdownDelay - 30 < 0 ? 0 : config.shutdownDelay - 30;
mopi.setShutdownDelay(delay, (err) => {
if (err) return console.log("Error", err);
mopi.getShutdownDelay((err, value) => {
if (err) return console.log("Error", err);
console.log(JSON.stringify(value));
});
});
exec("shutdown -h now");
}
let shutdownInterval = setInterval(shouldShutdown, config.pollingInterval * 1000);
});