Merge branch 'master' into feature/Integrate-Emmelia-into-Cor

This commit is contained in:
2023-05-07 04:12:01 -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();
reqOptions = new requests.requestOptions("/nodes/newNode", "POST");
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);
})
}