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

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