Testing 2
This commit is contained in:
parent
180723e4f5
commit
63ef0da043
75
src/assets/HestiaClasses.js
Normal file
75
src/assets/HestiaClasses.js
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
// Custom Classes files
|
||||||
|
import mqtt from 'mqtt';
|
||||||
|
import { mqtt } from './config.json';
|
||||||
|
|
||||||
|
export class State {
|
||||||
|
constructor() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import { Communicator,State } from './HestiaClasses';
|
||||||
|
const psState = new State();
|
||||||
|
|
||||||
|
window.onload = function() {
|
||||||
|
refreshState(window.document);
|
||||||
|
};
|
||||||
|
|
||||||
|
function refreshState(doc) {
|
||||||
|
const igniterStatus = doc.getElementById("igniterStatus");
|
||||||
|
const exhaustStatus = doc.getElementById("exhaustStatus");
|
||||||
|
const augerStatus = doc.getElementById("augerStatus");
|
||||||
|
let statusString;
|
||||||
|
|
||||||
|
statusString = '';
|
||||||
|
if (psState.igniter.on) statusString = "On"; else statusString = "Off";
|
||||||
|
igniterStatus.innerHTML = statusString;
|
||||||
|
|
||||||
|
statusString = '';
|
||||||
|
if (psState.exhaust.on) statusString = "On"; else statusString = "Off";
|
||||||
|
exhaustStatus.innerHTML = statusString;
|
||||||
|
|
||||||
|
statusString = '';
|
||||||
|
if (psState.auger.on) statusString = "On"; else statusString = "Off";
|
||||||
|
augerStatus.innerHTML = statusString;
|
||||||
|
}
|
@ -6,6 +6,7 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Hestia</title>
|
<title>Hestia</title>
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
<script type="module" src="./assets/hestia.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="bg-gray-900 text-white font-sans">
|
<body class="bg-gray-900 text-white font-sans">
|
||||||
|
Loading…
Reference in New Issue
Block a user