From 259700a1b5d699ca94555c375c3cde0a0bccf5b8 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 15 Aug 2024 11:12:54 -0400 Subject: [PATCH] Unconsolidate generating of states --- src/assets/HestiaClasses.js | 58 +++++++++++++++++++++++-------------- src/assets/hestia.js | 16 +--------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/assets/HestiaClasses.js b/src/assets/HestiaClasses.js index ed163bb..adcb6d4 100644 --- a/src/assets/HestiaClasses.js +++ b/src/assets/HestiaClasses.js @@ -1,25 +1,40 @@ export class State { constructor() { - this.publisher = 'front'; - return this; - }; + this.igniter = { + on: false, + topic: 'hestia/status/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)); + } + }; - init(config) { - config.mqtt.subscriptions.forEach(sub => { - this[sub.name] = { - on: false, - topic: sub.topic, - 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[sub.name].on ? this[sub.name].on = false : this[sub.name].on = true; - communicator.send(sub.topic, JSON.stringify(this)); - } - }; - console.log(`${sub.name} initialized.`) - }); + this.exhaust = { + on: false, + topic: 'hestia/status/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)); + } + }; + + this.auger = { + on: false, + feedRate: 500, + topic: 'hestia/status/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)); + } + }; console.log(`State initialized.`) - } + }; }; export class Communicator { @@ -40,11 +55,10 @@ export class Communicator { client.on('connect', () => { console.log('Connected to MQTT broker'); // Subscribe to status topics - config.mqtt.subscriptions.forEach(sub => { - client.subscribe(sub.topic, (err) => { + state.forEach(element => { + client.subscribe(element.topic, (err) => { if (!err) { - console.log(`Subscribed to ${sub.topic}`); - state[sub.name].topic = sub.topic; + console.log(`Subscribed to ${element.topic}`); } }); }); diff --git a/src/assets/hestia.js b/src/assets/hestia.js index b1d83e9..3f294a2 100644 --- a/src/assets/hestia.js +++ b/src/assets/hestia.js @@ -5,21 +5,7 @@ const config = { "mqtt": { "address": "wss://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" - } - ] + "password": "hestia" } };