let online,percentage,status; const update = function () { let todo=3; fetch("https://spaceapi.c3re.de/prusaxl_online.json") .then(function (response) { return response.json(); }) .then(function (data) { online=data todo--; if(todo===0){ run(); } }); fetch("https://spaceapi.c3re.de/prusaxl_status.json") .then(function (response) { return response.text(); }) .then(function (data) { status=data todo--; if(todo===0){ run(); } }); fetch("https://spaceapi.c3re.de/prusaxl_percentage.json") .then(function (response) { return response.text(); }) .then(function (data) { percentage=parseInt(data) todo--; if(todo===0){ run(); } }); } update(); setInterval(update, 1000); function run(){ if(!online){ setImage("OFF"); }else{ setImage(status,percentage); } } //[ IDLE, BUSY, PRINTING, PAUSED, FINISHED, STOPPED, ERROR, ATTENTION, READY ] function setImage(status,percentage){ let el=document.getElementsByClassName("header")[0]; let imagepath=getBannerPath(status,percentage); if(imagepath) { el.style.backgroundImage = "url('" + imagepath + "')"; } } function getBannerPath(status,percentage){ if(status === "IDLE") { return "/img/banner/idle.jpg"; } else if(status === "BUSY") { return null; } else if(status === "PRINTING") { percentage = parseInt(percentage); percentage = Math.min(100, percentage); percentage = Math.max(0, percentage); percentage = Math.floor(percentage/5)*5; return "/img/banner/"+percentage+".jpg"; } else if(status === "PAUSED") { return null; } else if(status === "FINISHED") { return "/img/banner/100.jpg"; } else if(status === "STOPPED") { return "/img/banner/attention.jpg"; } else if(status === "ERROR") { return "/img/banner/attention.jpg"; } else if(status === "ATTENTION") { return "/img/banner/attention.jpg"; } else if(status === "READY") { return "/img/banner/idle.jpg"; } else { return "/img/banner/off.jpg"; } }