Update API and add webapp saving

This commit is contained in:
Logan Cusano
2023-07-16 18:56:47 -04:00
committed by logan
parent d7ea6bbbd4
commit 648782658c
4 changed files with 103 additions and 34 deletions

View File

@@ -110,8 +110,8 @@ exports.newNode = async (req, res) => {
* @param {*} res Defualt express res from router
*/
exports.getNodeInfo = async (req, res) => {
if (!req.query.id) return res.status(400).json("No id specified");
getNodeInfoFromId(req.query.id, (nodeInfo) => {
if (!req.params.id) return res.status(400).json("No id specified");
getNodeInfoFromId(req.params.id, (nodeInfo) => {
res.status(200).json(nodeInfo);
})
}
@@ -121,9 +121,9 @@ exports.getNodeInfo = async (req, res) => {
* @param {*} req Default express req from router
* @param {*} res Defualt express res from router
*/
exports.nodeCheckIn = async (req, res) => {
if (!req.body.id) return res.status(400).json("No id specified");
getNodeInfoFromId(req.body.id, (nodeInfo) => {
exports.updateExistingNode = async = (req, res) => {
if (!req.params.nodeId) return res.status(400).json("No id specified");
getNodeInfoFromId(req.params.nodeId, (nodeInfo) => {
let checkInObject = {};
// Convert the online status to a boolean to be worked with
log.DEBUG("REQ Body: ", req.body);
@@ -163,9 +163,9 @@ exports.nodeCheckIn = async (req, res) => {
// If no changes are made tell the client
if (!isObjectUpdated) return res.status(200).json("No keys updated");
log.INFO("Updating the following keys for ID: ", req.body.id, checkInObject);
log.INFO("Updating the following keys for ID: ", req.params.nodeId, checkInObject);
checkInObject._id = req.body.id;
checkInObject._id = req.params.nodeId;
checkInObject = new nodeObject(checkInObject);
if (!nodeInfo) {
@@ -182,6 +182,24 @@ exports.nodeCheckIn = async (req, res) => {
});
}
/** Allows the bots to check in and get any updates from the server
*
* @param {*} req Default express req from router
* @param {*} res Defualt express res from router
*/
exports.nodeCheckIn = async (req, res) => {
if (!req.params.nodeId) return res.status(400).json("No id specified");
getNodeInfoFromId(req.params.nodeId, (nodeInfo) => {
if (!nodeInfo.online) {
nodeInfo.online = true;
updateNodeInfo(nodeInfo, () => {
return res.status(200).json(nodeInfo);
})
}
else return res.status(200).json(nodeInfo);
});
}
/**
* Requests a specific node to check in with the server, if it's online
*

View File

@@ -78,11 +78,12 @@ function createToast(notificationMessage){
document.getElementById("toastZone").appendChild(wrapperDiv);
$('.toast').toast('show');
return $('.toast');
}
function sendNodeHeartbeat(nodeId){
const Http = new XMLHttpRequest();
const url='/nodes/'+nodeId;
const url='/nodes/nodeCheckIn/'+nodeId;
Http.open("GET", url);
Http.send();
@@ -139,5 +140,51 @@ function leaveServer(){
console.log(Http.status);
console.log(responseObject);
createToast(`${responseObject} is leaving`);
setTimeout(() => {}, 45000);
}
}
function saveNodeDetails() {
const nodeId = document.getElementById("nodeId").value;
const nodeName = document.getElementById("inputNodeName").value;
const nodeIp = document.getElementById("inputNodeIp").value;
const nodePort = document.getElementById("inputOrgName").value;
const nodeLocation = document.getElementById("inputNodeLocation").value;
const reqBody = {
'id': nodeId,
'name': nodeName,
'ip': nodeIp,
'port': nodePort,
'location': nodeLocation,
'online': true
}
console.log("Request Body: ", reqBody);
const Http = new XMLHttpRequest();
const url='/nodes/'+nodeId;
Http.open("PUT", 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(`Node Updated!`);
}
}
function addNewSystem() {
}
function updateSystem() {
}
function requestNodeUpdate() {
}

View File

@@ -5,6 +5,13 @@ const nodesController = require('../controllers/nodesController');
/* GET nodes the server knows */
router.get('/', nodesController.listAllNodes);
// TODO Need to authenticate this request
/* GET the information the server has on a particular node */
router.get('/:nodeId', nodesController.getNodeInfo);
// Update an existing node
router.put('/:nodeId', nodesController.updateExistingNode);
// TODO Need to authenticate this request
/* POST a new node to the server
*
@@ -20,15 +27,11 @@ router.get('/', nodesController.listAllNodes);
*/
router.post('/newNode', nodesController.newNode);
// TODO Need to authenticate this request
/* GET the information the server has on a particular node */
router.get('/nodeInfo', nodesController.getNodeInfo);
// TODO Need to authenticate this request
// Client checkin with the server to update information
router.post('/nodeCheckIn', nodesController.nodeCheckIn);
router.post('/nodeCheckIn/:nodeId', nodesController.nodeCheckIn);
// Request a node to check in with the server
router.get('/:nodeId', nodesController.requestNodeCheckIn);
router.get('/nodeCheckIn/:nodeId', nodesController.requestNodeCheckIn);
module.exports = router;

View File

@@ -9,29 +9,30 @@
</p>
</div>
<div class="card-body">
<div class="col-md-12 pt-2">
<label class="small mb-1" for="nodeStatus">Online Status:</label>
<% if(node.online){%> <span class="badge badge-soft-success mb-0 align-middle fs-6" id="nodeStatus">Online</span>
<% } else {%> <span class="badge badge-soft-danger mb-0 align-middle fs-6">Offline</span>
<% } %>
<br>
<div class="py-2"></div>
<!-- Join Server button-->
<a type="button" class="btn btn-info text-white<% if(!node.online) { %>disabled<% } %>" data-bs-toggle="modal" data-bs-target="#joinModal" href="#">Join Server</a>
<!-- Leave Server button -->
<a type="button" class="btn btn-danger <% if(!node.online) { %>disabled<% } %>" href="#" onclick="leaveServer()">Leave Server</a>
<!-- Checkin with client button -->
<a type="button" class="btn btn-secondary" href="#" onclick="sendNodeHeartbeat('<%=node.id%>')">Check-in with Node</a>
<!-- Update Client button -->
<a type="button" class="btn btn-warning" href="#" onclick="requestNodeUpdate('<%=node.id%>')">Update Node</a>
</div>
<hr>
<form>
<div class="row gx-3 mb-3">
<div class="">
</div>
<div class="col-md-6">
<label class="small mb-1" for="nodeId">Node ID (this is the assigned Node ID and cannot be
changed)</label>
<input class="form-control" id="nodeId" type="text" value="<%=node.id%>" disabled></input>
</div>
<div class="col-md-6">
<label class="small mb-1" for="nodeStatus">Online Status:</label>
<% if(node.online){%> <span class="badge badge-soft-success mb-0 align-middle fs-6" id="nodeStatus">Online</span>
<% } else {%> <span class="badge badge-soft-danger mb-0 align-middle fs-6">Offline</span>
<% } %>
<hr>
<!-- Join Server button-->
<a type="button" class="btn btn-info <% if(!node.online) { %>disabled<% } %>" data-bs-toggle="modal" data-bs-target="#joinModal" href="#">Join Server</a>
<!-- Leave Server button -->
<a type="button" class="btn btn-danger <% if(!node.online) { %>disabled<% } %>" href="#" onclick="leaveServer()">Leave Server</a>
<!-- Checkin with client button -->
<a type="button" class="btn btn-secondary" href="#" onclick="sendNodeHeartbeat('<%=node.id%>')">Check-in with Node</a>
</div>
</div>
</div>
<div class="row gx-3 mb-3">
@@ -122,11 +123,11 @@
</div>
</div>
<!-- Save changes button-->
<button class="btn btn-primary <% if(!node.online) { %>disabled<% } %>" type="button">Save changes</button>
<button class="btn btn-primary <% if(!node.online) { %>disabled<% } %>" type="button" onclick="saveNodeDetails()">Save changes</button>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary float-right <% if(!node.online) { %>disabled<% } %>" data-bs-toggle="modal"
data-bs-target="#newSystemModal">Add New System</button>
</form>
data-bs-target="#newSystemModal">Add New System</button>
</form>
</div>
</div>
</div>