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)); 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) { init(state, config) {
// Connect to the MQTT Broker // 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, { this.client = mqtt.connect(config.mqtt.address, {
username: 'hestia', username: config.mqtt.username,
password: 'hestia' password: config.mqtt.password
}); });
const { client } = this; 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'; import { Communicator, State } from './HestiaClasses.js';
let config;
const psState = new State(); const psState = new State();
const comms = new Communicator(psState); 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() { window.onload = function() {
await fetch('/assets/config.json') psState.init(config);
.then(response => { comms.init(psState, config);
if (!response.ok) { refreshState(window.document, psState);
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);
});
}; };
function refreshState(doc, state) { function refreshState(doc, state) {
@ -39,6 +46,7 @@ function refreshState(doc, state) {
statusString = ''; statusString = '';
if (state.auger.on) statusString = "On"; else statusString = "Off"; if (state.auger.on) statusString = "On"; else statusString = "Off";
augerStatus.innerHTML = statusString; augerStatus.innerHTML = statusString;
console.log('State refreshed.');
} }
function power(element) { function power(element) {