From e7cf1d34f24465c2340240d3f6d77da0ca965db0 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 24 Nov 2024 15:17:38 -0500 Subject: [PATCH] Testing auger cycle --- src/custom_modules/functions.js | 52 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index feff1f6..8224856 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -19,21 +19,29 @@ module.exports = { return new Promise(resolve => setTimeout(resolve, ms)); }, gpio: { - setDefaults(communicator, state) { + setDefaults(comlink, state) { + // Set all output pins to their default states return new Promise((resolve, reject) => { + // Create an array to store state changes let stateChanges = []; + // Create an array of promises for each pin const promises = pins.map(pin => { if (pin.mode === 'OUT') { return gpio.setPin(pin.board, pin.defaultState).then(() => { stateChanges.push(`Set ${pin.key} pin to ${pin.defaultState}.`); - state[pin.key].power(communicator, pin.defaultState); + state[pin.key].power(comlink, pin.defaultState); + setTimeout(() => { + gpio.readPin(pin.board).then(pinState => { + module.exports.log(`Read Pin: ${pin.key} is ${pinState}`); + }).catch(e => console.error(e)); + }, 1000); }).catch(e => console.error(e)); } else if (pin.mode === 'IN') { return gpio.readPin(pin.board).then(pinState => { const boolState = pinState === '1' ? true : false; state[pin.key].on = boolState; - communicator.send(config.mqtt.topics[pin.key], JSON.stringify(state[pin.key])); + comlink.send(config.mqtt.topics[pin.key], JSON.stringify(state[pin.key])); stateChanges.push(`${pin.key}: ${state}`); }).catch(e => console.error(e)); } @@ -45,36 +53,36 @@ module.exports = { }).catch(reject); }); }, - async init(communicator, state) { + async init(comlink, state) { module.exports.log('Resetting all output pins.'); - module.exports.gpio.setDefaults(communicator, state).then((changes) => { + module.exports.gpio.setDefaults(comlink, state).then((changes) => { module.exports.log(changes); }).catch(e => console.error(e)); } }, routines: { - startup(communicator, state) { + startup(comlink, state) { return new Promise(async (resolve, reject) => { const mod = module.exports; // Set pins to default states - mod.gpio.setDefaults(communicator, state).then(changes => { + mod.gpio.setDefaults(comlink, state).then(changes => { mod.log(changes); }).catch(e => console.error(e)); // Turn on the exhaust - mod.power.exhaust.on(communicator, state).then((res) => { + mod.power.exhaust.on(comlink, state).then((res) => { // Wait for vacuum mod.sleep(config.power.start.vacuumCheckDelay).then(() => { // Vacuum switch is in series with auger so no need for logic check // Turn on the auger - mod.power.auger.on(communicator, state).then((res) => { + mod.power.auger.on(comlink, state).then((res) => { // Wait for auger to start }).catch(e => console.error(e)); }); }).catch(e => console.error(e)); }); }, - shutdown(communicator, state) { + shutdown(comlink, state) { return new Promise(async (resolve, reject) => { }); @@ -149,7 +157,7 @@ module.exports = { } }, start: { - init(communicator, state) { + init(comlink, state) { return new Promise(async (resolve, reject) => { // TODO: Check pin states? @@ -255,20 +263,30 @@ module.exports = { }, handlers: { stateChange(oldState, state) { + // Create a promise for each state to check const promises = pins.map(pin => { + // Check if the power state changed if (oldState[pin.key].on !== state[pin.key].on) { + // Did it turn on or off? if (state[pin.key].on) { - return gpio.setPin(pin.board, 1).then(() => { - console.log(`${pin.key} powered on.`); - }).catch(e => console.error(e)); + if (pin.key !== 'auger') { + // Set the corresponding pin to the new state + return gpio.setPin(pin.board, 1).then(() => { + console.log(`State Change Handler: ${pin.key} powered on.`); + }).catch(e => console.error(`GPIO Set Pin: ${e}`)); + } } else { - return gpio.setPin(pin.board, 0).then(() => { - console.log(`${pin.key} powered off.`); - }).catch(e => console.error(e)); + if (pin.key !== 'auger') { + // Set the corresponding pin to the new state + return gpio.setPin(pin.board, 0).then(() => { + console.log(`State Change Handler: ${pin.key} powered off.`); + }).catch(e => console.error(`GPIO Set Pin: ${e}`)); + } } } }); + // Wait for all promises to resolve Promise.all(promises).then(() => { console.log('State change complete.'); }).catch(e => console.error(e));