71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
|
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;
|
||
|
}
|
||
|
}
|