Finish changing config file updates
This commit is contained in:
parent
d1ac00f737
commit
ca333e5603
2
.vscode/pssnippets.code-snippets
vendored
2
.vscode/pssnippets.code-snippets
vendored
@ -19,7 +19,7 @@
|
||||
"Log if in Debug mode": {
|
||||
"scope": "javascript",
|
||||
"prefix": "log",
|
||||
"body": "if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: $1`);\n$0",
|
||||
"body": "if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: $1`);\n$0",
|
||||
"description": "Log output to console if in debug mode"
|
||||
},
|
||||
"Run only on Pi": {
|
||||
|
@ -9,7 +9,7 @@
|
||||
"shutdown": 0
|
||||
},
|
||||
"timestamps": {
|
||||
"startup": 0,
|
||||
"procStart": 0,
|
||||
"blowerOff": 0,
|
||||
"igniterOn": 0,
|
||||
"igniterOff": 0
|
||||
|
105
functions.js
105
functions.js
@ -66,19 +66,19 @@ const functions = {
|
||||
// Turn the auger on
|
||||
this.on(gpio).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
// Sleep for the time set in env variables
|
||||
functions.sleep(config.augerOnTime).then((res) => {
|
||||
functions.sleep(config.intervals.augerOn).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
// Turn the auger off
|
||||
this.off(gpio).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
// Sleep for the time set in env variables
|
||||
functions.sleep(config.augerOffTime).then((res) => {
|
||||
functions.sleep(config.intervals.augerOff).then((res) => {
|
||||
// Log action if in debug mode
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
// Resolve the promise, letting the main script know the cycle is complete
|
||||
resolve("Cycle complete.");
|
||||
});
|
||||
@ -88,6 +88,19 @@ const functions = {
|
||||
});
|
||||
},
|
||||
},
|
||||
blower: {
|
||||
canShutdown() {
|
||||
// If the blowerOff timestamp hasn't been set, return false as the blower hasn't been asked to turn off yet
|
||||
if (config.timestamps.blowerOff == 0) return false;
|
||||
// If the current time is past the blowerOff timestamp, we can turn off the blower
|
||||
if (Date.now() > config.timestamps.blowerOff) {
|
||||
return true;
|
||||
// Otherwise, return false because we're not ready to
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
},
|
||||
files: {
|
||||
// Check for a preset-list of files in the root directory of the app
|
||||
check() {
|
||||
@ -146,9 +159,9 @@ const functions = {
|
||||
// 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`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pausing for ${config.intervals.pause}ms`);
|
||||
|
||||
functions.sleep(config.pauseTime).then(() => { resolve(); });
|
||||
functions.sleep(config.intervals.pause).then(() => { resolve(); });
|
||||
});
|
||||
},
|
||||
// Reload the environment variables on the fly
|
||||
@ -164,7 +177,7 @@ const functions = {
|
||||
// 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=${config.augerOnTime}\nOFFTIME=${config.augerOffTime}\nPAUSETIME=${config.pauseTime}\nDEBUG=${config.debugMode}\nONPI=${process.env.ONPI}`);
|
||||
console.log(`ONTIME=${config.intervals.augerOn}\nOFFTIME=${config.intervals.augerOff}\nPAUSETIME=${config.intervals.pause}\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();
|
||||
});
|
||||
@ -186,12 +199,12 @@ const functions = {
|
||||
ignite(gpio) {
|
||||
config.status.igniter = 1;
|
||||
config.status.auger = 1;
|
||||
config.igniterOnTime = Date.now();
|
||||
config.igniterOffTime = config.igniterOnTime + config.igniterWaitTime; // 7 Minutes, 420,000ms
|
||||
config.timestamps.igniterOn = Date.now();
|
||||
config.timestamps.igniterOff = config.timestamps.igniterOn + config.intervals.igniterStart; // 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 (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Delete the ignite file.`);
|
||||
});
|
||||
if (process.env.ONPI == 'true') {
|
||||
gpio.write(igniterPin, true, (err) => {
|
||||
@ -204,24 +217,28 @@ const functions = {
|
||||
});
|
||||
},
|
||||
shutdown(gpio) {
|
||||
// If the auger is enabled, disable it
|
||||
if (config.status.auger == 1) {
|
||||
config.status.auger = 0;
|
||||
}
|
||||
// If the igniter is on, shut it off.
|
||||
if (config.status.igniter == 1) {
|
||||
functions.power.igniter.off(gpio).then(res => {
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: Shut off igniter.`);
|
||||
}); // TODO catch an error here
|
||||
}
|
||||
// TODO Change this so it gives a delay after shutting down so smoke doesn't enter the house
|
||||
if (config.status.blower == 1) {
|
||||
config.times.blowerOff = Date.now() + 600000; // 10 minutes, TODO move to config
|
||||
// TODO Move this to another function, to run after tests pass
|
||||
// functions.power.blower.off(gpio).then(res => {
|
||||
// if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: Shut off blower.`);
|
||||
|
||||
// });
|
||||
// Only run if a shutdown isn't already started
|
||||
if (config.status.shutdown == 0) {
|
||||
// set shutdown flag to 1
|
||||
config.status.shutdown = 1;
|
||||
// If the auger is enabled, disable it
|
||||
if (config.status.auger == 1) {
|
||||
config.status.auger = 0;
|
||||
}
|
||||
// If the igniter is on, shut it off.
|
||||
if (config.status.igniter == 1) {
|
||||
functions.power.igniter.off(gpio).then(res => {
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Shut off igniter.`);
|
||||
}); // TODO catch an error here
|
||||
}
|
||||
// TODO Change this so it gives a delay after shutting down so smoke doesn't enter the house
|
||||
if (config.status.blower == 1) {
|
||||
// Set the timestamp to turn the blower off at
|
||||
config.timestamps.blowerOff = Date.now() + config.intervals.blowerStop;
|
||||
}
|
||||
return "Shutdown has been initiated.";
|
||||
} else {
|
||||
return "A shutdown has already been initiated.";
|
||||
}
|
||||
},
|
||||
},
|
||||
@ -252,15 +269,15 @@ const functions = {
|
||||
} else {
|
||||
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();
|
||||
if (Date.now() < config.igniterOffTime && config.status.igniter == 1) {
|
||||
if (config.timestamps.igniterOn > 0) {
|
||||
const humanStartTime = new Date(config.timestamps.igniterOn).toISOString();
|
||||
const humanEndTime = new Date(config.timestamps.igniterOff).toISOString();
|
||||
if (Date.now() < config.timestamps.igniterOff && 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 ((Date.now() > config.timestamps.igniterOff) && (config.status.igniter == 1)) {
|
||||
if (process.env.ONPI == 'true') {
|
||||
gpio.write(igniterPin, false, (err) => {
|
||||
if (err) throw(err);
|
||||
@ -280,8 +297,8 @@ const functions = {
|
||||
config.status.igniter = 0;
|
||||
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 if ((Date.now() > config.timestamps.igniterOff) && (config.status.igniter == 0)) {
|
||||
statusMsg += `The igniter was turned off at ${new Date(config.timestamps.igniterOff).toISOString()}.`;
|
||||
}
|
||||
} else {
|
||||
statusMsg += 'The igniter hasn\'t been started yet.';
|
||||
@ -290,13 +307,7 @@ const functions = {
|
||||
});
|
||||
},
|
||||
blowerOffDelay() {
|
||||
if (config.times.blowerOff == 0) return false;
|
||||
// TODO Implement the blower shutdown delay as a test here
|
||||
if (Date.now() > config.times.blowerOff) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
},
|
||||
power: {
|
||||
@ -366,9 +377,9 @@ const functions = {
|
||||
== Startup Time: ${new Date().toISOString()}
|
||||
==
|
||||
== Environment variables:
|
||||
== == ONTIME=${config.augerOnTime}
|
||||
== == OFFTIME=${config.augerOffTime}
|
||||
== == PAUSETIME=${config.pauseTime}
|
||||
== == ONTIME=${config.intervals.augerOn}
|
||||
== == OFFTIME=${config.intervals.augerOff}
|
||||
== == PAUSETIME=${config.intervals.pause}
|
||||
== == DEBUG=${config.debugMode}
|
||||
== == ONPI=${process.env.ONPI}`);
|
||||
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
|
||||
|
32
main.js
32
main.js
@ -12,32 +12,32 @@ const fn = require('./functions.js').functions;
|
||||
|
||||
// Config File
|
||||
const config = require('./config.json');
|
||||
config.startTime = Date.now();
|
||||
config.timestamps.procStart = Date.now();
|
||||
|
||||
// Environment Variables Importing
|
||||
const dotenv = require('dotenv').config();
|
||||
|
||||
// Setup for use with the Pi's GPIO pins
|
||||
if (process.env.ONPI == 'true') {
|
||||
console.log(`[${(Date.now() - config.startTime)/1000}] == Running on a Raspberry Pi.`);
|
||||
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] == Running on a Raspberry Pi.`);
|
||||
const gpio = require('rpi-gpio');
|
||||
fn.init(gpio).then((res) => {
|
||||
console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
main(fn, gpio);
|
||||
}).catch(rej => {
|
||||
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
|
||||
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
|
||||
});
|
||||
} else if (process.env.ONPI == 'false') {
|
||||
console.log(`[${(Date.now() - config.startTime)/1000}] I: Not running on a Raspberry Pi.`);
|
||||
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Not running on a Raspberry Pi.`);
|
||||
const gpio = 'gpio';
|
||||
fn.init(gpio).then(res => {
|
||||
console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
main(fn, gpio);
|
||||
}).catch(rej => {
|
||||
console.error(rej);
|
||||
});
|
||||
} else {
|
||||
console.error(`[${Date.now() - config.startTime}] E: Problem with ENV file.`);
|
||||
console.error(`[${Date.now() - config.timestamps.procStart}] E: Problem with ENV file.`);
|
||||
}
|
||||
|
||||
// TODO Add logic for other sensors
|
||||
@ -49,7 +49,7 @@ async function main(fn, gpio) {
|
||||
// Check for the existence of certain files
|
||||
fn.files.check().then((res,rej) => {
|
||||
// Log the result of the check if in debug mode
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: File Check: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: File Check: ${res}`);
|
||||
// Choose what to do depending on the result of the check
|
||||
switch (res) {
|
||||
case "pause":
|
||||
@ -65,7 +65,7 @@ async function main(fn, gpio) {
|
||||
// Rerun this function once the reload has finished
|
||||
main(fn, gpio);
|
||||
}).catch(rej => {
|
||||
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
|
||||
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
|
||||
});
|
||||
break;
|
||||
case "quit":
|
||||
@ -78,7 +78,7 @@ async function main(fn, gpio) {
|
||||
if (config.debugMode) console.log(res);
|
||||
statusCheck(fn, gpio);
|
||||
}).catch(rej => {
|
||||
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
|
||||
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${rej}`);
|
||||
fn.commands.shutdown(gpio).then(res => {
|
||||
fn.commands.quit();
|
||||
}).catch(rej => {
|
||||
@ -100,7 +100,7 @@ async function 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}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
// Run the status check function
|
||||
statusCheck(fn, gpio);
|
||||
// Rerun this function once the cycle is complete
|
||||
@ -115,7 +115,7 @@ async function main(fn, gpio) {
|
||||
|
||||
default:
|
||||
// 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.
|
||||
console.error(`[${(Date.now() - config.startTime)/1000}] E: No result was received, something is wrong.\nres: ${res}`);
|
||||
console.error(`[${(Date.now() - config.timestamps.procStart)/1000}] E: No result was received, something is wrong.\nres: ${res}`);
|
||||
fn.commands.quit();
|
||||
break;
|
||||
}
|
||||
@ -124,7 +124,7 @@ async function main(fn, gpio) {
|
||||
|
||||
function statusCheck(fn, gpio) {
|
||||
fn.tests.igniter(gpio).then((res) => {
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
|
||||
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
|
||||
main(fn, gpio);
|
||||
});
|
||||
|
||||
@ -139,15 +139,15 @@ function statusCheck(fn, gpio) {
|
||||
// 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
|
||||
// TODO Shutdown will handle checks if it's being called repeatedly.
|
||||
if (config.status.igniterFinished && (!status)) fn.commands.shutdown(gpio);
|
||||
});
|
||||
|
||||
// blowerOffDelay() returns true only if the blower shutdown has
|
||||
// blower.canShutdown() returns true only if the blower shutdown has
|
||||
// been initiated AND the specified cooldown time has passed
|
||||
if(fn.tests.blowerOffDelay()) {
|
||||
if(fn.blower.canShutdown()) {
|
||||
fn.power.blower.off(gpio).then(res => {
|
||||
// Since the blower shutting off is the last step in the shutdown, we can quit.
|
||||
// TODO eventually we don't want to ever quit the program, so it can be restarted remotely
|
||||
fn.commands.quit();
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user