Testing auger cycle

This commit is contained in:
Skylar Grant 2024-11-24 15:17:38 -05:00
parent e9c54210aa
commit e7cf1d34f2

View File

@ -19,21 +19,29 @@ module.exports = {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
}, },
gpio: { gpio: {
setDefaults(communicator, state) { setDefaults(comlink, state) {
// Set all output pins to their default states
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Create an array to store state changes
let stateChanges = []; let stateChanges = [];
// Create an array of promises for each pin
const promises = pins.map(pin => { const promises = pins.map(pin => {
if (pin.mode === 'OUT') { if (pin.mode === 'OUT') {
return gpio.setPin(pin.board, pin.defaultState).then(() => { return gpio.setPin(pin.board, pin.defaultState).then(() => {
stateChanges.push(`Set ${pin.key} pin to ${pin.defaultState}.`); 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)); }).catch(e => console.error(e));
} else if (pin.mode === 'IN') { } else if (pin.mode === 'IN') {
return gpio.readPin(pin.board).then(pinState => { return gpio.readPin(pin.board).then(pinState => {
const boolState = pinState === '1' ? true : false; const boolState = pinState === '1' ? true : false;
state[pin.key].on = boolState; 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}`); stateChanges.push(`${pin.key}: ${state}`);
}).catch(e => console.error(e)); }).catch(e => console.error(e));
} }
@ -45,36 +53,36 @@ module.exports = {
}).catch(reject); }).catch(reject);
}); });
}, },
async init(communicator, state) { async init(comlink, state) {
module.exports.log('Resetting all output pins.'); 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); module.exports.log(changes);
}).catch(e => console.error(e)); }).catch(e => console.error(e));
} }
}, },
routines: { routines: {
startup(communicator, state) { startup(comlink, state) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const mod = module.exports; const mod = module.exports;
// Set pins to default states // Set pins to default states
mod.gpio.setDefaults(communicator, state).then(changes => { mod.gpio.setDefaults(comlink, state).then(changes => {
mod.log(changes); mod.log(changes);
}).catch(e => console.error(e)); }).catch(e => console.error(e));
// Turn on the exhaust // Turn on the exhaust
mod.power.exhaust.on(communicator, state).then((res) => { mod.power.exhaust.on(comlink, state).then((res) => {
// Wait for vacuum // Wait for vacuum
mod.sleep(config.power.start.vacuumCheckDelay).then(() => { mod.sleep(config.power.start.vacuumCheckDelay).then(() => {
// Vacuum switch is in series with auger so no need for logic check // Vacuum switch is in series with auger so no need for logic check
// Turn on the auger // 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 // Wait for auger to start
}).catch(e => console.error(e)); }).catch(e => console.error(e));
}); });
}).catch(e => console.error(e)); }).catch(e => console.error(e));
}); });
}, },
shutdown(communicator, state) { shutdown(comlink, state) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
}); });
@ -149,7 +157,7 @@ module.exports = {
} }
}, },
start: { start: {
init(communicator, state) { init(comlink, state) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
// TODO: Check pin states? // TODO: Check pin states?
@ -255,20 +263,30 @@ module.exports = {
}, },
handlers: { handlers: {
stateChange(oldState, state) { stateChange(oldState, state) {
// Create a promise for each state to check
const promises = pins.map(pin => { const promises = pins.map(pin => {
// Check if the power state changed
if (oldState[pin.key].on !== state[pin.key].on) { if (oldState[pin.key].on !== state[pin.key].on) {
// Did it turn on or off?
if (state[pin.key].on) { if (state[pin.key].on) {
if (pin.key !== 'auger') {
// Set the corresponding pin to the new state
return gpio.setPin(pin.board, 1).then(() => { return gpio.setPin(pin.board, 1).then(() => {
console.log(`${pin.key} powered on.`); console.log(`State Change Handler: ${pin.key} powered on.`);
}).catch(e => console.error(e)); }).catch(e => console.error(`GPIO Set Pin: ${e}`));
}
} else { } else {
if (pin.key !== 'auger') {
// Set the corresponding pin to the new state
return gpio.setPin(pin.board, 0).then(() => { return gpio.setPin(pin.board, 0).then(() => {
console.log(`${pin.key} powered off.`); console.log(`State Change Handler: ${pin.key} powered off.`);
}).catch(e => console.error(e)); }).catch(e => console.error(`GPIO Set Pin: ${e}`));
}
} }
} }
}); });
// Wait for all promises to resolve
Promise.all(promises).then(() => { Promise.all(promises).then(() => {
console.log('State change complete.'); console.log('State change complete.');
}).catch(e => console.error(e)); }).catch(e => console.error(e));