From 24470fe6f2e7fc3b0e3b64eba6df6d160813e786 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Wed, 21 Aug 2024 21:18:43 -0400 Subject: [PATCH] Reconfigure module --- src/custom_modules/HestiaClasses.js | 177 ++++++++++++++-------------- 1 file changed, 90 insertions(+), 87 deletions(-) diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index 62b0a5e..b80919e 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -1,93 +1,96 @@ -export class State { - constructor(config) { - this.igniter = { - on: false, - 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(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); - } +module.exports = { + // State class + State: class State { + constructor(config) { + this.igniter = { + on: false, + 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(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); + } + }; + + this.exhaust = { + on: false, + 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(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); + } + }; + + this.auger = { + on: false, + name: "auger", + feedRate: 500, + 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(config.mqtt.topics.auger, JSON.stringify(this.auger)); + } + }; + console.log(`State initialized.`) }; + }, + // Communicator class + Communicator: class Communicator { + constructor(state) { + this.publisher = state.publisher; + return this; + } - this.exhaust = { - on: false, - 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(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); - } - }; - - this.auger = { - on: false, - name: "auger", - feedRate: 500, - 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(config.mqtt.topics.auger, JSON.stringify(this.auger)); - } - }; - console.log(`State initialized.`) - }; -}; - -export class Communicator { - constructor(state) { - this.publisher = state.publisher; - return this; - } - - init(state, config) { - // 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, { - username: config.mqtt.username, - password: config.mqtt.password - }); - const { client } = this; - - client.on('connect', () => { - console.log('Connected to MQTT broker'); - // Subscribe to status topics - config.states.elements.forEach(element => { - client.subscribe(state[element].topic, (err) => { - if (!err) { - console.log(`Subscribed to ${state[element].topic}`); - } - }); + init(state, config) { + // 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, { + username: config.mqtt.username, + password: config.mqtt.password }); - - }); + const { client } = this; - // Handle when the Broker sends us a message - client.on('message', (topic, message) => { - const msgStr = message.toString(); - const msgJson = JSON.parse(msgStr); - console.log(`Message received on topic ${topic}: ${msgStr}`); - console.log(msgJson); - state[msgJson.name].on = msgJson.on; - window.refreshState(window.document, state); - }); - } + client.on('connect', () => { + console.log('Connected to MQTT broker'); + // Subscribe to status topics + config.states.elements.forEach(element => { + client.subscribe(state[element].topic, (err) => { + if (!err) { + console.log(`Subscribed to ${state[element].topic}`); + } + }); + }); + + }); - // Publish a message to the MQTT Broker - send(topic, message) { - // Publish with retain flag set to true - this.client.publish(topic, message, { retain: true }, (err) => { - if (err) { - console.error('Failed to publish message:', err); - } else { - console.log('Message published and retained on topic:', topic); - } - }); + // Handle when the Broker sends us a message + client.on('message', (topic, message) => { + const msgStr = message.toString(); + const msgJson = JSON.parse(msgStr); + console.log(`Message received on topic ${topic}: ${msgStr}`); + console.log(msgJson); + state[msgJson.name].on = msgJson.on; + window.refreshState(window.document, state); + }); + } + + // Publish a message to the MQTT Broker + send(topic, message) { + // Publish with retain flag set to true + this.client.publish(topic, message, { retain: true }, (err) => { + if (err) { + console.error('Failed to publish message:', err); + } else { + console.log('Message published and retained on topic:', topic); + } + }); + } } -} \ No newline at end of file +}; \ No newline at end of file