From a59dd3fe6fee8c8ee5553fdbbfa99d29224514f8 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 30 Nov 2024 16:19:47 -0500 Subject: [PATCH] Changed interface switching to use .env --- src/custom_modules/VoidGPIO.js | 26 +++++++++++++------------- src/main.js | 2 ++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/custom_modules/VoidGPIO.js b/src/custom_modules/VoidGPIO.js index f83f272..b7ce4f9 100644 --- a/src/custom_modules/VoidGPIO.js +++ b/src/custom_modules/VoidGPIO.js @@ -1,21 +1,21 @@ 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 = { - // 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 readPin(pin) { 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 (stderr) reject(new Error(stderr)); 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 setPin(pin, state) { 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) { reject(error); } diff --git a/src/main.js b/src/main.js index 27954fd..3db81b1 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,8 @@ /***************************************************************************************/ // Import modules /***************************************************************************************/ +const dotenv = require('dotenv'); +dotenv.config(); const gpio = require('./custom_modules/VoidGPIO.js'); const config = require('./custom_modules/config.json'); const fn = require('./custom_modules/functions.js');