From 2444cbde4d347b4745819d191691f0eb4c5ebec6 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 3 Dec 2022 20:22:43 -0500 Subject: [PATCH] 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;