Ready to test startup and shutdown

This commit is contained in:
Skylar Grant 2024-08-22 22:08:10 -04:00
parent 7fa0339791
commit bdeb3152d2
3 changed files with 44 additions and 20 deletions

View File

@ -104,25 +104,35 @@ module.exports = {
// Handle when the Broker sends us a message // Handle when the Broker sends us a message
client.on('message', (topic, message) => { client.on('message', (topic, message) => {
// Save the existing state if (topic.startsWith('hestia/status')) {
const oldState = JSON.parse(JSON.stringify(state)); // Save the existing state
// The message is a buffer which will need to be converted to string const oldState = JSON.parse(JSON.stringify(state));
const msgStr = message.toString(); // The message is a buffer which will need to be converted to string
// Since the message is a JSON object, we can parse it const msgStr = message.toString();
const msgJson = JSON.parse(msgStr); // Since the message is a JSON object, we can parse it
// Log the message const msgJson = JSON.parse(msgStr);
// console.log(`Message received on topic ${topic}:`); // Log the message
// console.log(msgJson); // console.log(`Message received on topic ${topic}:`);
// Check if the message is from the backend // console.log(msgJson);
if (msgJson.publisher === this.publisher) { // Check if the message is from the backend
// console.log('Message is from the backend, ignoring'); if (msgJson.publisher === this.publisher) {
return; // 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);
}); });
} }

View File

@ -8,7 +8,9 @@
"exhaust": "hestia/status/exhaust", "exhaust": "hestia/status/exhaust",
"auger": "hestia/status/auger", "auger": "hestia/status/auger",
"pof": "hestia/status/pof", "pof": "hestia/status/pof",
"vacuum": "hestia/status/vacuum" "vacuum": "hestia/status/vacuum",
"startup": "hestia/command/startup",
"shutdown": "hestia/command/shutdown"
} }
}, },
"states": { "states": {
@ -17,7 +19,9 @@
"exhaust", "exhaust",
"auger", "auger",
"pof", "pof",
"vacuum" "vacuum",
"startup",
"shutdown"
] ]
}, },
"pins": [ "pins": [

View File

@ -31,3 +31,13 @@ comms.on('stateChange', (oldState, state) => {
console.log(`State change detected.`); console.log(`State change detected.`);
fn.handlers.stateChange(oldState, state); 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));
});