Remove config file for client, moved to .env

This commit is contained in:
Logan Cusano
2023-05-07 04:40:46 -04:00
parent e0b6e567c1
commit ed79403a9b
6 changed files with 54 additions and 45 deletions

View File

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

View File

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

View File

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

View File

@@ -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',

View File

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

View File

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