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) {
|
||||
|
||||
Reference in New Issue
Block a user