hestia/main.js

112 lines
4.2 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-06 22:08:47 +00:00
// Config File
const config = require('./config.json');
config.startTime = Date.now();
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 22:08:47 +00:00
console.log(`[${(Date.now() - config.startTime)/1000}] == 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) {
2022-12-06 22:08:47 +00:00
console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
2022-12-04 00:55:47 +00:00
main(fn, gpio);
} else {
2022-12-06 22:08:47 +00:00
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
2022-12-04 00:55:47 +00:00
}
});
} else if (process.env.ONPI == 'false') {
2022-12-06 22:08:47 +00:00
console.log(`[${(Date.now() - config.startTime)/1000}] I: 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) {
2022-12-06 22:08:47 +00:00
console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
2022-12-04 00:55:47 +00:00
main(fn, gpio);
} else {
console.error(rej);
}
});
} else {
2022-12-06 22:08:47 +00:00
console.error(`[${Date.now() - config.startTime}] 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-06 22:08:47 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/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 => {
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
});
break;
case "quit":
2022-12-04 01:54:30 +00:00
// Quit the script
fn.commands.quit();
break;
2022-12-06 22:08:47 +00:00
case "ignite":
// Start the igniter and timer
fn.commands.ignite(gpio).then(res => {
if (config.debugMode) console.log(res);
}).catch(rej => {
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
});
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.
2022-12-06 22:08:47 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
// Run the status check function
statusCheck(fn, gpio);
2022-12-04 01:54:30 +00:00
// Rerun this function once the cycle is complete
2022-12-06 22:08:47 +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.
2022-12-06 22:08:47 +00:00
console.error(`[${(Date.now() - config.startTime)/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) => {
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
main(fn, gpio);
});
2022-11-27 00:49:07 +00:00
}