8 Commits

Author SHA1 Message Date
205f285e0a Merge branch 'master' into feature/Integrate-Emmelia-into-Cor 2023-05-07 04:12:01 -04:00
Logan Cusano
f77eb5444a Updating the running config as well as the file 2023-05-06 17:18:21 -04:00
Logan Cusano
177d25e54e Didn't update require statement 2023-05-06 17:16:01 -04:00
Logan Cusano
6880c5952a Resolved bug updating the config 2023-05-06 17:14:30 -04:00
Logan Cusano
a14c56b645 Bug with whitespaces 2023-05-06 17:04:17 -04:00
Logan Cusano
35b81758e3 Bug in getting the IP address on windows 2023-05-06 16:44:39 -04:00
Logan Cusano
7871b07113 Improving config handling & startup logic 2023-05-06 16:40:15 -04:00
Logan Cusano
6682d97156 Improve client controller config handling 2023-05-06 16:22:20 -04:00
2 changed files with 48 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ const modes = require("../config/modes");
// Modules
const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js");
// Utilities
const updateConfig = require("../utilities/updateConfig");
const { updateId, updateConfig } = require("../utilities/updateConfig");
const updatePreset = require("../utilities/updatePresets");
const requests = require("../utilities/httpRequests");
@@ -37,7 +37,7 @@ async function checkLocalIP() {
// Windows
var networkConfig = await executeAsyncConsoleCommand("ipconfig");
log.DEBUG('Network Config: ', networkConfig);
var networkConfigLines = networkConfig.split("\n").filter(line => {
var networkConfigLines = await networkConfig.split("\n").filter(line => {
if (!line.includes(":")) return false;
line = line.split(":");
@@ -46,13 +46,15 @@ async function checkLocalIP() {
return true;
}).map(line => {
line = line.split(':', 1);
line[0] = String(line[0]).replace("/\./g", "").trim();
line[1] = String(line[1].replace(/[\r|\n]/g, ""))
line = String(line).split(':', 2);
line[0] = String(line[0]).replace(/[.]|[\s]/g, "").trim();
line[1] = String(line[1]).replace(/(\\r|\\n)/g, "").trim();
return line;
});
log.DEBUG("Parsed IP Config Results: ", networkConfigLines);
return networkConfigLines['IPv4 Address'];
networkConfig = Object.fromEntries(networkConfigLines);
log.DEBUG("Parsed IP Config Results: ", networkConfig);
log.DEBUG("Local IP address: ", networkConfig['IPv4Address']);
return networkConfig['IPv4Address'];
}
else {
// Linux
@@ -60,23 +62,47 @@ 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) {
const ipAddr = await checkLocalIP();
updateConfig('ip', ipAddr);
config.clientConfig.ip = ipAddr;
}
if(!config.clientConfig.name) {
const lastOctet = await String(checkLocalIP()).spit('.')[-1];
const clientName = `Radio-Node-${lastOctet}`;
updateConfig('name', clientName);
config.clientConfig.name = clientName;
}
if(!config.clientConfig.port) {
const port = 3010;
updateConfig('port', port);
config.clientConfig.port = port;
}
}
/** Check in with the server
* If the bot has a saved ID, check in with the server to update any information or just check back in
* If the bot does not have a saved ID, it will attempt to request a new ID from the server
*/
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) {
// ID was not found in the config, creating a new node
reqOptions = new requests.requestOptions("/nodes/newNode", "POST");
delete config.clientConfig.id;
config.clientConfig.ip = checkLocalIP();
requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => {
// Update the client's ID if the server accepted it
if (responseObject.statusCode === 202) {
config.clientConfig.id = responseObject.body.nodeId;
updateConfig.updateId(responseObject.body.nodeId);
updateId(responseObject.body.nodeId);
}
});
}

View File

@@ -20,10 +20,20 @@ class Options {
* @param updatedId The updated ID assigned to the bot
*/
exports.updateId = (updatedId) => {
const options = new Options("id", updatedId);
this.updateConfig('id', updatedId);
}
/**
*
* @param {string} key The config file key to update with the value
* @param {string} value The value to update the key with
*/
exports.updateConfig = function updateConfig(key, value) {
const options = new Options(key, value);
updateConfigFile(options, (updatedFiles) => {
// Do Something
log.DEBUG("Updated config file: ", updatedFiles);
})
}