Red/green/yellow dot to tell slave status on /nodeDetails/

This commit is contained in:
Danielv123 committed 2017-05-26 09:06:04 +02:00
1 parent 74348be0a8
commit b520d9611b
1 file changed
+35 -12
+35 -12
View File
@@ -12,19 +12,24 @@ function post(url, data, callback) {
}
xhr.send(JSON.stringify(data));
}
// callback(err, json)
function getJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
// triggers if connection is refused
xhr.onerror = function(e){
callback(e);
};
xhr.send();
};
// function to sort arrays of objects after a keys value
function sortByKey(array, key) {
@@ -43,6 +48,24 @@ function getParameterByName(name, url) {
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
/// code below here
// update the online indicator dot
setInterval(function(){
getJSON("/api/slaves", function(err, slaveData){
if(err){
document.querySelector("#onlineIndicator").style.backgroundColor = "yellow";
// compare with Math.floor(x/100000) allows us to check if they are within 10s of each other
} else if(Math.floor(slaveData[getParameterByName("slaveID")].time/100000) == Math.floor(Date.now()/100000)){
document.querySelector("#onlineIndicator").style.backgroundColor = "green";
} else {
document.querySelector("#onlineIndicator").style.backgroundColor = "red";
}
});
}, 1000);
function populateSlaveInfo(){
let slaveID = getParameterByName("slaveID");
if(!slaveID){