- 新增 /api/monitor/video-status 接口(复用 FetchVideoSourceStatus) - console.html: 设备视图+视频视图视频窗口正中显示状态覆盖层 离线(红字+原因)/连接中/在线(隐藏)/状态未知 - monitor.html: 视频监控页同样处理,HLS 播放失败显示'无视频输出' - HLS fatal 错误兜底提示(即使状态接口不可用)
143 lines
5.7 KiB
HTML
143 lines
5.7 KiB
HTML
{{define "monitor"}}
|
|
<div class="card">
|
|
<div class="section-title">
|
|
<div>
|
|
<h2 class="title-with-icon">{{icon "devices"}}<span>视频监控</span></h2>
|
|
<div class="form-hint">所有在线设备的实时画面</div>
|
|
</div>
|
|
<div class="actions compact">
|
|
<button class="btn ghost" type="button" onclick="loadAll()">刷新</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="video-wall" style="display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px;margin-top:16px">
|
|
<div class="card muted" style="text-align:center;grid-column:1/-1">加载中…</div>
|
|
</div>
|
|
|
|
<script src="/assets/vendor/hls.min.js"></script>
|
|
<script>
|
|
function loadAll() {
|
|
var wall = document.getElementById("video-wall");
|
|
wall.innerHTML = '<div class="card muted" style="text-align:center;grid-column:1/-1">加载中…</div>';
|
|
|
|
var devices = [
|
|
{{range .Devices}}{{if .Online}}
|
|
{id:"{{.DeviceID}}",name:"{{.DisplayName}}"},
|
|
{{end}}{{end}}
|
|
];
|
|
if (devices.length === 0) {
|
|
wall.innerHTML = '<div class="card muted" style="text-align:center;grid-column:1/-1">暂无在线设备</div>';
|
|
return;
|
|
}
|
|
|
|
var promises = devices.map(function(dev) {
|
|
return fetch('/api/monitor/channels?device_id=' + encodeURIComponent(dev.id))
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) { return (data.channels||[]).map(function(ch) { ch._dev = dev.name; ch._devId = dev.id; return ch; }); })
|
|
.catch(function() { return []; });
|
|
});
|
|
|
|
Promise.all(promises).then(function(results) {
|
|
var all = [];
|
|
results.forEach(function(chs) { all = all.concat(chs); });
|
|
if (all.length === 0) {
|
|
wall.innerHTML = '<div class="card muted" style="text-align:center;grid-column:1/-1">没有可预览的通道</div>';
|
|
return;
|
|
}
|
|
var cols = all.length <= 2 ? all.length : 2;
|
|
wall.style.gridTemplateColumns = "repeat(" + cols + ",minmax(0,1fr))";
|
|
var html = "";
|
|
all.forEach(function(ch, i) {
|
|
var proxyUrl = '/hls/' + ch._devId + '/hls/' + ch.name + '/index.m3u8';
|
|
html += '<div class="card" style="padding:8px;position:relative" data-device-id="' + ch._devId + '" data-channel="' + esc(ch.name) + '">';
|
|
html += '<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px">';
|
|
html += '<span style="font-size:12px;font-weight:500">' + esc(ch._dev) + ' · ' + esc(ch.name) + '</span>';
|
|
html += '</div>';
|
|
if (ch.hls_url) {
|
|
html += '<video id="v' + i + '" controls autoplay muted style="width:100%;max-height:340px;background:#000;position:relative;z-index:1"></video>';
|
|
} else {
|
|
html += '<div class="muted" style="height:200px;display:flex;align-items:center;justify-content:center">无预览地址</div>';
|
|
}
|
|
html += '</div>';
|
|
});
|
|
wall.innerHTML = html;
|
|
// 视频源状态覆盖层
|
|
function esc2(s) { return esc(s); }
|
|
function applyStatus(card, status, hlsError) {
|
|
var overlay = card.querySelector('.video-status-overlay');
|
|
if (!overlay) {
|
|
overlay = document.createElement('div');
|
|
overlay.className = 'video-status-overlay';
|
|
card.appendChild(overlay);
|
|
}
|
|
if (hlsError) {
|
|
overlay.innerHTML = '<div class="video-status-text offline">无视频输出</div><div class="video-status-detail">HLS 流不可用,请检查视频源</div>';
|
|
overlay.style.display = 'flex';
|
|
return;
|
|
}
|
|
if (!status) {
|
|
overlay.innerHTML = '<div class="video-status-text unknown">状态未知</div>';
|
|
overlay.style.display = 'flex';
|
|
return;
|
|
}
|
|
if (status.streaming && status.input_fps > 0) {
|
|
overlay.style.display = 'none';
|
|
return;
|
|
}
|
|
if (status.streaming) {
|
|
overlay.innerHTML = '<div class="video-status-text connecting">连接中</div>';
|
|
overlay.style.display = 'flex';
|
|
return;
|
|
}
|
|
overlay.innerHTML = '<div class="video-status-text offline">离线</div>' +
|
|
(status.last_error ? '<div class="video-status-detail">' + esc(status.last_error) + '</div>' : '');
|
|
overlay.style.display = 'flex';
|
|
}
|
|
|
|
// 拉取各设备视频源状态
|
|
var statusByDev = {};
|
|
devices.forEach(function(dev) {
|
|
fetch('/api/monitor/video-status?device_id=' + encodeURIComponent(dev.id))
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
var byName = {};
|
|
(data.sources || []).forEach(function(s) { byName[s.graph_name] = s; });
|
|
statusByDev[dev.id] = byName;
|
|
all.forEach(function(ch) {
|
|
if (ch._devId !== dev.id) return;
|
|
var card = wall.querySelector('[data-device-id="' + ch._devId + '"][data-channel="' + cssEscape(ch.name) + '"]');
|
|
if (card) applyStatus(card, byName[ch.name], false);
|
|
});
|
|
})
|
|
.catch(function() {});
|
|
});
|
|
|
|
// Initialize HLS players
|
|
all.forEach(function(ch, i) {
|
|
if (!ch.hls_url || !ch._devId) return;
|
|
var proxyUrl = '/hls/' + ch._devId + '/hls/' + ch.name + '/index.m3u8';
|
|
var video = document.getElementById('v' + i);
|
|
if (!video) return;
|
|
if (Hls.isSupported()) {
|
|
var hls = new Hls();
|
|
hls.on(Hls.Events.ERROR, function(evt, data) {
|
|
if (data.fatal) {
|
|
var card = video.closest('.card');
|
|
if (card) applyStatus(card, null, true);
|
|
}
|
|
});
|
|
hls.loadSource(proxyUrl);
|
|
hls.attachMedia(video);
|
|
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
|
video.src = ch.hls_url;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
function cssEscape(s) { return String(s).replace(/["\\]/g, '\\$&'); }
|
|
function esc(s) { return (s||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""); }
|
|
loadAll();
|
|
</script>
|
|
{{end}}
|