Minor changes

This commit is contained in:
Skylar Grant 2024-11-30 16:52:10 -05:00
parent a59dd3fe6f
commit 5e20cfff6e
4 changed files with 24 additions and 16 deletions

View File

@ -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

View File

@ -1,7 +1,5 @@
{
"mqtt": {
"address": "wss://192.168.0.12",
"port": 9001,
"topics": {
"igniter": "hestia/status/igniter",
"exhaust": "hestia/status/exhaust",

View File

@ -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) {

View File

@ -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');
});
});
/***************************************************************************************/
// Call Things
/***************************************************************************************/
process.comlink.init(process.psState, process.env.MQTT_HOST, process.env.MQTT_PORT, config);
fn.gpio.init(process.comlink, process.psState);