hestia/main.js

156 lines
6.1 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:
* 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-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-18 18:14:34 +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-19 01:31:39 +00:00
fn.commands.shutdown(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 => {
if (config.debugMode) console.log(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-08 17:37:37 +00:00
fn.commands.shutdown(gpio).then(res => {
fn.commands.quit();
}).catch(rej => {
console.error(rej);
fn.commands.quit();
});
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-18 18:14:34 +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-19 02:51:15 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger Status: ${config.status.auger}`);
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) {
fn.tests.igniter(gpio).then((res) => {
2022-12-18 18:14:34 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-06 22:08:47 +00:00
main(fn, gpio);
});
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-17 03:30:12 +00:00
fn.tests.vacuum(gpio).then(status => {
if (!status) {
fn.commands.shutdown(gpio);
}
2022-12-18 17:47:03 +00:00
});
// Check the Proof of Fire Switch
fn.tests.pof(gpio).then(status => {
// If the igniter has finished running and no proof of fire is seen, shutdown the stove
if (config.status.igniterFinished && (!status)) fn.commands.shutdown(gpio);
});
2022-12-17 03:30:12 +00:00
2022-12-18 18:14:34 +00:00
// blower.canShutdown() returns true only if the blower shutdown has
2022-12-17 03:30:12 +00:00
// been initiated AND the specified cooldown time has passed
2022-12-18 18:14:34 +00:00
if(fn.blower.canShutdown()) {
2022-12-17 03:30:12 +00:00
fn.power.blower.off(gpio).then(res => {
// Since the blower shutting off is the last step in the shutdown, we can quit.
2022-12-18 18:14:34 +00:00
// TODO eventually we don't want to ever quit the program, so it can be restarted remotely
2022-12-17 03:30:12 +00:00
fn.commands.quit();
});
}
2022-11-27 00:49:07 +00:00
}