hestia/functions.js

265 lines
11 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
2022-12-06 05:28:14 +00:00
// Require the package for pulling version numbers
const package = require('./package.json');
// Import the config file
2023-01-03 22:01:23 +00:00
var config = require('./config.json');
2023-01-06 20:58:42 +00:00
config.timestamps.procStart = Date.now();
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
2023-01-06 20:58:42 +00:00
const main = (gpio) => {
2023-01-06 22:04:46 +00:00
functions.commands.refreshConfig().then(res => {
// If the auger is enabled
if (config.status.auger == 1) {
// Run a cycle of the auger
functions.auger.cycle(gpio).then(res => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
// Recursion ecursion cursion ursion rsion sion ion on n
main(gpio);
}).catch(err => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
});
} else {
// If the auger is disabled
functions.commands.pause().then(res => {
main(gpio);
}).catch(err => {
if (config.debugMode) console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: ${err}`);
main(gpio);
});
}
}).catch(rej => {
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: Problem refreshing the config file.`);
main(gpio);
});
2023-01-06 20:58:42 +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
2023-01-06 20:58:42 +00:00
resolve(`Auger cycled (${config.intervals.augerOn}/${config.intervals.augerOff})`);
});
});
});
});
});
},
},
commands: {
2022-12-08 17:37:37 +00:00
// Prepare the stove for starting
2023-01-06 20:58:42 +00:00
startup () {
// Basic startup just enables the auger
config.status.auger = 1;
2023-01-06 22:32:29 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger enabled.`);
2023-01-06 20:58:42 +00:00
return;
},
shutdown() {
// Basic shutdown only needs to disable the auger
config.status.auger = 0;
2023-01-06 22:32:29 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Auger disabled.`);
2022-12-08 17:37:37 +00:00
},
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();
});
},
2023-01-06 20:58:42 +00:00
refreshConfig(newSettings) {
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
// }
if (newSettings != undefined) {
config.intervals.augerOff = newSettings.augerOff;
config.intervals.augerOn = newSettings.augerOn;
config.intervals.pause = newSettings.pause;
2023-01-06 22:32:29 +00:00
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: Intervals updated: (${newSettings.augerOn}/${newSettings.augerOff}/${newSettings.pause})`);
2023-01-06 22:04:46 +00:00
}
fs.writeFile('./config.json', JSON.stringify(config), (err) => {
if (err) reject(err);
resolve();
});
})
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) => {
// 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) {
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;
config = require('./config.json');
})
})
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.');
2023-01-06 20:58:42 +00:00
// 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
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()}`;
}
}
2023-01-06 20:58:42 +00:00
// Setup for use with the Pi's GPIO pins
switch (process.env.ONPI) {
case 'true':
2023-01-06 22:32:29 +00:00
console.log(`== Running on a Raspberry Pi.`);
2023-01-06 20:58:42 +00:00
var gpio = require('rpi-gpio');
functions.init(gpio).then((res) => {
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
main(gpio);
}).catch(rej => {
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: Error during initialization: ${rej}`);
2023-01-06 20:58:42 +00:00
process.exit(1);
});
break;
case 'false':
2023-01-06 22:32:29 +00:00
console.log(`I: Not running on a Raspberry Pi.`);
2023-01-06 20:58:42 +00:00
var gpio = 'gpio';
functions.init(gpio).then(res => {
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] I: ${res}`);
main(gpio);
}).catch(rej => {
console.log(`[${(Date.now() - config.timestamps.procStart)/1000}] E: Error during initialization: ${rej}`);
2023-01-06 20:58:42 +00:00
process.exit(1);
});
break;
default:
console.log(`[${Date.now() - config.timestamps.procStart}] E: Problem with ENV file.`);
2023-01-06 20:58:42 +00:00
process.exit(1);
break;
}
2022-12-04 01:54:30 +00:00
// Export the above object, functions, as a module
module.exports = { functions };