Moving functions to their own file (untested)

This commit is contained in:
Skylar Grant 2022-12-03 18:03:57 -05:00
parent cf2dac1494
commit cc10b7fb0a
2 changed files with 126 additions and 74 deletions

105
functions.js Normal file
View File

@ -0,0 +1,105 @@
// Get environment variables
const dotenv = require('dotenv').config();
// Module for working with files
const fs = require('fs');
// Setup for use with the Pi's GPIO pins
const gpio = require('rpi-gpio');
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
gpio.setup(7, gpio.DIR_OUT, this.functions.auger.ready);
// The functions we'll export to be used in other files
const functions = {
auger: {
ready(err) {
if (err) throw err;
console.log('Auger GPIO Ready');
return;
}
// Turns the auger on (Pin 7 high)
on() {
gpio.write(7, true, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('Auger turned on.');
});
},
// Turns the auger off (pin 7 low)
off() {
gpio.write(7, false, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('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()
cycle() {
this.on();
functions.sleep(process.env.ONTIME);
this.off();
functions.sleep(process.env.OFFTIME);
return;
},
},
files: {
async check() {
// TODO this code needs to be finished from migration
// Check for pause file existing, then sleep for preset time, then run the function again.
if (fs.existsSync('./pause')) {
return "pause";
}
// Check for reload file existing, then reload environment variables, then delete the file.
if (fs.existsSync('./reload')) {
return "reload";
}
// Check for quit file existing, then delete it, then quit the program
if (fs.existsSync('./quit')) {
return "quit";
}
return "none";
},
},
commands: {
pause() {
console.log('Paused...');
await this.sleep(process.env.PAUSETIME);
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}`);
return;
},
quit() {
fs.unlink('./quit', (err) => {
if (err) throw err;
console.log('Removed quit file.');
});
console.log('Quitting...');
process.exit();
},
},
// Sleeps for any given milliseconds, call with await
sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`);
});
},
init() {
// Write the current env vars to console
console.log('Environment variables:');
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`);
return true;
}
}
}
module.exports = { functions }

95
main.js
View File

@ -1,83 +1,30 @@
// npm module for Raspberri Pi GPIO
var gpio = require('rpi-gpio');
// Module for importing environment variables
var dotenv = require('dotenv').config();
// Module for working with files
var fs = require('fs');
const fn = require('./functions').functions;
// Write the current env vars to console
console.log('Environment variables:');
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`);
// Set up GPIO 4 (pysical pin 7) as output, then call cycleAuger()
gpio.setup(7, gpio.DIR_OUT, cycleAuger);
// TODO Move functions to another file for cleaner code
// TODO Add logic for other sensors
// Turns the auger on (Pin 7 high)
function augerOn(err) {
if (err) throw err;
gpio.write(7, true, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('Auger turned on.');
});
}
// Turns the auger off (pin 7 low)
function augerOff(err) {
if (err) throw err;
gpio.write(7, false, function(err) {
if (err) throw err;
if (process.env.DEBUG == "true") console.log('Auger turned off.');
});
}
// Sleeps for any given milliseconds, call with await
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`);
});
while (true) {
await main();
}
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
async function cycleAuger(err) {
if (err) throw err;
// Check for pause file existing, then sleep for preset time, then run the function again.
if (fs.existsSync('./pause')) {
console.log('Paused...');
await sleep(process.env.PAUSETIME);
cycleAuger();
return;
}
async function main() {
fn.files.check();
// Check for reload file existing, then reload environment variables, then delete the file.
if (fs.existsSync('./reload')) {
var dotenv = require('dotenv');
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}`);
switch (fn.files.check()) {
case "pause":
fn.commands.pause();
break;
case "reload":
fn.commands.reload();
break;
case "quit":
fn.commands.quit();
break;
case "none":
fn.auger.cycle();
break;
default:
break;
}
// Check for quit file existing, then delete it, then quit the program
if (fs.existsSync('./quit')) {
fs.unlink('./quit', (err) => {
if (err) throw err;
console.log('Removed quit file.');
});
console.log('Quitting.');
process.exit();
}
// If none of the above checks are true, cycle the auger on and off, then repeat
augerOn();
await sleep(process.env.ONTIME);
augerOff();
await sleep(process.env.OFFTIME);
cycleAuger();
}