From bdeb3152d21348ef36b9b12060deb826498fc326 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 22 Aug 2024 22:08:10 -0400 Subject: [PATCH] Ready to test startup and shutdown --- src/custom_modules/HestiaClasses.js | 46 ++++++++++++++++++----------- src/custom_modules/config.json | 8 +++-- src/main.js | 10 +++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index 3d35994..9e8a576 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -104,25 +104,35 @@ module.exports = { // Handle when the Broker sends us a message client.on('message', (topic, message) => { - // Save the existing state - const oldState = JSON.parse(JSON.stringify(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); - // 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; + if (topic.startsWith('hestia/status')) { + // Save the existing state + const oldState = JSON.parse(JSON.stringify(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); + // 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; + } + // console.log('Message is from the frontend, updating state'); + // Update the state + state[msgJson.name].on = msgJson.on; + // Emit the state change + this.emit('stateChange', oldState, state); + } else if (topic === 'hestia/command/startup') { + // Empty block for 'hestia/command' topics + this.emit('startup'); + } else if (topic === 'hestia/command/shutdown') { + // Empty block for 'hestia/command' topics + this.emit('shutdown'); + } else { + console.log(`Unknown topic: ${topic}`); } - // console.log('Message is from the frontend, updating state'); - // Update the state - state[msgJson.name].on = msgJson.on; - // Emit the state change - this.emit('stateChange', oldState, state); }); } diff --git a/src/custom_modules/config.json b/src/custom_modules/config.json index 28b92a4..58e42c9 100644 --- a/src/custom_modules/config.json +++ b/src/custom_modules/config.json @@ -8,7 +8,9 @@ "exhaust": "hestia/status/exhaust", "auger": "hestia/status/auger", "pof": "hestia/status/pof", - "vacuum": "hestia/status/vacuum" + "vacuum": "hestia/status/vacuum", + "startup": "hestia/command/startup", + "shutdown": "hestia/command/shutdown" } }, "states": { @@ -17,7 +19,9 @@ "exhaust", "auger", "pof", - "vacuum" + "vacuum", + "startup", + "shutdown" ] }, "pins": [ diff --git a/src/main.js b/src/main.js index 62f29ea..396f2f1 100644 --- a/src/main.js +++ b/src/main.js @@ -30,4 +30,14 @@ setInterval(() => { comms.on('stateChange', (oldState, state) => { console.log(`State change detected.`); fn.handlers.stateChange(oldState, state); +}); + +comms.on('startup', () => { + console.log(`Startup detected.`); + fn.power.start.init().catch(e => console.error(e)); +}); + +comms.on('shutdown', () => { + console.log(`Shutdown detected.`); + fn.power.stop.init().catch(e => console.error(e)); }); \ No newline at end of file