testing fixes for toggles and sockets
Build Images and Deploy / Update-PROD-Stack (push) Successful in 20s

This commit is contained in:
2026-04-06 20:55:29 -04:00
parent cd0ca8cf7c
commit 93dc2952ec
+36 -29
View File
@@ -747,53 +747,60 @@ function renderDevices() {
}).join('');
// fetch current state for each device
devices.forEach((d, i) => {
devices.forEach(async (d, i) => {
// Skip fetching state if WebSocket is not connected
if (!ws || ws.readyState !== WebSocket.OPEN) {
console.log(`[DWM] Skipping device state fetch due to WebSocket connection issues`);
return;
}
try {
// For dimmer devices, fetch both brightness and state
if (d.isDimmer) {
// Fetch brightness first
api('GET', `/api/devices/${d.host}/${d.port}/brightness`)
.then((data) => {
// Fetch both brightness and state in parallel to avoid race conditions
const [brightnessData, stateData] = await Promise.allSettled([
api('GET', `/api/devices/${d.host}/${d.port}/brightness`),
api('GET', `/api/devices/${d.host}/${d.port}/state`)
]);
// Handle brightness data
if (brightnessData.status === 'fulfilled' && brightnessData.value.brightness !== undefined) {
const slider = document.getElementById('bright-'+i);
const value = document.getElementById('bright-val-'+i);
if (slider && value && data.brightness !== undefined) {
slider.value = data.brightness;
value.textContent = Math.round(data.brightness) + '%';
if (slider && value) {
slider.value = brightnessData.value.brightness;
value.textContent = Math.round(brightnessData.value.brightness) + '%';
}
}
})
.catch(() => {});
// Also fetch the actual on/off state from the BinaryState response
api('GET', `/api/devices/${d.host}/${d.port}/state`)
.then((on) => {
// Handle state data (this should set the toggle correctly)
if (stateData.status === 'fulfilled') {
const checkbox = document.getElementById('dchk-'+i);
if (checkbox) {
// Use the actual binary state, not brightness
const shouldBeChecked = !!on;
const shouldBeChecked = !!stateData.value;
checkbox.checked = shouldBeChecked;
console.log(`[DWM] Device ${d.host}:${d.port} - BinaryState: ${on ? 'ON' : 'OFF'}, Last brightness: ${data.brightness}%`);
console.log(`[DWM] Setting checkbox.checked to: ${shouldBeChecked}, actual checkbox.checked: ${checkbox.checked}`);
console.log(`[DWM] Device ${d.host}:${d.port} - BinaryState: ${stateData.value ? 'ON' : 'OFF'}, Last brightness: ${brightnessData.value?.brightness || 'N/A'}%`);
console.log(`[DWM] Setting checkbox.checked to: ${shouldBeChecked}`);
// Force a DOM update to ensure the change takes effect
checkbox.dispatchEvent(new Event('change'));
}
}
})
.catch(() => {});
} else {
// For non-dimmer devices, just fetch the binary state
api('GET', `/api/devices/${d.host}/${d.port}/state`)
.then((on) => {
const c = document.getElementById('dchk-'+i);
if (c) {
const shouldBeChecked = !!on;
c.checked = shouldBeChecked;
console.log(`[DWM] Non-dimmer device ${d.host}:${d.port} - State: ${on ? 'ON' : 'OFF'}, Setting checkbox to: ${shouldBeChecked}`);
// For non-dimmer devices, just fetch binary state
const stateResult = await api('GET', `/api/devices/${d.host}/${d.port}/state`);
const checkbox = document.getElementById('dchk-'+i);
if (checkbox) {
const shouldBeChecked = !!stateResult;
checkbox.checked = shouldBeChecked;
console.log(`[DWM] Non-dimmer device ${d.host}:${d.port} - State: ${stateResult ? 'ON' : 'OFF'}, Setting checkbox to: ${shouldBeChecked}`);
// Force a DOM update to ensure the change takes effect
checkbox.dispatchEvent(new Event('change'));
}
})
.catch(() => {});
}
} catch (err) {
console.error(`[DWM] Error fetching state for device ${d.host}:${d.port}:`, err);
}
});
}