hestia/main.js

81 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-12-03 23:10:31 +00:00
// Custom functions module to keep main script clean
const fn = require('./functions.js').functions;
2022-11-27 01:50:20 +00:00
2022-12-03 23:10:31 +00:00
// Environment Variables Importing
const dotenv = require('dotenv').config();
2022-12-04 00:55:47 +00:00
// Setup for use with the Pi's GPIO pins
if (process.env.ONPI == 'true') {
console.log('Running on a Raspberry Pi.');
const gpio = require('rpi-gpio');
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else if (process.env.ONPI == 'false') {
console.log('Not running on a Raspberry Pi.');
const gpio = 'gpio';
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else {
console.log('Problem with ENV file.');
}
2022-11-27 03:14:18 +00:00
// TODO Add logic for other sensors
2022-12-04 00:55:47 +00:00
2022-11-27 00:33:33 +00:00
2022-11-27 01:56:04 +00:00
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
2022-12-04 00:55:47 +00:00
async function main(fn, gpio) {
2022-12-04 01:54:30 +00:00
// Check for the existence of certain files
fn.files.check().then((res,rej) => {
2022-12-04 01:54:30 +00:00
// 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":
2022-12-04 01:54:30 +00:00
// Pause the script
fn.commands.pause().then(() => {
2022-12-04 01:54:30 +00:00
// Rerun this function once the pause has finished
2022-12-04 01:28:04 +00:00
main(fn, gpio);
});
break;
case "reload":
2022-12-04 01:54:30 +00:00
// Reload the environment variables
fn.commands.reload().then(() => {
2022-12-04 01:54:30 +00:00
// Rerun this function once the reload has finished
2022-12-04 01:28:04 +00:00
main(fn, gpio);
});
break;
case "quit":
2022-12-04 01:54:30 +00:00
// Quit the script
fn.commands.quit();
break;
case "none":
2022-12-04 01:54:30 +00:00
// If no special files are found, cycle the auger normally
fn.auger.cycle(gpio).then((res) => {
2022-12-04 01:54:30 +00:00
// Log the auger cycle results if in debug mode.
if (process.env.DEBUG == 'true') console.log(res);
2022-12-04 01:54:30 +00:00
// Rerun this function once the cycle is complete
2022-12-04 01:28:04 +00:00
main(fn, gpio);
});
break;
default:
2022-12-04 01:54:30 +00:00
// 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;
}
});
2022-11-27 00:49:07 +00:00
}