This commit is contained in:
@@ -253,6 +253,7 @@
|
|||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<h2>Devices</h2>
|
<h2>Devices</h2>
|
||||||
<div style="flex:1"></div>
|
<div style="flex:1"></div>
|
||||||
|
<button class="btn btn-ghost btn-sm" onclick="refreshDeviceStates()">⟳ Refresh</button>
|
||||||
<button class="btn btn-ghost btn-sm" onclick="openAddDeviceModal()">+ Add IP</button>
|
<button class="btn btn-ghost btn-sm" onclick="openAddDeviceModal()">+ Add IP</button>
|
||||||
<button class="btn btn-primary" id="btn-discover" onclick="discoverDevices()">⟳ Scan</button>
|
<button class="btn btn-primary" id="btn-discover" onclick="discoverDevices()">⟳ Scan</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -708,29 +709,55 @@ function renderDevices() {
|
|||||||
|
|
||||||
// fetch current state for each device
|
// fetch current state for each device
|
||||||
devices.forEach((d, i) => {
|
devices.forEach((d, i) => {
|
||||||
api('GET', `/api/devices/${d.host}/${d.port}/state`)
|
// For dimmer devices, fetch brightness first and use it to set toggle state
|
||||||
.then((on) => {
|
|
||||||
const c = document.getElementById('dchk-'+i);
|
|
||||||
if (c) c.checked = !!on;
|
|
||||||
})
|
|
||||||
.catch(() => {});
|
|
||||||
|
|
||||||
// fetch brightness for dimmer devices
|
|
||||||
if (d.isDimmer) {
|
if (d.isDimmer) {
|
||||||
api('GET', `/api/devices/${d.host}/${d.port}/brightness`)
|
api('GET', `/api/devices/${d.host}/${d.port}/brightness`)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
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);
|
||||||
|
const checkbox = document.getElementById('dchk-'+i);
|
||||||
|
|
||||||
if (slider && value && data.brightness !== undefined) {
|
if (slider && value && data.brightness !== undefined) {
|
||||||
slider.value = data.brightness;
|
slider.value = data.brightness;
|
||||||
value.textContent = Math.round(data.brightness) + '%';
|
value.textContent = Math.round(data.brightness) + '%';
|
||||||
|
// Set toggle state based on brightness (0 = off, >0 = on)
|
||||||
|
if (checkbox) checkbox.checked = data.brightness > 0;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// Fallback to regular state check if brightness fails
|
||||||
|
api('GET', `/api/devices/${d.host}/${d.port}/state`)
|
||||||
|
.then((on) => {
|
||||||
|
const c = document.getElementById('dchk-'+i);
|
||||||
|
if (c) c.checked = !!on;
|
||||||
|
})
|
||||||
|
.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) c.checked = !!on;
|
||||||
|
})
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Refresh Device States ───────────────────────────────────────────────────
|
||||||
|
async function refreshDeviceStates() {
|
||||||
|
if (!devices.length) return;
|
||||||
|
|
||||||
|
// Show loading state
|
||||||
|
toast('Refreshing device states...', 'info');
|
||||||
|
|
||||||
|
// Re-render devices to refresh their states
|
||||||
|
renderDevices();
|
||||||
|
|
||||||
|
toast('Device states refreshed', 'success');
|
||||||
|
}
|
||||||
|
|
||||||
async function toggleDevice(i, e) {
|
async function toggleDevice(i, e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const dev = devices[i];
|
const dev = devices[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user