From e9c54210aad5d10cf4bb763ef7f1e91fd70e5833 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 24 Nov 2024 15:17:23 -0500 Subject: [PATCH] Better logging and naming --- src/custom_modules/HestiaClasses.js | 20 ++++++++++---------- src/main.js | 29 +++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index 369baac..4ff280e 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -12,10 +12,10 @@ module.exports = { name: "igniter", topic: config.mqtt.topics.igniter, publisher: this.publisher, - power: (communicator, pinState) => { + power: (comlink, pinState) => { // Set the power based on the desired pinState this.igniter.on = pinState === 1 ? true : false; - communicator.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); + comlink.send(config.mqtt.topics.igniter, JSON.stringify(this.igniter)); } }; @@ -24,10 +24,10 @@ module.exports = { name: "exhaust", topic: config.mqtt.topics.exhaust, publisher: this.publisher, - power: (communicator, pinState) => { + power: (comlink, pinState) => { // Set the power based on the desired pinState this.exhaust.on = pinState === 1 ? true : false; - communicator.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); + comlink.send(config.mqtt.topics.exhaust, JSON.stringify(this.exhaust)); } }; @@ -37,10 +37,10 @@ module.exports = { feedRate: 500, topic: config.mqtt.topics.auger, publisher: this.publisher, - power: (communicator, pinState) => { + power: (comlink, pinState) => { // Set the power based on the desired pinState this.auger.on = pinState === 1 ? true : false; - communicator.send(config.mqtt.topics.auger, JSON.stringify(this.auger)); + comlink.send(config.mqtt.topics.auger, JSON.stringify(this.auger)); } }; @@ -49,10 +49,10 @@ module.exports = { name: "pof", topic: config.mqtt.topics.pof, publisher: this.publisher, - power: (communicator, pinState) => { + power: (comlink, pinState) => { // Set the power based on the desired pinState this.pof.on = pinState === 1 ? true : false; - communicator.send(config.mqtt.topics.pof, JSON.stringify(this.pof)); + comlink.send(config.mqtt.topics.pof, JSON.stringify(this.pof)); } }; @@ -61,10 +61,10 @@ module.exports = { name: "vacuum", topic: config.mqtt.topics.vacuum, publisher: this.publisher, - power: (communicator, pinState) => { + power: (comlink, pinState) => { // Set the power based on the desired pinState this.vacuum.on = pinState === 1 ? true : false; - communicator.send(config.mqtt.topics.vacuum, JSON.stringify(this.vacuum)); + comlink.send(config.mqtt.topics.vacuum, JSON.stringify(this.vacuum)); } }; diff --git a/src/main.js b/src/main.js index c9f1114..da5e5e7 100644 --- a/src/main.js +++ b/src/main.js @@ -1,28 +1,41 @@ +// Variables +process.debug = true; + // Import modules const gpio = require('./custom_modules/VoidGPIO.js'); const config = require('./custom_modules/config.json'); const fn = require('./custom_modules/functions.js'); const { State, Communicator } = require('./custom_modules/HestiaClasses.js'); +// Initialize state and comlink process.psState = new State(config); const comms = new Communicator(process.psState); - comms.init(process.psState, config); +// Initialize GPIO fn.gpio.init(comms, process.psState); // Sensor detection loop setInterval(() => { + // Iterate through pins for (const pin of config.pins) { + // If pin is an input, read it if (pin.mode === 'IN') { + fn.log(`I: Sensor Detection Loop: Reading pin ${pin.board}`); + // Read pin gpio.readPin(pin.board).then(state => { + fn.log(`I: Sensor Detection Loop: Pin ${pin.board} is ${state}`); + // Convert the state from string to boolean const boolState = state === '1' ? true : false; + // Compare the state to the last known state if (boolState !== process.psState[pin.key].on) { + // Update the state process.psState[pin.key].on = boolState; + // Send the state to the MQTT broker comms.send(config.mqtt.topics[pin.key], JSON.stringify(process.psState[pin.key])); - fn.log(`${pin.key}: ${state}`); + fn.log(`I: Sensor Detection Loop: ${pin.key}: ${state}`); } - }).catch(e => console.error(e)); + }).catch(e => console.error(`E: Sensor Detection Loop: ${e}`)); } } }, 1000); @@ -31,16 +44,16 @@ setInterval(() => { setInterval(fn.routines.cycleAuger, config.augerTotalCycleTime); comms.on('stateChange', (oldState, state) => { - console.log(`State change detected.`); + fn.log(`Event: State change detected.`); fn.handlers.stateChange(oldState, state); }); comms.on('startup', () => { - console.log(`Startup detected.`); - fn.power.start.init(comms, process.psState).catch(e => console.error(e)); + fn.log(`I: Event: Startup detected.`); + fn.power.start.init(comms, process.psState).catch(e => console.error(`E: Power Start Init: ${e}`)); }); comms.on('shutdown', () => { - console.log(`Shutdown detected.`); - fn.power.stop.init(comms, process.psState).catch(e => console.error(e)); + fn.log(`I: Event: Shutdown detected.`); + fn.power.stop.init(comms, process.psState).catch(e => console.error(`E: Power Stop Init: ${e}`)); }); \ No newline at end of file