Merge pull request #1 from voidf1sh/dev

This commit is contained in:
Skylar Grant 2022-12-03 20:34:33 -05:00 committed by GitHub
commit f356df7096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 241 additions and 74 deletions

145
functions.js Normal file
View File

@ -0,0 +1,145 @@
// Get environment variables
const dotenv = require('dotenv').config();
// Module for working with files
const fs = require('fs');
const { resolve } = require('path');
// 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) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(7, true, function(err) {
if (err) throw err;
resolve('Auger turned on.');
});
} else {
console.log('NOPI Auger turned on.');
resolve('NOPI Auger turned on.');
}
});
},
// Turns the auger off (pin 7 low)
off(gpio) {
return new Promise((resolve) => {
if (process.env.ONPI == 'true') {
gpio.write(7, false, function(err) {
if (err) throw err;
resolve('Auger turned on.');
});
} else {
console.log('NOPI Auger turned off.');
resolve('NOPI 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(gpio) {
return new Promise((resolve) => {
this.on(gpio).then((res) => {
if (process.env.DEBUG == 'true') console.log(res);
functions.sleep(process.env.ONTIME).then((res) => {
if (process.env.DEBUG == 'true') console.log(res);
this.off(gpio).then((res) => {
if (process.env.DEBUG == 'true') console.log(res);
functions.sleep(process.env.OFFTIME).then((res) => {
if (process.env.DEBUG == 'true') console.log(res);
resolve("Cycle complete.");
});
});
});
});
});
},
},
files: {
check() {
return new Promise((resolve, reject) => {
// 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')) {
resolve("pause");
}
// Check for reload file existing, then reload environment variables, then delete the file.
if (fs.existsSync('./reload')) {
resolve("reload");
}
// Check for quit file existing, then delete it, then quit the program
if (fs.existsSync('./quit')) {
resolve("quit");
}
resolve("none");
});
},
},
commands: {
pause() {
return new Promise((resolve) => {
functions.sleep(process.env.PAUSETIME).then(() => { resolve(); });
});
},
reload(envs) {
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() {
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) => {
if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`);
const finish = () => {
resolve(`Slept for ${ms}ms`);
}
setTimeout(finish, ms);
});
},
init(gpio) {
return new Promise((resolve, reject) => {
// 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}\nONPI=${process.env.ONPI}`);
// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready()
if (process.env.ONPI == 'true') {
gpio.setup(7, gpio.DIR_OUT, (err) => {
if (err) reject(err);
resolve('GPIO Initialized');
});
} else {
resolve('GPIO Not Available');
}
});
},
}
module.exports = { functions };

131
main.js
View File

@ -1,83 +1,68 @@
// 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');
// Custom functions module to keep main script clean
const fn = require('./functions.js').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}`);
// Environment Variables Importing
const dotenv = require('dotenv').config();
// Set up GPIO 4 (pysical pin 7) as output, then call cycleAuger()
gpio.setup(7, gpio.DIR_OUT, cycleAuger);
// Setup for use with the Pi's GPIO pins
if (process.env.ONPI == 'true') {
console.log('Running on a Raspberry Pi.');
const gpio = require('rpi-gpio');
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else if (process.env.ONPI == 'false') {
console.log('Not running on a Raspberry Pi.');
const gpio = 'gpio';
fn.init(gpio).then((res, rej) => {
if (res != undefined) {
console.log(res);
main(fn, gpio);
} else {
console.error(rej);
}
});
} else {
console.log('Problem with ENV file.');
}
// 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`);
});
}
// 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, gpio) {
fn.files.check().then((res,rej) => {
console.log('File Check: ' + res);
switch (res) {
case "pause":
fn.commands.pause().then(() => {
main(fn, gpio);
});
break;
case "reload":
fn.commands.reload().then(() => {
main(fn, gpio);
});
break;
case "quit":
fn.commands.quit();
break;
case "none":
fn.auger.cycle(gpio).then((res) => {
if (process.env.DEBUG == 'true') console.log(res);
main(fn, gpio);
});
break;
// 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}`);
}
// 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();
default:
main(fn, gpio);
break;
}
});
}

28
package-lock.json generated Normal file
View File

@ -0,0 +1,28 @@
{
"name": "pscontrolpanel",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "pscontrolpanel",
"dependencies": {
"dotenv": "^16.0.3"
}
},
"node_modules/dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==",
"engines": {
"node": ">=12"
}
}
},
"dependencies": {
"dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ=="
}
}
}

9
package.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "pscontrolpanel",
"version": "0.1.0",
"requires": true,
"packages": {},
"dependencies": {
"dotenv": "^16.0.3"
}
}