Update client startup for new nodes
- Still needs to get IP from linux nodes - Other tasks still, idk
This commit is contained in:
@@ -5,7 +5,7 @@ const log = new DebugBuilder("client", "clientController");
|
||||
require('dotenv').config();
|
||||
const modes = require("../config/modes");
|
||||
// Modules
|
||||
const { executeAsyncConsoleCommand, nodeObject } = require("../utilities/utilities");
|
||||
const { executeAsyncConsoleCommand, nodeObject, BufferToJson } = require("../utilities/utilities");
|
||||
// Utilities
|
||||
const { updateId, updateConfig } = require("../utilities/updateConfig");
|
||||
const { updatePreset, addNewPreset, getPresets, removePreset } = require("../utilities/updatePresets");
|
||||
@@ -68,26 +68,26 @@ async function checkLocalIP() {
|
||||
*/
|
||||
exports.checkConfig = async function checkConfig() {
|
||||
if (!runningClientConfig.id || runningClientConfig.id == 0 || runningClientConfig.id == '0') {
|
||||
updateConfig('id', "");
|
||||
runningClientConfig.id = null;
|
||||
await updateId(0);
|
||||
runningClientConfig.id = 0;
|
||||
}
|
||||
|
||||
if (!runningClientConfig.ip) {
|
||||
const ipAddr = await checkLocalIP();
|
||||
updateConfig('ip', ipAddr);
|
||||
await updateConfig('CLIENT_IP', ipAddr);
|
||||
runningClientConfig.ip = ipAddr;
|
||||
}
|
||||
|
||||
if(!runningClientConfig.name) {
|
||||
const lastOctet = await String(checkLocalIP()).spit('.')[-1];
|
||||
const name = `Radio-Node-${lastOctet}`;
|
||||
updateConfig('name', name);
|
||||
await updateConfig('CLIENT_NAME', name);
|
||||
runningClientConfig.name = name;
|
||||
}
|
||||
|
||||
if(!runningClientConfig.port) {
|
||||
const port = 3010;
|
||||
updateConfig('port', port);
|
||||
await updateConfig('CLIENT_PORT', port);
|
||||
runningClientConfig.port = port;
|
||||
}
|
||||
|
||||
@@ -102,20 +102,21 @@ exports.checkIn = async () => {
|
||||
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
|
||||
try {
|
||||
if (!runningClientConfig?.id || runningClientConfig.id == null) {
|
||||
if (!runningClientConfig?.id || runningClientConfig.id == 0) {
|
||||
// ID was not found in the config, creating a new node
|
||||
reqOptions = new requestOptions("/nodes/newNode", "POST");
|
||||
sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
|
||||
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), async (responseObject) => {
|
||||
// Update the client's ID if the server accepted it
|
||||
if (responseObject.statusCode === 202) {
|
||||
if (responseObject.statusCode === 202) {
|
||||
runningClientConfig.id = responseObject.body.nodeId;
|
||||
updateId(responseObject.body.nodeId);
|
||||
log.DEBUG("Response object from new node: ", responseObject, runningClientConfig);
|
||||
await updateId(runningClientConfig.id);
|
||||
}
|
||||
|
||||
if (responseObject.statusCode >= 300) {
|
||||
// Server threw an error
|
||||
log.DEBUG("HTTP Error: ", responseObject);
|
||||
onHttpError(responseObject.statusCode);
|
||||
log.DEBUG("HTTP Error: ", responseObject, await BufferToJson(responseObject.body));
|
||||
await onHttpError(responseObject.statusCode);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -126,6 +127,7 @@ exports.checkIn = async () => {
|
||||
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), (responseObject) => {
|
||||
log.DEBUG("Check In Respose: ", responseObject);
|
||||
if (responseObject.statusCode === 202) {
|
||||
log.DEBUG("Updated keys: ", responseObject.body.updatedKeys)
|
||||
// Server accepted an update
|
||||
}
|
||||
if (responseObject.statusCode === 200) {
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user