Changed interface switching to use .env

This commit is contained in:
Skylar Grant 2024-11-30 16:19:47 -05:00
parent c01e18c644
commit a59dd3fe6f
2 changed files with 15 additions and 13 deletions

View File

@ -1,21 +1,21 @@
const { exec } = require('child_process'); const { exec } = require('child_process');
const config = require('./config.json'); let gpioScript = new String();
if (process.env.GPIO_MODE === 'EMULATED') {
gpioScript = 'src/python/fake_gpio_interface.py';
} else if (process.env.GPIO_MODE === 'PHYSICAL') {
gpioScript = 'src/python/gpio_interface.py';
} else {
throw new Error('GPIO_MODE environment variable not set');
}
console.log(`GPIO_MODE: ${process.env.GPIO_MODE}`);
module.exports = { module.exports = {
// Calls the GPIO Interface script to toggle a pin's state opposite of its current state
// togglePin(pin) {
// return new Promise((resolve, reject) => {
// exec(`python3 ${config.gpioScript} toggle ${pin}`, (error, stdout, stderr) => {
// if (error) reject(error);
// if (stderr) reject(new Error(stderr));
// resolve();
// });
// });
// },
// Calls the GPIO Interface script to read a pin's state // Calls the GPIO Interface script to read a pin's state
readPin(pin) { readPin(pin) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`python3 ${config.gpioScript} read ${pin}`, (error, stdout, stderr) => { exec(`python3 ${gpioScript} read ${pin}`, (error, stdout, stderr) => {
if (error) reject(error); if (error) reject(error);
if (stderr) reject(new Error(stderr)); if (stderr) reject(new Error(stderr));
resolve(stdout.trim()); resolve(stdout.trim());
@ -25,7 +25,7 @@ module.exports = {
// Calls the GPIO Interface script to set a pin's state regardless of its current state // Calls the GPIO Interface script to set a pin's state regardless of its current state
setPin(pin, state) { setPin(pin, state) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`python3 ${config.gpioScript} set ${pin} ${state}`, (error, stdout, stderr) => { exec(`python3 ${gpioScript} set ${pin} ${state}`, (error, stdout, stderr) => {
if (error) { if (error) {
reject(error); reject(error);
} }

View File

@ -1,6 +1,8 @@
/***************************************************************************************/ /***************************************************************************************/
// Import modules // Import modules
/***************************************************************************************/ /***************************************************************************************/
const dotenv = require('dotenv');
dotenv.config();
const gpio = require('./custom_modules/VoidGPIO.js'); const gpio = require('./custom_modules/VoidGPIO.js');
const config = require('./custom_modules/config.json'); const config = require('./custom_modules/config.json');
const fn = require('./custom_modules/functions.js'); const fn = require('./custom_modules/functions.js');