poweroff-tool
Version:
This is application which power off a pc when it triggered
68 lines (57 loc) • 1.46 kB
JavaScript
const async = require('async');
const Validators = require('./utils/validators');
const SNPM = require('./utils/snmp');
const conditionChecker = require('./utils/condition-checker');
const poweroff = require('./utils/power-off');
function PowerOff() {
const application = {};
let isRunning = false;
if (process.argv.length < 3) {
console.log ('usage: node poweroff-tool <IP address> [...OIDs]');
process.exit(1);
}
var target = process.argv[2];
if (!Validators.validateIP(target)) {
console.log ('IP address is not valid');
process.exit(1);
}
var oids = process.argv.slice(3, process.argv.length);
// Init SNMP spoiler
var snpmSpoiler = new SNPM(target, oids);
var steps = [];
steps.push((callback) => {
console.time('workflows');
callback(null);
})
steps.push((callback) => {
snpmSpoiler.get(callback);
});
steps.push(conditionChecker);
steps.push(poweroff);
this.start = () => {
if (isRunning) {
return;
} else {
isRunning = true;
}
async.forever(function(next) {
async.waterfall(steps, (err, result) => {
if (err) {
console.log(err);
}
console.timeEnd('workflows');
if (isRunning) {
setTimeout(next, 2000);
}
});
}, function(err) {
console.error(err);
});
}
this.stop = (force) => {
if (isRunning) {
isRunning = false;
}
}
}
module.exports = PowerOff;