diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index 8e6daf4..6e82af3 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -102,22 +102,21 @@ module.exports = { }, cycleAuger() { const mod = module.exports; - return new Promise(async (resolve, reject) => { - // Check if the auger is enabled - if (process.psState.auger.on) { - mod.power.auger.on().then((res) => { - // Sleep while auger is feeding - mod.sleep(process.psState.auger.feedRate).then(() => { - // Turn off auger - mod.power.auger.off().then((res) => { - resolve('Auger cycle complete.'); - }).catch(e => reject(e)); - }); - }).catch(e => reject(e)); - } else { - resolve('Auger is disabled.'); - } - }); + // Check if the auger is enabled + if (process.psState.auger.on) { + mod.power.auger.on().then((res) => { + // Sleep while auger is feeding + mod.log(`Auger feeding at ${process.psState.auger.feedRate}ms.`, 'DEBUG'); + mod.sleep(process.psState.auger.feedRate).then(() => { + // Turn off auger + mod.power.auger.off().then((res) => { + mod.log('Auger cycle complete.', 'DEBUG'); + }).catch(e => reject(e)); + }); + }).catch(e => reject(e)); + } else { + mod.log('Auger is disabled.', 'DEBUG'); + } } }, power: { @@ -282,27 +281,48 @@ module.exports = { if (oldState[pin.key].on !== state[pin.key].on) { // Did it turn on or off? if (state[pin.key].on) { - 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}`)); + switch (pin.key) { + case 'exhaust': + module.exports.log('Exhaust powered on.', 'DEBUG'); + return module.exports.power.exhaust.on(); + case 'igniter': + module.exports.log('Igniter powered on.', 'DEBUG'); + return module.exports.power.igniter.on(); + case 'auger': + module.exports.log('Auger powered on.', 'DEBUG'); + return module.exports.handlers.toggleAuger(); + default: + module.exports.log('Invalid pin key.', 'ERROR'); + throw new Error('Invalid pin key.'); } } else { - 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}`)); + switch (pin.key) { + case 'exhaust': + module.exports.log('Exhaust powered off.', 'DEBUG'); + return module.exports.power.exhaust.off(); + case 'igniter': + module.exports.log('Igniter powered off.', 'DEBUG'); + return module.exports.power.igniter.off(); + case 'auger': + module.exports.log('Auger powered off.', 'DEBUG'); + return module.exports.handlers.toggleAuger(); + default: + module.exports.log('Invalid pin key.', 'ERROR'); + throw new Error('Invalid pin key.'); } + } } }); // Wait for all promises to resolve - Promise.all(promises).then(() => { - console.log('State change complete.'); - }).catch(e => console.error(e)); + Promise.all(promises).catch(e => console.error(e)); + }, + toggleAuger() { + return new Promise(async (resolve, reject) => { + // This is so stupid but I wanted to use promises.all so here we are (: + resolve(); + }); } } }