testing fixes for toggles and sockets
Build Images and Deploy / Update-PROD-Stack (push) Successful in 20s
Build Images and Deploy / Update-PROD-Stack (push) Successful in 20s
This commit is contained in:
@@ -747,53 +747,60 @@ function renderDevices() {
|
|||||||
}).join('');
|
}).join('');
|
||||||
|
|
||||||
// fetch current state for each device
|
// fetch current state for each device
|
||||||
devices.forEach((d, i) => {
|
devices.forEach(async (d, i) => {
|
||||||
// Skip fetching state if WebSocket is not connected
|
// Skip fetching state if WebSocket is not connected
|
||||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||||
console.log(`[DWM] Skipping device state fetch due to WebSocket connection issues`);
|
console.log(`[DWM] Skipping device state fetch due to WebSocket connection issues`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
// For dimmer devices, fetch both brightness and state
|
// For dimmer devices, fetch both brightness and state
|
||||||
if (d.isDimmer) {
|
if (d.isDimmer) {
|
||||||
// Fetch brightness first
|
// Fetch both brightness and state in parallel to avoid race conditions
|
||||||
api('GET', `/api/devices/${d.host}/${d.port}/brightness`)
|
const [brightnessData, stateData] = await Promise.allSettled([
|
||||||
.then((data) => {
|
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 slider = document.getElementById('bright-'+i);
|
||||||
const value = document.getElementById('bright-val-'+i);
|
const value = document.getElementById('bright-val-'+i);
|
||||||
|
if (slider && value) {
|
||||||
if (slider && value && data.brightness !== undefined) {
|
slider.value = brightnessData.value.brightness;
|
||||||
slider.value = data.brightness;
|
value.textContent = Math.round(brightnessData.value.brightness) + '%';
|
||||||
value.textContent = Math.round(data.brightness) + '%';
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(() => {});
|
|
||||||
|
|
||||||
// Also fetch the actual on/off state from the BinaryState response
|
// Handle state data (this should set the toggle correctly)
|
||||||
api('GET', `/api/devices/${d.host}/${d.port}/state`)
|
if (stateData.status === 'fulfilled') {
|
||||||
.then((on) => {
|
|
||||||
const checkbox = document.getElementById('dchk-'+i);
|
const checkbox = document.getElementById('dchk-'+i);
|
||||||
if (checkbox) {
|
if (checkbox) {
|
||||||
// Use the actual binary state, not brightness
|
const shouldBeChecked = !!stateData.value;
|
||||||
const shouldBeChecked = !!on;
|
|
||||||
checkbox.checked = shouldBeChecked;
|
checkbox.checked = shouldBeChecked;
|
||||||
console.log(`[DWM] Device ${d.host}:${d.port} - BinaryState: ${on ? 'ON' : 'OFF'}, Last brightness: ${data.brightness}%`);
|
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}, actual checkbox.checked: ${checkbox.checked}`);
|
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 {
|
} else {
|
||||||
// For non-dimmer devices, just fetch the binary state
|
// For non-dimmer devices, just fetch binary state
|
||||||
api('GET', `/api/devices/${d.host}/${d.port}/state`)
|
const stateResult = await api('GET', `/api/devices/${d.host}/${d.port}/state`);
|
||||||
.then((on) => {
|
const checkbox = document.getElementById('dchk-'+i);
|
||||||
const c = document.getElementById('dchk-'+i);
|
if (checkbox) {
|
||||||
if (c) {
|
const shouldBeChecked = !!stateResult;
|
||||||
const shouldBeChecked = !!on;
|
checkbox.checked = shouldBeChecked;
|
||||||
c.checked = shouldBeChecked;
|
console.log(`[DWM] Non-dimmer device ${d.host}:${d.port} - State: ${stateResult ? 'ON' : 'OFF'}, Setting checkbox to: ${shouldBeChecked}`);
|
||||||
console.log(`[DWM] Non-dimmer device ${d.host}:${d.port} - State: ${on ? '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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user