Adding detection for files to control the script

This commit is contained in:
Skylar Grant 2022-11-26 21:40:51 -05:00
parent 202472bfbb
commit 2f9b114660

31
main.js
View File

@ -2,6 +2,8 @@
var gpio = require('rpi-gpio'); var gpio = require('rpi-gpio');
// Module for importing environment variables // Module for importing environment variables
var dotenv = require('dotenv').config(); var dotenv = require('dotenv').config();
// Module for working with files
var fs = require('fs');
// Set up GPIO 4 (pysical pin 7) as output, then call cycleAuger() // Set up GPIO 4 (pysical pin 7) as output, then call cycleAuger()
gpio.setup(7, gpio.DIR_OUT, cycleAuger); gpio.setup(7, gpio.DIR_OUT, cycleAuger);
@ -44,7 +46,34 @@ function sleep(ms) {
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
async function cycleAuger(err) { async function cycleAuger(err) {
if (err) throw err; if (err) throw err;
// TODO: Detect file 'pause', 'reload', 'quit' // Check for pause file existing, then sleep for preset time, then run the function again.
if (fs.existsSync('./pause')) {
console.log('Paused...');
await sleep(process.env.PAUSETIME);
cycleAuger();
}
// Check for reload file existing, then reload environment variables, then delete the file.
if (fs.existsSync('./reload')) {
dotenv.config({ override: true })
fs.unlink('./reload', (err) => {
if (err) throw err;
console.log('Deleted reload file.');
});
console.log('Reloaded environment variables.');
}
// Check for quit file existing, then delete it, then quit the program
if (fs.existsSync('./quit')) {
fs.unlink('./quit', (err) => {
if (err) throw err;
console.log('Removed quit file.');
});
console.log('Quitting.');
process.exit();
}
// If none of the above checks are true, cycle the auger on and off, then repeat
augerOn(); augerOn();
await sleep(process.env.ONTIME); await sleep(process.env.ONTIME);
augerOff(); augerOff();