var timeLogLevel = ""; function setTimeLogLevel() { var level = logLevel.value; fetch("/api/time/loglevel", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ level: level }), }) .then(function (r) { if (!r.ok) { return r.text().then(function (t) { throw new Error(t); }); } timeLogLevel = level; }) .catch(function (e) { logLevel.value = timeLogLevel; }); } function getTimeLogLevel() { fetch("/api/time/loglevel") .then(function (r) { if (!r.ok) { return r.text().then(function (t) { throw new Error(t); }); } return r.json(); }) .then(function (d) { timeLogLevel = d.log_level; logLevel.value = d.log_level; }) .catch(function (e) { timeLogLevel = ""; logLevel.value = ""; }); } function downloadTimeLogFile(event) { var btn = event.currentTarget; btn.disabled = true; fetch("/api/time/logfile") .then(function (r) { if (!r.ok) { throw new Error("HTTP " + r.status); } var disposition = r.headers.get("Content-Disposition"); var filename = "time.log.tar.gz"; if (disposition) { var match = disposition.match(/filename="?([^";]+)"?/); if (match) { filename = match[1]; } } return r.blob().then(function (blob) { return { blob: blob, filename: filename }; }); }) .then(function (res) { downloadBlobFile(res.blob, res.filename); alert("下载成功"); }) .catch(function (e) { console.error("download failed:", e); alert("下载失败: " + e.message); }) .finally(function () { btn.disabled = false; btn.textContent = "下载"; }); } function restartTimeService(event) { if (!confirm("确定重启时间服务吗?")) { return; } var btn = event.currentTarget; btn.disabled = true; fetch("/api/time/restart", { method: "POST", }) .then(function (r) { if (!r.ok) { return r.text().then(function (t) { throw new Error(t); }); } alert("重启成功"); }) .catch(function (e) { console.error("restart failed:", e); alert("重启失败: " + e.message); }) .finally(function () { btn.disabled = false; }); } var timeStatusTimer = null; function getTimeStatus() { fetch("/api/time/status") .then(function (r) { if (!r.ok) { throw new Error("HTTP " + r.status); } return r.json(); }) .then(function (data) { ["var1", "var2", "var3", "var4", "var5", "var6"].forEach(function (id) { return setText(id, data[id], "page3"); }); }) .catch(function (e) { console.error("getTimeStatus() failed:", e); ["var1", "var2", "var3", "var4", "var5", "var6"].forEach(function (id) { return setText(id, null, "page3"); }); }); getTimeLogLevel(); } function startTimeStatusRefresh() { if (timeStatusTimer) { return; } getTimeStatus(); timeStatusTimer = setInterval(function () { getTimeStatus(); }, 1000); } function stopTimeStatusRefresh() { if (timeStatusTimer) { clearInterval(timeStatusTimer); timeStatusTimer = null; } } function initPage3() { startTimeStatusRefresh(); connectLog("yfkj-timesyncd.service", "text1"); } function exitPage3() { stopTimeStatusRefresh(); disconnectLog(); }