From cc10b7fb0a3fdb365455e9884c99bdd5afd2b196 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:03:57 -0500 Subject: [PATCH 01/15] Moving functions to their own file (untested) --- functions.js | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.js | 95 +++++++++++----------------------------------- 2 files changed, 126 insertions(+), 74 deletions(-) create mode 100644 functions.js diff --git a/functions.js b/functions.js new file mode 100644 index 0000000..536c18b --- /dev/null +++ b/functions.js @@ -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 } \ No newline at end of file diff --git a/main.js b/main.js index f107d00..1a0cabb 100644 --- a/main.js +++ b/main.js @@ -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(); } \ No newline at end of file From 0cd0c928090529e6dd7a1973bbe4ed6b57acd5e8 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:10:31 -0500 Subject: [PATCH 02/15] Moving dotenv vars --- main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.js b/main.js index 1a0cabb..3af2495 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,9 @@ +// Custom functions module to keep main script clean const fn = require('./functions').functions; +// Environment Variables Importing +const dotenv = require('dotenv').config(); + // TODO Add logic for other sensors while (true) { From 2606a5258f3d0f6d3a1502017cdaf805dbe8b8be Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:13:32 -0500 Subject: [PATCH 03/15] Remove erroneous await --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index 3af2495..639fdc1 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,7 @@ const dotenv = require('dotenv').config(); // TODO Add logic for other sensors while (true) { - await main(); + main(); } // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. From 9ef858cc9489f8cd410a52d3684fcea5f48bb257 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:20:38 -0500 Subject: [PATCH 04/15] Bugfixes --- functions.js | 7 +++---- package-lock.json | 28 ++++++++++++++++++++++++++++ package.json | 8 ++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/functions.js b/functions.js index 536c18b..45adc23 100644 --- a/functions.js +++ b/functions.js @@ -15,7 +15,7 @@ const functions = { if (err) throw err; console.log('Auger GPIO Ready'); return; - } + }, // Turns the auger on (Pin 7 high) on() { gpio.write(7, true, function(err) { @@ -63,7 +63,7 @@ const functions = { commands: { pause() { console.log('Paused...'); - await this.sleep(process.env.PAUSETIME); + this.sleep(process.env.PAUSETIME).then(() => { return; }); return; }, reload() { @@ -97,9 +97,8 @@ const functions = { 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 } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..eef442a --- /dev/null +++ b/package-lock.json @@ -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==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..869b739 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "pscontrolpanel", + "requires": true, + "packages": {}, + "dependencies": { + "dotenv": "^16.0.3" + } +} From 6bd49f1c3363844b7f7f05cf8bf5f8fe8d46e033 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:22:25 -0500 Subject: [PATCH 05/15] Incorrect function path --- functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.js b/functions.js index 45adc23..68903fb 100644 --- a/functions.js +++ b/functions.js @@ -6,7 +6,7 @@ const fs = require('fs'); 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); +gpio.setup(7, gpio.DIR_OUT, functions.auger.ready()); // The functions we'll export to be used in other files const functions = { From e3e22b61bef0fc1e0299ad88322249ad1fe8bf54 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:23:30 -0500 Subject: [PATCH 06/15] Move location of GPIO init --- functions.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/functions.js b/functions.js index 68903fb..5851963 100644 --- a/functions.js +++ b/functions.js @@ -5,9 +5,6 @@ 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, functions.auger.ready()); - // The functions we'll export to be used in other files const functions = { auger: { @@ -100,5 +97,7 @@ const functions = { }, } +// Set up GPIO 4 (pysical pin 7) as output, then call functions.auger.ready() +gpio.setup(7, gpio.DIR_OUT, functions.auger.ready()); module.exports = { functions } \ No newline at end of file From 5ba4fbada92eb94799016d1bcd836c0eadcedcee Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 18:25:38 -0500 Subject: [PATCH 07/15] Remove redundant call to files.check() --- main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.js b/main.js index 639fdc1..7b38518 100644 --- a/main.js +++ b/main.js @@ -12,8 +12,6 @@ while (true) { // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. async function main() { - fn.files.check(); - switch (fn.files.check()) { case "pause": fn.commands.pause(); From a29701e6eb2b8fdf333eb7835d73177fb7bf636f Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 19:42:47 -0500 Subject: [PATCH 08/15] Finish moving and testing functions file, Promises --- functions.js | 130 ++++++++++++++++++++++++++++++++++----------------- main.js | 50 +++++++++++--------- 2 files changed, 115 insertions(+), 65 deletions(-) diff --git a/functions.js b/functions.js index 5851963..601c112 100644 --- a/functions.js +++ b/functions.js @@ -2,8 +2,16 @@ const dotenv = require('dotenv').config(); // Module for working with files const fs = require('fs'); +const { resolve } = require('path'); // 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 const functions = { @@ -15,63 +23,90 @@ const functions = { }, // 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.'); + return new Promise((resolve) => { + if (process.env.ONPI == 'true') { + 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) off() { - gpio.write(7, false, function(err) { - if (err) throw err; - if (process.env.DEBUG == "true") console.log('Auger turned off.'); + return new Promise((resolve) => { + if (process.env.ONPI == 'true') { + 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()) // Sleeps in between cycles using functions.sleep() cycle() { - this.on(); - functions.sleep(process.env.ONTIME); - this.off(); - functions.sleep(process.env.OFFTIME); - return; + return new Promise((resolve) => { + this.on().then(() => { + functions.sleep(process.env.ONTIME).then(() => { + this.off().then(() => { + functions.sleep(process.env.OFFTIME).then(() => { + resolve("Cycle complete."); + }); + }); + }); + }); + }); }, }, 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() { + 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')) { - return "reload"; - } + // 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')) { - return "quit"; - } - return "none"; + // Check for quit file existing, then delete it, then quit the program + if (fs.existsSync('./quit')) { + resolve("quit"); + } + resolve("none"); + }); }, }, commands: { pause() { - console.log('Paused...'); - this.sleep(process.env.PAUSETIME).then(() => { return; }); - return; - }, - reload() { - dotenv.config({ override: true }) - fs.unlink('./reload', (err) => { - if (err) throw err; - console.log('Deleted reload file.'); + return new Promise((resolve) => { + functions.sleep(process.env.PAUSETIME).then(() => { resolve(); }); }); - console.log('Reloaded environment variables.'); - console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`); - return; + }, + 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) => { @@ -85,19 +120,26 @@ const functions = { // 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`); + if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`); + const finish = () => { + if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`); + resolve(); + } + setTimeout(finish, 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}`); + console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}\nONPI=${process.env.ONPI}`); return true; }, } // 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 } \ No newline at end of file +module.exports = { functions }; \ No newline at end of file diff --git a/main.js b/main.js index 7b38518..e22792a 100644 --- a/main.js +++ b/main.js @@ -1,32 +1,40 @@ // Custom functions module to keep main script clean -const fn = require('./functions').functions; +const fn = require('./functions.js').functions; // Environment Variables Importing const dotenv = require('dotenv').config(); // TODO Add logic for other sensors -while (true) { - main(); -} +main(fn); // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. -async function main() { - switch (fn.files.check()) { - case "pause": - fn.commands.pause(); +async function main(fn) { + fn.files.check().then((res,rej) => { + console.log('File Check: ' + res); + switch (res) { + case "pause": + fn.commands.pause().then(() => { + main(fn); + }); + break; + case "reload": + fn.commands.reload().then(() => { + main(fn); + }); + break; + case "quit": + fn.commands.quit(); + break; + case "none": + fn.auger.cycle().then(() => { + main(fn); + }); + break; + + default: + main(fn); break; - case "reload": - fn.commands.reload(); - break; - case "quit": - fn.commands.quit(); - break; - case "none": - fn.auger.cycle(); - break; - - default: - break; - } + } + }); } \ No newline at end of file From a0628313768936e6e3c93514a7d7b8996e1535c4 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 19:55:47 -0500 Subject: [PATCH 09/15] Reimplement GPIO initialization --- functions.js | 37 +++++++++++++++++-------------------- main.js | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/functions.js b/functions.js index 601c112..dda1911 100644 --- a/functions.js +++ b/functions.js @@ -3,15 +3,7 @@ const dotenv = require('dotenv').config(); // Module for working with files const fs = require('fs'); const { resolve } = require('path'); -// 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'); -} 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 const functions = { @@ -22,7 +14,7 @@ const functions = { return; }, // Turns the auger on (Pin 7 high) - on() { + on(gpio) { return new Promise((resolve) => { if (process.env.ONPI == 'true') { gpio.write(7, true, function(err) { @@ -129,17 +121,22 @@ const functions = { }); }, - 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}\nONPI=${process.env.ONPI}`); - return true; + 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'); + } + }); }, } -// 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, functions.auger.ready()); -} - module.exports = { functions }; \ No newline at end of file diff --git a/main.js b/main.js index e22792a..a3f70ac 100644 --- a/main.js +++ b/main.js @@ -4,12 +4,39 @@ const fn = require('./functions.js').functions; // Environment Variables Importing const dotenv = require('dotenv').config(); +// 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 Add logic for other sensors -main(fn); + // Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats. -async function main(fn) { +async function main(fn, gpio) { fn.files.check().then((res,rej) => { console.log('File Check: ' + res); switch (res) { @@ -27,7 +54,7 @@ async function main(fn) { fn.commands.quit(); break; case "none": - fn.auger.cycle().then(() => { + fn.auger.cycle(gpio).then(() => { main(fn); }); break; From 216daf1e38a879dba15e998b87e7a5d55802ebfa Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 19:59:12 -0500 Subject: [PATCH 10/15] Add missing variables --- functions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions.js b/functions.js index dda1911..47a28f6 100644 --- a/functions.js +++ b/functions.js @@ -45,11 +45,11 @@ const functions = { }, // 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() { + cycle(gpio) { return new Promise((resolve) => { - this.on().then(() => { + this.on(gpio).then(() => { functions.sleep(process.env.ONTIME).then(() => { - this.off().then(() => { + this.off(gpio).then(() => { functions.sleep(process.env.OFFTIME).then(() => { resolve("Cycle complete."); }); From 973816757e8987f7764bd3a7ee9ec8b284946bb3 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:16:54 -0500 Subject: [PATCH 11/15] Better logging --- functions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functions.js b/functions.js index 47a28f6..007ab2c 100644 --- a/functions.js +++ b/functions.js @@ -22,8 +22,8 @@ const functions = { if (process.env.DEBUG == "true") console.log('Auger turned on.'); }); } else { - console.log('Auger turned on.'); - resolve('Auger turned on.'); + console.log('NOPI Auger turned on.'); + resolve('NOPI Auger turned on.'); } }); @@ -37,8 +37,8 @@ const functions = { if (process.env.DEBUG == "true") console.log('Auger turned off.'); }); } else { - console.log('Auger turned off.'); - resolve('Auger turned off.'); + console.log('NOPI Auger turned off.'); + resolve('NOPI Auger turned off.'); } }); From 2444cbde4d347b4745819d191691f0eb4c5ebec6 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:22:43 -0500 Subject: [PATCH 12/15] Better logging, and resolving of promises --- functions.js | 21 ++++++++++++--------- main.js | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/functions.js b/functions.js index 007ab2c..d4bfbc0 100644 --- a/functions.js +++ b/functions.js @@ -19,7 +19,7 @@ const functions = { if (process.env.ONPI == 'true') { gpio.write(7, true, function(err) { if (err) throw err; - if (process.env.DEBUG == "true") console.log('Auger turned on.'); + resolve('Auger turned on.'); }); } else { console.log('NOPI Auger turned on.'); @@ -34,7 +34,8 @@ const functions = { if (process.env.ONPI == 'true') { gpio.write(7, false, function(err) { if (err) throw err; - if (process.env.DEBUG == "true") console.log('Auger turned off.'); + resolve('Auger turned on.'); + }); } else { console.log('NOPI Auger turned off.'); @@ -47,10 +48,14 @@ const functions = { // Sleeps in between cycles using functions.sleep() cycle(gpio) { return new Promise((resolve) => { - this.on(gpio).then(() => { - functions.sleep(process.env.ONTIME).then(() => { - this.off(gpio).then(() => { - functions.sleep(process.env.OFFTIME).then(() => { + 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."); }); }); @@ -114,11 +119,9 @@ const functions = { return new Promise((resolve) => { if (process.env.DEBUG == "true") console.log(`Sleeping for ${ms}ms`); const finish = () => { - if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`); - resolve(); + resolve(`Slept for ${ms}ms`); } setTimeout(finish, ms); - }); }, init(gpio) { diff --git a/main.js b/main.js index a3f70ac..d23842b 100644 --- a/main.js +++ b/main.js @@ -54,7 +54,8 @@ async function main(fn, gpio) { fn.commands.quit(); break; case "none": - fn.auger.cycle(gpio).then(() => { + fn.auger.cycle(gpio).then((res) => { + if (process.env.DEBUG == 'true') console.log(res); main(fn); }); break; From c2226d1c6fa4433a426c6d4ac1c0354d7981dad2 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:25:18 -0500 Subject: [PATCH 13/15] Missing passoff of variable --- functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.js b/functions.js index d4bfbc0..d82b141 100644 --- a/functions.js +++ b/functions.js @@ -29,7 +29,7 @@ const functions = { }, // Turns the auger off (pin 7 low) - off() { + off(gpio) { return new Promise((resolve) => { if (process.env.ONPI == 'true') { gpio.write(7, false, function(err) { From cc15dbd64b682028ff9f3c1b4733bf3a8d30c647 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:28:04 -0500 Subject: [PATCH 14/15] More missing variable passings --- main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index d23842b..fe6723e 100644 --- a/main.js +++ b/main.js @@ -42,12 +42,12 @@ async function main(fn, gpio) { switch (res) { case "pause": fn.commands.pause().then(() => { - main(fn); + main(fn, gpio); }); break; case "reload": fn.commands.reload().then(() => { - main(fn); + main(fn, gpio); }); break; case "quit": @@ -56,12 +56,12 @@ async function main(fn, gpio) { case "none": fn.auger.cycle(gpio).then((res) => { if (process.env.DEBUG == 'true') console.log(res); - main(fn); + main(fn, gpio); }); break; default: - main(fn); + main(fn, gpio); break; } }); From b91503dfa6f9984d79158663f36932ebb97b259b Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:31:49 -0500 Subject: [PATCH 15/15] Increment version --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 869b739..0922555 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "pscontrolpanel", + "version": "0.1.0", "requires": true, "packages": {}, "dependencies": {