diff --git a/Server/controllers/nodesController.js b/Server/controllers/nodesController.js index fa7b622..3012067 100644 --- a/Server/controllers/nodesController.js +++ b/Server/controllers/nodesController.js @@ -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 * diff --git a/Server/public/res/js/node.js b/Server/public/res/js/node.js index 24b784e..e439c30 100644 --- a/Server/public/res/js/node.js +++ b/Server/public/res/js/node.js @@ -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() { + } \ No newline at end of file diff --git a/Server/routes/nodes.js b/Server/routes/nodes.js index 773bc45..3a2b7d2 100644 --- a/Server/routes/nodes.js +++ b/Server/routes/nodes.js @@ -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; diff --git a/Server/views/node.ejs b/Server/views/node.ejs index 9575b9f..0bc3a9c 100644 --- a/Server/views/node.ejs +++ b/Server/views/node.ejs @@ -9,29 +9,30 @@