Files
home-automation/apps/ui/templates/dashboard.html
2025-11-08 16:03:58 +01:00

879 lines
29 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Automation</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, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
background: white;
border-radius: 16px;
padding: 2rem;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
}
h1 {
color: #333;
margin-bottom: 0.5rem;
}
.status {
display: inline-block;
padding: 0.25rem 0.75rem;
border-radius: 12px;
font-size: 0.875rem;
font-weight: 500;
}
.status.connected {
background: #d4edda;
color: #155724;
}
.status.disconnected {
background: #f8d7da;
color: #721c24;
}
.room {
background: white;
border-radius: 20px;
padding: 2rem;
margin-bottom: 2rem;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
transition: transform 0.2s, box-shadow 0.2s;
}
.room:hover {
transform: translateY(-2px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}
.room-title {
color: #333;
font-size: 1.75rem;
font-weight: 700;
margin-bottom: 1.5rem;
padding-bottom: 1rem;
border-bottom: 3px solid #667eea;
display: flex;
align-items: center;
gap: 0.75rem;
}
.room-title::before {
content: '🏠';
font-size: 2rem;
}
.devices {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1.5rem;
}
.device-card {
background: white;
border-radius: 16px;
padding: 1.5rem;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.device-header {
margin-bottom: 1.5rem;
}
.device-name {
font-size: 1.5rem;
font-weight: 600;
color: #333;
margin-bottom: 0.25rem;
}
.device-type {
font-size: 0.875rem;
color: #666;
text-transform: uppercase;
}
.device-id {
font-size: 0.75rem;
color: #999;
margin-top: 0.25rem;
font-family: 'Courier New', monospace;
}
.device-state {
padding: 0.5rem 1rem;
background: #f8f9fa;
border-radius: 8px;
margin: 1rem 0;
font-family: 'Courier New', monospace;
font-size: 0.875rem;
}
.state-label {
color: #666;
font-weight: 500;
}
.state-value {
color: #333;
font-weight: 600;
}
.state-value.on {
color: #28a745;
}
.state-value.off {
color: #dc3545;
}
.controls {
display: flex;
flex-direction: column;
gap: 1rem;
}
.toggle-button {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
color: white;
}
.toggle-button.on {
background: #28a745;
}
.toggle-button.on:hover {
background: #218838;
}
.toggle-button.off {
background: #6c757d;
}
.toggle-button.off:hover {
background: #5a6268;
}
.brightness-control {
margin-top: 1rem;
}
.brightness-label {
font-size: 0.875rem;
color: #666;
display: block;
margin-bottom: 0.5rem;
}
.brightness-slider {
width: 100%;
height: 8px;
border-radius: 4px;
background: #ddd;
outline: none;
-webkit-appearance: none;
}
.brightness-slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: #667eea;
cursor: pointer;
}
.brightness-slider::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: #667eea;
cursor: pointer;
border: none;
}
/* Thermostat styles */
.thermostat-display {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin: 1rem 0;
}
.temp-reading {
background: #f8f9fa;
border-radius: 8px;
padding: 1rem;
text-align: center;
}
.temp-label {
font-size: 0.75rem;
color: #666;
text-transform: uppercase;
margin-bottom: 0.25rem;
}
.temp-value {
font-size: 2rem;
font-weight: 700;
color: #333;
}
.temp-unit {
font-size: 1rem;
color: #999;
}
.mode-display {
background: #f8f9fa;
border-radius: 8px;
padding: 0.75rem;
text-align: center;
margin-bottom: 1rem;
}
.mode-label {
font-size: 0.75rem;
color: #666;
text-transform: uppercase;
}
.mode-value {
font-size: 1rem;
font-weight: 600;
color: #667eea;
text-transform: uppercase;
}
.temp-controls {
display: flex;
gap: 0.5rem;
margin-bottom: 1rem;
}
.temp-button {
flex: 1;
padding: 0.75rem;
border: none;
border-radius: 8px;
font-size: 1.125rem;
font-weight: 700;
cursor: pointer;
transition: all 0.2s;
background: #667eea;
color: white;
min-height: 44px;
}
.temp-button:hover {
background: #5568d3;
}
.temp-button:active {
transform: scale(0.95);
}
.mode-controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.5rem;
}
.mode-button {
padding: 0.75rem;
border: 2px solid #ddd;
border-radius: 8px;
font-size: 0.875rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
background: white;
color: #666;
min-height: 44px;
text-transform: uppercase;
}
.mode-button:hover {
border-color: #667eea;
color: #667eea;
}
.mode-button.active {
background: #667eea;
border-color: #667eea;
color: white;
}
.mode-button:active {
transform: scale(0.95);
}
.events {
margin-top: 2rem;
background: white;
border-radius: 16px;
padding: 1.5rem;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.events h2 {
color: #333;
margin-bottom: 1rem;
font-size: 1.25rem;
}
.event-list {
max-height: 300px;
overflow-y: auto;
}
.event-item {
padding: 0.75rem;
border-left: 3px solid #667eea;
background: #f8f9fa;
margin-bottom: 0.5rem;
border-radius: 4px;
font-size: 0.875rem;
}
.event-time {
color: #666;
font-size: 0.75rem;
}
.event-data {
color: #333;
margin-top: 0.25rem;
font-family: 'Courier New', monospace;
}
.empty-state {
background: white;
border-radius: 16px;
padding: 3rem;
text-align: center;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.empty-state p {
color: #666;
margin-bottom: 0.5rem;
}
.hint {
font-size: 0.875rem;
color: #999;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>🏠 Home Automation</h1>
<p>Realtime Status: <span class="status disconnected" id="connection-status">Verbinde...</span></p>
</header>
{% if rooms %}
{% for room in rooms %}
<section class="room">
<h2 class="room-title">{{ room.name }}</h2>
<div class="devices">
{% for device in room.devices %}
<div class="device-card" data-device-id="{{ device.device_id }}">
<div class="device-header">
<div class="device-name">{{ device.icon }} {{ device.title }}</div>
<div class="device-type">
{% if device.type == "light" %}
Light
{% if device.features.brightness %}• Dimmbar{% endif %}
{% elif device.type == "thermostat" %}
Thermostat
{% else %}
{{ device.type or "Unknown" }}
{% endif %}
</div>
<div class="device-id">{{ device.device_id }}</div>
</div>
{% if device.type == "light" %}
<div class="device-state">
<span class="state-label">Status:</span>
<span class="state-value off" id="state-{{ device.device_id }}">off</span>
{% if device.features.brightness %}
<br>
<span class="state-label">Helligkeit:</span>
<span class="state-value" id="brightness-{{ device.device_id }}">50</span>%
{% endif %}
</div>
{% if device.features.power %}
<div class="controls">
<button
class="toggle-button off"
id="toggle-{{ device.device_id }}"
onclick="toggleDevice('{{ device.device_id }}')">
Einschalten
</button>
{% if device.features.brightness %}
<div class="brightness-control">
<label for="brightness-slider-{{ device.device_id }}" class="brightness-label">
Helligkeit: <span id="brightness-value-{{ device.device_id }}">50</span>%
</label>
<input
type="range"
class="brightness-slider"
id="brightness-slider-{{ device.device_id }}"
min="0"
max="100"
value="50"
oninput="updateBrightnessValue('{{ device.device_id }}', this.value)"
onchange="setBrightness('{{ device.device_id }}', this.value)">
</div>
{% endif %}
</div>
{% endif %}
{% elif device.type == "thermostat" %}
<div class="thermostat-display">
<div class="temp-reading">
<div class="temp-label">Ist</div>
<div class="temp-value">
<span id="state-{{ device.device_id }}-current">--</span>
<span class="temp-unit">°C</span>
</div>
</div>
<div class="temp-reading">
<div class="temp-label">Soll</div>
<div class="temp-value">
<span id="state-{{ device.device_id }}-target">21.0</span>
<span class="temp-unit">°C</span>
</div>
</div>
</div>
<div class="mode-display">
<div class="mode-label">Modus</div>
<div class="mode-value" id="state-{{ device.device_id }}-mode">OFF</div>
</div>
<div class="temp-controls">
<button class="temp-button" onclick="adjustTarget('{{ device.device_id }}', -0.5)">
-0.5
</button>
<button class="temp-button" onclick="adjustTarget('{{ device.device_id }}', 0.5)">
+0.5
</button>
</div>
<div class="mode-controls">
<button
class="mode-button"
id="mode-{{ device.device_id }}-off"
onclick="setMode('{{ device.device_id }}', 'off')">
Off
</button>
<button
class="mode-button"
id="mode-{{ device.device_id }}-heat"
onclick="setMode('{{ device.device_id }}', 'heat')">
Heat
</button>
<button
class="mode-button"
id="mode-{{ device.device_id }}-auto"
onclick="setMode('{{ device.device_id }}', 'auto')">
Auto
</button>
</div>
{% endif %}
</div>
{% endfor %}
</div>
</section>
{% endfor %}
{% else %}
<div class="empty-state">
<p>Keine Räume oder Geräte konfiguriert.</p>
<p class="hint">Prüfe config/layout.yaml und das API-Gateway.</p>
</div>
{% endif %}
<div class="events">
<h2>📡 Realtime Events</h2>
<div class="event-list" id="event-list">
<p style="color: #666; font-size: 0.875rem;">Warte auf Events...</p>
</div>
</div>
</div>
<script>
// API_BASE injected from backend (supports Docker/K8s environments)
window.API_BASE = '{{ api_base }}';
// Helper function to construct API URLs
function api(url) {
return `${window.API_BASE}${url}`;
}
let eventSource = null;
let currentState = {};
let thermostatTargets = {};
let thermostatModes = {};
// Initialize device states
{% for room in rooms %}
{% for device in room.devices %}
{% if device.type == "light" %}
currentState['{{ device.device_id }}'] = 'off';
{% elif device.type == "thermostat" %}
thermostatTargets['{{ device.device_id }}'] = 21.0;
thermostatModes['{{ device.device_id }}'] = 'off';
{% endif %}
{% endfor %}
{% endfor %}
// Toggle device state
async function toggleDevice(deviceId) {
const newState = currentState[deviceId] === 'on' ? 'off' : 'on';
try {
const response = await fetch(api(`/devices/${deviceId}/set`), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'light',
payload: {
power: newState
}
})
});
if (response.ok) {
console.log(`Sent ${newState} command to ${deviceId}`);
addEvent({
action: 'command_sent',
device_id: deviceId,
state: newState
});
}
} catch (error) {
console.error('Failed to toggle device:', error);
}
}
// Update brightness value display
function updateBrightnessValue(deviceId, value) {
const valueSpan = document.getElementById(`brightness-value-${deviceId}`);
if (valueSpan) {
valueSpan.textContent = value;
}
}
// Set brightness
async function setBrightness(deviceId, brightness) {
try {
const response = await fetch(api(`/devices/${deviceId}/set`), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'light',
payload: {
brightness: parseInt(brightness)
}
})
});
if (response.ok) {
console.log(`Sent brightness ${brightness} to ${deviceId}`);
addEvent({
action: 'brightness_set',
device_id: deviceId,
brightness: parseInt(brightness)
});
}
} catch (error) {
console.error('Failed to set brightness:', error);
}
}
// Adjust thermostat target temperature
async function adjustTarget(deviceId, delta) {
const currentTarget = thermostatTargets[deviceId] || 21.0;
const currentMode = thermostatModes[deviceId] || 'off';
const newTarget = Math.max(5.0, Math.min(30.0, currentTarget + delta));
try {
const response = await fetch(api(`/devices/${deviceId}/set`), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'thermostat',
payload: {
mode: currentMode,
target: newTarget
}
})
});
if (response.ok) {
thermostatTargets[deviceId] = newTarget;
console.log(`Sent target ${newTarget} to ${deviceId}`);
addEvent({
action: 'target_adjusted',
device_id: deviceId,
target: newTarget
});
}
} catch (error) {
console.error('Failed to adjust target:', error);
}
}
// Set thermostat mode
async function setMode(deviceId, mode) {
const currentTarget = thermostatTargets[deviceId] || 21.0;
try {
const response = await fetch(api(`/devices/${deviceId}/set`), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
type: 'thermostat',
payload: {
mode: mode,
target: currentTarget
}
})
});
if (response.ok) {
console.log(`Sent mode ${mode} to ${deviceId}`);
addEvent({
action: 'mode_set',
device_id: deviceId,
mode: mode
});
}
} catch (error) {
console.error('Failed to set mode:', error);
}
}
// Update device UI
function updateDeviceUI(deviceId, power, brightness) {
currentState[deviceId] = power;
const stateSpan = document.getElementById(`state-${deviceId}`);
const toggleButton = document.getElementById(`toggle-${deviceId}`);
if (stateSpan) {
stateSpan.textContent = power;
stateSpan.className = `state-value ${power}`;
}
if (toggleButton) {
if (power === 'on') {
toggleButton.textContent = 'Ausschalten';
toggleButton.className = 'toggle-button on';
} else {
toggleButton.textContent = 'Einschalten';
toggleButton.className = 'toggle-button off';
}
}
// Update brightness display and slider
if (brightness !== undefined) {
const brightnessSpan = document.getElementById(`brightness-${deviceId}`);
const brightnessValue = document.getElementById(`brightness-value-${deviceId}`);
const brightnessSlider = document.getElementById(`brightness-slider-${deviceId}`);
if (brightnessSpan) {
brightnessSpan.textContent = brightness;
}
if (brightnessValue) {
brightnessValue.textContent = brightness;
}
if (brightnessSlider) {
brightnessSlider.value = brightness;
}
}
}
// Update thermostat UI
function updateThermostatUI(deviceId, current, target, mode) {
const currentSpan = document.getElementById(`state-${deviceId}-current`);
const targetSpan = document.getElementById(`state-${deviceId}-target`);
const modeSpan = document.getElementById(`state-${deviceId}-mode`);
if (current !== undefined && currentSpan) {
currentSpan.textContent = current.toFixed(1);
}
if (target !== undefined) {
if (targetSpan) {
targetSpan.textContent = target.toFixed(1);
}
thermostatTargets[deviceId] = target;
}
if (mode !== undefined) {
if (modeSpan) {
modeSpan.textContent = mode.toUpperCase();
}
thermostatModes[deviceId] = mode;
// Update mode button states
['off', 'heat', 'auto'].forEach(m => {
const btn = document.getElementById(`mode-${deviceId}-${m}`);
if (btn) {
if (m === mode.toLowerCase()) {
btn.classList.add('active');
} else {
btn.classList.remove('active');
}
}
});
}
}
// Add event to list
function addEvent(event) {
const eventList = document.getElementById('event-list');
// Clear placeholder
if (eventList.children.length === 1 && eventList.children[0].tagName === 'P') {
eventList.innerHTML = '';
}
const eventItem = document.createElement('div');
eventItem.className = 'event-item';
const now = new Date().toLocaleTimeString('de-DE');
eventItem.innerHTML = `
<div class="event-time">${now}</div>
<div class="event-data">${JSON.stringify(event, null, 2)}</div>
`;
eventList.insertBefore(eventItem, eventList.firstChild);
// Keep only last 10 events
while (eventList.children.length > 10) {
eventList.removeChild(eventList.lastChild);
}
}
// Connect to SSE
function connectSSE() {
eventSource = new EventSource(api('/realtime'));
eventSource.onopen = () => {
console.log('SSE connected');
document.getElementById('connection-status').textContent = 'Verbunden';
document.getElementById('connection-status').className = 'status connected';
};
eventSource.addEventListener('message', (e) => {
const data = JSON.parse(e.data);
console.log('SSE message:', data);
addEvent(data);
// Update device state
if (data.type === 'state' && data.device_id && data.payload) {
const card = document.querySelector(`[data-device-id="${data.device_id}"]`);
// Check if it's a light
if (data.payload.power !== undefined) {
updateDeviceUI(
data.device_id,
data.payload.power,
data.payload.brightness
);
}
// Check if it's a thermostat
if (data.payload.mode !== undefined || data.payload.target !== undefined || data.payload.current !== undefined) {
updateThermostatUI(
data.device_id,
data.payload.current,
data.payload.target,
data.payload.mode
);
}
}
});
eventSource.addEventListener('ping', (e) => {
console.log('Heartbeat received');
});
eventSource.onerror = (error) => {
console.error('SSE error:', error);
document.getElementById('connection-status').textContent = 'Getrennt';
document.getElementById('connection-status').className = 'status disconnected';
eventSource.close();
// Reconnect after 5 seconds
setTimeout(connectSSE, 5000);
};
}
// Initialize
connectSSE();
// Optional: Load initial state from API
async function loadDevices() {
try {
const response = await fetch(api('/devices'));
const devices = await response.json();
console.log('Loaded devices:', devices);
} catch (error) {
console.error('Failed to load devices:', error);
}
}
loadDevices();
</script>
</body>
</html>