Moving functions to their own file (untested)
This commit is contained in:
parent
cf2dac1494
commit
cc10b7fb0a
105
functions.js
Normal file
105
functions.js
Normal 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
95
main.js
@ -1,83 +1,30 @@
|
|||||||
// npm module for Raspberri Pi GPIO
|
const fn = require('./functions').functions;
|
||||||
var gpio = require('rpi-gpio');
|
|
||||||
// Module for importing environment variables
|
|
||||||
var dotenv = require('dotenv').config();
|
|
||||||
// Module for working with files
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
// 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
|
// TODO Add logic for other sensors
|
||||||
|
|
||||||
// Turns the auger on (Pin 7 high)
|
while (true) {
|
||||||
function augerOn(err) {
|
await main();
|
||||||
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`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 cycleAuger(err) {
|
async function main() {
|
||||||
if (err) throw err;
|
fn.files.check();
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for reload file existing, then reload environment variables, then delete the file.
|
switch (fn.files.check()) {
|
||||||
if (fs.existsSync('./reload')) {
|
case "pause":
|
||||||
var dotenv = require('dotenv');
|
fn.commands.pause();
|
||||||
dotenv.config({ override: true })
|
break;
|
||||||
fs.unlink('./reload', (err) => {
|
case "reload":
|
||||||
if (err) throw err;
|
fn.commands.reload();
|
||||||
console.log('Deleted reload file.');
|
break;
|
||||||
});
|
case "quit":
|
||||||
console.log('Reloaded environment variables.');
|
fn.commands.quit();
|
||||||
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`);
|
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();
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user