90 lines
4.0 KiB
HTML
90 lines
4.0 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>局域网 UPS</title>
|
|
<style>
|
|
:root { color-scheme: dark; font: 16px system-ui, sans-serif; background: #101720; color: #eef4fa; }
|
|
body { margin: 0; }
|
|
main { max-width: 860px; margin: 0 auto; padding: clamp(24px, 6vw, 70px) 22px; }
|
|
h1 { font-size: clamp(2rem, 5vw, 3.5rem); letter-spacing: -.035em; margin: 0 0 6px; }
|
|
p { margin: 0; }
|
|
.muted, dt { color: #aab9c8; }
|
|
#source, #detail { overflow-wrap: anywhere; }
|
|
.status { margin: 30px 0 16px; padding: 22px; background: #182330; border: 1px solid #344658; border-left: 4px solid #82a9cc; border-radius: 10px; }
|
|
.status[data-tone="ok"] { border-left-color: #5ed4a8; }
|
|
.status[data-tone="warn"] { border-left-color: #ffbd72; }
|
|
h2 { margin: 0 0 8px; font-size: 1.3rem; }
|
|
#detail:not(:empty) { margin-top: 8px; color: #ffcc91; }
|
|
dl { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 0; }
|
|
dl div { padding: 18px; border: 1px solid #344658; border-radius: 10px; background: #182330; }
|
|
dd { margin: 8px 0 0; font-size: 1.5rem; font-variant-numeric: tabular-nums; }
|
|
[hidden] { display: none !important; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<h1>局域网 UPS</h1>
|
|
<p class="muted" id="source"></p>
|
|
<section class="status" id="status" role="status">
|
|
<h2 id="headline">正在连接</h2>
|
|
<p class="muted" id="lastOK"></p>
|
|
<p id="detail"></p>
|
|
</section>
|
|
<dl id="readings" aria-label="UPS 读数" hidden>
|
|
<div><dt>电池电量</dt><dd data-key="battery.charge" data-unit="%"></dd></div>
|
|
<div><dt>负载</dt><dd data-key="ups.load" data-unit="%"></dd></div>
|
|
<div><dt>输入电压</dt><dd data-key="input.voltage" data-unit=" V"></dd></div>
|
|
<div><dt>输出电压</dt><dd data-key="output.voltage" data-unit=" V"></dd></div>
|
|
</dl>
|
|
</main>
|
|
<script>
|
|
const source = document.querySelector('#source');
|
|
const status = document.querySelector('#status');
|
|
const headline = document.querySelector('#headline');
|
|
const lastOK = document.querySelector('#lastOK');
|
|
const detail = document.querySelector('#detail');
|
|
const readings = document.querySelector('#readings');
|
|
const metrics = readings.querySelectorAll('[data-key]');
|
|
let latest;
|
|
|
|
function render(snapshot, requestError = '') {
|
|
const live = !requestError && snapshot?.health === 'live';
|
|
const waiting = !requestError && snapshot?.health === 'waiting';
|
|
const flags = (snapshot?.values?.['ups.status'] ?? '').split(/\s+/);
|
|
const low = flags.includes('LB');
|
|
const battery = flags.includes('OB');
|
|
const online = flags.includes('OL');
|
|
status.dataset.tone = waiting ? '' : live && online && !low && !battery ? 'ok' : 'warn';
|
|
headline.textContent = waiting ? '正在连接' : !live ? '通讯中断' :
|
|
low ? '电池电量低' : battery ? '电池供电' : online ? '市电供电' : `UPS 状态 ${flags.join(' ')}`;
|
|
source.textContent = snapshot?.source ?? '';
|
|
lastOK.textContent = snapshot?.lastOK ?
|
|
`${live ? '更新于' : '历史读数 · 最后读取于'} ${new Date(snapshot.lastOK).toLocaleString()}` :
|
|
'等待首次有效读数';
|
|
detail.textContent = requestError || snapshot?.error || '';
|
|
readings.hidden = !snapshot?.values;
|
|
for (const metric of metrics) {
|
|
const value = snapshot?.values?.[metric.dataset.key];
|
|
metric.textContent = value ? value + metric.dataset.unit : '—';
|
|
}
|
|
}
|
|
|
|
async function refresh() {
|
|
try {
|
|
const response = await fetch('/api/status', { signal: AbortSignal.timeout(4000) });
|
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
latest = await response.json();
|
|
render(latest);
|
|
} catch {
|
|
render(latest, '无法连接监控服务');
|
|
} finally {
|
|
setTimeout(refresh, 2000);
|
|
}
|
|
}
|
|
refresh();
|
|
</script>
|
|
</body>
|
|
</html>
|