1
0
dwelling-radio/internal/handlers/web/assets/js/main.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-03-08 01:17:24 +04:00
function $(id) { return document.getElementById(id); }
function updateRadioStatus() {
fetch("/stats")
.then(r => r.json())
.then(r => {
$("radio-status").innerHTML =
`On-air since <time datetime="${r.server_start_iso8601}">${r.server_start_date}</time>`;
$("radio-song").textContent = r.song;
$("radio-listeners").textContent = r.listeners;
$("radio-listener-peak").textContent = r.listener_peak;
}).catch(() => {
$("radio-status").textContent = "Radio is offline.";
$("radio-song").textContent =
$("radio-listeners").textContent =
$("radio-listener-peak").textContent = "n/a";
});
}
function updateLastPlayedSong() {
fetch('/lastsong')
.then(r => r.json())
.then(last_played => {
2022-03-31 17:55:36 +04:00
let cur_time = $('last-played').firstChild.lastChild.firstChild.innerText;
2022-03-08 01:17:24 +04:00
2022-03-31 17:55:36 +04:00
if (last_played.time == cur_time)
2022-03-08 01:17:24 +04:00
return;
$('last-played').firstChild.firstChild.remove();
let row = $('last-played').insertRow();
let start_time = row.insertCell();
start_time.appendChild(document.createTextNode(last_played.time));
let listeners_cell = row.insertCell();
listeners_cell.appendChild(document.createTextNode(last_played.listeners));
let song_cell = row.insertCell();
song_cell.appendChild(document.createTextNode(last_played.song));
2022-03-08 01:17:24 +04:00
});
}
document.getElementById("btn-update").addEventListener("click", () => {
updateLastPlayedSong();
updateRadioStatus();
})
setInterval(updateRadioStatus, 45000);
setInterval(updateLastPlayedSong, 45000);