Improve client controller config handling

This commit is contained in:
Logan Cusano
2023-05-06 16:22:20 -04:00
parent 6e8af5dbcc
commit 6682d97156

View File

@@ -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) {