fixes 2
This commit is contained in:
@@ -963,18 +963,33 @@
|
|||||||
// Initialize
|
// Initialize
|
||||||
connectSSE();
|
connectSSE();
|
||||||
|
|
||||||
// Optional: Load initial state from API
|
// Load initial device states
|
||||||
async function loadDevices() {
|
async function loadDevices() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(api('/devices'));
|
const response = await fetch(api('/devices'));
|
||||||
const devices = await response.json();
|
const devices = await response.json();
|
||||||
console.log('Loaded devices:', devices);
|
console.log('Loaded initial device states:', devices);
|
||||||
|
|
||||||
|
// Update UI with initial states
|
||||||
|
devices.forEach(device => {
|
||||||
|
if (device.type === 'light' && device.state) {
|
||||||
|
currentState[device.id] = device.state.power;
|
||||||
|
updateDeviceUI(device.id, device.state.power, device.state.brightness);
|
||||||
|
} else if (device.type === 'thermostat' && device.state) {
|
||||||
|
if (device.state.mode) thermostatModes[device.id] = device.state.mode;
|
||||||
|
if (device.state.target) thermostatTargets[device.id] = device.state.target;
|
||||||
|
updateThermostatUI(device.id, device.state.current, device.state.target, device.state.mode);
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load devices:', error);
|
console.error('Failed to load initial device states:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadDevices();
|
// Load initial states before connecting SSE
|
||||||
|
loadDevices().then(() => {
|
||||||
|
console.log('Initial states loaded, now connecting SSE...');
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user