From 594d177348a4532d9c758d43fb98a09123fb6452 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 22 Aug 2024 19:16:15 -0400 Subject: [PATCH] Testing improved init logic --- src/custom_modules/HestiaClasses.js | 44 +++++++++++++++++------------ src/custom_modules/functions.js | 20 +++++++++---- src/main.js | 6 ++-- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index ad768e6..b08b227 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -9,10 +9,10 @@ module.exports = { on: false, name: "igniter", topic: config.mqtt.topics.igniter, - publisher: 'front', - power: (communicator) => { - // This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true - this.igniter.on ? this.igniter.on = false : this.igniter.on = true; + publisher: 'backend', + power: (communicator, pinState) => { + // Set the power based on the desired pinState + this.igniter.on = pinState === 1 ? true : false; communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); } }; @@ -21,10 +21,10 @@ module.exports = { on: false, name: "exhaust", topic: config.mqtt.topics.exhaust, - publisher: 'front', - power: (communicator) => { - // This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true - this.exhaust.on ? this.exhaust.on = false : this.exhaust.on = true; + publisher: 'backend', + power: (communicator, pinState) => { + // Set the power based on the desired pinState + this.exhaust.on = pinState === 1 ? true : false; communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); } }; @@ -34,10 +34,10 @@ module.exports = { name: "auger", feedRate: 500, topic: config.mqtt.topics.auger, - publisher: 'front', - power: (communicator) => { - // This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true - this.auger.on ? this.auger.on = false : this.auger.on = true; + publisher: 'backend', + power: (communicator, pinState) => { + // Set the power based on the desired pinState + this.auger.on = pinState === 1 ? true : false; communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger)); } }; @@ -75,16 +75,24 @@ module.exports = { // Handle when the Broker sends us a message client.on('message', (topic, message) => { + // Save the existing state + const oldState = { ...state }; + // The message is a buffer which will need to be converted to string const msgStr = message.toString(); + // Since the message is a JSON object, we can parse it const msgJson = JSON.parse(msgStr); - console.log(`Message received on topic ${topic}: ${msgStr}`); + // Log the message + console.log(`Message received on topic ${topic}:`); console.log(msgJson); + // Check if the message is from the backend + if (msgJson.publisher === this.publisher) { + console.log('Message is from the backend, ignoring'); + return; + } + // Update the state state[msgJson.name].on = msgJson.on; - const change = { - name: msgJson.name, - on: msgJson.on - }; - this.emit('stateChange', change); + // Emit the state change + this.emit('stateChange', oldState, state); }); } diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index 0e3fc87..b63153e 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -19,7 +19,7 @@ module.exports = { return new Promise(resolve => setTimeout(resolve, ms)); }, gpio: { - setDefaults() { + setDefaults(communicator, state) { return new Promise((resolve, reject) => { let stateChanges = []; @@ -27,6 +27,7 @@ module.exports = { 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); }).catch(e => console.error(e)); } }); @@ -147,10 +148,19 @@ module.exports = { } }, handlers: { - stateChange(change) { - gpio.togglePin(pinMap.get(change.name).board).then(() => { - console.log(`${change.name} toggled.`); + stateChange(oldState, state) { + const promises = pins.map(pin => { + if (oldState[pin.key].on !== state[pin.key].on) { + if (state[pin.key].on) { + return gpio.setPin(pin.board, 1).then(() => { + console.log(`${pin.key} powered on.`); + }).catch(e => console.error(e)); + } + } + }); + + Promise.all(promises).then(() => { + console.log('State change complete.'); }).catch(e => console.error(e)); - } } } diff --git a/src/main.js b/src/main.js index 4f7595a..5bf67fe 100644 --- a/src/main.js +++ b/src/main.js @@ -22,7 +22,7 @@ setInterval(() => { } }, 1000); -comms.on('stateChange', (change) => { - console.log(`State change detected: ${change.name}`); - fn.handlers.stateChange(change); +comms.on('stateChange', (oldState, state) => { + console.log(`State change detected.`); + fn.handlers.stateChange(oldState, state); }); \ No newline at end of file