From 6b4ffc88b3246c8906773916d02acafa1a513772 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 16 Jul 2023 23:21:05 -0400 Subject: [PATCH] Implemented functional method to add a new system to a new through web app --- Server/controllers/nodesController.js | 42 ++++++++++++++++++++- Server/public/res/js/node.js | 30 ++++++++++++++- Server/routes/nodes.js | 5 ++- Server/views/node.ejs | 23 ++--------- Server/views/partials/modifySystemModal.ejs | 17 +++++---- 5 files changed, 86 insertions(+), 31 deletions(-) diff --git a/Server/controllers/nodesController.js b/Server/controllers/nodesController.js index 63588a6..6799391 100644 --- a/Server/controllers/nodesController.js +++ b/Server/controllers/nodesController.js @@ -117,6 +117,44 @@ exports.getNodeInfo = async (req, res) => { }) } +/** Adds a specific system/preset on a given node + * + * @param {*} req Default express req from router + * @param {*} res Defualt express res from router + * @param {*} req.params.nodeId The Node ID to add the preset/system to + * @param {*} req.body.systemName The name of the system to add + * @param {*} req.body.mode The radio mode of the preset + * @param {*} req.body.frequencies The frequencies of the preset + * @param {*} req.body.trunkFile The trunk file to use for digital stations + */ +exports.addNodeSystem = async (req, res) => { + if (!req.params.nodeId) return res.status(400).json("No id specified"); + if (!req.body.systemName) return res.status(400).json("No system specified"); + log.DEBUG("Adding system for node: ", req.params.nodeId, req.body); + getNodeInfoFromId(req.params.nodeId, (node) => { + const reqOptions = new requestOptions("/client/addPreset", "POST", node.ip, node.port); + const reqBody = { + 'systemName': req.body.systemName, + 'mode': req.body.mode, + 'frequencies': req.body.frequencies, + } + if(digitalModes.includes(req.body.mode)) reqBody['trunkFile'] = req.body.trunkFile ?? 'none' + + log.DEBUG("Request body for adding node system: ", reqBody, reqOptions); + sendHttpRequest(reqOptions, JSON.stringify(reqBody), async (responseObj) => { + if(responseObj){ + // Good + log.DEBUG("Response from adding node system: ", reqBody, responseObj); + return res.sendStatus(200) + } else { + // Bad + log.DEBUG("No Response from adding Node system"); + return res.status(400).json("No Response from adding Node, could be offline"); + } + }) + }) +} + /** Updates a specific system/preset on a given node * * @param {*} req Default express req from router @@ -144,11 +182,11 @@ exports.updateNodeSystem = async (req, res) => { sendHttpRequest(reqOptions, JSON.stringify(reqBody), async (responseObj) => { if(responseObj){ // Good - log.DEBUG("Response from updating node: ", reqBody, responseObj); + log.DEBUG("Response from updating node system: ", reqBody, responseObj); return res.sendStatus(200) } else { // Bad - log.DEBUG("No Response from updating Node"); + log.DEBUG("No Response from updating Node system"); return res.status(400).json("No Response from updating Node, could be offline"); } }) diff --git a/Server/public/res/js/node.js b/Server/public/res/js/node.js index e4e5274..6c732bd 100644 --- a/Server/public/res/js/node.js +++ b/Server/public/res/js/node.js @@ -185,8 +185,35 @@ function saveNodeDetails() { } } -function addNewSystem() { +function addNewSystem(systemName) { + const nodeId = document.getElementById("nodeId").value; + const systemMode = document.getElementById(`${systemName}_systemMode`).value; + const inputSystemFreqs = $(`[id^="${systemName}_systemFreq_"]`); + let systemFreqs = []; + for (const inputFreq of inputSystemFreqs){ + systemFreqs.push(inputFreq.value); + } + const reqBody = { + 'systemName': systemName, + 'mode': systemMode, + 'frequencies': systemFreqs + } + + console.log("Request Body: ", reqBody); + const Http = new XMLHttpRequest(); + const url='/nodes/'+nodeId+"/systems"; + Http.open("POST", url); + Http.setRequestHeader("Content-Type", "application/json"); + Http.send(JSON.stringify(reqBody)); + + Http.onloadend = (e) => { + const responseObject = JSON.parse(Http.responseText) + console.log(Http.status); + console.log(responseObject); + createToast(`${systemName} Added!`); + location.reload(); + } } function updateSystem(systemName) { @@ -216,6 +243,7 @@ function updateSystem(systemName) { console.log(Http.status); console.log(responseObject); createToast(`${systemName} Updated!`); + location.reload(); } } diff --git a/Server/routes/nodes.js b/Server/routes/nodes.js index d110343..2ef23b4 100644 --- a/Server/routes/nodes.js +++ b/Server/routes/nodes.js @@ -12,7 +12,10 @@ router.get('/:nodeId', nodesController.getNodeInfo); // Update an existing node router.put('/:nodeId', nodesController.updateExistingNode); -// Update an existing node's system +// Add a system to an existing node +router.post('/:nodeId/systems', nodesController.addNodeSystem); + +// Update a system on an existing node router.put('/:nodeId/systems', nodesController.updateNodeSystem); // TODO Need to authenticate this request diff --git a/Server/views/node.ejs b/Server/views/node.ejs index 0bc3a9c..45a7e28 100644 --- a/Server/views/node.ejs +++ b/Server/views/node.ejs @@ -113,7 +113,7 @@ <% // Update system modal %> - <%- include("partials/modifySystemModal.ejs", {'system': system, 'frequencies': node.nearbySystems[system].frequencies}) %> + <%- include("partials/modifySystemModal.ejs", {'system': system, 'frequencies': node.nearbySystems[system].frequencies, 'mode': node.nearbySystems[system].mode}) %> <% } %> @@ -126,31 +126,14 @@ + data-bs-target="#updateSystemModal_New_System">Add New System <% // new System Modal %> - - <%- include('partials/joinModal.ejs', {'nearbySystems': node.nearbySystems}) %> + <%- include("partials/modifySystemModal.ejs", {'system': "New System", 'frequencies': [], 'mode': ''}) %> <%- include('partials/bodyEnd.ejs') %> <%- include('partials/htmlFooter.ejs') %> \ No newline at end of file diff --git a/Server/views/partials/modifySystemModal.ejs b/Server/views/partials/modifySystemModal.ejs index 88ae381..a609964 100644 --- a/Server/views/partials/modifySystemModal.ejs +++ b/Server/views/partials/modifySystemModal.ejs @@ -3,7 +3,7 @@