Finish moving and testing functions file, Promises

This commit is contained in:
Skylar Grant 2022-12-03 19:42:47 -05:00
parent 5ba4fbada9
commit a29701e6eb
2 changed files with 115 additions and 65 deletions

View File

@ -2,8 +2,16 @@
const dotenv = require('dotenv').config(); const dotenv = require('dotenv').config();
// Module for working with files // Module for working with files
const fs = require('fs'); const fs = require('fs');
const { resolve } = require('path');
// Setup for use with the Pi's GPIO pins // Setup for use with the Pi's GPIO pins
const gpio = require('rpi-gpio'); if (process.env.ONPI == 'true') {
console.log('Running on a Raspberry Pi.');
const gpio = require('rpi-gpio');
} else if (process.env.ONPI == 'false') {
console.log('Not running on a Raspberry Pi.');
} else {
console.log('Problem with ENV file.');
}
// The functions we'll export to be used in other files // The functions we'll export to be used in other files
const functions = { const functions = {
@ -15,63 +23,90 @@ const functions = {
}, },
// Turns the auger on (Pin 7 high) // Turns the auger on (Pin 7 high)
on() { on() {
gpio.write(7, true, function(err) { return new Promise((resolve) => {
if (err) throw err; if (process.env.ONPI == 'true') {
if (process.env.DEBUG == "true") console.log('Auger turned on.'); gpio.write(7, true, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('Auger turned on.');
});
} else {
console.log('Auger turned on.');
resolve('Auger turned on.');
}
}); });
}, },
// Turns the auger off (pin 7 low) // Turns the auger off (pin 7 low)
off() { off() {
gpio.write(7, false, function(err) { return new Promise((resolve) => {
if (err) throw err; if (process.env.ONPI == 'true') {
if (process.env.DEBUG == "true") console.log('Auger turned off.'); gpio.write(7, false, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('Auger turned off.');
});
} else {
console.log('Auger turned off.');
resolve('Auger turned off.');
}
}); });
}, },
// Cycles the auger using the two functions above this one (functions.auger.on() and functions.auger.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() // Sleeps in between cycles using functions.sleep()
cycle() { cycle() {
this.on(); return new Promise((resolve) => {
functions.sleep(process.env.ONTIME); this.on().then(() => {
this.off(); functions.sleep(process.env.ONTIME).then(() => {
functions.sleep(process.env.OFFTIME); this.off().then(() => {
return; functions.sleep(process.env.OFFTIME).then(() => {
resolve("Cycle complete.");
});
});
});
});
});
}, },
}, },
files: { files: {
async check() { check() {
// TODO this code needs to be finished from migration return new Promise((resolve, reject) => {
// Check for pause file existing, then sleep for preset time, then run the function again. // TODO this code needs to be finished from migration
if (fs.existsSync('./pause')) { // Check for pause file existing, then sleep for preset time, then run the function again.
return "pause"; if (fs.existsSync('./pause')) {
} resolve("pause");
}
// Check for reload file existing, then reload environment variables, then delete the file. // Check for reload file existing, then reload environment variables, then delete the file.
if (fs.existsSync('./reload')) { if (fs.existsSync('./reload')) {
return "reload"; resolve("reload");
} }
// Check for quit file existing, then delete it, then quit the program // Check for quit file existing, then delete it, then quit the program
if (fs.existsSync('./quit')) { if (fs.existsSync('./quit')) {
return "quit"; resolve("quit");
} }
return "none"; resolve("none");
});
}, },
}, },
commands: { commands: {
pause() { pause() {
console.log('Paused...'); return new Promise((resolve) => {
this.sleep(process.env.PAUSETIME).then(() => { return; }); functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
return;
},
reload() {
dotenv.config({ override: true })
fs.unlink('./reload', (err) => {
if (err) throw err;
console.log('Deleted reload file.');
}); });
console.log('Reloaded environment variables.'); },
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`); reload(envs) {
return; return new Promise((resolve) => {
const dotenv = require('dotenv').config({ override: true });
fs.unlink('./reload', (err) => {
if (err) throw err;
console.log('Deleted reload file.');
});
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}`);
resolve();
});
}, },
quit() { quit() {
fs.unlink('./quit', (err) => { fs.unlink('./quit', (err) => {
@ -85,19 +120,26 @@ const functions = {
// Sleeps for any given milliseconds, call with await // Sleeps for any given milliseconds, call with await
sleep(ms) { sleep(ms) {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(resolve, ms); if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`); const finish = () => {
if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`);
resolve();
}
setTimeout(finish, ms);
}); });
}, },
init() { init() {
// Write the current env vars to console // Write the current env vars to console
console.log('Environment variables:'); console.log('Environment variables:');
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`); console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`);
return true; return true;
}, },
} }
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready() // Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
gpio.setup(7, gpio.DIR_OUT, functions.auger.ready()); if (process.env.ONPI == 'true') {
gpio.setup(7, gpio.DIR_OUT, functions.auger.ready());
}
module.exports = { functions } module.exports = { functions };

48
main.js
View File

@ -1,32 +1,40 @@
// Custom functions module to keep main script clean // Custom functions module to keep main script clean
const fn = require('./functions').functions; const fn = require('./functions.js').functions;
// Environment Variables Importing // Environment Variables Importing
const dotenv = require('dotenv').config(); const dotenv = require('dotenv').config();
// TODO Add logic for other sensors // TODO Add logic for other sensors
while (true) { main(fn);
main();
}
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
async function main() { async function main(fn) {
switch (fn.files.check()) { fn.files.check().then((res,rej) => {
case "pause": console.log('File Check: ' + res);
fn.commands.pause(); switch (res) {
break; case "pause":
case "reload": fn.commands.pause().then(() => {
fn.commands.reload(); main(fn);
break; });
case "quit": break;
fn.commands.quit(); case "reload":
break; fn.commands.reload().then(() => {
case "none": main(fn);
fn.auger.cycle(); });
break; break;
case "quit":
fn.commands.quit();
break;
case "none":
fn.auger.cycle().then(() => {
main(fn);
});
break;
default: default:
main(fn);
break; break;
} }
});
} }