644 lines
22 KiB
HTML
644 lines
22 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PicUpper Gallery</title>
|
|
<style>
|
|
:root {
|
|
--bg-color: #1a1a1a;
|
|
--card-bg: #2d2d2d;
|
|
--text-color: #e0e0e0;
|
|
--accent-color: #3b82f6;
|
|
--error-color: #ef4444;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
background-color: var(--bg-color);
|
|
color: var(--text-color);
|
|
margin: 0;
|
|
padding: 20px;
|
|
}
|
|
|
|
h1,
|
|
h2,
|
|
h3 {
|
|
color: #fff;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
/* Layout */
|
|
.controls {
|
|
display: flex;
|
|
gap: 20px;
|
|
margin-bottom: 20px;
|
|
background: var(--card-bg);
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
select {
|
|
background: #404040;
|
|
color: white;
|
|
padding: 8px;
|
|
border: 1px solid #555;
|
|
border-radius: 4px;
|
|
font-size: 1rem;
|
|
}
|
|
|
|
/* Grid */
|
|
.grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
|
/* Slightly wider for scrubbing */
|
|
gap: 15px;
|
|
}
|
|
|
|
.card {
|
|
background: var(--card-bg);
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
transition: transform 0.2s;
|
|
cursor: pointer;
|
|
position: relative;
|
|
}
|
|
|
|
.card:hover {
|
|
transform: translateY(-5px);
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
|
|
}
|
|
|
|
.card img {
|
|
width: 100%;
|
|
height: 180px;
|
|
/* Taller for better view */
|
|
object-fit: cover;
|
|
display: block;
|
|
pointer-events: none;
|
|
/* Let clicks pass to card */
|
|
}
|
|
|
|
.card-info {
|
|
padding: 10px;
|
|
font-size: 0.9rem;
|
|
color: #ccc;
|
|
background: rgba(0, 0, 0, 0.6);
|
|
position: absolute;
|
|
bottom: 0;
|
|
width: 100%;
|
|
}
|
|
|
|
/* Modal */
|
|
.modal {
|
|
display: none;
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.9);
|
|
justify-content: center;
|
|
align-items: center;
|
|
z-index: 100;
|
|
}
|
|
|
|
.modal.active {
|
|
display: flex;
|
|
}
|
|
|
|
.modal img {
|
|
max-width: 90%;
|
|
max-height: 90vh;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.modal-close {
|
|
position: absolute;
|
|
top: 20px;
|
|
right: 30px;
|
|
color: white;
|
|
font-size: 3rem;
|
|
cursor: pointer;
|
|
}
|
|
|
|
/* Loading */
|
|
.loader {
|
|
display: none;
|
|
text-align: center;
|
|
padding: 20px;
|
|
width: 100%;
|
|
}
|
|
|
|
.empty {
|
|
text-align: center;
|
|
color: #888;
|
|
margin-top: 50px;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="container">
|
|
<h1>📷 PicUpper Gallery</h1>
|
|
|
|
<div class="controls">
|
|
<div>
|
|
<label>Camera:</label>
|
|
<select id="cameraSelect">
|
|
<!-- Populated via API -->
|
|
<option value="">Loading cameras...</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div>
|
|
<label>Date:</label>
|
|
<select id="dateSelect" disabled>
|
|
<option value="">Select a camera first</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div style="margin-left: auto;">
|
|
<button id="refreshBtn" onclick="loadImages()">Refresh</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="loader" class="loader">Loading images...</div>
|
|
<div id="grid" class="grid"></div>
|
|
<div id="empty" class="empty">Select a camera and date to view images</div>
|
|
|
|
<!-- Brightness Chart -->
|
|
<div id="chartContainer"
|
|
style="margin-top: 40px; background: var(--card-bg); padding: 20px; border-radius: 8px; display: none;">
|
|
<h3 id="chartTitle">Brightness Levels</h3>
|
|
<canvas id="brightnessChart" style="width: 100%; height: 200px;"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Lightbox Modal -->
|
|
<div id="modal" class="modal" onclick="closeModal()">
|
|
<span class="modal-close">×</span>
|
|
<img id="modalImg" src="" alt="Full size">
|
|
</div>
|
|
|
|
<script>
|
|
// State
|
|
const state = {
|
|
cameras: [],
|
|
selectedCamera: localStorage.getItem('lastCamera') || '',
|
|
selectedDate: localStorage.getItem('lastDate') || '',
|
|
currentSettings: {}
|
|
};
|
|
|
|
// DOM Elements
|
|
const cameraSelect = document.getElementById('cameraSelect');
|
|
const dateSelect = document.getElementById('dateSelect');
|
|
const grid = document.getElementById('grid');
|
|
const loader = document.getElementById('loader');
|
|
const emptyMsg = document.getElementById('empty');
|
|
const modal = document.getElementById('modal');
|
|
const modalImg = document.getElementById('modalImg');
|
|
|
|
// Init
|
|
async function init() {
|
|
if (!await checkAuth()) return; // Simple check if API key needed (UI is public read locally)
|
|
await loadCameras();
|
|
}
|
|
|
|
// Authenticated? (For this internal tool, we assume read access is open or basic auth handled by nginx if tailored later)
|
|
async function checkAuth() {
|
|
// Since we exposed uploads and api publicly in server.js, try listing cameras via API
|
|
// NOTE: The API requires authentication. For this web UI to work publicly,
|
|
// we either need to loosen auth for GET requests or prompt for API key.
|
|
// Let's implement an API Key prompt if fetch fails with 401.
|
|
|
|
const apiKey = localStorage.getItem('apiKey');
|
|
if (!apiKey) {
|
|
const key = prompt("Enter API Key to view gallery:");
|
|
if (key) {
|
|
localStorage.setItem('apiKey', key);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function getHeaders() {
|
|
return { 'X-API-Key': localStorage.getItem('apiKey') || '' };
|
|
}
|
|
|
|
async function loadCameras() {
|
|
try {
|
|
const res = await fetch('cameras', { headers: getHeaders() });
|
|
if (res.status === 401) {
|
|
localStorage.removeItem('apiKey');
|
|
location.reload();
|
|
return;
|
|
}
|
|
const data = await res.json();
|
|
state.cameras = data.cameras;
|
|
|
|
cameraSelect.innerHTML = '<option value="">Select Camera</option>';
|
|
state.cameras.forEach(cam => {
|
|
const opt = document.createElement('option');
|
|
opt.value = cam;
|
|
opt.textContent = cam;
|
|
if (cam === state.selectedCamera) opt.selected = true;
|
|
cameraSelect.appendChild(opt);
|
|
});
|
|
|
|
cameraSelect.disabled = false;
|
|
|
|
if (state.selectedCamera) {
|
|
loadDates(state.selectedCamera);
|
|
loadSettings(state.selectedCamera);
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
cameraSelect.innerHTML = '<option>Error loading cameras</option>';
|
|
}
|
|
}
|
|
|
|
async function loadDates(cameraId) {
|
|
try {
|
|
dateSelect.innerHTML = '<option>Loading...</option>';
|
|
dateSelect.disabled = true;
|
|
|
|
const res = await fetch(`cameras/${cameraId}/dates`, { headers: getHeaders() });
|
|
|
|
const data = await res.json();
|
|
|
|
dateSelect.innerHTML = '<option value="">Select Date</option>';
|
|
data.dates.forEach(date => {
|
|
const opt = document.createElement('option');
|
|
opt.value = date;
|
|
opt.textContent = date;
|
|
if (date === state.selectedDate) opt.selected = true;
|
|
dateSelect.appendChild(opt);
|
|
});
|
|
|
|
dateSelect.disabled = false;
|
|
|
|
if (state.selectedDate && data.dates.includes(state.selectedDate)) {
|
|
loadImages();
|
|
} else if (data.dates.length > 0) {
|
|
// Auto select newest
|
|
dateSelect.value = data.dates[0];
|
|
state.selectedDate = data.dates[0];
|
|
loadImages();
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
dateSelect.innerHTML = '<option>Error loading dates</option>';
|
|
}
|
|
}
|
|
|
|
async function loadSettings(cameraId) {
|
|
try {
|
|
const res = await fetch(`settings/${cameraId}`, { headers: getHeaders() });
|
|
const data = await res.json();
|
|
state.currentSettings = data.settings || {};
|
|
} catch (err) {
|
|
console.error('Failed to load settings', err);
|
|
state.currentSettings = {};
|
|
}
|
|
}
|
|
|
|
async function loadImages(isAutoUpdate = false) {
|
|
const cameraId = cameraSelect.value;
|
|
const dateStr = dateSelect.value; // YYYY-MM-DD
|
|
|
|
if (!cameraId || !dateStr) return;
|
|
|
|
// Update state/localstorage
|
|
state.selectedCamera = cameraId;
|
|
state.selectedDate = dateStr;
|
|
localStorage.setItem('lastCamera', cameraId);
|
|
localStorage.setItem('lastDate', dateStr);
|
|
|
|
if (!isAutoUpdate) {
|
|
loader.style.display = 'block';
|
|
grid.innerHTML = '';
|
|
emptyMsg.style.display = 'none';
|
|
document.getElementById('chartContainer').style.display = 'none';
|
|
}
|
|
|
|
const [year, month, day] = dateStr.split('-');
|
|
|
|
try {
|
|
const res = await fetch(`cameras/${cameraId}/${year}/${month}/${day}`);
|
|
const data = await res.json();
|
|
|
|
if (!isAutoUpdate) {
|
|
loader.style.display = 'none';
|
|
}
|
|
|
|
if (data.images.length === 0) {
|
|
if (!isAutoUpdate) emptyMsg.style.display = 'block';
|
|
return;
|
|
}
|
|
|
|
// For auto-update, assume we have data now, clear grid to rebuild
|
|
if (isAutoUpdate) {
|
|
grid.innerHTML = '';
|
|
}
|
|
|
|
// Group by hour
|
|
const hours = {};
|
|
data.images.forEach(img => {
|
|
let h = '00';
|
|
try {
|
|
// Parse UTC timestamp from filename and convert to local time
|
|
const parts = img.timestamp.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2})[-:](\d{2})[-:](\d{2})/);
|
|
if (parts) {
|
|
const [_, y, m, d, hUtc, mUtc, sUtc] = parts;
|
|
const date = new Date(Date.UTC(y, m - 1, d, hUtc, mUtc, sUtc));
|
|
h = String(date.getHours()).padStart(2, '0');
|
|
} else {
|
|
const d = new Date(img.timestamp);
|
|
if (!isNaN(d.getTime())) h = String(d.getHours()).padStart(2, '0');
|
|
}
|
|
} catch (e) { }
|
|
|
|
if (!hours[h]) hours[h] = [];
|
|
hours[h].push(img);
|
|
});
|
|
|
|
// Render Hour Cards
|
|
Object.keys(hours).sort().forEach(hour => {
|
|
const group = hours[hour];
|
|
// Sort images in this hour
|
|
group.sort((a, b) => a.filename.localeCompare(b.filename));
|
|
|
|
const card = document.createElement('div');
|
|
card.className = 'card';
|
|
const lastIdx = group.length - 1;
|
|
card.dataset.idx = lastIdx; // Default to last (newest)
|
|
|
|
const thumb = document.createElement('img');
|
|
thumb.src = group[lastIdx].thumbnailUrl;
|
|
|
|
const info = document.createElement('div');
|
|
info.className = 'card-info';
|
|
info.textContent = `${hour}:00 (${group.length} pics)`;
|
|
|
|
// Hover scrubbing logic
|
|
card.onmousemove = (e) => {
|
|
if (group.length <= 1) return;
|
|
|
|
const rect = card.getBoundingClientRect();
|
|
const x = e.clientX - rect.left;
|
|
const width = rect.width;
|
|
const percent = Math.max(0, Math.min(1, x / width));
|
|
const index = Math.floor(percent * group.length);
|
|
const safeIndex = Math.min(group.length - 1, Math.max(0, index));
|
|
|
|
if (card.dataset.idx != safeIndex) {
|
|
thumb.src = group[safeIndex].thumbnailUrl;
|
|
card.dataset.idx = safeIndex;
|
|
info.textContent = `${hour}:00 - ` + getPrettyTime(group[safeIndex].timestamp);
|
|
}
|
|
};
|
|
|
|
card.onmouseleave = () => {
|
|
const lastIdx = group.length - 1;
|
|
if (card.dataset.idx != lastIdx) {
|
|
thumb.src = group[lastIdx].thumbnailUrl;
|
|
card.dataset.idx = lastIdx;
|
|
info.textContent = `${hour}:00 (${group.length} pics)`;
|
|
}
|
|
};
|
|
|
|
card.onclick = () => {
|
|
const idx = parseInt(card.dataset.idx || '0');
|
|
openModal(group[idx].url);
|
|
};
|
|
|
|
card.appendChild(thumb);
|
|
card.appendChild(info);
|
|
grid.appendChild(card);
|
|
});
|
|
|
|
// Draw brightness chart if stats exist
|
|
if (data.stats && data.stats.length > 0) {
|
|
document.getElementById('chartContainer').style.display = 'block';
|
|
drawChart(data.stats);
|
|
}
|
|
|
|
} catch (err) {
|
|
loader.style.display = 'none';
|
|
console.error(err);
|
|
grid.innerHTML = '<div style="color:red">Error loading images</div>';
|
|
document.getElementById('chartContainer').style.display = 'none';
|
|
}
|
|
}
|
|
|
|
function drawChart(stats) {
|
|
const canvas = document.getElementById('brightnessChart');
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
// Resize canvas to match display size
|
|
const rect = canvas.parentNode.getBoundingClientRect();
|
|
canvas.width = rect.width;
|
|
canvas.height = 200;
|
|
|
|
const width = canvas.width;
|
|
const height = canvas.height;
|
|
const padding = { top: 20, right: 30, bottom: 30, left: 45 };
|
|
|
|
ctx.clearRect(0, 0, width, height);
|
|
|
|
// Sort stats by timestamp just in case
|
|
stats.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
|
|
// Determine data source and range
|
|
// If any entry has 'ocr_val', prefer that.
|
|
const hasOcr = stats.some(s => s.ocr_val !== undefined && s.ocr_val !== null);
|
|
const valueKey = hasOcr ? 'ocr_val' : 'brightness';
|
|
|
|
// Clean data (filter nulls)
|
|
const validPoints = stats
|
|
.filter(s => s[valueKey] !== undefined && s[valueKey] !== null)
|
|
.map(s => ({
|
|
t: s.timestamp,
|
|
v: Number(s[valueKey])
|
|
}));
|
|
|
|
if (validPoints.length === 0) return;
|
|
|
|
let minY = Math.min(...validPoints.map(p => p.v));
|
|
let maxY = Math.max(...validPoints.map(p => p.v));
|
|
|
|
// Add some padding to Y range
|
|
const range = maxY - minY;
|
|
if (range === 0) {
|
|
minY -= 10;
|
|
maxY += 10;
|
|
} else {
|
|
minY -= range * 0.1;
|
|
maxY += range * 0.1;
|
|
}
|
|
// For brightness, clamp to 0-255 if not OCR?
|
|
if (!hasOcr) {
|
|
minY = 0;
|
|
maxY = 255;
|
|
}
|
|
|
|
// Helper to parsing time to minutes from start of day (local time)
|
|
const getMinutes = (iso) => {
|
|
// Parse UTC timestamp to Date object
|
|
const parts = iso.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2})[-:](\d{2})[-:](\d{2})/);
|
|
let d;
|
|
if (parts) {
|
|
const [_, y, m, day, hUtc, mUtc, sUtc] = parts;
|
|
d = new Date(Date.UTC(y, m - 1, day, hUtc, mUtc, sUtc));
|
|
} else {
|
|
d = new Date(iso);
|
|
}
|
|
return d.getHours() * 60 + d.getMinutes();
|
|
};
|
|
|
|
const points = validPoints.map(p => ({
|
|
x: getMinutes(p.t),
|
|
y: p.v
|
|
}));
|
|
|
|
// Scales
|
|
const mapX = (minutes) => padding.left + (minutes / 1440) * (width - padding.left - padding.right);
|
|
// Invert Y (0 at bottom)
|
|
const mapY = (val) => height - padding.bottom - ((val - minY) / (maxY - minY)) * (height - padding.top - padding.bottom);
|
|
|
|
// Draw Axes
|
|
ctx.strokeStyle = '#555';
|
|
ctx.lineWidth = 1;
|
|
|
|
// X-Axis
|
|
ctx.beginPath();
|
|
ctx.moveTo(padding.left, height - padding.bottom);
|
|
ctx.lineTo(width - padding.right, height - padding.bottom);
|
|
ctx.stroke();
|
|
|
|
// Y-Axis
|
|
ctx.beginPath();
|
|
ctx.moveTo(padding.left, padding.top);
|
|
ctx.lineTo(padding.left, height - padding.bottom);
|
|
ctx.stroke();
|
|
|
|
// Draw Line
|
|
ctx.strokeStyle = hasOcr ? '#10b981' : '#3b82f6'; // Green for OCR, Blue for Brightness
|
|
ctx.lineWidth = 2;
|
|
ctx.beginPath();
|
|
|
|
if (points.length > 0) {
|
|
ctx.moveTo(mapX(points[0].x), mapY(points[0].y));
|
|
for (let i = 1; i < points.length; i++) {
|
|
ctx.lineTo(mapX(points[i].x), mapY(points[i].y));
|
|
}
|
|
}
|
|
ctx.stroke();
|
|
|
|
// Draw Dots
|
|
ctx.fillStyle = hasOcr ? '#10b981' : '#3b82f6';
|
|
for (const p of points) {
|
|
ctx.beginPath();
|
|
ctx.arc(mapX(p.x), mapY(p.y), 3, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
}
|
|
|
|
// Draw Labels (Time)
|
|
ctx.fillStyle = '#ccc';
|
|
ctx.font = '12px sans-serif';
|
|
ctx.textAlign = 'center';
|
|
|
|
for (let h = 0; h <= 24; h += 4) {
|
|
const x = mapX(h * 60);
|
|
if (x > padding.left) {
|
|
ctx.fillText(`${h}:00`, x, height - 10);
|
|
}
|
|
}
|
|
|
|
// Draw Labels (Value)
|
|
ctx.textAlign = 'right';
|
|
ctx.textBaseline = 'middle';
|
|
// Draw 5 ticks
|
|
for (let i = 0; i <= 4; i++) {
|
|
const val = minY + (i / 4) * (maxY - minY);
|
|
const y = mapY(val);
|
|
ctx.fillText(Math.round(val), padding.left - 5, y);
|
|
}
|
|
|
|
// Title
|
|
ctx.save();
|
|
ctx.translate(15, height / 2);
|
|
ctx.rotate(-Math.PI / 2);
|
|
ctx.textAlign = 'center';
|
|
ctx.textAlign = 'center';
|
|
const label = state.currentSettings.chartLabel || (hasOcr ? "Value" : "Brightness");
|
|
ctx.fillText(label, 0, 0);
|
|
|
|
// Update Header too
|
|
const titleEl = document.getElementById('chartTitle');
|
|
if (titleEl) titleEl.textContent = label;
|
|
|
|
ctx.restore();
|
|
}
|
|
|
|
function getPrettyTime(ts) {
|
|
const parts = ts.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2})[-:](\d{2})[-:](\d{2})/);
|
|
if (parts) {
|
|
const [_, y, m, d, hUtc, mUtc, sUtc] = parts;
|
|
const date = new Date(Date.UTC(y, m - 1, d, hUtc, mUtc, sUtc));
|
|
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`;
|
|
}
|
|
return ts;
|
|
}
|
|
|
|
// Modal
|
|
function openModal(src) {
|
|
modalImg.src = src;
|
|
modal.classList.add('active');
|
|
}
|
|
|
|
function closeModal() {
|
|
modal.classList.remove('active');
|
|
setTimeout(() => modalImg.src = '', 200);
|
|
}
|
|
|
|
// Listeners
|
|
cameraSelect.addEventListener('change', (e) => {
|
|
state.selectedCamera = e.target.value;
|
|
if (state.selectedCamera) {
|
|
loadDates(state.selectedCamera);
|
|
loadSettings(state.selectedCamera);
|
|
} else dateSelect.disabled = true;
|
|
});
|
|
|
|
dateSelect.addEventListener('change', (e) => {
|
|
state.selectedDate = e.target.value;
|
|
if (state.selectedDate) loadImages();
|
|
});
|
|
|
|
// Auto-update every 60 seconds
|
|
setInterval(() => {
|
|
if (state.selectedCamera && state.selectedDate) {
|
|
// Only auto-update if we are looking at something
|
|
loadImages(true);
|
|
}
|
|
}, 60000);
|
|
|
|
// Start
|
|
init();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html> |