Semi functional client webUI

- can update client info for sure
- Working on notifications now
This commit is contained in:
Logan Cusano
2023-07-22 03:27:39 -04:00
parent 62c0504028
commit f5d58d45da
12 changed files with 964 additions and 47 deletions

View File

@@ -5,7 +5,7 @@ const log = new DebugBuilder("client", "clientController");
require('dotenv').config();
const modes = require("../config/modes");
// Modules
const { executeAsyncConsoleCommand, BufferToJson } = require("../utilities/utilities");
const { executeAsyncConsoleCommand, BufferToJson, nodeObject } = require("../utilities/utilities");
// Utilities
const { getFullConfig } = require("../utilities/configHandler");
const { updateId, updateConfig, updateClientConfig } = require("../utilities/updateConfig");
@@ -22,11 +22,11 @@ var runningClientConfig = getFullConfig()
* @returns {*}
*/
function checkBodyForPresetFields(req, res, callback) {
if (!req.body?.systemName) return res.status(403).json({"message": "No system in the request"});
if (!req.body?.frequencies && Array.isArray(req.body.frequencies)) return res.status(403).json({"message": "No frequencies in the request or type is not an array"});
if (!req.body?.mode && typeof req.body.mode === "string") return res.status(403).json({"message": "No mode in the request"});
if (!req.body?.systemName) return res.status(403).json({ "message": "No system in the request" });
if (!req.body?.frequencies && Array.isArray(req.body.frequencies)) return res.status(403).json({ "message": "No frequencies in the request or type is not an array" });
if (!req.body?.mode && typeof req.body.mode === "string") return res.status(403).json({ "message": "No mode in the request" });
if (!req.body?.trunkFile) {
if (modes.digitalModes.includes(req.body.mode)) return res.status(403).json({"message": "No trunk file in the request but digital mode specified. If you are not using a trunk file for this frequency make sure to specify 'none' for trunk file in the request"})
if (modes.digitalModes.includes(req.body.mode)) return res.status(403).json({ "message": "No trunk file in the request but digital mode specified. If you are not using a trunk file for this frequency make sure to specify 'none' for trunk file in the request" })
// If there is a value keep it but if not, add nothing so the system can update that key (if needed)
req.body.trunkFile = req.body.trunkFile ?? "none";
}
@@ -34,7 +34,7 @@ function checkBodyForPresetFields(req, res, callback) {
return callback();
}
async function checkLocalIP() {
async function checkLocalIP() {
if (process.platform === "win32") {
// Windows
var networkConfig = await executeAsyncConsoleCommand("ipconfig");
@@ -72,21 +72,21 @@ exports.checkConfig = async function checkConfig() {
await updateId(0);
runningClientConfig.id = 0;
}
if (!runningClientConfig.ip) {
const ipAddr = await checkLocalIP();
await updateConfig('CLIENT_IP', ipAddr);
runningClientConfig.ip = ipAddr;
}
if(!runningClientConfig.name) {
if (!runningClientConfig.name) {
const lastOctet = await String(checkLocalIP()).spit('.')[-1];
const name = `Radio-Node-${lastOctet}`;
await updateConfig('CLIENT_NAME', name);
runningClientConfig.name = name;
}
if(!runningClientConfig.port) {
if (!runningClientConfig.port) {
const port = 3010;
await updateConfig('CLIENT_PORT', port);
runningClientConfig.port = port;
@@ -104,11 +104,11 @@ exports.checkIn = async (update = false) => {
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
try {
if (!runningClientConfig?.id || runningClientConfig.id == 0) {
try {
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(runningClientConfig), async (responseObject) => {
reqOptions = new requestOptions("/nodes/newNode", "POST");
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), async (responseObject) => {
// Check if the server responded
if (!responseObject) {
log.WARN("Server did not respond to checkIn. Will wait 60 seconds then try again");
@@ -120,7 +120,7 @@ exports.checkIn = async (update = false) => {
}
// Update the client's ID if the server accepted its
if (responseObject.statusCode === 202) {
if (responseObject.statusCode === 202) {
runningClientConfig.id = responseObject.body.nodeId;
log.DEBUG("Response object from new node: ", responseObject, runningClientConfig);
await updateId(runningClientConfig.id);
@@ -131,7 +131,7 @@ exports.checkIn = async (update = false) => {
log.DEBUG("HTTP Error: ", responseObject, await BufferToJson(responseObject.body));
await onHttpError(responseObject.statusCode);
}
});
}
else {
@@ -149,15 +149,17 @@ exports.checkIn = async (update = false) => {
}, 60000);
return
}
if (responseObject.statusCode === 202) {
log.DEBUG("Updated keys: ", responseObject.body.updatedKeys)
// Server accepted an update
}
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 (!update){
const tempUpdatedConfig = updateClientConfig(responseObject.body);
if (!tempUpdatedConfig.length > 0) return;
}
}
if (responseObject.statusCode >= 300) {
// Server threw an error
@@ -179,6 +181,24 @@ exports.requestCheckIn = async (req, res) => {
return res.sendStatus(200);
}
/**
* Express JS Wrapper for checking and updating client config
* @param {*} req
* @param {*} res
* @returns
*/
exports.updateClientConfigWrapper = async (req, res) => {
// Convert the online status to a boolean to be worked with
log.DEBUG("REQ Body: ", req.body);
const updatedKeys = await updateClientConfig(req.body);
if (updatedKeys) {
log.DEBUG("Keys have been updated, updating running config and checking in with the server: ", updatedKeys);
runningClientConfig = await getFullConfig();
await this.checkIn(true);
}
res.status(200).json(updatedKeys);
}
/** Controller for the /client/presets endpoint
* This is the endpoint wrapper to get the presets object
*/
@@ -196,7 +216,7 @@ exports.updatePreset = async (req, res) => {
runningClientConfig.nearbySystems = getPresets();
this.checkIn(true);
return res.sendStatus(200);
}, {frequencies: req.body.frequencies, mode: req.body.mode, trunkFile: req.body.trunkFile});
}, { frequencies: req.body.frequencies, mode: req.body.mode, trunkFile: req.body.trunkFile });
})
}
@@ -218,7 +238,7 @@ exports.addNewPreset = async (req, res) => {
*/
exports.removePreset = async (req, res) => {
checkBodyForPresetFields(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."})
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);