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));
},
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));