Files
traffic-monitoring-new/templates/index.html
2025-07-16 20:22:18 +08:00

143 lines
3.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Traffic Monitor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 30px;
background-color: #f4f4f4;
}
h1 {
color: #333;
}
#stats {
margin-bottom: 20px;
}
.stat {
font-size: 18px;
margin-bottom: 10px;
}
table {
border-collapse: collapse;
width: 100%;
background-color: white;
}
th,
td {
border: 1px solid #ccc;
padding: 8px 12px;
text-align: center;
}
th {
background-color: #eee;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
.loading {
color: #888;
}
</style>
</head>
<body>
<h1>Monitoring Trafik</h1>
<div id="stats">
<div class="stat">🔢 Total Entri Log Kendaraan: <span id="total-entries" class="loading">Loading...</span></div>
<div class="stat">🚗 Kendaraan yang Saat Ini Berada di Antara Jalur: <span id="vehicle-zone"
class="loading">Loading...</span></div>
</div>
<h2>📋 10 Entri Kendaraan Terakhir</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>ID Kenderaan</th>
<th>Arah</th>
<th>Waktu</th>
<th>Kecepatan (km/h)</th>
</tr>
</thead>
<tbody id="log-table-body">
<tr>
<td colspan="5" class="loading">Loading data...</td>
</tr>
</tbody>
</table>
<script>
function formatToMakassar(utcStr) {
const options = {
timeZone: 'Asia/Makassar',
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
weekday: 'short',
hour12: false,
};
const date = new Date(utcStr);
const formatter = new Intl.DateTimeFormat('en-US', options);
const parts = formatter.formatToParts(date);
const get = (type) => parts.find(p => p.type === type)?.value;
return `${get('weekday')}, ${get('day')} ${get('month')} ${get('year')} ${get('hour')}:${get('minute')}:${get('second')} GMT+8`;
}
async function loadData() {
try {
const response = await fetch('/get_video');
const data = await response.json();
// Update stats
document.getElementById('total-entries').textContent = data.total_entries;
document.getElementById('vehicle-zone').textContent = data.vehicles_in_zone;
// Populate table
const tbody = document.getElementById('log-table-body');
tbody.innerHTML = '';
data.last_10_entries.forEach(entry => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${entry.id}</td>
<td>${entry.vehicle_id}</td>
<td>${entry.direction}</td>
<td>${formatToMakassar(entry.timestamp)}</td>
<td>${entry.speed.toFixed(1)}</td>
`;
tbody.appendChild(row);
});
} catch (err) {
console.error('Error loading data:', err);
document.getElementById('log-table-body').innerHTML = `<tr><td colspan="5">Error loading data</td></tr>`;
}
}
// Initial load
loadData();
// Refresh every 10 seconds
setInterval(loadData, 5000);
</script>
</body>
</html>