Added some debug logging, moved config to main file

This commit is contained in:
Skylar Grant 2024-08-15 10:24:49 -04:00
parent 358136a6ef
commit 97c2343993
3 changed files with 31 additions and 39 deletions

View File

@ -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;

View File

@ -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"
}
]
}
}

View File

@ -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) {