Spaghetti at wall.

This commit is contained in:
Skylar Grant 2024-08-15 11:31:31 -04:00
parent 432e2e7dfd
commit 1c37b63314
2 changed files with 32 additions and 13 deletions

View File

@ -1,37 +1,39 @@
export class State { export class State {
constructor() { constructor(config) {
this.elements = ["igniter", "exhaust", "auger"],
this.igniter = { this.igniter = {
on: false, on: false,
topic: 'hestia/status/igniter', name: "igniter",
topic: config.mqtt.topics.igniter,
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.igniter.on ? this.igniter.on = false : this.igniter.on = 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 = { this.exhaust = {
on: false, on: false,
topic: 'hestia/status/exhaust', name: "exhaust",
topic: config.mqtt.topics.exhaust,
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.exhaust.on ? this.exhaust.on = false : this.exhaust.on = 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 = { this.auger = {
on: false, on: false,
name: "auger",
feedRate: 500, feedRate: 500,
topic: 'hestia/status/auger', topic: config.mqtt.topics.auger,
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.auger.on ? this.auger.on = false : this.auger.on = 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.`) console.log(`State initialized.`)
@ -56,7 +58,7 @@ export class Communicator {
client.on('connect', () => { client.on('connect', () => {
console.log('Connected to MQTT broker'); console.log('Connected to MQTT broker');
// Subscribe to status topics // Subscribe to status topics
state.elements.forEach(element => { config.states.elements.forEach(element => {
client.subscribe(state[element].topic, (err) => { client.subscribe(state[element].topic, (err) => {
if (!err) { if (!err) {
console.log(`Subscribed to ${state[element].topic}`); console.log(`Subscribed to ${state[element].topic}`);
@ -68,7 +70,12 @@ export class Communicator {
// Handle when the Broker sends us a message // Handle when the Broker sends us a message
client.on('message', (topic, 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);
}); });
} }

View File

@ -1,13 +1,25 @@
import { Communicator, State } from './HestiaClasses.js'; import { Communicator, State } from './HestiaClasses.js';
const psState = new State();
const comms = new Communicator(psState);
const config = { const config = {
"mqtt": { "mqtt": {
"address": "wss://mqtt.3411.one", "address": "wss://mqtt.3411.one",
"username": "hestia", "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) { export function refreshState(doc, state) {
const igniterStatus = doc.getElementById("igniter-status"); const igniterStatus = doc.getElementById("igniter-status");