hestia/main.js

164 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-12-06 05:05:35 +00:00
/* Pellet Stove Control Panel
* Written by Skylar Grant
2022-12-18 17:47:03 +00:00
* v0.2
2022-12-06 05:05:35 +00:00
*
* TODO:
2022-12-21 02:12:22 +00:00
* Update documentation
* Move some of these functions to the functions file so they can be called from the web server
* Or move the web server here and remove the first init
* Or just remove the first init call and start the init elsewhere after main() is called
2022-12-06 05:05:35 +00:00
*/
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-06 22:08:47 +00:00
// Config File
const config = require('./config.json');
2022-12-18 18:14:34 +00:00
config.timestamps.procStart = Date.now();
2022-12-06 22:08:47 +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-18 18:14:34 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] == Running on a Raspberry Pi.`);
2022-12-04 00:55:47 +00:00
const gpio = require('rpi-gpio');
2022-12-08 17:45:46 +00:00
fn.init(gpio).then((res) => {
2022-12-18 18:14:34 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-08 17:45:46 +00:00
main(fn, gpio);
}).catch(rej => {
2022-12-18 18:14:34 +00:00
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
2022-12-04 00:55:47 +00:00
});
} else if (process.env.ONPI == 'false') {
2022-12-18 18:14:34 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Not running on a Raspberry Pi.`);
2022-12-04 00:55:47 +00:00
const gpio = 'gpio';
2022-12-08 17:45:46 +00:00
fn.init(gpio).then(res => {
2022-12-18 18:14:34 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-08 17:45:46 +00:00
main(fn, gpio);
}).catch(rej => {
console.error(rej);
2022-12-04 00:55:47 +00:00
});
} else {
2022-12-18 18:14:34 +00:00
console.error(`[${Date.now() - config.timestamps.procStart}] E: 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
2022-12-20 02:25:17 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: File Check: ${res}`);
2022-12-04 01:54:30 +00:00
// 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);
2022-12-06 22:08:47 +00:00
}).catch(rej => {
2022-12-18 18:14:34 +00:00
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
});
break;
case "quit":
2022-12-04 01:54:30 +00:00
// Quit the script
2022-12-20 20:04:37 +00:00
console.log(fn.commands.shutdown(gpio));
statusCheck(fn, gpio);
break;
2022-12-06 22:08:47 +00:00
case "ignite":
2022-12-19 02:29:50 +00:00
// Start the ignite sequence
2022-12-06 22:08:47 +00:00
fn.commands.ignite(gpio).then(res => {
2022-12-20 02:25:17 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-08 17:54:20 +00:00
statusCheck(fn, gpio);
2022-12-06 22:08:47 +00:00
}).catch(rej => {
2022-12-18 18:14:34 +00:00
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
2022-12-20 02:25:17 +00:00
fn.commands.shutdown(gpio);
2022-12-06 22:08:47 +00:00
});
2022-12-08 17:51:39 +00:00
break;
2022-12-08 17:37:37 +00:00
case "start":
// Start the stove
fn.commands.startup(gpio).then(res => {
2022-12-06 22:08:47 +00:00
statusCheck(fn, gpio);
2022-12-08 17:37:37 +00:00
}).catch(rej => {
2022-12-08 17:54:20 +00:00
// TODO
});
2022-12-08 17:51:39 +00:00
break;
2022-12-08 17:37:37 +00:00
case "none":
// If no special files are found, cycle the auger normally
if (config.status.auger == 1) {
fn.auger.cycle(gpio).then((res) => {
// Log the auger cycle results if in debug mode.
2022-12-21 18:50:09 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-08 17:37:37 +00:00
// Run the status check function
statusCheck(fn, gpio);
// Rerun this function once the cycle is complete
// main(fn, gpio);
});
} else {
2022-12-21 18:50:09 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger Status: ${config.status.auger}`);
2022-12-19 02:51:15 +00:00
2022-12-08 17:37:37 +00:00
fn.commands.pause().then(res => {
statusCheck(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.
2022-12-18 18:14:34 +00:00
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: No result was received, something is wrong.\nres: ${res}`);
2022-12-04 01:54:30 +00:00
fn.commands.quit();
break;
}
});
2022-12-06 22:08:47 +00:00
}
function statusCheck(fn, gpio) {
2022-12-21 18:50:09 +00:00
fn.commands.writeConfig();
2022-12-20 20:04:37 +00:00
if (config.status.shutdown == 1) {
console.log(fn.commands.shutdown(gpio) || 'Shutting down...');
main(fn, gpio);
return;
}
if (config.status.igniter == 1) {
fn.tests.igniter(gpio).then((res) => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
});
}
2022-12-17 03:30:12 +00:00
2022-12-18 17:47:03 +00:00
// Check the vacuum switch, if the test returns true, the vacuum is sensed
// if it returns false, we will initiate a shutdown
2022-12-20 02:25:17 +00:00
// TODO this is messed up
fn.tests.vacuum(gpio).then(vacStatus => {
if (!vacStatus) {
console.error('No vacuum detected, beginning shutdown procedure.');
2022-12-20 20:04:37 +00:00
console.log(fn.commands.shutdown(gpio));
main(fn, gpio);
2022-12-20 02:25:17 +00:00
} else {
// Check the Proof of Fire Switch
fn.tests.pof(gpio).then(pofStatus => {
// If the igniter has finished running and no proof of fire is seen, shutdown the stove
if (config.status.igniterFinished && (!pofStatus)) {
console.error('No Proof of Fire after the igniter shut off, beginning shutdown procedure.');
2022-12-20 20:04:37 +00:00
console.log(fn.commands.shutdown(gpio));
main(fn, gpio);
2022-12-20 02:25:17 +00:00
} else {
2022-12-21 18:50:09 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Vacuum and Proof of Fire OK.`);
2022-12-20 02:25:17 +00:00
main(fn, gpio);
}
});
2022-12-17 03:30:12 +00:00
}
2022-12-18 17:47:03 +00:00
});
2022-12-21 02:12:22 +00:00
}
module.exports = main;