This commit is contained in:
Mateusz Gruszczyński
2026-09-03 10:26:18 +02:00
parent 8f6169fbe3
commit 4c9f8ff403
31 changed files with 673 additions and 71 deletions
+43 -4
View File
@@ -77,7 +77,7 @@ document.addEventListener('click', async event => {
if (action === 'open-ping' && device) { openDevicePing(device.id); return; }
if (action === 'ping-toggle') { if (app.pingMonitor.running) stopPingMonitor(); else startPingMonitor(); return; }
if (action === 'power' && device) return sendDeviceCommand(device.id, current => ({ power: !current?.power }));
if (action === 'temperature' && device) return sendDeviceCommand(device.id, current => ({ target_temperature: clamp(Number(current?.target_temperature ?? device.target_temperature) + Number(button.dataset.delta), 8, 30) }));
if (action === 'temperature' && device) return queueDeviceTemperature(device.id, Number(button.dataset.delta));
if (action === 'mode' && device) return sendDeviceCommand(device.id, { mode: button.dataset.value, power: true });
if (action === 'fan' && device) return sendDeviceCommand(device.id, { fan_speed: Number(button.dataset.value) });
if (action === 'toggle' && device) return sendDeviceCommand(device.id, current => ({ [button.dataset.field]: !current?.[button.dataset.field] }));
@@ -321,10 +321,49 @@ $('#discoveryNamesForm').addEventListener('submit', async event => {
});
$('#renameDeviceForm').addEventListener('submit', async event => {
event.preventDefault(); const form = event.currentTarget, raw = Object.fromEntries(new FormData(form));
event.preventDefault();
const form = event.currentTarget, raw = Object.fromEntries(new FormData(form));
const saveMode = event.submitter?.dataset.saveMode || 'save';
const previous = app.devices.find(item => item.id === raw.id);
const result = $('#deviceConfigCheckResult');
if (result) { result.hidden = true; result.textContent = ''; result.classList.remove('success', 'error'); }
await runFormTask(form, async () => {
const device = await api(`/api/devices/${encodeURIComponent(raw.id)}`, { method: 'PATCH', body: { name: raw.name.trim(), ip: raw.ip.trim(), port: Number(raw.port), protocol_version: Number(raw.protocol_version) } });
updateDevice(device); form.closest('dialog').close(); renderAll(); toast(tr('common.saved'));
let device = await api(`/api/devices/${encodeURIComponent(raw.id)}`, { method: 'PATCH', body: { name: raw.name.trim(), ip: raw.ip.trim(), port: Number(raw.port), protocol_version: Number(raw.protocol_version) } });
updateDevice(device);
if (saveMode !== 'check') {
form.closest('dialog').close(); renderAll(); toast(tr('common.saved')); return;
}
try {
const protocolChanged = previous && Number(previous.protocol_version) !== Number(raw.protocol_version);
if (!device.simulated && protocolChanged) {
device = await api(`/api/devices/${encodeURIComponent(device.id)}/bind`, { method: 'POST' });
updateDevice(device);
}
let probe;
try {
probe = await api(`/api/devices/${encodeURIComponent(device.id)}/probe`, { method: 'POST' });
} catch (firstError) {
if (device.simulated || protocolChanged) throw firstError;
device = await api(`/api/devices/${encodeURIComponent(device.id)}/bind`, { method: 'POST' });
updateDevice(device);
probe = await api(`/api/devices/${encodeURIComponent(device.id)}/probe`, { method: 'POST' });
}
if (result) {
result.hidden = false; result.classList.add('success');
result.textContent = tr('devices.connectionCheckOk', { ms: Math.round(Number(probe.response_time_ms || 0)) });
}
markFormClean(form);
renderAll();
toast(tr('devices.savedAndChecked'));
} catch (error) {
if (result) {
result.hidden = false; result.classList.add('error');
result.textContent = tr('devices.connectionCheckFailed', { error: error.message || String(error) });
}
markFormClean(form);
renderAll();
toast(tr('devices.savedCheckFailed'), true);
}
});
});