Files
home-automation/apps/simulator/templates/index.html
2025-11-06 12:14:39 +01:00

538 lines
17 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Device Simulator - Monitor</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
min-height: 100vh;
padding: 2rem;
color: #ecf0f1;
}
.container {
max-width: 1400px;
margin: 0 auto;
}
header {
background: rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 2rem;
margin-bottom: 2rem;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
}
h1 {
font-size: 2rem;
margin-bottom: 0.5rem;
}
.status-bar {
display: flex;
gap: 2rem;
margin-top: 1rem;
flex-wrap: wrap;
}
.status-item {
display: flex;
align-items: center;
gap: 0.5rem;
}
.status-indicator {
width: 12px;
height: 12px;
border-radius: 50%;
animation: pulse 2s infinite;
}
.status-indicator.connected {
background: #2ecc71;
box-shadow: 0 0 10px #2ecc71;
}
.status-indicator.disconnected {
background: #e74c3c;
box-shadow: 0 0 10px #e74c3c;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 2rem;
margin-bottom: 2rem;
}
@media (max-width: 1200px) {
.grid {
grid-template-columns: 1fr;
}
}
.panel {
background: rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 1.5rem;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.panel h2 {
font-size: 1.25rem;
margin-bottom: 1rem;
border-bottom: 2px solid rgba(255, 255, 255, 0.3);
padding-bottom: 0.5rem;
}
.device-list {
display: grid;
gap: 0.75rem;
}
.device-item {
background: rgba(0, 0, 0, 0.2);
padding: 0.75rem 1rem;
border-radius: 8px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 0.875rem;
border-left: 3px solid transparent;
}
.device-item.light {
border-left-color: #f39c12;
}
.device-item.thermostat {
border-left-color: #3498db;
}
.events-panel {
grid-column: 1 / -1;
}
.event-list {
max-height: 600px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.event-item {
background: rgba(0, 0, 0, 0.3);
padding: 1rem;
border-radius: 8px;
border-left: 4px solid;
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateX(-20px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.event-item.command_received {
border-left-color: #9b59b6;
}
.event-item.state_published {
border-left-color: #27ae60;
}
.event-item.light_updated {
border-left-color: #f39c12;
}
.event-item.thermostat_updated {
border-left-color: #3498db;
}
.event-item.temperature_drift {
border-left-color: #1abc9c;
}
.event-item.error {
border-left-color: #e74c3c;
}
.event-item.simulator_connected,
.event-item.devices_initialized,
.event-item.subscriptions_complete {
border-left-color: #2ecc71;
}
.event-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;
}
.event-type {
font-weight: 600;
text-transform: uppercase;
font-size: 0.75rem;
letter-spacing: 0.05em;
}
.event-time {
font-size: 0.75rem;
opacity: 0.7;
}
.event-data {
font-family: 'Monaco', 'Courier New', monospace;
font-size: 0.875rem;
white-space: pre-wrap;
word-break: break-all;
background: rgba(0, 0, 0, 0.2);
padding: 0.75rem;
border-radius: 4px;
margin-top: 0.5rem;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.stat-card {
background: rgba(0, 0, 0, 0.2);
padding: 1rem;
border-radius: 8px;
text-align: center;
}
.stat-value {
font-size: 2rem;
font-weight: 700;
margin-bottom: 0.25rem;
}
.stat-label {
font-size: 0.875rem;
opacity: 0.8;
}
.controls {
display: flex;
gap: 1rem;
margin-top: 1rem;
}
button {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
font-size: 0.875rem;
}
button.primary {
background: #3498db;
color: white;
}
button.primary:hover {
background: #2980b9;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(52, 152, 219, 0.4);
}
button.danger {
background: #e74c3c;
color: white;
}
button.danger:hover {
background: #c0392b;
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: rgba(0, 0, 0, 0.2);
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.3);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.5);
}
.empty-state {
text-align: center;
padding: 3rem;
opacity: 0.6;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>🔌 Device Simulator Monitor</h1>
<p>Real-time monitoring of simulated home automation devices</p>
<div class="status-bar">
<div class="status-item">
<div class="status-indicator {% if connected %}connected{% else %}disconnected{% endif %}" id="mqtt-status"></div>
<span>MQTT: <span id="mqtt-text">{{ broker }}</span></span>
</div>
<div class="status-item">
<div class="status-indicator connected" id="app-status"></div>
<span>Simulator: <span id="app-text">Running</span></span>
</div>
<div class="status-item">
<span>💡 {{ light_devices|length }} Lights</span>
</div>
<div class="status-item">
<span>🌡️ {{ thermostat_devices|length }} Thermostats</span>
</div>
</div>
</header>
<div class="grid">
<div class="panel">
<h2>💡 Light Devices</h2>
<div class="device-list">
{% for device_id in light_devices %}
<div class="device-item light">
<strong>{{ device_id }}</strong>
<div id="light-{{ device_id }}" style="margin-top: 0.25rem; opacity: 0.8;">
Power: off, Brightness: 50
</div>
</div>
{% endfor %}
</div>
</div>
<div class="panel">
<h2>🌡️ Thermostat Devices</h2>
<div class="device-list">
{% for device_id in thermostat_devices %}
<div class="device-item thermostat">
<strong>{{ device_id }}</strong>
<div id="thermo-{{ device_id }}" style="margin-top: 0.25rem; opacity: 0.8;">
Mode: auto, Target: 21.0°C, Current: 20.5°C
</div>
</div>
{% endfor %}
</div>
</div>
<div class="panel events-panel">
<h2>📡 Real-time Events</h2>
<div class="stats">
<div class="stat-card">
<div class="stat-value" id="total-events">0</div>
<div class="stat-label">Total Events</div>
</div>
<div class="stat-card">
<div class="stat-value" id="commands-received">0</div>
<div class="stat-label">Commands Received</div>
</div>
<div class="stat-card">
<div class="stat-value" id="states-published">0</div>
<div class="stat-label">States Published</div>
</div>
</div>
<div class="controls">
<button class="primary" onclick="clearEvents()">Clear Events</button>
<button class="primary" onclick="toggleAutoScroll()">
<span id="autoscroll-text">Auto-scroll: ON</span>
</button>
</div>
<div class="event-list" id="event-list">
<div class="empty-state">
<p>Waiting for events...</p>
</div>
</div>
</div>
</div>
</div>
<script>
let eventSource = null;
let autoScroll = true;
let stats = {
total: 0,
commands: 0,
states: 0
};
// Connect to SSE
function connectSSE() {
eventSource = new EventSource('/realtime');
eventSource.onopen = () => {
console.log('SSE connected');
updateStatus('app', true, 'Running');
};
eventSource.addEventListener('message', (e) => {
const event = JSON.parse(e.data);
handleEvent(event);
});
eventSource.addEventListener('ping', (e) => {
console.log('Heartbeat received');
});
eventSource.onerror = (error) => {
console.error('SSE error:', error);
updateStatus('app', false, 'Disconnected');
eventSource.close();
// Reconnect after 5 seconds
setTimeout(connectSSE, 5000);
};
}
function handleEvent(event) {
// Update stats
stats.total++;
if (event.type === 'command_received') stats.commands++;
if (event.type === 'state_published') stats.states++;
document.getElementById('total-events').textContent = stats.total;
document.getElementById('commands-received').textContent = stats.commands;
document.getElementById('states-published').textContent = stats.states;
// Update device states
if (event.type === 'light_updated' && event.new_state) {
updateLightState(event.device_id, event.new_state);
} else if (event.type === 'thermostat_updated' && event.new_state) {
updateThermostatState(event.device_id, event.new_state);
} else if (event.type === 'state_published') {
if (event.device_type === 'light') {
updateLightState(event.device_id, event.payload);
} else if (event.device_type === 'thermostat') {
updateThermostatState(event.device_id, event.payload);
}
} else if (event.type === 'simulator_connected') {
updateStatus('mqtt', true, event.broker);
}
// Add to event list
addEventToList(event);
}
function updateLightState(deviceId, state) {
const el = document.getElementById(`light-${deviceId}`);
if (el) {
el.textContent = `Power: ${state.power}, Brightness: ${state.brightness}`;
}
}
function updateThermostatState(deviceId, state) {
const el = document.getElementById(`thermo-${deviceId}`);
if (el) {
el.textContent = `Mode: ${state.mode}, Target: ${state.target}°C, Current: ${state.current}°C`;
}
}
function updateStatus(type, connected, text) {
const indicator = document.getElementById(`${type}-status`);
const textEl = document.getElementById(`${type}-text`);
if (indicator) {
indicator.className = `status-indicator ${connected ? 'connected' : 'disconnected'}`;
}
if (textEl) {
textEl.textContent = text;
}
}
function addEventToList(event) {
const eventList = document.getElementById('event-list');
// Remove empty state
const emptyState = eventList.querySelector('.empty-state');
if (emptyState) {
emptyState.remove();
}
const eventItem = document.createElement('div');
eventItem.className = `event-item ${event.type}`;
const time = new Date(event.timestamp).toLocaleTimeString('de-DE');
const eventData = { ...event };
delete eventData.timestamp;
eventItem.innerHTML = `
<div class="event-header">
<span class="event-type">${event.type}</span>
<span class="event-time">${time}</span>
</div>
<div class="event-data">${JSON.stringify(eventData, null, 2)}</div>
`;
eventList.insertBefore(eventItem, eventList.firstChild);
// Keep only last 50 events
while (eventList.children.length > 50) {
eventList.removeChild(eventList.lastChild);
}
// Auto-scroll to top
if (autoScroll) {
eventList.scrollTop = 0;
}
}
function clearEvents() {
const eventList = document.getElementById('event-list');
eventList.innerHTML = '<div class="empty-state"><p>Events cleared</p></div>';
stats = { total: 0, commands: 0, states: 0 };
document.getElementById('total-events').textContent = '0';
document.getElementById('commands-received').textContent = '0';
document.getElementById('states-published').textContent = '0';
}
function toggleAutoScroll() {
autoScroll = !autoScroll;
document.getElementById('autoscroll-text').textContent = `Auto-scroll: ${autoScroll ? 'ON' : 'OFF'}`;
}
// Initialize
connectSSE();
</script>
</body>
</html>