openCountdown/static/js/script.js

298 lines
8.8 KiB
JavaScript
Raw Permalink Normal View History

2022-02-23 22:17:12 +01:00
var newDateObj = new Date();
2022-02-25 17:22:59 +01:00
newDateObj = new Date(newDateObj.getTime() + 1000 * 20)
2022-02-23 22:17:12 +01:00
2022-03-10 18:56:51 +01:00
backReqInt = 0;
websocketFailed = false
recoveryAttempts = 0;
let socket = new ReconnectingWebSocket("ws://localhost:" + location.port);
2022-03-10 20:55:23 +01:00
let ackdSessionToken = false
let isFirstPacket = true
2022-03-10 18:56:51 +01:00
2022-03-19 18:28:56 +01:00
let lastTime = "00:00:00";
let timerCountdownFirst = true;
2022-03-10 18:56:51 +01:00
socket.onopen = function (e) {
// alert("[open] Connection established");
//alert("Sending to server");
socket.send("new client");
};
socket.onmessage = function (event) {
// alert(`[message] Data received from server: ${event.data}`);
2022-03-10 20:55:23 +01:00
let inData = JSON.parse(event.data)
if (isFirstPacket) {
isFirstPacket = false
dataFame = JSON.parse(event.data);
timeDiff = new Date().getTime() - dataFame.srvTime
} else {
if (inData.sessionToken == dataFame.sessionToken) {
dataFame = JSON.parse(event.data);
timeDiff = new Date().getTime() - dataFame.srvTime
} else {
if (ackdSessionToken == false) {
ackdSessionToken = true
if (confirm("Session token mismatch, reload the page?")) {
location.reload();
}
}
}
}
2022-03-10 18:56:51 +01:00
};
socket.onclose = function (event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.error('[close] Connection died');
}
};
2022-02-24 21:08:31 +01:00
allowFullscreen = true
let dataFame = {}
let timeDiff = 0
2022-02-24 21:08:31 +01:00
2022-02-24 19:11:13 +01:00
function enterFullscreen(element) {
2022-02-25 17:22:59 +01:00
if (element.requestFullscreen) {
2022-02-24 19:11:13 +01:00
element.requestFullscreen();
2022-03-19 18:28:56 +01:00
} else if (element.msRequestFullscreen) {
2022-02-24 19:11:13 +01:00
element.msRequestFullscreen();
2022-03-19 18:28:56 +01:00
} else if (element.webkitRequestFullscreen) {
2022-02-24 19:11:13 +01:00
element.webkitRequestFullscreen();
}
}
2022-02-25 17:22:59 +01:00
function returnData() {
return (JSON.parse(document.getElementById("incomeData").innerHTML))
}
2022-02-24 22:18:21 +01:00
function percentage(partialValue, totalValue) {
return (100 * partialValue) / totalValue;
2022-02-25 17:22:59 +01:00
}
2022-02-24 22:18:21 +01:00
2022-02-25 17:22:59 +01:00
function updateFullscreen() {
if (JSON.parse(document.getElementById("incomeData").innerHTML).defaultFullScreen && allowFullscreen) {
2022-02-24 19:11:13 +01:00
enterFullscreen(document.documentElement)
}
}
2022-02-25 17:22:59 +01:00
2022-02-24 19:11:13 +01:00
function msToTime(s, data) {
isSmallerThenZero = false
2022-02-25 17:22:59 +01:00
if (s < 0) {
2022-02-24 19:11:13 +01:00
isSmallerThenZero = true
}
var ms = s % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
let out = ""
2022-02-25 17:22:59 +01:00
if (!data.showMilliSeconds) {
out = ('00' + Math.abs(hrs)).slice(-2) + ':' + ('00' + Math.abs(mins)).slice(-2) + ':' + ('00' + Math.abs(secs)).slice(-2)
} else {
out = ('00' + Math.abs(hrs)).slice(-2) + ':' + ('00' + Math.abs(mins)).slice(-2) + ':' + ('00' + Math.abs(secs)).slice(-2) + '.' + ('000' + Math.abs(ms)).slice(-3)
2022-02-24 19:11:13 +01:00
}
2022-02-25 17:22:59 +01:00
if (isSmallerThenZero) {
2022-02-24 19:11:13 +01:00
out = "-" + out
}
return out;
}
2022-02-25 17:22:59 +01:00
function findProgessColor(colors, value) {
2022-02-24 22:18:21 +01:00
const allColors = Object.keys(colors);
let resColor = colors["START"]
2022-02-25 17:22:59 +01:00
for (let color in allColors) {
2022-02-24 22:18:21 +01:00
const currColor = allColors[color]
2022-02-25 17:22:59 +01:00
// console.log(color, currColor, parseInt(currColor), value, colors[String(currColor)], resColor)
if (value <= parseInt(currColor)) {
resColor = colors[String(currColor)]
break
2022-02-24 22:18:21 +01:00
}
}
2022-02-25 17:22:59 +01:00
return (resColor)
2022-02-24 22:18:21 +01:00
}
function requestBackend() {
2022-02-24 21:08:31 +01:00
resp = httpGet("/api/v1/data");
2022-02-25 17:22:59 +01:00
resp.onloadend = function (e) {
if (resp.status == 200) {
2022-02-25 17:22:59 +01:00
resp = resp.responseText;
var data = JSON.parse(resp);
timeDiff = new Date().getTime() - data.srvTime
dataFame = data;
}
}
}
2022-02-25 22:24:45 +01:00
let isSlowed = false
function handleRecovery() {
var img = document.body.appendChild(document.createElement("img"));
2022-03-10 18:56:51 +01:00
img.onload = function () {
location.reload();
};
img.src = "SMPTE_Color_Bars.svg";
}
let recoInter = -1
function handleUpdate() {
var data = dataFame;
document.getElementById("incomeData").innerHTML = JSON.stringify(data)
document.getElementById("timediff").innerHTML = timeDiff + "<br>" + String(new Date().getTime() - data.srvTime);
2022-03-19 18:28:56 +01:00
if (data.debug) {
document.getElementById("timediff").style.display = "block";
} else {
document.getElementById("timediff").style.display = "none";
}
if (data.defaultFullScreen && document.getElementById("initalDate").innerHTML.includes("true") && allowFullscreen) {
enterFullscreen(document.documentElement);
document.getElementById("initalDate").innerHTML = "false"
}
if (data.showMessage) {
document.getElementById("overlay").style.display = "block";
document.getElementById("text").innerHTML = data.message
} else {
document.getElementById("overlay").style.display = "none";
}
if (data.showTimeOnCountdown && data.mode == "timer") {
document.getElementById("clockSec").innerHTML = getTime();
} else {
document.getElementById("clockSec").innerHTML = "";
}
if (new Date().getTime() - data.messageAppearTime < 5000) {
if (!document.getElementById("text").classList.contains('blink')) {
document.getElementById("text").classList.add("blink")
}
} else {
if (document.getElementById("text").classList.contains('blink')) {
document.getElementById("text").classList.remove("blink")
}
}
if (data.mode == "clock") {
document.getElementById("timer").innerHTML = getTime();
document.getElementById("testImg").style.display = "none";
document.getElementById("wholeProgBar").style.display = "none";
document.getElementById("clockSec").innerHTML = "";
document.getElementById("timer").style.color = "white"
2022-03-19 18:28:56 +01:00
timerCountdownFirst = true;
} else if (data.mode == "timer") {
2022-07-12 22:15:27 +02:00
if(data.showProgressbar) {
document.getElementById("wholeProgBar").style.display = "block";
} else {
document.getElementById("wholeProgBar").style.display = "none";
}
2022-03-19 18:28:56 +01:00
const now = new Date()
if(timerCountdownFirst){
const diff = data.countdownGoal - now.getTime()
timerCountdownFirst = false
document.getElementById("timer").innerHTML = msToTime(diff, data);
if (diff > 0) {
document.getElementById("progBar").style.width = percentage(diff, data.timeAmountInital) + "%";
} else {
document.getElementById("progBar").style.width = "0%";
}
document.getElementById("progBar").style.backgroundColor = findProgessColor(data.colorSegments, diff)
document.getElementById("testImg").style.display = "none";
if (data.enableColoredText) {
document.getElementById("timer").style.color = findProgessColor(data.textColors, diff)
} else {
document.getElementById("timer").style.color = "white"
}
}
if (data.timerRunState) {
const diff = data.countdownGoal - now.getTime()
document.getElementById("timer").innerHTML = msToTime(diff, data);
2022-03-19 18:28:56 +01:00
lastTime = msToTime(diff, data);
if (diff > 0) {
document.getElementById("progBar").style.width = percentage(diff, data.timeAmountInital) + "%";
2022-02-25 17:22:59 +01:00
} else {
document.getElementById("progBar").style.width = "0%";
2022-02-25 17:22:59 +01:00
}
document.getElementById("progBar").style.backgroundColor = findProgessColor(data.colorSegments, diff)
document.getElementById("testImg").style.display = "none";
if (data.enableColoredText) {
document.getElementById("timer").style.color = findProgessColor(data.textColors, diff)
2022-02-25 17:22:59 +01:00
} else {
2022-02-25 22:24:45 +01:00
document.getElementById("timer").style.color = "white"
2022-02-25 17:22:59 +01:00
}
2022-02-24 22:18:21 +01:00
2022-03-19 18:28:56 +01:00
} else {
document.getElementById("timer").innerHTML = lastTime
}
} else if (data.mode == "black") {
2022-03-19 18:28:56 +01:00
timerCountdownFirst = true;
document.getElementById("timer").innerHTML = "";
document.getElementById("testImg").style.display = "none";
document.getElementById("wholeProgBar").style.display = "none";
document.getElementById("clockSec").innerHTML = "";
} else if (data.mode == "test") {
2022-03-19 18:28:56 +01:00
timerCountdownFirst = true;
document.getElementById("timer").innerHTML = "";
document.getElementById("testImg").style.display = "block";
document.getElementById("progBar").style.display = "none";
document.getElementById("clockSec").innerHTML = "";
}
2022-02-23 21:55:13 +01:00
}
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
2022-02-25 17:22:59 +01:00
xmlHttp.open("GET", theUrl, true); // false for synchronous request
2022-02-23 21:55:13 +01:00
xmlHttp.send(null);
2022-02-25 17:22:59 +01:00
xmlHttp.onerror = function (e) {
console.log(e);
};
return xmlHttp;
2022-02-23 21:55:13 +01:00
}
function getTime() {
var date = new Date();
var h = date.getHours(); // 0 - 23
var m = date.getMinutes(); // 0 - 59
var s = date.getSeconds(); // 0 - 59
h = h < 10 ? "0" + h : h;
m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
var time = h + ":" + m + ":" + s;
return time;
}
2022-02-24 21:08:31 +01:00
2022-03-10 18:56:51 +01:00
// setInterval(requestBackend, 500);
updateInter = setInterval(handleUpdate, 2);
2022-02-24 19:11:13 +01:00
let temp = new URLSearchParams(window.location.search).get("smaller");
if (temp == "true") {
var cssURL = '/css/smallerTextMod.css';
2022-02-25 17:22:59 +01:00
document.head.innerHTML += '<link rel="stylesheet" href="' + cssURL + '"/>'
2022-02-24 21:08:31 +01:00
allowFullscreen = false
2022-02-24 19:11:13 +01:00
}