hestia/functions.js

512 lines
23 KiB
JavaScript
Raw Normal View History

2022-12-17 03:30:12 +00:00
// TODOs: Add tests for PoF and Vacuum switches, add delays for shutting down blower, test logic for igniter
2022-12-20 02:25:17 +00:00
// TODO: Move these to config
2022-12-06 05:05:35 +00:00
// Physical Pin numbers for GPIO
2022-12-08 18:18:24 +00:00
const augerPin = 26; // Pin for controlling the relay for the pellet auger motor.
2022-12-06 05:05:35 +00:00
const igniterPin = 13; // Pin for controlling the relay for the igniter.
const blowerPin = 15; // Pin for controlling the relay for the combustion blower/exhaust.
const pofPin = 16; // Pin for sensing the status (open/closed) of the Proof of Fire switch.
2022-12-08 18:18:24 +00:00
// const tempPin = 18; // Pin for receiving data from a DS18B20 OneWire temperature sensor.
2022-12-06 05:05:35 +00:00
const vacuumPin = 22; // Pin for sensing the status (open/closed) of the vacuum switch.
2022-12-06 05:28:14 +00:00
// Require the package for pulling version numbers
const package = require('./package.json');
// Import the config file
const config = require('./config.json');
2022-12-06 05:28:14 +00:00
// Get environment variables
const dotenv = require('dotenv').config();
// Module for working with files
const fs = require('fs');
2022-12-20 02:25:17 +00:00
const { time } = require('console');
2022-12-04 00:55:47 +00:00
// The functions we'll export to be used in other files
const functions = {
auger: {
2022-12-04 01:54:30 +00:00
// Gets called once the Auger Pin has been setup by rpi-gpio
ready(err) {
if (err) throw err;
console.log('Auger GPIO Ready');
return;
2022-12-03 23:20:38 +00:00
},
// Turns the auger on (Pin 7 high)
2022-12-04 00:55:47 +00:00
on(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
2022-12-08 18:18:24 +00:00
gpio.write(augerPin, true, function(err) {
if (err) throw err;
resolve('Auger turned on.');
});
} else {
resolve('Simulated auger turned on.');
}
});
},
// Turns the auger off (pin 7 low)
2022-12-04 01:25:18 +00:00
off(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
2022-12-08 18:18:24 +00:00
gpio.write(augerPin, false, function(err) {
if (err) throw err;
2022-12-06 05:32:26 +00:00
resolve('Auger turned off.');
});
} else {
resolve('Simulated auger turned off.');
}
});
},
// Cycles the auger using the two functions above this one (functions.auger.on() and functions.auger.off())
// Sleeps in between cycles using functions.sleep()
2022-12-04 00:59:12 +00:00
cycle(gpio) {
return new Promise((resolve) => {
2022-12-04 01:54:30 +00:00
// Turn the auger on
this.on(gpio).then((res) => {
2022-12-04 01:54:30 +00:00
// Log action if in debug mode
2022-12-20 02:25:17 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-04 01:54:30 +00:00
// Sleep for the time set in env variables
2022-12-18 18:14:34 +00:00
functions.sleep(config.intervals.augerOn).then((res) => {
2022-12-04 01:54:30 +00:00
// Log action if in debug mode
2022-12-20 02:25:17 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-04 01:54:30 +00:00
// Turn the auger off
this.off(gpio).then((res) => {
2022-12-04 01:54:30 +00:00
// Log action if in debug mode
2022-12-20 02:25:17 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-04 01:54:30 +00:00
// Sleep for the time set in env variables
2022-12-18 18:14:34 +00:00
functions.sleep(config.intervals.augerOff).then((res) => {
2022-12-04 01:54:30 +00:00
// Log action if in debug mode
2022-12-20 02:25:17 +00:00
// if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know the cycle is complete
2022-12-20 02:25:17 +00:00
resolve("Auger cycled.");
});
});
});
});
});
},
},
2022-12-18 18:14:34 +00:00
blower: {
2022-12-20 02:25:17 +00:00
blocksShutdown() {
// If the current time is past the blowerOff timestamp, we can turn finish shutting down the stove
if ((config.timestamps.blowerOff > 0) && (Date.now() > config.timestamps.blowerOff)) {
2022-12-18 18:14:34 +00:00
return false;
2022-12-20 02:25:17 +00:00
// Otherwise, return true because we're not ready to shutdown yet
} else {
return true;
2022-12-18 18:14:34 +00:00
}
}
},
files: {
2022-12-04 01:54:30 +00:00
// Check for a preset-list of files in the root directory of the app
check() {
return new Promise((resolve, reject) => {
2022-12-04 01:54:30 +00:00
// Check for pause file existing
if (fs.existsSync('./pause')) {
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know what we found
resolve("pause");
}
2022-12-04 01:54:30 +00:00
// Check for reload file existing
if (fs.existsSync('./reload')) {
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know what we found
resolve("reload");
}
2022-12-04 01:54:30 +00:00
// Check for quit file existing
if (fs.existsSync('./quit')) {
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know what we found
resolve("quit");
}
// Check for ignite file existing
if (fs.existsSync('./ignite')) {
resolve('ignite');
}
2022-12-08 17:37:37 +00:00
// Check for start file existing
if (fs.existsSync('./start')) {
resolve('start');
}
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know what we found (nothing)
resolve("none");
});
},
},
commands: {
2022-12-08 17:37:37 +00:00
// 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.`);
}
});
},
2022-12-04 01:54:30 +00:00
// Pauses the script for the time defined in env variables
pause() {
return new Promise((resolve) => {
2022-12-18 18:14:34 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Pausing for ${config.intervals.pause}ms`);
2022-12-08 17:37:37 +00:00
2022-12-18 18:14:34 +00:00
functions.sleep(config.intervals.pause).then(() => { resolve(); });
});
},
2022-12-04 01:54:30 +00:00
// Reload the environment variables on the fly
reload(envs) {
return new Promise((resolve) => {
2022-12-04 01:54:30 +00:00
// Re-require dotenv because inheritance in js sucks
const dotenv = require('dotenv').config({ override: true });
2022-12-04 01:54:30 +00:00
// Delete the reload file
fs.unlink('./reload', (err) => {
if (err) throw err;
if (config.debugMode) console.log('Deleted reload file.');
});
2022-12-04 01:54:30 +00:00
// Print out the new environment variables
// This should be printed regardless of debug status, maybe prettied up TODO?
console.log('Reloaded environment variables.');
2022-12-18 18:14:34 +00:00
console.log(`ONTIME=${config.intervals.augerOn}\nOFFTIME=${config.intervals.augerOff}\nPAUSETIME=${config.intervals.pause}\nDEBUG=${config.debugMode}\nONPI=${process.env.ONPI}`);
2022-12-04 01:54:30 +00:00
// Resolve the promise, letting the main script know we're done reloading the variables and the cycle can continue
resolve();
});
},
2022-12-04 01:54:30 +00:00
// Shutdown the script gracefully
quit() {
2022-12-17 03:30:12 +00:00
// TODO add quit file detection, not always going to be quitting from files
2022-12-04 01:54:30 +00:00
// Delete the quit file
fs.unlink('./quit', (err) => {
if (err) throw err;
if (config.debugMode) console.log('Removed quit file.');
});
2022-12-04 01:54:30 +00:00
// Print out that the script is quitting
console.log('Quitting...');
2022-12-04 01:54:30 +00:00
// Quit the script
process.exit();
},
ignite(gpio) {
return new Promise((resolve, reject) => {
2022-12-19 02:51:15 +00:00
// Check if we got here from a file, then delete it.
if (fs.existsSync('./ignite')) fs.unlink('./ignite', (err) => { if (err) throw err; });
2022-12-20 02:25:17 +00:00
functions.power.blower.on(gpio).then(res => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Turn on the igniter
functions.power.igniter.on(gpio).then(res => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Enable the auger
config.status.auger = 1;
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger enabled.`);
resolve('Ignition sequence started successfully.');
}).catch(err => {
reject(err);
});
2022-12-20 02:25:17 +00:00
});
});
},
2022-12-17 03:30:12 +00:00
shutdown(gpio) {
2022-12-18 18:14:34 +00:00
// Only run if a shutdown isn't already started
if (config.status.shutdown == 0) {
// set shutdown flag to 1
config.status.shutdown = 1;
2022-12-19 02:38:13 +00:00
// Check if this was invoked from a 'quit' file, if so, delete the file
2022-12-19 02:39:07 +00:00
if (fs.existsSync('./quit')) fs.unlink('./quit', (err) => { if (err) throw err; });
2022-12-18 18:14:34 +00:00
// If the auger is enabled, disable it
if (config.status.auger == 1) {
config.status.auger = 0;
2022-12-20 20:04:37 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger disabled.`);
2022-12-18 18:14:34 +00:00
}
// If the igniter is on, shut it off.
if (config.status.igniter == 1) {
functions.power.igniter.off(gpio).then(res => {
2022-12-20 02:25:17 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-18 18:14:34 +00:00
}); // 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 {
2022-12-20 20:04:37 +00:00
// blower.blocksShutdown() returns false only if the blower shutdown has
2022-12-20 02:25:17 +00:00
// been initiated AND the specified cooldown time has passed
2022-12-20 20:04:37 +00:00
if(!(functions.blower.blocksShutdown())) {
2022-12-20 02:25:17 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Blower can be turned off.`);
2022-12-20 20:04:37 +00:00
functions.power.blower.off(gpio).then(res => {
2022-12-20 02:25:17 +00:00
// 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
2022-12-21 18:50:09 +00:00
// functions.commands.quit();
config.status.shutdown = 0;
2022-12-20 02:25:17 +00:00
});
} else {
return "A shutdown has already been initiated and the blower is preventing shutdown.";
}
2022-12-17 03:30:12 +00:00
}
},
2022-12-21 18:50:09 +00:00
writeConfig() {
fs.writeFile('./config.json', JSON.stringify(config), (err) => {
if (err) throw err;
});
}
},
tests: {
vacuum(gpio) {
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
if (process.env.ONPI == 'true') {
gpio.read(vacuumPin, (err, status) => {
if (err) reject(err);
resolve(status);
});
} else {
switch (config.status.vacuum) {
case 0:
resolve(false);
break;
case 1:
resolve(true);
break;
default:
reject('Unable to determine vacuum status.');
break;
}
}
});
},
pof(gpio) {
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
if (process.env.ONPI == 'true') {
gpio.read(pofPin, (err, status) => {
if (err) reject(err);
resolve(status);
});
} else {
switch (config.status.pof) {
case 0:
resolve(false);
break;
case 1:
resolve(true);
break;
default:
reject('Unable to determine proof of fire status.');
break;
}
}
});
},
igniter(gpio) {
2022-12-08 17:37:37 +00:00
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
// Create a blank string to store the status message in as we build it
var statusMsg = "";
2022-12-20 02:25:17 +00:00
// Determine if the igniter is on
if (config.status.igniter == 1) {
2022-12-20 02:25:17 +00:00
statusMsg += "The igniter is on. ";
} else if (config.status.igniter == 0) {
2022-12-20 02:25:17 +00:00
statusMsg += "The igniter is off. ";
} else {
2022-12-20 02:25:17 +00:00
reject("Unable to determine igniter status.");
}
2022-12-20 02:25:17 +00:00
// Run this if the igniter has been turned on
2022-12-18 18:14:34 +00:00
if (config.timestamps.igniterOn > 0) {
if (Date.now() < config.timestamps.igniterOff && config.status.igniter == 1) {
2022-12-20 02:25:17 +00:00
statusMsg += `Started: ${functions.time(config.timestamps.igniterOn)}. `;
statusMsg += `Stopping: ${functions.time(config.timestamps.igniterOff)}. `;
2022-12-08 17:37:37 +00:00
}
// Shut the igniter off if it's past the waiting period
2022-12-18 18:14:34 +00:00
if ((Date.now() > config.timestamps.igniterOff) && (config.status.igniter == 1)) {
2022-12-20 02:25:17 +00:00
// if (process.env.ONPI == 'true') {
// gpio.write(igniterPin, false, (err) => {
// if (err) throw(err);
// config.status.igniter = 0;
// 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 => {
2022-12-08 17:37:37 +00:00
2022-12-20 02:25:17 +00:00
// });
// });
// } else {
// config.status.igniter = 0;
// statusMsg += `${new Date().toISOString()} I: Simulated igniter turned off.`;
// }
// TODO I think this needs to be moved elsewhere, it doesn't finish resolving before the resolve call on line 354 is called (344+10=354)
functions.power.igniter.off(gpio).then(res => {
2022-12-21 18:50:09 +00:00
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
2022-12-20 02:25:17 +00:00
});
2022-12-18 18:14:34 +00:00
} 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.';
}
resolve(statusMsg);
});
2022-12-17 03:30:12 +00:00
},
blowerOffDelay() {
2022-12-18 18:14:34 +00:00
2022-12-17 03:30:12 +00:00
},
},
power: {
igniter: {
on(gpio) {
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
config.timestamps.igniterOn = Date.now();
config.timestamps.igniterOff = Date.now() + config.intervals.igniterStart;
if (process.env.ONPI == 'true') {
gpio.write(igniterPin, true, (err) => {
if (err) reject(err);
config.status.igniter = 1;
resolve('Igniter turned on.');
});
} else {
2022-12-19 02:29:50 +00:00
config.status.igniter = 1;
resolve('Igniter turned on.');
2022-12-20 02:25:17 +00:00
}
});
},
off(gpio) {
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
config.timestamps.igniterOff = Date.now();
2022-12-20 20:04:37 +00:00
config.status.igniterFinished = true;
2022-12-20 02:25:17 +00:00
if (process.env.ONPI == 'true') {
gpio.write(igniterPin, false, (err) => {
if (err) reject(err);
config.status.igniter = 0;
resolve('Igniter turned off.');
});
} else {
2022-12-19 02:29:50 +00:00
config.status.igniter = 0;
resolve('Igniter turned off.');
2022-12-20 02:25:17 +00:00
}
});
},
},
blower: {
on(gpio) {
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
config.timestamps.blowerOn = Date.now();
if (process.env.ONPI == 'true') {
gpio.write(blowerPin, true, (err) => {
if (err) reject(err);
config.status.blower = 1;
resolve('Blower turned on.');
});
} else {
2022-12-19 02:29:50 +00:00
config.status.blower = 1;
resolve('Blower turned on.');
2022-12-20 02:25:17 +00:00
}
});
},
off(gpio) {
2022-12-20 02:25:17 +00:00
config.timestamps.blowerOff = Date.now();
return new Promise((resolve, reject) => {
2022-12-20 02:25:17 +00:00
if (process.env.ONPI == 'true') {
gpio.write(blowerPin, false, (err) => {
if (err) reject(err);
config.status.blower = 0;
resolve('Blower turned off.');
});
} else {
2022-12-19 02:29:50 +00:00
config.status.blower = 0;
resolve('Blower turned off.');
2022-12-20 02:25:17 +00:00
}
});
},
},
},
2022-12-06 05:05:35 +00:00
// Sleeps for any given milliseconds
sleep(ms) {
return new Promise((resolve) => {
// if (config.debugMode) console.log(`Sleeping for ${ms}ms`);
2022-12-04 01:54:30 +00:00
// Function to be called when setTimeout finishes
const finish = () => {
2022-12-04 01:54:30 +00:00
// Resolve the promise
resolve(`Slept for ${ms}ms`);
2022-12-04 01:54:30 +00:00
};
// The actual sleep function, sleeps for ms then calls finish()
setTimeout(finish, ms);
});
},
2022-12-04 01:54:30 +00:00
// Initializes rpi-gpio, or resolves if not on a raspberry pi
2022-12-04 00:55:47 +00:00
init(gpio) {
2022-12-20 02:25:17 +00:00
// TODO this boot splash needs updating
2022-12-04 00:55:47 +00:00
return new Promise((resolve, reject) => {
2022-12-06 05:29:03 +00:00
// Boot/About/Info
2022-12-06 05:31:21 +00:00
console.log(`== Lennox Winslow PS40
2022-12-06 05:28:14 +00:00
== Pellet Stove Control Panel
== Author: Skylar Grant
== Version: v${package.version}
==
== Startup Time: ${new Date().toISOString()}
==
== Environment variables:
2022-12-18 18:14:34 +00:00
== == ONTIME=${config.intervals.augerOn}
== == OFFTIME=${config.intervals.augerOff}
== == PAUSETIME=${config.intervals.pause}
== == DEBUG=${config.debugMode}
2022-12-06 05:28:14 +00:00
== == ONPI=${process.env.ONPI}`);
2022-12-04 00:55:47 +00:00
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
if (process.env.ONPI == 'true') {
2022-12-06 05:05:35 +00:00
// Init the Auger pin
gpio.setup(augerPin, gpio.DIR_OUT, (err) => {
2022-12-04 00:55:47 +00:00
if (err) reject(err);
if (config.debugMode) console.log('== Auger pin initialized.');
2022-12-06 05:05:35 +00:00
// Init the igniter pin
gpio.setup(igniterPin, gpio.DIR_OUT, (err) => {
if (err) reject(err);
if (config.debugMode) console.log('== Igniter pin initialized.');
2022-12-06 05:05:35 +00:00
// Init the blower pin
gpio.setup(blowerPin, gpio.DIR_OUT, (err) => {
if (err) reject(err);
if (config.debugMode) console.log('== Combustion blower pin initialized.');
2022-12-06 05:05:35 +00:00
// Init the Proof of Fire pin
gpio.setup(pofPin, gpio.DIR_IN, (err) => {
if (err) reject(err);
if (config.debugMode) console.log('== Proof of Fire pin initialized.');
2022-12-08 17:47:50 +00:00
// Init the Vacuum Switch pin
gpio.setup(vacuumPin, gpio.DIR_IN, (err) => {
2022-12-06 05:05:35 +00:00
if (err) reject(err);
2022-12-08 17:47:50 +00:00
if (config.debugMode) console.log('== Vacuum Switch pin initialized.');
// Resolve the promise now that all pins have been initialized
resolve('== GPIO Initialized.');
2022-12-06 05:05:35 +00:00
});
2022-12-08 17:47:50 +00:00
// Init the Temp Sensor pin
// gpio.setup(tempPin, gpio.DIR_IN, (err) => {
// if (err) reject(err);
// if (config.debugMode) console.log('== Temperature pin initialized.');
// });
2022-12-06 05:05:35 +00:00
});
});
});
2022-12-04 00:55:47 +00:00
});
} else {
2022-12-04 01:54:30 +00:00
// Resolve the promise
resolve('== GPIO Not Available');
2022-12-04 00:55:47 +00:00
}
});
2022-12-03 23:20:38 +00:00
},
2022-12-20 02:25:17 +00:00
time(stamp) {
const time = new Date(stamp);
return `${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
}
}
2022-12-04 01:54:30 +00:00
// Export the above object, functions, as a module
module.exports = { functions };