From 63ef0da0435a134062b89281649642f8180bbcf2 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Wed, 14 Aug 2024 19:00:58 -0400 Subject: [PATCH] Testing 2 --- src/assets/HestiaClasses.js | 75 +++++++++++++++++++++++++++++++++++++ src/assets/hestia.js | 25 +++++++++++++ src/assets/main.css | 0 src/index.html | 1 + 4 files changed, 101 insertions(+) create mode 100644 src/assets/HestiaClasses.js delete mode 100644 src/assets/main.css diff --git a/src/assets/HestiaClasses.js b/src/assets/HestiaClasses.js new file mode 100644 index 0000000..aa5bf75 --- /dev/null +++ b/src/assets/HestiaClasses.js @@ -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; + } +} \ No newline at end of file diff --git a/src/assets/hestia.js b/src/assets/hestia.js index e69de29..cef474a 100644 --- a/src/assets/hestia.js +++ b/src/assets/hestia.js @@ -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; +} \ No newline at end of file diff --git a/src/assets/main.css b/src/assets/main.css deleted file mode 100644 index e69de29..0000000 diff --git a/src/index.html b/src/index.html index ebd4830..ff2deae 100644 --- a/src/index.html +++ b/src/index.html @@ -6,6 +6,7 @@ Hestia +