From 1c37b63314e09f6e236c6ed9d8d5533ab08fc092 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 15 Aug 2024 11:31:31 -0400 Subject: [PATCH] Spaghetti at wall. --- src/assets/HestiaClasses.js | 27 +++++++++++++++++---------- src/assets/hestia.js | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/assets/HestiaClasses.js b/src/assets/HestiaClasses.js index 8ba7315..7401563 100644 --- a/src/assets/HestiaClasses.js +++ b/src/assets/HestiaClasses.js @@ -1,37 +1,39 @@ export class State { - constructor() { - this.elements = ["igniter", "exhaust", "auger"], + constructor(config) { this.igniter = { on: false, - topic: 'hestia/status/igniter', + name: "igniter", + topic: config.mqtt.topics.igniter, 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.igniter.on ? this.igniter.on = false : this.igniter.on = true; - communicator.send('hestia/status/igniter', JSON.stringify(this.igniter)); + communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); } }; this.exhaust = { on: false, - topic: 'hestia/status/exhaust', + name: "exhaust", + topic: config.mqtt.topics.exhaust, 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.exhaust.on ? this.exhaust.on = false : this.exhaust.on = true; - communicator.send('hestia/status/exhaust', JSON.stringify(this.exhaust)); + communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); } }; this.auger = { on: false, + name: "auger", feedRate: 500, - topic: 'hestia/status/auger', + topic: config.mqtt.topics.auger, 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.auger.on ? this.auger.on = false : this.auger.on = true; - communicator.send('hestia/status/auger', JSON.stringify(this.auger)); + communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger)); } }; console.log(`State initialized.`) @@ -56,7 +58,7 @@ export class Communicator { client.on('connect', () => { console.log('Connected to MQTT broker'); // Subscribe to status topics - state.elements.forEach(element => { + config.states.elements.forEach(element => { client.subscribe(state[element].topic, (err) => { if (!err) { console.log(`Subscribed to ${state[element].topic}`); @@ -68,7 +70,12 @@ export class Communicator { // Handle when the Broker sends us a message client.on('message', (topic, message) => { - console.log(`Message received on topic ${topic}: ${message.toString()}`); + const msgStr = message.toString(); + const msgJson = JSON.parse(msgStr); + console.log(`Message received on topic ${topic}: ${msgStr}`); + console.log(msgJson); + state[msgJson.name] = msgJson; + window.refreshState(window.document, state); }); } diff --git a/src/assets/hestia.js b/src/assets/hestia.js index b43ca49..9fee2a1 100644 --- a/src/assets/hestia.js +++ b/src/assets/hestia.js @@ -1,13 +1,25 @@ import { Communicator, State } from './HestiaClasses.js'; -const psState = new State(); -const comms = new Communicator(psState); const config = { "mqtt": { "address": "wss://mqtt.3411.one", "username": "hestia", - "password": "hestia" + "password": "hestia", + "topics": { + "igniter": "hestia/status/igniter", + "exhaust": "hestia/status/exhaust", + "auger": "hestia/status/auger" + } + }, + "states": { + "elements": [ + "igniter", + "exhaust", + "auger" + ] } }; +const psState = new State(config); +const comms = new Communicator(psState); export function refreshState(doc, state) { const igniterStatus = doc.getElementById("igniter-status");