RGBMatrixDisplay/http/main.js

109 lines
3.1 KiB
JavaScript

const index = parseInt(new URLSearchParams(window.location.search).get('index')) || 0;
const dlYe = document.getElementById("dlYe");
const dlMo = document.getElementById("dlMo");
const dlDa = document.getElementById("dlDa");
const dlHo = document.getElementById("dlHo");
const dlMi = document.getElementById("dlMi");
const dlSe = document.getElementById("dlSe");
const tmDa = document.getElementById("tmDa");
const tmHo = document.getElementById("tmHo");
const tmMi = document.getElementById("tmMi");
const tmSe = document.getElementById("tmSe");
const brightness = document.getElementById("brightness")
const speed = document.getElementById("speed")
let interval = undefined;
function dl() {
const y = parseInt(dlYe.value);
const M = parseInt(dlMo.value) - 1;
const d = parseInt(dlDa.value);
const h = parseInt(dlHo.value) || 0;
const m = parseInt(dlMi.value) || 0;
const s = parseInt(dlSe.value) || 0;
const deadlineEpoch = (new Date(y, M, d, h, m, s).getTime() / 1000).toFixed(0);
set("deadlineEpoch", deadlineEpoch);
}
function tm() {
const d = parseInt(tmDa.value) || 0;
const h = parseInt(tmHo.value) || 0;
const m = parseInt(tmMi.value) || 0;
const s = parseInt(tmSe.value) || 0;
const timerMillis = (((d * 24 + h) * 60 + m) * 60 + s) * 1000;
set("timerMillis", timerMillis);
}
const sm = (v) => set('mode', v);
const br = (v) => set('brightness', v);
const sp = (v) => set('speed', v);
const set = (n, v) => gf(`/set?n=${n}&v=${v}`)
const fetch = () => {
// if (interval !== undefined) {
// clearInterval(interval);
// interval = undefined;
// }
// interval = setInterval(fetch, 2000);
get("/state", showState());
}
const gf = (path) => {
get(path, fetch);
}
function get(path, cb = null) {
const r = new XMLHttpRequest();
r.onreadystatechange = () => !!cb && r.readyState === 4 && r.status === 200 && cb(r);
r.open("GET", path, true);
r.send();
}
function showState() {
return function (r) {
const json = JSON.parse(r.responseText);
// noinspection JSUnresolvedReference
const d = new Date(parseInt(json.config.deadlineEpoch || 0) * 1000);
dlYe.value = "" + d.getFullYear();
dlMo.value = "" + d.getMonth() + 1;
dlDa.value = "" + d.getDate();
dlHo.value = "" + d.getHours();
dlMi.value = "" + d.getMinutes();
dlSe.value = "" + d.getSeconds();
// noinspection JSUnresolvedReference
const s = parseInt(json.config.timerMillis || 0) / 1000;
const m = Math.floor(s / 60);
const h = Math.floor(m / 60);
tmDa.value = "" + Math.floor(h / 24);
tmHo.value = "" + h % 24;
tmMi.value = "" + m % 60;
tmSe.value = "" + s % 60;
const id = parseInt(json.config.mode);
for (const mode of document.getElementsByClassName("mode")) {
if (mode.id === "mode" + id) {
if (!mode.classList.contains("modeActive")) {
mode.classList.add("modeActive");
}
} else {
mode.classList.remove("modeActive");
}
}
// noinspection JSUnresolvedReference
brightness.innerText = (parseInt(json.config.brightness) / 2.56).toFixed(0) + "%";
speed.innerText = parseInt(json.config.speed).toFixed(5) + "x";
};
}
fetch();