// --- begin, important constants const data = { duration: 1, // will be multiplied by 30 machine_id: "", onesignal_subscription_id: "", } const API_URL = "https://backend.laundryweb.altafcreator.com" // --- check user status // returns true if there exists a timer, and returns false otherwise. // cookie need to be included. async function checkUserStatus() { const response = await fetch(`${API_URL}/check`, { credentials: "include", method: "POST", }); return await response.text(); } // --- check machine status // returns a 2d array representing machines // []: root array // []: block // enum: machine status async function checkMachineStatus() { const response = await fetch(`${API_URL}/status`, { method: "POST", credentials: "include", }); return await response.json(); } // --- timer duration const minField = document.getElementById("min-field"); const decBtn = document.getElementById("decTime"); function increaseTime() { data.duration += 1; minField.innerText = (30 * data.duration).toString() + " minutes"; decBtn.disabled = false; } function decreaseTime() { data.duration -= 1; data.duration = Math.max(data.duration, 1); if (data.duration == 1) { decBtn.disabled = true; } minField.innerText = (30 * data.duration).toString() + " minutes"; } // --- starting a timer async function start() { fetch(`${API_URL}/start`, { credentials: "include", method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }).then(response => response.text()) .then(data => { console.log(data); if (data == "all good bro timer started") { window.location.href = "/timer/"; } }); } // --- information loading + cookie setting (from server) async function information(urlParam = null) { const response = await fetch(`${API_URL}/info`, { credentials: "include", method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ "machine_id": urlParam ? urlParam : "" }), }); const json = await response.json(); if (json["block"]) { document.getElementById("logo-id").innerText = "H" + json["block"]; } else { document.getElementById("logo-id").innerText = "H?"; } return Promise.resolve(json["block"]); } // ------ page specific ----- // ---- machine status page const STATUS_INTERVAL = 30 // --- machines visual async function startUpdateMachines() { const urlParams = new URLSearchParams(window.location.search); await information(urlParams.get("machine")); console.log("info done") updateMachines(); while (true) { await delay(STATUS_INTERVAL * 1000); await updateMachines(); } } async function updateMachines() { const dryer1 = document.getElementById("dryer1-img"); const washer1 = document.getElementById("washer1-img"); const dryer2 = document.getElementById("dryer2-img"); const washer2 = document.getElementById("washer2-img"); const machine_imgs = [dryer1, washer1, dryer2, washer2]; const dryer1txt = document.getElementById("dryer1-span"); const washer1txt = document.getElementById("washer1-span"); const dryer2txt = document.getElementById("dryer2-span"); const washer2txt = document.getElementById("washer2-span"); const machine_txts = [dryer1txt, washer1txt, dryer2txt, washer2txt]; const status = await checkMachineStatus(); console.log(status); for (let i = 0; i < status[0].length; i++) { if (status[0][i] == "RUNNING") { if ((i + 1) % 2 == 0) { machine_imgs[i].src = "/assets/img/washer_on.png"; } else { machine_imgs[i].src = "/assets/img/dryer_on.png"; } const now = Date.now(); const end = Date.parse(status[2][i]); machine_txts[i].innerHTML = Math.ceil((end - now) / 60000).toString() + " min(s) left"; } else if (status[0][i] == "FINISHED") { if ((i + 1) % 2 == 0) { machine_imgs[i].src = "/assets/img/washer_clothes.png"; } else { machine_imgs[i].src = "/assets/img/dryer_clothes.png"; } machine_txts[i].innerHTML = "Idle" } else { if ((i + 1) % 2 == 0) { machine_imgs[i].src = "/assets/img/washer_off.png"; } else { machine_imgs[i].src = "/assets/img/dryer_off.png"; } machine_txts[i].innerHTML = "Idle" } } } // --- current timers async function startLoadTimers() { information(); const timersData = await fetchTimers(); if (timersData[0] != 200) { console.error(timersData[0].toString() + " from backend: " + timersData[1]); return; } const timers = timersData[1]; const container = document.getElementById("timer-container") const textList = [] const progList = [] const startTimestamps = [] const endTimestamps = [] for (let i = 0; i < timers.length; i++) { container.innerHTML += `