From 63053d6a87c736e884b77c72562c9fe55ab07cd1 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 15 Aug 2024 09:46:45 -0400 Subject: [PATCH] Gonna go testing --- src/assets/HestiaClasses.js | 41 +++++++++++++++++++++++++++---------- src/assets/config.json | 2 +- src/assets/hestia.js | 14 ++++++++++--- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/assets/HestiaClasses.js b/src/assets/HestiaClasses.js index bbc065b..62a0826 100644 --- a/src/assets/HestiaClasses.js +++ b/src/assets/HestiaClasses.js @@ -1,29 +1,48 @@ export class State { - constructor(config) { - config.mqtt.subscriptions.forEach(subscription => { - this[subscription.name] = { + constructor() { + this.publisher = 'front'; + return this; + }; + + init(config) { + config.mqtt.subscriptions.forEach(sub => { + this[sub.name] = { on: false, - topic: subscription.topic, + topic: sub.topic, publisher: 'front', power: (communicator) => { // 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; - communicator.send(subscription.name, JSON.stringify(this)); + this[sub.name].on ? this[sub.name].on = false : this[sub.name].on = true; + communicator.send(sub.topic, JSON.stringify(this)); } }; }); - }; + } }; export class Communicator { constructor(state) { + this.publisher = state.publisher; + return this; + } + + init(state) { // Connect to the MQTT Broker this.client = mqtt.connect(config.mqtt.address); + const { client } = this; - // Subscribe to status topics - config.mqtt.subscriptions.forEach(subscription => { - this.client.subscribe(subscription.topic); - state[subscription.name].topic = subscription.topic; + client.on('connect', () => { + console.log('Connected to MQTT broker'); + // Subscribe to status topics + config.mqtt.subscriptions.forEach(sub => { + client.subscribe(sub.topic, (err) => { + if (!err) { + console.log(`Subscribed to ${sub.topic}`); + state[sub.name].topic = sub.topic; + } + }); + }); + }); } diff --git a/src/assets/config.json b/src/assets/config.json index 129060e..5d78abf 100644 --- a/src/assets/config.json +++ b/src/assets/config.json @@ -1,6 +1,6 @@ { "mqtt": { - "address": "wd://broker-url:port", + "address": "ws://192.168.0.3:1883", "subscriptions": [ { "name": "igniter", diff --git a/src/assets/hestia.js b/src/assets/hestia.js index 3088bef..13dae6c 100644 --- a/src/assets/hestia.js +++ b/src/assets/hestia.js @@ -1,4 +1,7 @@ -import { Communicator,State } from './HestiaClasses.js'; +import { Communicator, State } from './HestiaClasses.js'; +let config; +const psState = new State(); +const comms = new Communicator(state); window.onload = async function() { await fetch('/assets/config.json') @@ -9,9 +12,10 @@ window.onload = async function() { return response.json(); // Parse the JSON data from the response }) .then(data => { - const config = data; + config = data; console.log(config); - const psState = new State(config); + psState.init(config); + comms.init(psState); refreshState(window.document, psState); }) .catch(error => { @@ -36,4 +40,8 @@ function refreshState(doc, state) { statusString = ''; if (state.auger.on) statusString = "On"; else statusString = "Off"; augerStatus.innerHTML = statusString; +} + +function power(element) { + psState[element].power(comms); } \ No newline at end of file