From 673aa2faf576974f154589f319a8c6aa9dcf1c75 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Wed, 21 Aug 2024 21:01:55 -0400 Subject: [PATCH] Logic and MQTT Basics added not tested --- TODO.md | 3 +- src/custom_modules/config.json | 2 +- src/custom_modules/functions.js | 75 ++++++++++++++++++++++++--------- src/main.js | 8 ++++ 4 files changed, 66 insertions(+), 22 deletions(-) diff --git a/TODO.md b/TODO.md index 3588a9e..30bb8a5 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,8 @@ # In Progress 1. ~~Strip to bones~~ 2. Better commenting -2. Add startup and shutdown logic (start half implemented, not tested) +3. Add startup and shutdown logic (implemented, not tested) +4. Connect to MQTT # Done 1. GPIO Interface diff --git a/src/custom_modules/config.json b/src/custom_modules/config.json index d3108a2..dee8a45 100644 --- a/src/custom_modules/config.json +++ b/src/custom_modules/config.json @@ -1,6 +1,6 @@ { "mqtt": { - "address": "wd://broker-url:port", + "address": "https://mqtt.3411.one/", "subscriptions": [ { "name": "igniter", diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index 908287b..a272bca 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -1,7 +1,7 @@ const dotenv = require('dotenv').config(); const debug = process.env.DEBUG === "TRUE"; const config = require('./config.json'); -const { pins } = require('./config.json'); +const { pins } = config; const gpio = require('./VoidGPIO.js'); const pinMap = new Map(); @@ -59,10 +59,9 @@ module.exports = { }); break; case 'IN': - gpio.readPin(pin.board, (err, state) => { - if (err) throw err; + gpio.readPin(pin.board).then(state => { module.exports.log(`${pin.key} state: ${state}`); - }); + }).catch(e => console.error(e)); default: break; } @@ -75,41 +74,77 @@ module.exports = { return new Promise(async (resolve, reject) => { // TODO: Check pin states? - // T_DONE: Set pins to default states + // Set pins to default states module.exports.gpio.setDefaults().then(changes => { module.exports.log(changes); }).catch(e => console.error(e)); - // T_DONE: Power on igniter + // Power on igniter gpio.setPin(pinMap.igniter.board, 1).then(async () => { - // T_DONE: Wait for igniter preheat + // Wait for igniter preheat await module.exports.sleep(config.power.start.exhaustDelay); }).catch(e => console.error(e)); - // T_DONE: Start exhaust + // Start exhaust gpio.setPin(pinMap.exhaust.board, 1).then(async () => { - // T_DONE: Finish igniter preheat + // Finish igniter preheat await module.exports.sleep(config.power.start.augerDelay); }).catch(e => console.error(e)); // Check for vacuum - + gpio.readPin(pinMap.vacuum.board).then(state => { + if (state === '0') { + // Power off exhaust + gpio.setPin(pinMap.exhaust.board, 0).then(() => { + // Report vacuum failure + reject(new Error('Vacuum failure.')); + return; + }).catch(e => console.error(e)); + } else { + // Start auger + gpio.setPin(pinMap.auger.board, 1).then(async () => { + // Wait for fire + await module.exports.sleep(config.power.start.fireCheckDelay); - // Start auger - - // Wait for fire - - // Check for fire - - // Power off igniter - - // Report successful ignition + // Power off igniter + gpio.setPin(pinMap.igniter.board, 0).then(() => { + // Check for fire on pof + gpio.readPin(pinMap.pof.board).then(state => { + if (state === '0') { + // Power off auger + gpio.setPin(pinMap.auger.board, 0).then(() => { + // Report failed ignition + reject(new Error('Failed ignition.')); + }).catch(e => console.error(e)); + } else { + // Power off auger + gpio.setPin(pinMap.auger.board, 0).then(() => { + // Report successful ignition + resolve('Successful ignition.'); + }).catch(e => console.error(e)); + } + }).catch(e => console.error(e)); + }).catch(e => console.error(e)); + }).catch(e => console.error(e)); + } + }).catch(e => console.error(e)); }); } }, stop: { init() { - + return new Promise(async (resolve, reject) => { + // Power off auger + gpio.setPin(pinMap.auger.board, 0).then(async () => { + // Wait for exhaust shutdown delay + await module.exports.sleep(config.power.stop.exhaustDelay); + // Power off exhaust + gpio.setPin(pinMap.exhaust.board, 0).then(() => { + // Report successful shutdown + resolve('Successful shutdown.'); + }).catch(e => console.error(e)); + }).catch(e => console.error(e)); + }); } } } diff --git a/src/main.js b/src/main.js index 839042a..5acbfcf 100644 --- a/src/main.js +++ b/src/main.js @@ -2,8 +2,16 @@ const gpio = require('./custom_modules/VoidGPIO.js'); const { pins } = require('./custom_modules/config.json'); const fn = require('./custom_modules/functions.js'); +const { State, Communicator } = require('./custom_modules/HestiaClasses.js'); + +const psState = new State(config); +const comms = new Communicator(psState); + +comms.init(psState, config); fn.gpio.debugInit(); + +// Sensor detection loop setInterval(() => { for (const pin of pins) { if (pin.mode === 'IN') {