Finalizing Server webUI

Still needed:
- Way to update clients' versions
- Way to delete nodes
- working dashboard
- working search function
This commit is contained in:
Logan Cusano
2023-07-22 01:19:54 -04:00
parent 75156d059e
commit ace762fc76
18 changed files with 327 additions and 67 deletions

View File

@@ -5,13 +5,14 @@ const log = new DebugBuilder("client", "clientController");
require('dotenv').config();
const modes = require("../config/modes");
// Modules
const { executeAsyncConsoleCommand, nodeObject, BufferToJson } = require("../utilities/utilities");
const { executeAsyncConsoleCommand, BufferToJson } = require("../utilities/utilities");
// Utilities
const { updateId, updateConfig } = require("../utilities/updateConfig");
const { getFullConfig } = require("../utilities/configHandler");
const { updateId, updateConfig, updateClientConfig } = require("../utilities/updateConfig");
const { updatePreset, addNewPreset, getPresets, removePreset } = require("../utilities/updatePresets");
const { onHttpError, requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: getPresets(), _online: process.env.CLIENT_ONLINE});
var runningClientConfig = getFullConfig()
/**
* Check the body for the required fields to update or add a preset
@@ -97,12 +98,11 @@ exports.checkConfig = async function checkConfig() {
* If the bot has a saved ID, check in with the server to get any updated 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
*
* @param {boolean} init If set to true, the client will update the server to it's config, instead of taking the server's config
* @param {boolean} update If set to true, the client will update the server to it's config, instead of taking the server's config
*/
exports.checkIn = async (init = false) => {
exports.checkIn = async (update = false) => {
let reqOptions;
await this.checkConfig();
runningClientConfig.online = true;
// 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 == 0) {
@@ -136,7 +136,7 @@ exports.checkIn = async (init = false) => {
}
else {
// ID is in the config, checking in with the server
if (init) reqOptions = new requestOptions(`/nodes/${runningClientConfig.id}`, "PUT");
if (update) reqOptions = new requestOptions(`/nodes/${runningClientConfig.id}`, "PUT");
else reqOptions = new requestOptions(`/nodes/nodeCheckIn/${runningClientConfig.id}`, "POST");
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), (responseObject) => {
log.DEBUG("Check In Respose: ", responseObject);
@@ -156,6 +156,8 @@ exports.checkIn = async (init = false) => {
}
if (responseObject.statusCode === 200) {
// Server accepted the response but there were no keys to be updated
const tempUpdatedConfig = updateClientConfig(responseObject.body);
if (!tempUpdatedConfig.length > 0) return;
}
if (responseObject.statusCode >= 300) {
// Server threw an error
@@ -192,6 +194,7 @@ exports.updatePreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
updatePreset(req.body.systemName, () => {
runningClientConfig.nearbySystems = getPresets();
this.checkIn(true);
return res.sendStatus(200);
}, {frequencies: req.body.frequencies, mode: req.body.mode, trunkFile: req.body.trunkFile});
})
@@ -204,6 +207,7 @@ exports.addNewPreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
addNewPreset(req.body.systemName, req.body.frequencies, req.body.mode, () => {
runningClientConfig.nearbySystems = getPresets();
this.checkIn(true);
return res.sendStatus(200);
}, req.body.trunkFile);
});
@@ -217,6 +221,7 @@ exports.removePreset = async (req, res) => {
if (!req.body.systemName) return res.status("500").json({"message": "You must specify a system name to delete, this must match exactly to how the system name is saved."})
removePreset(req.body.systemName, () => {
runningClientConfig.nearbySystems = getPresets();
this.checkIn(true);
return res.sendStatus(200);
}, req.body.trunkFile);
});