- client 模式抓取水星 MW305R DHCP 列表,记录上下线,本地看板 - server 模式收上报,按站点分组持久化,多站点看板,7天滚动 - ESP8266 低功耗固件抓原始 HTML 上报 /api/raw,服务器端解析 - 部署脚本+systemd 单元+文档,交叉编译 Linux 二进制
195 lines
8.8 KiB
HTML
195 lines
8.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>DHCP 多站点看板</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0f172a; --card: #1e293b; --border: #334155;
|
|
--text: #e2e8f0; --muted: #94a3b8;
|
|
--green: #22c55e; --red: #ef4444; --blue: #3b82f6; --amber: #f59e0b;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { background: var(--bg); color: var(--text); font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; padding: 20px; }
|
|
.top { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
|
|
h1 { font-size: 22px; }
|
|
.sub { color: var(--muted); font-size: 13px; }
|
|
.sitebar { display: flex; gap: 8px; flex-wrap: wrap; }
|
|
.sitebtn { background: var(--card); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-size: 13px; }
|
|
.sitebtn:hover { border-color: var(--blue); }
|
|
.sitebtn.active { background: var(--blue); border-color: var(--blue); color: #fff; }
|
|
.sitebtn .cnt { margin-left: 6px; color: var(--muted); font-size: 12px; }
|
|
.sitebtn.active .cnt { color: rgba(255,255,255,.8); }
|
|
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 24px; }
|
|
.stat { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
|
|
.stat .num { font-size: 26px; font-weight: 700; }
|
|
.stat .lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }
|
|
.num.online { color: var(--green); } .num.offline { color: var(--red); } .num.total { color: var(--blue); }
|
|
.num.events { color: var(--amber); }
|
|
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 24px; }
|
|
.card h2 { font-size: 15px; margin-bottom: 12px; color: var(--muted); font-weight: 600; }
|
|
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
|
th { text-align: left; color: var(--muted); font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
|
|
td { padding: 8px 10px; border-bottom: 1px solid rgba(51,65,85,.4); white-space: nowrap; }
|
|
tr:hover td { background: rgba(59,130,246,.06); }
|
|
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
|
|
.badge.online { background: rgba(34,197,94,.15); color: var(--green); }
|
|
.badge.offline { background: rgba(239,68,68,.15); color: var(--red); }
|
|
.badge.stale { background: rgba(245,158,11,.15); color: var(--amber); }
|
|
.mac { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
.muted { color: var(--muted); }
|
|
.evt { display: flex; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid rgba(51,65,85,.3); font-size: 13px; }
|
|
.evt:last-child { border-bottom: none; }
|
|
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
|
|
.dot.up { background: var(--green); } .dot.down { background: var(--red); }
|
|
.evt .time { color: var(--muted); white-space: nowrap; }
|
|
.evt .name { font-weight: 600; }
|
|
.evt .detail { color: var(--muted); font-size: 12px; }
|
|
.footer { color: var(--muted); font-size: 12px; text-align: center; padding: 10px 0 30px; }
|
|
.scroll { max-height: 460px; overflow-y: auto; }
|
|
.empty { color: var(--muted); padding: 20px 0; text-align: center; }
|
|
@media (max-width: 640px) { body { padding: 10px; } table { font-size: 12px; } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="top">
|
|
<div>
|
|
<h1>📡 DHCP 多站点看板</h1>
|
|
<div class="sub" id="sub"></div>
|
|
</div>
|
|
<div class="sitebar" id="sites"></div>
|
|
</div>
|
|
|
|
<div class="stats">
|
|
<div class="stat"><div class="num online" id="st-online">-</div><div class="lbl">在线设备</div></div>
|
|
<div class="stat"><div class="num offline" id="st-offline">-</div><div class="lbl">离线设备</div></div>
|
|
<div class="stat"><div class="num total" id="st-total">-</div><div class="lbl">设备总数</div></div>
|
|
<div class="stat"><div class="num events" id="st-events">-</div><div class="lbl">事件总数</div></div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2 id="dev-title">设备列表</h2>
|
|
<table>
|
|
<thead><tr>
|
|
<th>状态</th><th>设备名</th><th>MAC 地址</th><th>IP 地址</th>
|
|
<th>首次发现</th><th>最近上线</th><th>最近下线</th><th>在线次数</th><th>累计在线</th>
|
|
</tr></thead>
|
|
<tbody id="devices"></tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>最近事件</h2>
|
|
<div class="scroll" id="events"></div>
|
|
</div>
|
|
|
|
<div class="footer" id="footer"></div>
|
|
|
|
<script>
|
|
const $ = id => document.getElementById(id);
|
|
const fmtTime = ts => {
|
|
if (!ts) return '-';
|
|
const d = new Date(ts);
|
|
const p = n => String(n).padStart(2, '0');
|
|
return `${d.getMonth()+1}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
};
|
|
const fmtDur = sec => {
|
|
sec = Number(sec) || 0;
|
|
const h = Math.floor(sec/3600), m = Math.floor(sec%3600/60);
|
|
return h > 0 ? `${h}小时${m}分` : `${m}分钟`;
|
|
};
|
|
const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
|
|
let currentSite = null;
|
|
let siteMap = {};
|
|
|
|
// 从 URL 读取 token(?token=xxx),所有 API 请求带上
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const AUTH_TOKEN = urlParams.get('token') || '';
|
|
const api = path => {
|
|
const sep = path.includes('?') ? '&' : '?';
|
|
return AUTH_TOKEN ? path + sep + 'token=' + encodeURIComponent(AUTH_TOKEN) : path;
|
|
};
|
|
|
|
async function loadSites() {
|
|
try {
|
|
const data = await fetch(api('/api/sites')).then(r => r.json());
|
|
siteMap = {};
|
|
(data.sites || []).forEach(s => siteMap[s.site] = s);
|
|
$('sub').textContent = `数据保留 ${data.retain_days || 7} 天 · 站点离线判定: 3 分钟无上报`;
|
|
|
|
const bar = $('sites');
|
|
bar.innerHTML = '';
|
|
(data.sites || []).forEach(s => {
|
|
const btn = document.createElement('button');
|
|
btn.className = 'sitebtn' + (s.site === currentSite ? ' active' : '');
|
|
btn.innerHTML = `${esc(s.site)} <span class="cnt">${s.online_count}/${s.device_count}</span>`;
|
|
btn.onclick = () => { currentSite = s.site; loadSite(); loadSites(); };
|
|
bar.appendChild(btn);
|
|
});
|
|
if (!currentSite && data.sites && data.sites.length) {
|
|
currentSite = data.sites[0].site;
|
|
loadSite();
|
|
}
|
|
} catch (e) {
|
|
$('sub').textContent = '加载站点失败: ' + e.message;
|
|
}
|
|
}
|
|
|
|
async function loadSite() {
|
|
if (!currentSite) {
|
|
$('devices').innerHTML = '<tr><td colspan="9" class="empty">暂无站点数据</td></tr>';
|
|
$('events').innerHTML = '<div class="empty">暂无事件</div>';
|
|
$('st-online').textContent = $('st-offline').textContent = $('st-total').textContent = $('st-events').textContent = '-';
|
|
return;
|
|
}
|
|
try {
|
|
const data = await fetch(api('/api/site?site=' + encodeURIComponent(currentSite) + '&limit=100')).then(r => r.json());
|
|
$('dev-title').textContent = `设备列表 · ${esc(currentSite)}`;
|
|
|
|
let online = 0, offline = 0;
|
|
(data.devices || []).forEach(d => { if (d.status === 'online') online++; else offline++; });
|
|
|
|
$('st-online').textContent = online;
|
|
$('st-offline').textContent = offline;
|
|
$('st-total').textContent = (data.devices || []).length;
|
|
$('st-events').textContent = (data.events || []).length;
|
|
|
|
$('devices').innerHTML = (data.devices || []).map(d => `
|
|
<tr>
|
|
<td><span class="badge ${d.status === 'online' ? 'online' : 'offline'}">${d.status === 'online' ? '在线' : '离线'}</span></td>
|
|
<td>${esc(d.name || '-')}</td>
|
|
<td class="mac">${esc(d.mac)}</td>
|
|
<td class="mac">${esc(d.last_ip || '-')}</td>
|
|
<td>${fmtTime(d.first_seen)}</td>
|
|
<td>${fmtTime(d.last_online)}</td>
|
|
<td>${fmtTime(d.last_offline)}</td>
|
|
<td>${d.online_count ?? 0}</td>
|
|
<td>${fmtDur(d.total_online_seconds)}</td>
|
|
</tr>`).join('') || '<tr><td colspan="9" class="empty">暂无设备</td></tr>';
|
|
|
|
$('events').innerHTML = (data.events || []).map(e => {
|
|
const up = e.event === 'online';
|
|
return `<div class="evt">
|
|
<span class="dot ${up ? 'up' : 'down'}"></span>
|
|
<span class="time">${fmtTime(e.timestamp)}</span>
|
|
<span class="name">${esc(e.name || '-')}</span>
|
|
<span class="mac muted">${esc(e.mac)}</span>
|
|
<span class="mac muted">${esc(e.ip || '')}</span>
|
|
<span class="detail">${esc(e.detail || '')}</span>
|
|
</div>`;
|
|
}).join('') || '<div class="empty">暂无事件</div>';
|
|
|
|
$('footer').textContent = `当前站点: ${currentSite} · 最近上报 ${fmtTime(data.last_seen)} · 页面自动刷新 · ${new Date().toLocaleString()}`;
|
|
} catch (e) {
|
|
$('footer').textContent = '加载站点失败: ' + e.message;
|
|
}
|
|
}
|
|
|
|
loadSites();
|
|
loadSite();
|
|
setInterval(() => { loadSites(); loadSite(); }, 5000);
|
|
</script>
|
|
</body>
|
|
</html> |