From 5e20cfff6eccd8ff913d642b4312baee8c23a5e6 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 30 Nov 2024 16:52:10 -0500 Subject: [PATCH] Minor changes --- src/custom_modules/HestiaClasses.js | 21 +++++++++++++++++---- src/custom_modules/config.json | 2 -- src/custom_modules/functions.js | 6 ------ src/main.js | 11 +++++++---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index f78da31..738b6e9 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -88,11 +88,11 @@ module.exports = { return this; } - init(state, config) { + init(state, host, port, config) { // Connect to the MQTT Broker - this.emit('announcement', `Attempting MQTT connection to broker: ${config.mqtt.address}`); - this.client = mqtt.connect(config.mqtt.address, { - port: config.mqtt.port, + this.emit('announcement', `Attempting MQTT connection to broker: ${host}:${port}`); + this.client = mqtt.connect(host, { + port: port, }); const { client } = this; @@ -144,6 +144,19 @@ module.exports = { this.emit('announcement', `Unknown topic: ${topic}`); } }); + + // Other MQTT client events [pingreq, pingresp, disconnect] + client.on('pingreq', () => { + this.emit('announcement', 'Ping request received from MQTT broker'); + }); + + client.on('pingresp', () => { + this.emit('announcement', 'Ping response sent to MQTT broker'); + }); + + client.on('disconnect', () => { + this.emit('announcement', 'Disconnected from MQTT broker'); + }); } // Publish a message to the MQTT Broker diff --git a/src/custom_modules/config.json b/src/custom_modules/config.json index e11e9ab..4b69e7a 100644 --- a/src/custom_modules/config.json +++ b/src/custom_modules/config.json @@ -1,7 +1,5 @@ { "mqtt": { - "address": "wss://192.168.0.12", - "port": 9001, "topics": { "igniter": "hestia/status/igniter", "exhaust": "hestia/status/exhaust", diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index 533d760..8e6daf4 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -1,13 +1,7 @@ -const dotenv = require('dotenv').config(); const debug = process.env.DEBUG === "TRUE"; const config = require('./config.json'); const { pins } = config; const gpio = require('./VoidGPIO.js'); -process.pinMap = new Map(); - -for (const pin of pins) { - process.pinMap.set(pin.key, pin); -} module.exports = { log(message, level) { diff --git a/src/main.js b/src/main.js index 3db81b1..4d4db59 100644 --- a/src/main.js +++ b/src/main.js @@ -11,7 +11,6 @@ const { State, Communicator } = require('./custom_modules/HestiaClasses.js'); /***************************************************************************************/ // Variables /***************************************************************************************/ -process.debug = true; process.pinMap = new Map(); for (const pin of config.pins) { process.pinMap.set(pin.key, pin); @@ -22,8 +21,6 @@ for (const pin of config.pins) { /***************************************************************************************/ process.psState = new State(config); process.comlink = new Communicator(process.psState); -process.comlink.init(process.psState, config); -fn.gpio.init(process.comlink, process.psState); /***************************************************************************************/ // Loops @@ -80,4 +77,10 @@ process.comlink.on('announcement', msg => { process.psState.on('announcement', msg => { fn.log(`State: ${msg}`, 'INFO'); -}); \ No newline at end of file +}); + +/***************************************************************************************/ +// Call Things +/***************************************************************************************/ +process.comlink.init(process.psState, process.env.MQTT_HOST, process.env.MQTT_PORT, config); +fn.gpio.init(process.comlink, process.psState); \ No newline at end of file