Merge pull request #2 from voidf1sh:dev

Documentation and error handling
This commit is contained in:
Skylar Grant 2022-12-03 20:55:50 -05:00 committed by GitHub
commit 37a8c9486f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 10 deletions

View File

@ -2,12 +2,14 @@
const dotenv = require('dotenv').config(); const dotenv = require('dotenv').config();
// Module for working with files // Module for working with files
const fs = require('fs'); const fs = require('fs');
// Promises I think?
const { resolve } = require('path'); const { resolve } = require('path');
// The functions we'll export to be used in other files // The functions we'll export to be used in other files
const functions = { const functions = {
auger: { auger: {
// Gets called once the Auger Pin has been setup by rpi-gpio
ready(err) { ready(err) {
if (err) throw err; if (err) throw err;
console.log('Auger GPIO Ready'); console.log('Auger GPIO Ready');
@ -48,14 +50,23 @@ const functions = {
// Sleeps in between cycles using functions.sleep() // Sleeps in between cycles using functions.sleep()
cycle(gpio) { cycle(gpio) {
return new Promise((resolve) => { return new Promise((resolve) => {
// Turn the auger on
this.on(gpio).then((res) => { this.on(gpio).then((res) => {
// Log action if in debug mode
if (process.env.DEBUG == 'true') console.log(res); if (process.env.DEBUG == 'true') console.log(res);
// Sleep for the time set in env variables
functions.sleep(process.env.ONTIME).then((res) => { functions.sleep(process.env.ONTIME).then((res) => {
// Log action if in debug mode
if (process.env.DEBUG == 'true') console.log(res); if (process.env.DEBUG == 'true') console.log(res);
// Turn the auger off
this.off(gpio).then((res) => { this.off(gpio).then((res) => {
// Log action if in debug mode
if (process.env.DEBUG == 'true') console.log(res); if (process.env.DEBUG == 'true') console.log(res);
// Sleep for the time set in env variables
functions.sleep(process.env.OFFTIME).then((res) => { functions.sleep(process.env.OFFTIME).then((res) => {
// Log action if in debug mode
if (process.env.DEBUG == 'true') console.log(res); if (process.env.DEBUG == 'true') console.log(res);
// Resolve the promise, letting the main script know the cycle is complete
resolve("Cycle complete."); resolve("Cycle complete.");
}); });
}); });
@ -65,52 +76,67 @@ const functions = {
}, },
}, },
files: { files: {
// Check for a preset-list of files in the root directory of the app
check() { check() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// TODO this code needs to be finished from migration // Check for pause file existing
// Check for pause file existing, then sleep for preset time, then run the function again.
if (fs.existsSync('./pause')) { if (fs.existsSync('./pause')) {
// Resolve the promise, letting the main script know what we found
resolve("pause"); resolve("pause");
} }
// Check for reload file existing, then reload environment variables, then delete the file. // Check for reload file existing
if (fs.existsSync('./reload')) { if (fs.existsSync('./reload')) {
// Resolve the promise, letting the main script know what we found
resolve("reload"); resolve("reload");
} }
// Check for quit file existing, then delete it, then quit the program // Check for quit file existing
if (fs.existsSync('./quit')) { if (fs.existsSync('./quit')) {
// Resolve the promise, letting the main script know what we found
resolve("quit"); resolve("quit");
} }
// Resolve the promise, letting the main script know what we found (nothing)
resolve("none"); resolve("none");
}); });
}, },
}, },
commands: { commands: {
// Pauses the script for the time defined in env variables
pause() { pause() {
return new Promise((resolve) => { return new Promise((resolve) => {
functions.sleep(process.env.PAUSETIME).then(() => { resolve(); }); functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
}); });
}, },
// Reload the environment variables on the fly
reload(envs) { reload(envs) {
return new Promise((resolve) => { return new Promise((resolve) => {
// Re-require dotenv because inheritance in js sucks
const dotenv = require('dotenv').config({ override: true }); const dotenv = require('dotenv').config({ override: true });
// Delete the reload file
fs.unlink('./reload', (err) => { fs.unlink('./reload', (err) => {
if (err) throw err; if (err) throw err;
console.log('Deleted reload file.'); if (process.env.DEBUG == 'true') console.log('Deleted reload file.');
}); });
// Print out the new environment variables
// This should be printed regardless of debug status, maybe prettied up TODO?
console.log('Reloaded environment variables.'); console.log('Reloaded environment variables.');
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`); console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
// Resolve the promise, letting the main script know we're done reloading the variables and the cycle can continue
resolve(); resolve();
}); });
}, },
// Shutdown the script gracefully
quit() { quit() {
// Delete the quit file
fs.unlink('./quit', (err) => { fs.unlink('./quit', (err) => {
if (err) throw err; if (err) throw err;
console.log('Removed quit file.'); if (process.env.DEBUG == 'true') console.log('Removed quit file.');
}); });
// Print out that the script is quitting
console.log('Quitting...'); console.log('Quitting...');
// Quit the script
process.exit(); process.exit();
}, },
}, },
@ -118,12 +144,16 @@ const functions = {
sleep(ms) { sleep(ms) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`); if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
// Function to be called when setTimeout finishes
const finish = () => { const finish = () => {
// Resolve the promise
resolve(`Slept for ${ms}ms`); resolve(`Slept for ${ms}ms`);
} };
// The actual sleep function, sleeps for ms then calls finish()
setTimeout(finish, ms); setTimeout(finish, ms);
}); });
}, },
// Initializes rpi-gpio, or resolves if not on a raspberry pi
init(gpio) { init(gpio) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Write the current env vars to console // Write the current env vars to console
@ -133,13 +163,16 @@ const functions = {
if (process.env.ONPI == 'true') { if (process.env.ONPI == 'true') {
gpio.setup(7, gpio.DIR_OUT, (err) => { gpio.setup(7, gpio.DIR_OUT, (err) => {
if (err) reject(err); if (err) reject(err);
// Resolve the promise
resolve('GPIO Initialized'); resolve('GPIO Initialized');
}); });
} else { } else {
// Resolve the promise
resolve('GPIO Not Available'); resolve('GPIO Not Available');
} }
}); });
}, },
} }
// Export the above object, functions, as a module
module.exports = { functions }; module.exports = { functions };

19
main.js
View File

@ -37,32 +37,45 @@ if (process.env.ONPI == 'true') {
// 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 main(fn, gpio) { async function main(fn, gpio) {
// Check for the existence of certain files
fn.files.check().then((res,rej) => { fn.files.check().then((res,rej) => {
console.log('File Check: ' + res); // Log the result of the check if in debug mode
if (process.env.DEBUG == 'true') console.log('File Check: ' + res);
// Choose what to do depending on the result of the check
switch (res) { switch (res) {
case "pause": case "pause":
// Pause the script
fn.commands.pause().then(() => { fn.commands.pause().then(() => {
// Rerun this function once the pause has finished
main(fn, gpio); main(fn, gpio);
}); });
break; break;
case "reload": case "reload":
// Reload the environment variables
fn.commands.reload().then(() => { fn.commands.reload().then(() => {
// Rerun this function once the reload has finished
main(fn, gpio); main(fn, gpio);
}); });
break; break;
case "quit": case "quit":
// Quit the script
fn.commands.quit(); fn.commands.quit();
break; break;
case "none": case "none":
// If no special files are found, cycle the auger normally
fn.auger.cycle(gpio).then((res) => { fn.auger.cycle(gpio).then((res) => {
// Log the auger cycle results if in debug mode.
if (process.env.DEBUG == 'true') console.log(res); if (process.env.DEBUG == 'true') console.log(res);
// Rerun this function once the cycle is complete
main(fn, gpio); main(fn, gpio);
}); });
break; break;
default: default:
main(fn, gpio); // If we don't get a result from the file check, or for some reason it's an unexpected response, log it and quit the script.
break; console.error(`No result was received, something is wrong.\nres: ${res}`);
fn.commands.quit();
break;
} }
}); });
} }