Gonna go testing

This commit is contained in:
Skylar Grant 2024-08-15 09:46:45 -04:00
parent 5633c2340a
commit 63053d6a87
3 changed files with 42 additions and 15 deletions

View File

@ -1,29 +1,48 @@
export class State { export class State {
constructor(config) { constructor() {
config.mqtt.subscriptions.forEach(subscription => { this.publisher = 'front';
this[subscription.name] = { return this;
};
init(config) {
config.mqtt.subscriptions.forEach(sub => {
this[sub.name] = {
on: false, on: false,
topic: subscription.topic, topic: sub.topic,
publisher: 'front', publisher: 'front',
power: (communicator) => { power: (communicator) => {
// This *should* toggle the state, asks if state is true, if it is set it false, otherwise set it true // 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; this[sub.name].on ? this[sub.name].on = false : this[sub.name].on = true;
communicator.send(subscription.name, JSON.stringify(this)); communicator.send(sub.topic, JSON.stringify(this));
} }
}; };
}); });
}; }
}; };
export class Communicator { export class Communicator {
constructor(state) { constructor(state) {
this.publisher = state.publisher;
return this;
}
init(state) {
// Connect to the MQTT Broker // Connect to the MQTT Broker
this.client = mqtt.connect(config.mqtt.address); this.client = mqtt.connect(config.mqtt.address);
const { client } = this;
client.on('connect', () => {
console.log('Connected to MQTT broker');
// Subscribe to status topics // Subscribe to status topics
config.mqtt.subscriptions.forEach(subscription => { config.mqtt.subscriptions.forEach(sub => {
this.client.subscribe(subscription.topic); client.subscribe(sub.topic, (err) => {
state[subscription.name].topic = subscription.topic; if (!err) {
console.log(`Subscribed to ${sub.topic}`);
state[sub.name].topic = sub.topic;
}
});
});
}); });
} }

View File

@ -1,6 +1,6 @@
{ {
"mqtt": { "mqtt": {
"address": "wd://broker-url:port", "address": "ws://192.168.0.3:1883",
"subscriptions": [ "subscriptions": [
{ {
"name": "igniter", "name": "igniter",

View File

@ -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() { window.onload = async function() {
await fetch('/assets/config.json') await fetch('/assets/config.json')
@ -9,9 +12,10 @@ window.onload = async function() {
return response.json(); // Parse the JSON data from the response return response.json(); // Parse the JSON data from the response
}) })
.then(data => { .then(data => {
const config = data; config = data;
console.log(config); console.log(config);
const psState = new State(config); psState.init(config);
comms.init(psState);
refreshState(window.document, psState); refreshState(window.document, psState);
}) })
.catch(error => { .catch(error => {
@ -37,3 +41,7 @@ function refreshState(doc, state) {
if (state.auger.on) statusString = "On"; else statusString = "Off"; if (state.auger.on) statusString = "On"; else statusString = "Off";
augerStatus.innerHTML = statusString; augerStatus.innerHTML = statusString;
} }
function power(element) {
psState[element].power(comms);
}