From 97c23439931a98fbde62695f04b8837406975c20 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 15 Aug 2024 10:24:49 -0400 Subject: [PATCH] Added some debug logging, moved config to main file --- src/assets/HestiaClasses.js | 7 ++++-- src/assets/config.json | 19 ---------------- src/assets/hestia.js | 44 ++++++++++++++++++++++--------------- 3 files changed, 31 insertions(+), 39 deletions(-) delete mode 100644 src/assets/config.json diff --git a/src/assets/HestiaClasses.js b/src/assets/HestiaClasses.js index aae041f..ed163bb 100644 --- a/src/assets/HestiaClasses.js +++ b/src/assets/HestiaClasses.js @@ -16,7 +16,9 @@ export class State { communicator.send(sub.topic, JSON.stringify(this)); } }; + console.log(`${sub.name} initialized.`) }); + console.log(`State initialized.`) } }; @@ -28,9 +30,10 @@ export class Communicator { init(state, config) { // Connect to the MQTT Broker + console.log(`Attempting MQTT connection to broker: ${config.mqtt.address}, with username: ${config.mqtt.username}`); this.client = mqtt.connect(config.mqtt.address, { - username: 'hestia', - password: 'hestia' + username: config.mqtt.username, + password: config.mqtt.password }); const { client } = this; diff --git a/src/assets/config.json b/src/assets/config.json deleted file mode 100644 index 29e37cb..0000000 --- a/src/assets/config.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "mqtt": { - "address": "https://mqtt.3411.one", - "subscriptions": [ - { - "name": "igniter", - "topic": "hestia/status/igniter" - }, - { - "name": "exhaust", - "topic": "hestia/status/exhaust" - }, - { - "name": "auger", - "topic": "hestia/status/auger" - } - ] - } -} \ No newline at end of file diff --git a/src/assets/hestia.js b/src/assets/hestia.js index 61f6dd5..668cda4 100644 --- a/src/assets/hestia.js +++ b/src/assets/hestia.js @@ -1,25 +1,32 @@ import { Communicator, State } from './HestiaClasses.js'; -let config; const psState = new State(); const comms = new Communicator(psState); +const config = { + "mqtt": { + "address": "https://mqtt.3411.one", + "username": "hestia", + "password": "hestia", + "subscriptions": [ + { + "name": "igniter", + "topic": "hestia/status/igniter" + }, + { + "name": "exhaust", + "topic": "hestia/status/exhaust" + }, + { + "name": "auger", + "topic": "hestia/status/auger" + } + ] + } +}; -window.onload = async function() { - await fetch('/assets/config.json') - .then(response => { - if (!response.ok) { - throw new Error('Network response was not ok'); - } - return response.json(); // Parse the JSON data from the response - }) - .then(data => { - config = data; - psState.init(config); - comms.init(psState, config); - refreshState(window.document, psState); - }) - .catch(error => { - console.error('There was a problem with the fetch operation:', error); - }); +window.onload = function() { + psState.init(config); + comms.init(psState, config); + refreshState(window.document, psState); }; function refreshState(doc, state) { @@ -39,6 +46,7 @@ function refreshState(doc, state) { statusString = ''; if (state.auger.on) statusString = "On"; else statusString = "Off"; augerStatus.innerHTML = statusString; + console.log('State refreshed.'); } function power(element) {