From 8b53d66109d48b835f583012bbf71460d964ea93 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Tue, 6 Dec 2022 17:07:47 -0500 Subject: [PATCH] Adding logic for igniter, and improving logging --- functions.js | 169 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 143 insertions(+), 26 deletions(-) diff --git a/functions.js b/functions.js index 6a2b78d..50e3489 100644 --- a/functions.js +++ b/functions.js @@ -8,6 +8,8 @@ const vacuumPin = 22; // Pin for sensing the status (open/closed) of the v // Require the package for pulling version numbers const package = require('./package.json'); +// Import the config file +const config = require('./config.json'); // Get environment variables const dotenv = require('dotenv').config(); @@ -35,8 +37,7 @@ const functions = { resolve('Auger turned on.'); }); } else { - console.log('NOPI Auger turned on.'); - resolve('NOPI Auger turned on.'); + resolve('Simulated auger turned on.'); } }); @@ -51,8 +52,7 @@ const functions = { }); } else { - console.log('NOPI Auger turned off.'); - resolve('NOPI Auger turned off.'); + resolve('Simulated auger turned off.'); } }); @@ -64,19 +64,19 @@ const functions = { // Turn the auger on this.on(gpio).then((res) => { // Log action if in debug mode - if (process.env.DEBUG == 'true') console.log(res); + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); // Sleep for the time set in env variables - functions.sleep(process.env.ONTIME).then((res) => { + functions.sleep(config.augerOnTime).then((res) => { // Log action if in debug mode - if (process.env.DEBUG == 'true') console.log(res); + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); // Turn the auger off this.off(gpio).then((res) => { // Log action if in debug mode - if (process.env.DEBUG == 'true') console.log(res); + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); // Sleep for the time set in env variables - functions.sleep(process.env.OFFTIME).then((res) => { + functions.sleep(config.augerOffTime).then((res) => { // Log action if in debug mode - if (process.env.DEBUG == 'true') console.log(res); + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`); // Resolve the promise, letting the main script know the cycle is complete resolve("Cycle complete."); }); @@ -107,6 +107,11 @@ const functions = { // Resolve the promise, letting the main script know what we found resolve("quit"); } + + // Check for ignite file existing + if (fs.existsSync('./ignite')) { + resolve('ignite'); + } // Resolve the promise, letting the main script know what we found (nothing) resolve("none"); }); @@ -116,7 +121,7 @@ const functions = { // Pauses the script for the time defined in env variables pause() { return new Promise((resolve) => { - functions.sleep(process.env.PAUSETIME).then(() => { resolve(); }); + functions.sleep(config.pauseTime).then(() => { resolve(); }); }); }, // Reload the environment variables on the fly @@ -127,12 +132,12 @@ const functions = { // Delete the reload file fs.unlink('./reload', (err) => { if (err) throw err; - if (process.env.DEBUG == 'true') console.log('Deleted reload file.'); + if (config.debugMode) console.log('Deleted reload file.'); }); // Print out the new environment variables // This should be printed regardless of debug status, maybe prettied up TODO? console.log('Reloaded environment variables.'); - console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`); + console.log(`ONTIME=${config.augerOnTime}\nOFFTIME=${config.augerOffTime}\nPAUSETIME=${config.pauseTime}\nDEBUG=${config.debugMode}\nONPI=${process.env.ONPI}`); // Resolve the promise, letting the main script know we're done reloading the variables and the cycle can continue resolve(); }); @@ -143,18 +148,130 @@ const functions = { // Delete the quit file fs.unlink('./quit', (err) => { if (err) throw err; - if (process.env.DEBUG == 'true') console.log('Removed quit file.'); + if (config.debugMode) console.log('Removed quit file.'); }); // Print out that the script is quitting console.log('Quitting...'); // Quit the script process.exit(); }, + ignite(gpio) { + config.status.igniter = 1; + config.igniterOnTime = Date.now(); + config.igniterOffTime = config.igniterOnTime + config.igniterWaitTime; // 7 Minutes, 420,000ms + return new Promise((resolve, reject) => { + fs.unlink('./ignite', (err) => { + if (err) reject(err); + if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: Delete the ignite file.`); + }); + if (process.env.ONPI == 'true') { + gpio.write(igniterPin, true, (err) => { + if (err) reject(err); + resolve('Igniter turned on.'); + }); + } else { + resolve('Simulated igniter turned on.'); + } + }); + }, + }, + tests: { + vacuum(gpio) { + return new Promise((resolve, reject) => { + gpio.read(vacuumPin, (err, status) => { + if (err) reject(err); + resolve(status); + }); + }); + }, + pof(gpio) { + return new Promise((resolve, reject) => { + gpio.read(pofPin, (err, status) => { + if (err) reject(err); + resolve(status); + }); + }); + }, + igniter(gpio) { + return new Promise((resolve) => { + var statusMsg = ""; + if (config.status.igniter == 1) { + statusMsg += "The igniter is on. "; + } else if (config.status.igniter == 0) { + statusMsg += "The igniter is off. "; + } else { + statusMsg += "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}`; + // 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.`; + }); + } else { + config.status.igniter = 0; + statusMsg += `\n${new Date().toISOString()} Simulated igniter turned off.`; + } + } + } else { + statusMsg += 'The igniter hasn\'t been started yet.'; + } + resolve(statusMsg); + }); + } + }, + power: { + igniter: { + on(gpio) { + // TODO + return new Promise((resolve, reject) => { + gpio.write(igniterPin, true, (err) => { + if (err) reject(err); + resolve(); + }); + }); + }, + off(gpio) { + // TODO + return new Promise((resolve, reject) => { + gpio.write(igniterPin, false, (err) => { + if (err) reject(err); + resolve(); + }); + }); + }, + }, + blower: { + on(gpio) { + // TODO + return new Promise((resolve, reject) => { + gpio.write(blowerPin, true, (err) => { + if (err) reject(err); + resolve(); + }); + }); + }, + off(gpio) { + // TODO + return new Promise((resolve, reject) => { + gpio.write(blowerPin, false, (err) => { + if (err) reject(err); + resolve(); + }); + }); + }, + }, }, // Sleeps for any given milliseconds sleep(ms) { return new Promise((resolve) => { - if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`); + // if (config.debugMode) console.log(`Sleeping for ${ms}ms`); // Function to be called when setTimeout finishes const finish = () => { // Resolve the promise @@ -176,37 +293,37 @@ const functions = { == Startup Time: ${new Date().toISOString()} == == Environment variables: -== == ONTIME=${process.env.ONTIME} -== == OFFTIME=${process.env.OFFTIME} -== == PAUSETIME=${process.env.PAUSETIME} -== == DEBUG=${process.env.DEBUG} +== == ONTIME=${config.augerOnTime} +== == OFFTIME=${config.augerOffTime} +== == PAUSETIME=${config.pauseTime} +== == DEBUG=${config.debugMode} == == ONPI=${process.env.ONPI}`); // Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready() if (process.env.ONPI == 'true') { // Init the Auger pin gpio.setup(augerPin, gpio.DIR_OUT, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Auger pin initialized.'); + if (config.debugMode) console.log('== Auger pin initialized.'); // Init the igniter pin gpio.setup(igniterPin, gpio.DIR_OUT, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Igniter pin initialized.'); + if (config.debugMode) console.log('== Igniter pin initialized.'); // Init the blower pin gpio.setup(blowerPin, gpio.DIR_OUT, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Combustion blower pin initialized.'); + if (config.debugMode) console.log('== Combustion blower pin initialized.'); // Init the Proof of Fire pin gpio.setup(pofPin, gpio.DIR_IN, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Proof of Fire pin initialized.'); + if (config.debugMode) console.log('== Proof of Fire pin initialized.'); // Init the Temp Sensor pin gpio.setup(tempPin, gpio.DIR_IN, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Temperature pin initialized.'); + if (config.debugMode) console.log('== Temperature pin initialized.'); // Init the Vacuum Switch pin gpio.setup(vacuumPin, gpio.DIR_IN, (err) => { if (err) reject(err); - if (process.env.DEBUG == 'true') console.log('== Vacuum Switch pin initialized.'); + if (config.debugMode) console.log('== Vacuum Switch pin initialized.'); // Resolve the promise now that all pins have been initialized resolve('== GPIO Initialized.'); }); @@ -217,7 +334,7 @@ const functions = { }); } else { // Resolve the promise - resolve('GPIO Not Available'); + resolve('== GPIO Not Available'); } }); },