From ed79403a9b3c1e3c1aaab407f0127c086425ef60 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 7 May 2023 04:40:46 -0400 Subject: [PATCH] Remove config file for client, moved to .env --- Client/config/clientConfig.js | 23 -------------------- Client/controllers/clientController.js | 29 ++++++++++++++------------ Client/controllers/radioController.js | 5 +++-- Client/utilities/httpRequests.js | 8 ++++--- Client/utilities/recordHelper.js | 26 +++++++++++++++++++++++ Client/utilities/updateConfig.js | 8 +++---- 6 files changed, 54 insertions(+), 45 deletions(-) delete mode 100644 Client/config/clientConfig.js create mode 100644 Client/utilities/recordHelper.js diff --git a/Client/config/clientConfig.js b/Client/config/clientConfig.js deleted file mode 100644 index aa8894f..0000000 --- a/Client/config/clientConfig.js +++ /dev/null @@ -1,23 +0,0 @@ -// Core config settings for the node, these are the settings that are checked with the server -const path = require("path"); -exports.clientConfig = { - "id": 13, - "name": "boilin balls in the hall", - "ip": "172.16.100.150", - "port": 3010, - "location": "the house", - "nearbySystems": ["Westchester Cty. Simulcast"], - "online": true -} - -// Configuration for the connection to the server -exports.serverConfig = { - "ip": "172.16.100.108", - "hostname": "localhost", - "port": 3000 -} - -// Configuration of the local OP25 application -exports.radioAppConfig = { - "bin": "H:/Logan/Projects/Discord-Radio-Bot-CnC/Client/.idea/testOP25Dir/multi_rx.py" -} \ No newline at end of file diff --git a/Client/controllers/clientController.js b/Client/controllers/clientController.js index f6b6fc5..6b4b3cd 100644 --- a/Client/controllers/clientController.js +++ b/Client/controllers/clientController.js @@ -2,7 +2,7 @@ const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "clientController"); // Configs -const config = require("../config/clientConfig"); +require('dotenv').config(); const modes = require("../config/modes"); // Modules const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js"); @@ -10,6 +10,9 @@ const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleComma const { updateId, updateConfig } = require("../utilities/updateConfig"); const updatePreset = require("../utilities/updatePresets"); const requests = require("../utilities/httpRequests"); +const { nodeObject } = require("../utilities/recordHelper.js"); + +var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: process.env.CLIENT_NEARBY_SYSTEMS, _online: process.env.CLIENT_ONLINE}); /** * Check the body for the required fields to update or add a preset @@ -66,23 +69,23 @@ async function checkLocalIP() { * Checks the config file for all required fields or gets and updates the required fields */ exports.checkConfig = async function checkConfig() { - if (!config.clientConfig.ip) { + if (!runningClientConfig.ip) { const ipAddr = await checkLocalIP(); updateConfig('ip', ipAddr); - config.clientConfig.ip = ipAddr; + runningClientConfig.ip = ipAddr; } - if(!config.clientConfig.name) { + if(!runningClientConfig.name) { const lastOctet = await String(checkLocalIP()).spit('.')[-1]; - const clientName = `Radio-Node-${lastOctet}`; - updateConfig('name', clientName); - config.clientConfig.name = clientName; + const name = `Radio-Node-${lastOctet}`; + updateConfig('name', name); + runningClientConfig.name = name; } - if(!config.clientConfig.port) { + if(!runningClientConfig.port) { const port = 3010; updateConfig('port', port); - config.clientConfig.port = port; + runningClientConfig.port = port; } } @@ -95,13 +98,13 @@ exports.checkIn = async () => { let reqOptions; await this.checkConfig(); // Check if there is an ID found, if not add the node to the server. If there was an ID, check in with the server to make sure it has the correct information - if (config.clientConfig.id === 0) { + if (clientId === 0) { // ID was not found in the config, creating a new node reqOptions = new requests.requestOptions("/nodes/newNode", "POST"); - requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => { + requests.sendHttpRequest(reqOptions, JSON.stringify(), (responseObject) => { // Update the client's ID if the server accepted it if (responseObject.statusCode === 202) { - config.clientConfig.id = responseObject.body.nodeId; + runningClientConfig.id = responseObject.body.nodeId; updateId(responseObject.body.nodeId); } }); @@ -109,7 +112,7 @@ exports.checkIn = async () => { else { // ID is in the config, checking in with the server reqOptions = new requests.requestOptions("/nodes/nodeCheckIn", "POST"); - requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => { + requests.sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), (responseObject) => { if (responseObject.statusCode === 202) { // Server accepted an update } diff --git a/Client/controllers/radioController.js b/Client/controllers/radioController.js index 943693a..9a7816a 100644 --- a/Client/controllers/radioController.js +++ b/Client/controllers/radioController.js @@ -3,13 +3,14 @@ const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "radioController"); // Modules const { resolve, dirname } = require('path'); +require('dotenv').config(); const fs = require('fs'); -const radioConfig = require('../config/clientConfig').radioAppConfig; const radioConfigHelper = require("../utilities/radioConfigHelper"); const presetWrappers = require("../utilities/updatePresets"); const spawn = require('child_process').spawn; const converter = require("convert-units"); +const radioBinPath = process.env.OP25_BIN_PATH; let radioChildProcess, tempRes, radioConfigPath; /** @@ -60,7 +61,7 @@ exports.openRadioSession = () => { * Get the location of the 'multi_rx.py' binary from the config */ function getRadioBinPath(){ - return resolve(radioConfig.bin); + return resolve(radioBinPath); } /** diff --git a/Client/utilities/httpRequests.js b/Client/utilities/httpRequests.js index ddd11b6..ee98681 100644 --- a/Client/utilities/httpRequests.js +++ b/Client/utilities/httpRequests.js @@ -2,16 +2,18 @@ const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "httpRequests"); // Config -const config = require("../config/clientConfig"); +require('dotenv').config(); // Modules const http = require("http"); +var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: process.env.CLIENT_NEARBY_SYSTEMS, _online: process.env.CLIENT_ONLINE}); + exports.requestOptions = class requestOptions { constructor(path, method, hostname = undefined, headers = undefined, port = undefined) { if (method === "POST"){ - this.hostname = hostname ?? config.serverConfig.hostname + this.hostname = hostname ?? runningClientConfig.hostname this.path = path - this.port = port ?? config.serverConfig.port + this.port = port ?? runningClientConfig.port this.method = method this.headers = headers ?? { 'Content-Type': 'application/json', diff --git a/Client/utilities/recordHelper.js b/Client/utilities/recordHelper.js new file mode 100644 index 0000000..d0f50be --- /dev/null +++ b/Client/utilities/recordHelper.js @@ -0,0 +1,26 @@ +/** + * + */ +class nodeObject { + /** + * + * @param {*} param0._id The ID of the node + * @param {*} param0._name The name of the node + * @param {*} param0._ip The IP that the master can contact the node at + * @param {*} param0._port The port that the client is listening on + * @param {*} param0._location The physical location of the node + * @param {*} param0._online An integer representation of the online status of the bot, ie 0=off, 1=on + * @param {*} param0._nearbySystems An object array of nearby systems + */ + constructor({ _id = null, _name = null, _ip = null, _port = null, _location = null, _nearbySystems = null, _online = null }) { + this.id = _id; + this.name = _name; + this.ip = _ip; + this.port = _port; + this.location = _location; + this.nearbySystems = _nearbySystems; + this.online = _online; + } +} + +exports.nodeObject = nodeObject; \ No newline at end of file diff --git a/Client/utilities/updateConfig.js b/Client/utilities/updateConfig.js index 4774dfa..ccaffad 100644 --- a/Client/utilities/updateConfig.js +++ b/Client/utilities/updateConfig.js @@ -6,12 +6,12 @@ const replace = require('replace-in-file'); class Options { constructor(key, updatedValue) { - this.files = "./config/clientConfig.js"; + this.files = "./.env"; // A regex of the line containing the key in the config file - this.from = new RegExp(`"${key}": (.+),`, "g"); + this.from = new RegExp(`${key}="(.+)",`, "g"); // Check to see if the value is a string and needs to be wrapped in double quotes - if (typeof updatedValue === "string") this.to = `"${key}": "${updatedValue}",`; - else this.to = `"${key}": ${updatedValue},`; + if (Array(["string", "number"]).includes(typeof updatedValue)) this.to = `${key}="${updatedValue}",`; + else this.to = `${key}=${updatedValue},`; } }