Documentation and error handling
This commit is contained in:
parent
b91503dfa6
commit
96339fa208
47
functions.js
47
functions.js
@ -2,12 +2,14 @@
|
||||
const dotenv = require('dotenv').config();
|
||||
// Module for working with files
|
||||
const fs = require('fs');
|
||||
// Promises I think?
|
||||
const { resolve } = require('path');
|
||||
|
||||
|
||||
// The functions we'll export to be used in other files
|
||||
const functions = {
|
||||
auger: {
|
||||
// Gets called once the Auger Pin has been setup by rpi-gpio
|
||||
ready(err) {
|
||||
if (err) throw err;
|
||||
console.log('Auger GPIO Ready');
|
||||
@ -48,14 +50,23 @@ const functions = {
|
||||
// Sleeps in between cycles using functions.sleep()
|
||||
cycle(gpio) {
|
||||
return new Promise((resolve) => {
|
||||
// Turn the auger on
|
||||
this.on(gpio).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (process.env.DEBUG == 'true') console.log(res);
|
||||
// Sleep for the time set in env variables
|
||||
functions.sleep(process.env.ONTIME).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (process.env.DEBUG == 'true') console.log(res);
|
||||
// Turn the auger off
|
||||
this.off(gpio).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (process.env.DEBUG == 'true') console.log(res);
|
||||
// Sleep for the time set in env variables
|
||||
functions.sleep(process.env.OFFTIME).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (process.env.DEBUG == 'true') console.log(res);
|
||||
// Resolve the promise, letting the main script know the cycle is complete
|
||||
resolve("Cycle complete.");
|
||||
});
|
||||
});
|
||||
@ -65,52 +76,67 @@ const functions = {
|
||||
},
|
||||
},
|
||||
files: {
|
||||
// Check for a preset-list of files in the root directory of the app
|
||||
check() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// TODO this code needs to be finished from migration
|
||||
// Check for pause file existing, then sleep for preset time, then run the function again.
|
||||
// Check for pause file existing
|
||||
if (fs.existsSync('./pause')) {
|
||||
// Resolve the promise, letting the main script know what we found
|
||||
resolve("pause");
|
||||
}
|
||||
|
||||
// Check for reload file existing, then reload environment variables, then delete the file.
|
||||
// Check for reload file existing
|
||||
if (fs.existsSync('./reload')) {
|
||||
// Resolve the promise, letting the main script know what we found
|
||||
resolve("reload");
|
||||
}
|
||||
|
||||
// Check for quit file existing, then delete it, then quit the program
|
||||
// Check for quit file existing
|
||||
if (fs.existsSync('./quit')) {
|
||||
// Resolve the promise, letting the main script know what we found
|
||||
resolve("quit");
|
||||
}
|
||||
// Resolve the promise, letting the main script know what we found (nothing)
|
||||
resolve("none");
|
||||
});
|
||||
},
|
||||
},
|
||||
commands: {
|
||||
// Pauses the script for the time defined in env variables
|
||||
pause() {
|
||||
return new Promise((resolve) => {
|
||||
functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
|
||||
});
|
||||
},
|
||||
// Reload the environment variables on the fly
|
||||
reload(envs) {
|
||||
return new Promise((resolve) => {
|
||||
// Re-require dotenv because inheritance in js sucks
|
||||
const dotenv = require('dotenv').config({ override: true });
|
||||
// Delete the reload file
|
||||
fs.unlink('./reload', (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(`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();
|
||||
});
|
||||
|
||||
},
|
||||
// Shutdown the script gracefully
|
||||
quit() {
|
||||
// Delete the quit file
|
||||
fs.unlink('./quit', (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...');
|
||||
// Quit the script
|
||||
process.exit();
|
||||
},
|
||||
},
|
||||
@ -118,12 +144,16 @@ const functions = {
|
||||
sleep(ms) {
|
||||
return new Promise((resolve) => {
|
||||
if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
|
||||
// Function to be called when setTimeout finishes
|
||||
const finish = () => {
|
||||
// Resolve the promise
|
||||
resolve(`Slept for ${ms}ms`);
|
||||
}
|
||||
};
|
||||
// The actual sleep function, sleeps for ms then calls finish()
|
||||
setTimeout(finish, ms);
|
||||
});
|
||||
},
|
||||
// Initializes rpi-gpio, or resolves if not on a raspberry pi
|
||||
init(gpio) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Write the current env vars to console
|
||||
@ -133,13 +163,16 @@ const functions = {
|
||||
if (process.env.ONPI == 'true') {
|
||||
gpio.setup(7, gpio.DIR_OUT, (err) => {
|
||||
if (err) reject(err);
|
||||
// Resolve the promise
|
||||
resolve('GPIO Initialized');
|
||||
});
|
||||
} else {
|
||||
// Resolve the promise
|
||||
resolve('GPIO Not Available');
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
// Export the above object, functions, as a module
|
||||
module.exports = { functions };
|
17
main.js
17
main.js
@ -37,31 +37,44 @@ 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.
|
||||
async function main(fn, gpio) {
|
||||
// Check for the existence of certain files
|
||||
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) {
|
||||
case "pause":
|
||||
// Pause the script
|
||||
fn.commands.pause().then(() => {
|
||||
// Rerun this function once the pause has finished
|
||||
main(fn, gpio);
|
||||
});
|
||||
break;
|
||||
case "reload":
|
||||
// Reload the environment variables
|
||||
fn.commands.reload().then(() => {
|
||||
// Rerun this function once the reload has finished
|
||||
main(fn, gpio);
|
||||
});
|
||||
break;
|
||||
case "quit":
|
||||
// Quit the script
|
||||
fn.commands.quit();
|
||||
break;
|
||||
case "none":
|
||||
// If no special files are found, cycle the auger normally
|
||||
fn.auger.cycle(gpio).then((res) => {
|
||||
// Log the auger cycle results if in debug mode.
|
||||
if (process.env.DEBUG == 'true') console.log(res);
|
||||
// Rerun this function once the cycle is complete
|
||||
main(fn, gpio);
|
||||
});
|
||||
break;
|
||||
|
||||
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.
|
||||
console.error(`No result was received, something is wrong.\nres: ${res}`);
|
||||
fn.commands.quit();
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user