diff --git a/src/custom_modules/HestiaClasses.js b/src/custom_modules/HestiaClasses.js index f6c3566..7bb76f5 100644 --- a/src/custom_modules/HestiaClasses.js +++ b/src/custom_modules/HestiaClasses.js @@ -32,9 +32,9 @@ export class Communicator { // Publish with retain flag set to true this.client.publish(topic, message, { retain: true }, (err) => { if (err) { - console.error('Failed to publish message:', err); + throw err; } else { - console.log('Message published and retained on topic:', topic); + // TODO: Pass back a success message } }); } diff --git a/src/custom_modules/VoidGPIO.js b/src/custom_modules/VoidGPIO.js index e01cbb1..e5eab60 100644 --- a/src/custom_modules/VoidGPIO.js +++ b/src/custom_modules/VoidGPIO.js @@ -2,11 +2,6 @@ const { exec } = require('child_process'); const { pins } = require('./config.json'); const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -pins.forEach(pin => { - console.log(`Key: ${pin.key}, Board: ${pin.board}, BCM: ${pin.bcm}, Mode: ${pin.mode}`); -}); - - module.exports = { // Calls the GPIO Interface script to toggle a pin's state opposite of its current state togglePin(pin, callback) { @@ -39,43 +34,5 @@ module.exports = { } callback(null); }) - }, - // Boot up sanity check during debug mode - async debugInit() { - console.log('Resetting all output pins.'); - pins.forEach(async (pin) => { - if (pin.mode === 'OUT') { - this.setPin(pin.board, pin.defaultState, err => { - if (err) throw err; - console.log(`Set ${pin.key} pin to ${pin.defaultState}.`); - }); - }; - }); - for (const pin of pins) { - switch (pin.mode) { - case 'OUT': - this.togglePin(pin.board, err => { - if (err) throw err; - console.log(`Toggled ${pin.key}`); - }); - // Wait 1000ms before toggling again. - await sleep(1000); - this.togglePin(pin.board, err => { - if (err) throw err; - console.log(`Toggled ${pin.key}`); - }); - break; - case 'IN': - this.readPin(pin.board, (err, state) => { - if (err) throw err; - console.log(`${pin.key} state: ${state}`); - }); - default: - break; - } - }; - }, - async poll() { - } } \ No newline at end of file diff --git a/src/custom_modules/functions.js b/src/custom_modules/functions.js index e69de29..30971c5 100644 --- a/src/custom_modules/functions.js +++ b/src/custom_modules/functions.js @@ -0,0 +1,47 @@ +const dotenv = require('dotenv').config(); +const debug = process.env.DEBUG === "TRUE"; + +module.exports = { + log(message) { + if (debug) { + console.log(message); + } + }, + gpio: { + // Boot up sanity check during debug mode + async debugInit() { + exports.log('Resetting all output pins.'); + pins.forEach(async (pin) => { + if (pin.mode === 'OUT') { + this.setPin(pin.board, pin.defaultState, err => { + if (err) throw err; + exports.log(`Set ${pin.key} pin to ${pin.defaultState}.`); + }); + }; + }); + for (const pin of pins) { + switch (pin.mode) { + case 'OUT': + this.togglePin(pin.board, err => { + if (err) throw err; + exports.log(`Toggled ${pin.key}`); + }); + // Wait 1000ms before toggling again. + await sleep(1000); + this.togglePin(pin.board, err => { + if (err) throw err; + exports.log(`Toggled ${pin.key}`); + }); + break; + case 'IN': + this.readPin(pin.board, (err, state) => { + if (err) throw err; + exports.log(`${pin.key} state: ${state}`); + }); + default: + break; + } + }; + } + } +} \ No newline at end of file diff --git a/src/main.js b/src/main.js index 35d555d..839042a 100644 --- a/src/main.js +++ b/src/main.js @@ -1,14 +1,15 @@ // Import modules const gpio = require('./custom_modules/VoidGPIO.js'); const { pins } = require('./custom_modules/config.json'); +const fn = require('./custom_modules/functions.js'); -gpio.debugInit(); +fn.gpio.debugInit(); setInterval(() => { for (const pin of pins) { if (pin.mode === 'IN') { gpio.readPin(pin.board, (err, state) => { if (err) throw err; - console.log(`${pin.key}: ${state}`); + fn.log(`${pin.key}: ${state}`); }); } }