hestia/main.js

90 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-12-06 05:05:35 +00:00
/* Pellet Stove Control Panel
* Written by Skylar Grant
* v0.2.0
*
* TODO:
* Add logic for other sensors
* More documentation?
*/
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') {
2022-12-06 05:31:21 +00:00
console.log('== Running on a Raspberry Pi.');
2022-12-04 00:55:47 +00:00
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') {
2022-12-06 05:31:21 +00:00
console.log('== Not running on a Raspberry Pi.');
2022-12-04 00:55:47 +00:00
const gpio = 'gpio';
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else {
2022-12-06 05:31:21 +00:00
console.log('== Problem with ENV file.');
2022-12-04 00:55:47 +00:00
}
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
}