Update client startup for new nodes

- Still needs to get IP from linux nodes
- Other tasks still, idk
This commit is contained in:
Logan Cusano
2023-06-19 00:05:27 -04:00
parent d96e6ad519
commit 93be4ca9dc
4 changed files with 41 additions and 20 deletions

View File

@@ -10,7 +10,7 @@ const { isJsonString } = require("./utilities.js");
exports.requestOptions = class requestOptions {
constructor(path, method, hostname = undefined, headers = undefined, port = undefined) {
if (method === "POST"){
log.DEBUG("Hostname Vars: ", hostname, process.env.SERVER_HOSTNAME, process.env.SERVER_IP);
log.VERBOSE("Hostname Vars: ", hostname, process.env.SERVER_HOSTNAME, process.env.SERVER_IP);
if (hostname) this.hostname = hostname;
if (process.env.SERVER_HOSTNAME) this.hostname = process.env.SERVER_HOSTNAME;
if (process.env.SERVER_IP) this.hostname = process.env.SERVER_IP;
@@ -36,12 +36,11 @@ exports.sendHttpRequest = function sendHttpRequest(requestOptions, data, callbac
// Create the request
const req = http.request(requestOptions, res => {
res.on('data', (data) => {
log.DEBUG("Response data from new node: ", data);
const responseObject = {
"statusCode": res.statusCode,
"body": (isJsonString(data.toString())) ? JSON.parse(data.toString()) : data.toString()
};
log.DEBUG("Response Object: ", responseObject);
log.VERBOSE("Response Object: ", responseObject);
callback(responseObject);
})
}).on('error', err => {

View File

@@ -8,10 +8,10 @@ class Options {
constructor(key, updatedValue) {
this.files = "./.env";
// A regex of the line containing the key in the config file
this.from = new RegExp(`${key}="(.+)",`, "g");
this.from = new RegExp(`${key}="?(.+)"?`, "g");
// Check to see if the value is a string and needs to be wrapped in double quotes
if (Array(["string", "number"]).includes(typeof updatedValue)) this.to = `${key}="${updatedValue}",`;
else this.to = `${key}=${updatedValue},`;
else this.to = `${key}=${updatedValue}`;
}
}
@@ -20,7 +20,7 @@ class Options {
* @param updatedId The updated ID assigned to the bot
*/
exports.updateId = (updatedId) => {
this.updateConfig('id', updatedId);
this.updateConfig('CLIENT_ID', updatedId);
}
/**
@@ -46,7 +46,7 @@ exports.updateConfig = function updateConfig(key, value) {
function updateConfigFile(options, callback){
replace(options, (error, changedFiles) => {
if (error) return console.error('Error occurred:', error);
log.DEBUG('Modified files:', changedFiles);
log.VERBOSE('Modified files:', changedFiles);
callback(changedFiles);
});
}

View File

@@ -261,4 +261,24 @@ function convertRadioPresetsToOP25Config(presetName){
log.DEBUG(updatedOP25Config);
return updatedOP25Config;
}
}
// Convert a buffer from the DB to JSON object
exports.BufferToJson = (buffer) => {
return JSON.parse(buffer.toString());
}
/**
* Check to see if the input is a valid JSON string
*
* @param {*} str The string to check for valud JSON
* @returns {true|false}
*/
exports.isJsonString = (str) => {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}