391 lines
12 KiB
HTML
391 lines
12 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;
|
|
}
|
|
|
|
.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-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;
|
|
}
|
|
|
|
.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;
|
|
}
|
|
</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>
|
|
|
|
<div class="devices">
|
|
<!-- Test Lampe Card -->
|
|
<div class="device-card">
|
|
<div class="device-header">
|
|
<div class="device-name">💡 Test Lampe 1</div>
|
|
<div class="device-type">Light</div>
|
|
</div>
|
|
|
|
<div class="device-state">
|
|
<span class="state-label">Status:</span>
|
|
<span class="state-value off" id="state-test_lampe_1">off</span>
|
|
</div>
|
|
|
|
<div class="controls">
|
|
<button
|
|
class="toggle-button off"
|
|
id="toggle-test_lampe_1"
|
|
onclick="toggleDevice('test_lampe_1')">
|
|
Einschalten
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="device-card">
|
|
<div class="device-header">
|
|
<div class="device-name">💡 Test Lampe 2</div>
|
|
<div class="device-type">Light</div>
|
|
</div>
|
|
|
|
<div class="device-state">
|
|
<span class="state-label">Status:</span>
|
|
<span class="state-value off" id="state-test_lampe_2">off</span>
|
|
</div>
|
|
|
|
<div class="controls">
|
|
<button
|
|
class="toggle-button off"
|
|
id="toggle-test_lampe_2"
|
|
onclick="toggleDevice('test_lampe_2')">
|
|
Einschalten
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<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>
|
|
const API_BASE = 'http://localhost:8001';
|
|
let eventSource = null;
|
|
let currentState = {
|
|
'test_lampe_1': 'off',
|
|
'test_lampe_2': 'off'
|
|
};
|
|
|
|
// Toggle device state
|
|
async function toggleDevice(deviceId) {
|
|
const newState = currentState[deviceId] === 'on' ? 'off' : 'on';
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/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 device UI
|
|
function updateDeviceUI(deviceId, power) {
|
|
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';
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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_BASE}/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) {
|
|
if (data.payload && data.payload.power) {
|
|
updateDeviceUI(data.device_id, data.payload.power);
|
|
}
|
|
}
|
|
});
|
|
|
|
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_BASE}/devices`);
|
|
const devices = await response.json();
|
|
console.log('Loaded devices:', devices);
|
|
} catch (error) {
|
|
console.error('Failed to load devices:', error);
|
|
}
|
|
}
|
|
|
|
loadDevices();
|
|
</script>
|
|
</body>
|
|
</html>
|