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
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);
});
}

View File

@ -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": [

View File

@ -31,3 +31,13 @@ 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));
});