From 16f0b4e9fc1dee6710aebb40cdc6c6dba0697121 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 8 Dec 2022 12:37:37 -0500 Subject: [PATCH] Adding startup and shutdown functions --- config.json | 3 ++- functions.js | 53 +++++++++++++++++++++++++++++++++++++++++++++------- main.js | 35 ++++++++++++++++++++++++++-------- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/config.json b/config.json index 9606159..30074e9 100644 --- a/config.json +++ b/config.json @@ -10,6 +10,7 @@ "status": { "igniter": 0, "blower": 0, - "auger": 0 + "auger": 0, + "seenFire": false } } \ No newline at end of file diff --git a/functions.js b/functions.js index 50e3489..1dd6f7e 100644 --- a/functions.js +++ b/functions.js @@ -112,15 +112,40 @@ const functions = { if (fs.existsSync('./ignite')) { resolve('ignite'); } + + // Check for start file existing + if (fs.existsSync('./start')) { + resolve('start'); + } // Resolve the promise, letting the main script know what we found (nothing) resolve("none"); }); }, }, commands: { + // Prepare the stove for starting + startup (gpio) { + fs.unlink('./start', (err) => { + if (err) throw err; + }); + return new Promise((resolve, reject) => { + if (process.env.ONPI == 'true') { + // Turn the combustion blower on + functions.power.blower.on(gpio).then(res => { + resolve(`I: Combustion blower has been enabled.`); + }).catch(rej => { + reject(`E: There was a problem starting the combustion blower: ${rej}`); + }); + } else { + resolve(`I: Simulated combustion blower turned on.`); + } + }); + }, // Pauses the script for the time defined in env variables pause() { return new Promise((resolve) => { + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: Pausing for ${config.pauseTime}ms`); + functions.sleep(config.pauseTime).then(() => { resolve(); }); }); }, @@ -193,31 +218,45 @@ const functions = { }); }, igniter(gpio) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { var statusMsg = ""; if (config.status.igniter == 1) { - statusMsg += "The igniter is on. "; + statusMsg += "The igniter is on.\n"; } else if (config.status.igniter == 0) { - statusMsg += "The igniter is off. "; + statusMsg += "The igniter is off.\n"; } else { - statusMsg += "E: Unable to determine igniter status. "; + reject("E: Unable to determine igniter status."); } if (config.igniterOnTime > 0) { const humanStartTime = new Date(config.igniterOnTime).toISOString(); const humanEndTime = new Date(config.igniterOffTime).toISOString(); - statusMsg += `Igniter started: ${humanStartTime}. Igniter scheduled to stop: ${humanEndTime}`; + if (Date.now() < config.igniterOffTime && config.status.igniter == 1) { + statusMsg += `Igniter started: ${humanStartTime}.\n`; + statusMsg += `Igniter scheduled to stop: ${humanEndTime}.\n`; + } // Shut the igniter off if it's past the waiting period if ((Date.now() > config.igniterOffTime) && (config.status.igniter == 1)) { if (process.env.ONPI == 'true') { gpio.write(igniterPin, false, (err) => { if (err) throw(err); config.status.igniter = 0; - statusMsg += `\n${new Date().toISOString()} Turned off igniter.`; + statusMsg += `${new Date().toISOString()} I: Turned off igniter.`; + functions.tests.pof(gpio).then(res => { + if (res) { + config.status.seenFire = true; + } else { + reject(`E: No Proof of Fire after igniter shut off.`); + } + }).catch(rej => { + + }); }); } else { config.status.igniter = 0; - statusMsg += `\n${new Date().toISOString()} Simulated igniter turned off.`; + statusMsg += `${new Date().toISOString()} I: Simulated igniter turned off.`; } + } else if ((Date.now() > config.igniterOffTime) && (config.status.igniter == 0)) { + statusMsg += `The igniter was turned off at ${new Date(config.igniterOffTime).toISOString()}.`; } } else { statusMsg += 'The igniter hasn\'t been started yet.'; diff --git a/main.js b/main.js index 170d83d..11b08f3 100644 --- a/main.js +++ b/main.js @@ -82,17 +82,36 @@ async function main(fn, gpio) { if (config.debugMode) console.log(res); }).catch(rej => { console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`); + fn.commands.shutdown(gpio).then(res => { + fn.commands.quit(); + }).catch(rej => { + console.error(rej); + fn.commands.quit(); + }); + }); + case "start": + // Start the stove + fn.commands.startup(gpio).then(res => { + statusCheck(fn, gpio); + }).catch(rej => { + }); case "none": // If no special files are found, cycle the auger normally - fn.auger.cycle(gpio).then((res) => { - // Log the auger cycle results if in debug mode. - if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); - // Run the status check function - statusCheck(fn, gpio); - // Rerun this function once the cycle is complete - // main(fn, gpio); - }); + if (config.status.auger == 1) { + fn.auger.cycle(gpio).then((res) => { + // Log the auger cycle results if in debug mode. + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); + // Run the status check function + statusCheck(fn, gpio); + // Rerun this function once the cycle is complete + // main(fn, gpio); + }); + } else { + fn.commands.pause().then(res => { + statusCheck(fn, gpio); + }); + } break; default: