Implement custom logging

This commit is contained in:
Skylar Grant 2024-08-18 14:26:28 -04:00
parent 02b2f77d4d
commit 1eb1dc8270
4 changed files with 52 additions and 47 deletions

View File

@ -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
}
});
}

View File

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

View File

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

View File

@ -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}`);
});
}
}