Import classes
This commit is contained in:
parent
288d19e95b
commit
bb3f0cfad5
71
src/custom_modules/HestiaClasses.js
Normal file
71
src/custom_modules/HestiaClasses.js
Normal 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;
|
||||
}
|
||||
}
|
19
src/custom_modules/config.json
Normal file
19
src/custom_modules/config.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user