Import classes

This commit is contained in:
Skylar Grant 2024-08-15 08:40:20 -04:00
parent 288d19e95b
commit bb3f0cfad5
3 changed files with 90 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -0,0 +1,71 @@
export class State {
constructor(config) {
config.mqtt.subscriptions.forEach(subscription => {
this[subscription.name] = {
on: false,
topic: subscription.topic,
power: () => {
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true
this[subscription.name].on ? this[subscription.name].on = false : this[subscription.name].on = true;
}
};
});
};
};
export class Communicator {
constructor(state) {
// Connect to the MQTT Broker
this.client = mqtt.connect(config.mqtt.address);
// Subscribe to status topics
config.mqtt.subscriptions.forEach(subscription => {
this.client.subscribe(subscription.topic);
state[subscription.name].topic = subscription.topic;
});
}
powerOn(element, state) {
// Send the enable igniter message
// Publish with retain flag set to true
this.client.publish(topic, message, { retain: true }, (err) => {
if (err) {
console.error('Failed to publish message:', err);
} else {
console.log('Message published and retained on topic:', topic);
}
// Optionally close the connection
client.end();
});
// Confirm
// Update state
state.igniterOn();
return state;
}
igniterOn(state) {
}
augerOn(state) {
// Send the enable auger message
// Confirm
// Update state
state.augerOn();
return state;
}
exhaustOn(state) {
// Send the enable exhaust message
// Confirm
// Update state
state.exhaustOn();
return state;
}
}

View File

@ -0,0 +1,19 @@
{
"mqtt": {
"address": "wd://broker-url:port",
"subscriptions": [
{
"name": "igniter",
"topic": "hestia/status/igniter"
},
{
"name": "exhaust",
"topic": "hestia/status/exhaust"
},
{
"name": "auger",
"topic": "hestia/status/auger"
}
]
}
}