Moving things from .env to config.json

This commit is contained in:
Skylar Grant 2022-12-06 17:08:47 -05:00
parent f85d9ab928
commit 223833a52d

42
main.js
View File

@ -10,34 +10,38 @@
// Custom functions module to keep main script clean // Custom functions module to keep main script clean
const fn = require('./functions.js').functions; const fn = require('./functions.js').functions;
// Config File
const config = require('./config.json');
config.startTime = Date.now();
// Environment Variables Importing // Environment Variables Importing
const dotenv = require('dotenv').config(); const dotenv = require('dotenv').config();
// Setup for use with the Pi's GPIO pins // Setup for use with the Pi's GPIO pins
if (process.env.ONPI == 'true') { if (process.env.ONPI == 'true') {
console.log('== Running on a Raspberry Pi.'); console.log(`[${(Date.now() - config.startTime)/1000}] == Running on a Raspberry Pi.`);
const gpio = require('rpi-gpio'); const gpio = require('rpi-gpio');
fn.init(gpio).then((res, rej) => { fn.init(gpio).then((res, rej) => {
if (res != undefined) { if (res != undefined) {
console.log(res); console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
main(fn, gpio); main(fn, gpio);
} else { } else {
console.error(rej); console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
} }
}); });
} else if (process.env.ONPI == 'false') { } else if (process.env.ONPI == 'false') {
console.log('== Not running on a Raspberry Pi.'); console.log(`[${(Date.now() - config.startTime)/1000}] I: Not running on a Raspberry Pi.`);
const gpio = 'gpio'; const gpio = 'gpio';
fn.init(gpio).then((res, rej) => { fn.init(gpio).then((res, rej) => {
if (res != undefined) { if (res != undefined) {
console.log(res); console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
main(fn, gpio); main(fn, gpio);
} else { } else {
console.error(rej); console.error(rej);
} }
}); });
} else { } else {
console.log('== Problem with ENV file.'); console.error(`[${Date.now() - config.startTime}] E: Problem with ENV file.`);
} }
// TODO Add logic for other sensors // TODO Add logic for other sensors
@ -49,7 +53,7 @@ async function main(fn, gpio) {
// Check for the existence of certain files // Check for the existence of certain files
fn.files.check().then((res,rej) => { fn.files.check().then((res,rej) => {
// Log the result of the check if in debug mode // Log the result of the check if in debug mode
if (process.env.DEBUG == 'true') console.log('File Check: ' + res); if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: File Check: ${res}`);
// Choose what to do depending on the result of the check // Choose what to do depending on the result of the check
switch (res) { switch (res) {
case "pause": case "pause":
@ -64,27 +68,45 @@ async function main(fn, gpio) {
fn.commands.reload().then(() => { fn.commands.reload().then(() => {
// Rerun this function once the reload has finished // Rerun this function once the reload has finished
main(fn, gpio); main(fn, gpio);
}).catch(rej => {
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
}); });
break; break;
case "quit": case "quit":
// Quit the script // Quit the script
fn.commands.quit(); fn.commands.quit();
break; break;
case "ignite":
// Start the igniter and timer
fn.commands.ignite(gpio).then(res => {
if (config.debugMode) console.log(res);
}).catch(rej => {
console.error(`[${(Date.now() - config.startTime)/1000}] E: ${rej}`);
});
case "none": case "none":
// If no special files are found, cycle the auger normally // If no special files are found, cycle the auger normally
fn.auger.cycle(gpio).then((res) => { fn.auger.cycle(gpio).then((res) => {
// Log the auger cycle results if in debug mode. // Log the auger cycle results if in debug mode.
if (process.env.DEBUG == 'true') console.log(res); if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
// Run the status check function
statusCheck(fn, gpio);
// Rerun this function once the cycle is complete // Rerun this function once the cycle is complete
main(fn, gpio); // main(fn, gpio);
}); });
break; break;
default: default:
// If we don't get a result from the file check, or for some reason it's an unexpected response, log it and quit the script. // If we don't get a result from the file check, or for some reason it's an unexpected response, log it and quit the script.
console.error(`No result was received, something is wrong.\nres: ${res}`); console.error(`[${(Date.now() - config.startTime)/1000}] E: No result was received, something is wrong.\nres: ${res}`);
fn.commands.quit(); fn.commands.quit();
break; break;
} }
}); });
}
function statusCheck(fn, gpio) {
fn.tests.igniter(gpio).then((res) => {
if (config.debugMode) console.log(`[${(Date.now() - config.startTime)/1000}] I: ${res}`);
main(fn, gpio);
});
} }