hestia/modules/functions.js

417 lines
18 KiB
JavaScript
Raw Permalink 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
2023-01-22 18:18:43 +00:00
// TODO: Move these to config
// Physical Pin numbers for GPIO
const augerPin = 7; // Pin for controlling the relay for the pellet auger motor.
const igniterPin = 13;
const exhaustPin = 15;
const pofPin = 16;
2022-12-17 03:30:12 +00:00
2022-12-06 05:28:14 +00:00
// Require the package for pulling version numbers
2023-01-22 02:33:52 +00:00
const package = require('../package.json');
// Database Functions
const dbfn = require('./database.js');
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');
2023-01-07 02:19:57 +00:00
const { exec } = require('child_process');
2023-01-22 02:33:52 +00:00
var config = require('../templates/config.json');
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') {
2023-01-22 02:33:52 +00:00
gpio.write(augerPin, true, function (err) {
if (err) throw err;
resolve('Auger turned on.');
});
} else {
resolve('Simulated auger turned on.');
}
});
2023-01-22 02:33:52 +00:00
},
// 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') {
2023-01-22 02:33:52 +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
2023-01-21 15:48:16 +00:00
// if (process.env.DEBUG) 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
2023-01-21 15:48:16 +00:00
// if (process.env.DEBUG) 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
2023-01-21 15:48:16 +00:00
// if (process.env.DEBUG) 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
2023-01-21 15:48:16 +00:00
// if (process.env.DEBUG) 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
2023-01-06 20:58:42 +00:00
resolve(`Auger cycled (${config.intervals.augerOn}/${config.intervals.augerOff})`);
});
});
});
});
});
},
},
igniter: {
// Gets called once the Igniter Pin has been setup by rpi-gpio
ready(err) {
if (err) throw err;
console.log('Igniter GPIO Ready');
return;
},
// Turns the Igniter on (Pin 7 high)
on(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(igniterPin, true, function (err) {
if (err) throw err;
resolve('Igniter turned on.');
});
} else {
resolve('Simulated Igniter turned on.');
}
});
},
// Turns the Igniter off (pin 7 low)
off(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(igniterPin, false, function (err) {
if (err) throw err;
resolve('Igniter turned off.');
});
} else {
resolve('Simulated Igniter turned off.');
}
});
}
},
exhaust: {
// Gets called once the Exhaust Pin has been setup by rpi-gpio
ready(err) {
if (err) throw err;
console.log('Exhaust GPIO Ready');
return;
},
// Turns the Exhaust on (Pin 7 high)
on(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(exhaustPin, true, function (err) {
if (err) throw err;
resolve('Exhaust turned on.');
});
} else {
resolve('Simulated Exhaust turned on.');
}
});
},
// Turns the Exhaust off (pin 7 low)
off(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(exhaustPin, false, function (err) {
if (err) throw err;
resolve('Exhaust turned off.');
});
} else {
resolve('Simulated Exhaust turned off.');
}
});
}
},
commands: {
2022-12-08 17:37:37 +00:00
// Prepare the stove for starting
2023-11-14 23:06:09 +00:00
augerOn() { // FKA startup()
2023-01-06 20:58:42 +00:00
// Basic startup just enables the auger
2023-01-22 16:27:58 +00:00
const enableAugerQuery = "UPDATE status SET value = 1 WHERE key = 'auger'";
dbfn.run(enableAugerQuery).then(res => {
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger enabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
2023-01-06 20:58:42 +00:00
},
2023-11-14 23:06:09 +00:00
augerOff() { // FKA shutdown()
2023-01-06 20:58:42 +00:00
// Basic shutdown only needs to disable the auger
2023-01-22 16:27:58 +00:00
const disableAugerQuery = "UPDATE status SET value = 0 WHERE key = 'auger'";
dbfn.run(disableAugerQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res.status}`);
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Auger disabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
2022-12-08 17:37:37 +00:00
},
igniterOn() {
const enableIgniterQuery = "UPDATE status SET value = 1 WHERE key = 'igniter'";
dbfn.run(enableIgniterQuery).then(res => {
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Igniter enabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
igniterOff() {
const disableIgniterQuery = "UPDATE status SET value = 0 WHERE key = 'igniter'";
dbfn.run(disableIgniterQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res.status}`);
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Igniter disabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
exhaustOn() {
2023-11-17 15:24:59 +00:00
const enableExhaustQuery = "UPDATE status SET value = 1 WHERE key = 'blower'";
dbfn.run(enableExhaustQuery).then(res => {
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Exhaust enabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
exhaustOff() {
2023-11-17 15:24:59 +00:00
const disableExhaustQuery = "UPDATE status SET value = 0 WHERE key = 'blower'";
dbfn.run(disableExhaustQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res.status}`);
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Exhaust disabled.`);
return;
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
},
2022-12-04 01:54:30 +00:00
// Pauses the script for the time defined in env variables
pause() {
return new Promise((resolve) => {
2023-01-22 02:33:52 +00:00
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Pausing for ${config.intervals.pause}ms`);
functions.sleep(config.intervals.pause).then((res) => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Pause finished.`);
2023-01-13 00:51:41 +00:00
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;
2023-01-21 15:48:16 +00:00
if (process.env.DEBUG) 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.');
2023-01-21 15:48:16 +00:00
console.log(`ONTIME=${config.intervals.augerOn}\nOFFTIME=${config.intervals.augerOff}\nPAUSETIME=${config.intervals.pause}\nDEBUG=${process.env.DEBUG}\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();
});
2023-01-22 02:33:52 +00:00
},
2023-01-22 02:33:52 +00:00
refreshConfig() {
2023-01-06 22:04:46 +00:00
return new Promise((resolve, reject) => {
// When the reload button is pressed, the call to this function will contain new config values
// {
// augerOff: 500,
// augerOn: 1500,
// pause: 5000
// }
2023-01-22 02:33:52 +00:00
// if (newSettings != undefined) {
// config.intervals.augerOff = newSettings.augerOff;
// config.intervals.augerOn = newSettings.augerOn;
// console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Intervals updated: (${newSettings.augerOn}/${newSettings.augerOff})`);
// }
// fs.writeFile('./config.json', JSON.stringify(config), (err) => {
// if (err) reject(err);
// resolve();
// });
// Get status
const selectStatusQuery = "SELECT * FROM status";
dbfn.all(selectStatusQuery).then(res => {
let { status } = config;
let { rows } = res;
status.auger = rows.auger;
2023-11-17 15:22:08 +00:00
status.exhaust = rows.blower; // TODO update db to use exhaust not blower
2023-01-22 02:33:52 +00:00
status.igniter = rows.igniter;
status.igniterFinished = rows.igniter_finished;
status.pof = rows.proof_of_fire;
status.shutdownNextCycle = rows.shutdown_next_cycle;
status.vacuum = rows.vacuum;
// Get timestamps
const selectTimestampsQuery = "SELECT * FROM timestamps";
dbfn.all(selectTimestampsQuery).then(res => {
let { timestamps } = config;
let { rows } = res;
2023-11-17 15:22:08 +00:00
timestamps.exhaustOff = rows.blower_off;
timestamps.exhaustOn = rows.blower_on;
2023-01-22 02:33:52 +00:00
timestamps.igniterOff = rows.igniter_off;
timestamps.igniterOn = rows.igniter_on;
timestamps.procStart = rows.process_start;
// Get intervals
const selectIntervalsQuery = "SELECT * FROM intervals";
dbfn.all(selectIntervalsQuery).then(res => {
let { intervals } = config;
let { rows } = res;
intervals.augerOff = rows.auger_off;
intervals.augerOn = rows.auger_on;
intervals.blowerStop = rows.blower_stop;
intervals.igniterStart = rows.igniter_start;
intervals.pause = rows.pause;
resolve({ "status": "Refreshed the config", "config": config });
}).catch(err => {
reject(err);
return;
});
}).catch(err => {
reject(err);
return;
});
}).catch(err => {
reject(err);
return;
2023-01-06 22:04:46 +00:00
});
2023-01-22 02:33:52 +00:00
});
2023-01-07 02:19:57 +00:00
},
quit() {
functions.commands.shutdown();
functions.auger.off(gpio).then(res => {
2023-01-22 02:33:52 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Exiting app...`);
2023-01-07 02:19:57 +00:00
process.exit(0);
}).catch(err => {
2023-01-22 02:33:52 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] E: Unable to shut off auger, rebooting Pi!`);
2023-01-07 02:19:57 +00:00
exec('shutdown -r 0');
});
2022-12-21 18:50:09 +00:00
}
},
2022-12-06 05:05:35 +00:00
// Sleeps for any given milliseconds
sleep(ms) {
return new Promise((resolve) => {
2023-01-21 15:48:16 +00:00
// if (process.env.DEBUG) 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) {
2023-01-03 22:01:23 +00:00
fs.readFile('./templates/config.json', (err, data) => {
fs.writeFile('./config.json', data, (err) => {
if (err) throw err;
2023-01-22 02:33:52 +00:00
config = require('../config.json');
2023-01-03 22:01:23 +00:00
})
})
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}
2023-01-21 15:48:16 +00:00
== == DEBUG=${process.env.DEBUG}
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);
2023-01-21 15:48:16 +00:00
if (process.env.DEBUG) console.log('== Auger pin initialized.');
2023-11-17 17:31:59 +00:00
this.auger.ready();
// Init the Igniter pin
gpio.setup(igniterPin, gpio.DIR_OUT, (err) => {
if (err) reject(err);
if (process.env.DEBUG) console.log('== Igniter pin initialized.');
this.igniter.ready();
// Init the Exhaust pin
gpio.setup(exhaustPin, gpio.DIR_OUT, (err) => {
if (err) reject(err);
if (process.env.DEBUG) console.log('== Exhaust pin initialized.');
this.exhaust.ready();
// Resolve the promise now that all pins have been initialized
resolve('== GPIO Initialized.');
});
});
2022-12-04 00:55:47 +00:00
});
} else {
2022-12-04 01:54:30 +00:00
// Resolve the promise
2023-11-17 16:43:04 +00:00
resolve('== GPIO Simulated');
2022-12-04 00:55:47 +00:00
}
});
2022-12-03 23:20:38 +00:00
},
2023-01-12 23:34:58 +00:00
checkForQuit() {
2023-01-13 00:51:41 +00:00
if (config.status.shutdownNextCycle == 1) {
2023-01-22 02:33:52 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Exiting Process!`);
2023-01-13 00:51:41 +00:00
process.exit();
}
2023-01-12 23:34:58 +00:00
return new Promise((resolve, reject) => {
if (fs.existsSync('./quit')) {
fs.unlink('./quit', err => {
if (err) console.log('Error removing the quit file: ' + err);
config.status.shutdownNextCycle = 1;
config.status.auger = 0;
const shutdownNextCycleQuery = "UPDATE status SET value = 1 WHERE key = 'shutdown_next_cycle'";
dbfn.run(shutdownNextCycleQuery).then(res => {
if (process.env.DEBUG) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res.status}`);
console.log(`[${(Date.now() - config.timestamps.procStart) / 1000}] I: Shutting down next cycle.`);
}).catch(err => console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`));
2023-01-12 23:34:58 +00:00
resolve();
});
2023-01-13 00:51:41 +00:00
} else {
resolve('Not shutting down');
2023-01-12 23:34:58 +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
2023-01-22 16:27:58 +00:00
module.exports = { functions, dbfn };