From 6682d97156375d107044431b1171411917cd139c Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 6 May 2023 16:22:20 -0400 Subject: [PATCH] Improve client controller config handling --- Client/controllers/clientController.js | 35 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/Client/controllers/clientController.js b/Client/controllers/clientController.js index 527ee71..8e347b5 100644 --- a/Client/controllers/clientController.js +++ b/Client/controllers/clientController.js @@ -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,12 +46,13 @@ 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(/[.]/g, "").trim(); + line[1] = String(line[1]).replace(/(\\r|\\n)/g, "").trim(); return line; }); log.DEBUG("Parsed IP Config Results: ", networkConfigLines); + log.DEBUG("Local IP address: ", networkConfigLines['IPv4 Address']); return networkConfigLines['IPv4 Address']; } else { @@ -60,18 +61,38 @@ 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); + } + + if(!config.clientConfig.name) { + const lastOctet = await checkLocalIP().spit('.')[-1]; + const clientName = `Radio-Node-${lastOctet}`; + updateConfig('name', clientName); + } + + if(!config.clientConfig.port) { + updateConfig('port', 3010); + } + +} + /** 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) {