2022-12-06 05:05:35 +00:00
|
|
|
// Physical Pin numbers for GPIO
|
|
|
|
const augerPin = 7; // Pin for controlling the relay for the pellet auger motor.
|
|
|
|
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.
|
|
|
|
const tempPin = 18; // Pin for receiving data from a DS18B20 OneWire temperature sensor.
|
|
|
|
const vacuumPin = 22; // Pin for sensing the status (open/closed) of the vacuum switch.
|
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
// Get environment variables
|
|
|
|
const dotenv = require('dotenv').config();
|
|
|
|
// Module for working with files
|
|
|
|
const fs = require('fs');
|
2022-12-04 01:54:30 +00:00
|
|
|
// Promises I think?
|
2022-12-04 00:42:47 +00:00
|
|
|
const { resolve } = require('path');
|
2022-12-04 00:55:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +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
|
2022-12-03 23:03:57 +00:00
|
|
|
ready(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Auger GPIO Ready');
|
|
|
|
return;
|
2022-12-03 23:20:38 +00:00
|
|
|
},
|
2022-12-03 23:03:57 +00:00
|
|
|
// Turns the auger on (Pin 7 high)
|
2022-12-04 00:55:47 +00:00
|
|
|
on(gpio) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (process.env.ONPI == 'true') {
|
|
|
|
gpio.write(7, true, function(err) {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve('Auger turned on.');
|
2022-12-04 00:42:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-12-04 01:16:54 +00:00
|
|
|
console.log('NOPI Auger turned on.');
|
|
|
|
resolve('NOPI Auger turned on.');
|
2022-12-04 00:42:47 +00:00
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
// Turns the auger off (pin 7 low)
|
2022-12-04 01:25:18 +00:00
|
|
|
off(gpio) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (process.env.ONPI == 'true') {
|
|
|
|
gpio.write(7, false, function(err) {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve('Auger turned on.');
|
|
|
|
|
2022-12-04 00:42:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-12-04 01:16:54 +00:00
|
|
|
console.log('NOPI Auger turned off.');
|
|
|
|
resolve('NOPI Auger turned off.');
|
2022-12-04 00:42:47 +00:00
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
// 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) {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Turn the auger on
|
2022-12-04 01:22:43 +00:00
|
|
|
this.on(gpio).then((res) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Log action if in debug mode
|
2022-12-04 01:22:43 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
2022-12-04 01:54:30 +00:00
|
|
|
// Sleep for the time set in env variables
|
2022-12-04 01:22:43 +00:00
|
|
|
functions.sleep(process.env.ONTIME).then((res) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Log action if in debug mode
|
2022-12-04 01:22:43 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
2022-12-04 01:54:30 +00:00
|
|
|
// Turn the auger off
|
2022-12-04 01:22:43 +00:00
|
|
|
this.off(gpio).then((res) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Log action if in debug mode
|
2022-12-04 01:22:43 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
2022-12-04 01:54:30 +00:00
|
|
|
// Sleep for the time set in env variables
|
2022-12-04 01:22:43 +00:00
|
|
|
functions.sleep(process.env.OFFTIME).then((res) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Log action if in debug mode
|
2022-12-04 01:22:43 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log(res);
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise, letting the main script know the cycle is complete
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("Cycle complete.");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-03 23:03:57 +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
|
2022-12-04 00:42:47 +00:00
|
|
|
check() {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Check for pause file existing
|
2022-12-04 00:42:47 +00:00
|
|
|
if (fs.existsSync('./pause')) {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise, letting the main script know what we found
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("pause");
|
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
|
2022-12-04 01:54:30 +00:00
|
|
|
// Check for reload file existing
|
2022-12-04 00:42:47 +00:00
|
|
|
if (fs.existsSync('./reload')) {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise, letting the main script know what we found
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("reload");
|
|
|
|
}
|
2022-12-03 23:03:57 +00:00
|
|
|
|
2022-12-04 01:54:30 +00:00
|
|
|
// Check for quit file existing
|
2022-12-04 00:42:47 +00:00
|
|
|
if (fs.existsSync('./quit')) {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise, letting the main script know what we found
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("quit");
|
|
|
|
}
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise, letting the main script know what we found (nothing)
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve("none");
|
|
|
|
});
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
commands: {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Pauses the script for the time defined in env variables
|
2022-12-03 23:03:57 +00:00
|
|
|
pause() {
|
2022-12-04 00:42:47 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
|
|
|
|
});
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
2022-12-04 01:54:30 +00:00
|
|
|
// Reload the environment variables on the fly
|
2022-12-04 00:42:47 +00:00
|
|
|
reload(envs) {
|
|
|
|
return new Promise((resolve) => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Re-require dotenv because inheritance in js sucks
|
2022-12-04 00:42:47 +00:00
|
|
|
const dotenv = require('dotenv').config({ override: true });
|
2022-12-04 01:54:30 +00:00
|
|
|
// Delete the reload file
|
2022-12-04 00:42:47 +00:00
|
|
|
fs.unlink('./reload', (err) => {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:54:30 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log('Deleted reload file.');
|
2022-12-04 00:42:47 +00:00
|
|
|
});
|
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?
|
2022-12-04 00:42:47 +00:00
|
|
|
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}`);
|
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
|
2022-12-04 00:42:47 +00:00
|
|
|
resolve();
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 00:42:47 +00:00
|
|
|
|
2022-12-03 23:03:57 +00:00
|
|
|
},
|
2022-12-04 01:54:30 +00:00
|
|
|
// Shutdown the script gracefully
|
2022-12-03 23:03:57 +00:00
|
|
|
quit() {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Delete the quit file
|
2022-12-03 23:03:57 +00:00
|
|
|
fs.unlink('./quit', (err) => {
|
|
|
|
if (err) throw err;
|
2022-12-04 01:54:30 +00:00
|
|
|
if (process.env.DEBUG == 'true') console.log('Removed quit file.');
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
2022-12-04 01:54:30 +00:00
|
|
|
// Print out that the script is quitting
|
2022-12-03 23:03:57 +00:00
|
|
|
console.log('Quitting...');
|
2022-12-04 01:54:30 +00:00
|
|
|
// Quit the script
|
2022-12-03 23:03:57 +00:00
|
|
|
process.exit();
|
|
|
|
},
|
|
|
|
},
|
2022-12-06 05:05:35 +00:00
|
|
|
// Sleeps for any given milliseconds
|
2022-12-03 23:03:57 +00:00
|
|
|
sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
2022-12-04 00:42:47 +00:00
|
|
|
if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
|
2022-12-04 01:54:30 +00:00
|
|
|
// Function to be called when setTimeout finishes
|
2022-12-04 00:42:47 +00:00
|
|
|
const finish = () => {
|
2022-12-04 01:54:30 +00:00
|
|
|
// Resolve the promise
|
2022-12-04 01:22:43 +00:00
|
|
|
resolve(`Slept for ${ms}ms`);
|
2022-12-04 01:54:30 +00:00
|
|
|
};
|
|
|
|
// The actual sleep function, sleeps for ms then calls finish()
|
2022-12-04 00:42:47 +00:00
|
|
|
setTimeout(finish, ms);
|
2022-12-03 23:03:57 +00:00
|
|
|
});
|
|
|
|
},
|
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) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Write the current env vars to console
|
2022-12-06 05:20:41 +00:00
|
|
|
console.log(`Environment variables:
|
|
|
|
ONTIME=${process.env.ONTIME}
|
|
|
|
OFFTIME=${process.env.OFFTIME}
|
|
|
|
PAUSETIME=${process.env.PAUSETIME}
|
|
|
|
DEBUG=${process.env.DEBUG}
|
|
|
|
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);
|
2022-12-06 05:05:35 +00:00
|
|
|
if (process.env.DEBUG == 'true') 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.');
|
|
|
|
// 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.');
|
|
|
|
// 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.');
|
|
|
|
// 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.');
|
|
|
|
// 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.');
|
|
|
|
// 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
|
2022-12-04 00:55:47 +00:00
|
|
|
resolve('GPIO Not Available');
|
|
|
|
}
|
|
|
|
});
|
2022-12-03 23:20:38 +00:00
|
|
|
},
|
2022-12-03 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 01:54:30 +00:00
|
|
|
// Export the above object, functions, as a module
|
2022-12-04 00:42:47 +00:00
|
|
|
module.exports = { functions };
|