Files
OTGateway/src_data/pages/upgrade.html
T
Yurii 0a6ae9c5d8 feat: update the web portal for the new firmware APIs
Restore legacy backups through the settings APIs, update the dashboard and sensor controls, and keep translations in sync. Package the backup converter separately and wait for all compressed web assets before building LittleFS.
2026-11-22 02:35:04 +03:00

419 lines
15 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title data-i18n>upgrade.title</title>
<link rel="stylesheet" href="/static/app.css?{BUILD_TIME}">
</head>
<body>
<header class="container">
<nav>
<ul>
<li>
<a href="/">
<div class="logo" data-i18n>logo</div>
</a>
</li>
</ul>
<ul>
<li>
<select id="lang" aria-label="Lang">
<option value="en" selected>EN</option>
<option value="cn">CN</option>
<option value="it">IT</option>
<option value="nl">NL</option>
<option value="ru">RU</option>
</select>
</li>
</ul>
</nav>
</header>
<main class="container">
<article>
<div>
<hgroup>
<h2 data-i18n>upgrade.section.backupAndRestore.title</h2>
<p data-i18n>upgrade.section.backupAndRestore.desc</p>
</hgroup>
<form id="restore">
<label for="restore-file">
<span data-i18n>upgrade.settingsFile</span>
<input type="file" name="settings" id="restore-file" accept=".json">
</label>
<div role="group">
<button type="submit" data-i18n>button.restore</button>
<button type="button" class="secondary" onclick="window.location='/api/backup/save';" data-i18n>button.backup</button>
</div>
</form>
</div>
</article>
<article>
<div>
<hgroup>
<h2 data-i18n>upgrade.section.upgrade.title</h2>
<p data-i18n>upgrade.section.upgrade.desc</p>
</hgroup>
<form action="/api/upgrade" id="upgrade">
<fieldset class="primary">
<label>
<span data-i18n>upgrade.fw</span>:
<div class="grid">
<input type="file" name="fw" accept=".bin">
<button type="button" class="fwResult hidden" disabled></button>
</div>
</label>
<label>
<span data-i18n>upgrade.fs</span>:
<div class="grid">
<input type="file" name="fs" accept=".bin">
<button type="button" class="fsResult hidden" disabled></button>
</div>
</label>
</fieldset>
<ul>
<li><mark data-i18n>upgrade.note.disclaimer1</mark></li>
<li><mark data-i18n>upgrade.note.disclaimer2</mark></li>
</ul>
<button type="submit" data-i18n>button.upgrade</button>
</form>
</div>
</article>
</main>
<footer class="container">
<small>
<b>Made by Laxilef</b>
• <a href="https://github.com/Laxilef/OTGateway/blob/master/LICENSE" target="_blank" class="secondary" data-i18n>nav.license</a>
• <a href="https://github.com/Laxilef/OTGateway/blob/master/" target="_blank" class="secondary" data-i18n>nav.source</a>
• <a href="https://github.com/Laxilef/OTGateway/wiki" target="_blank" class="secondary" data-i18n>nav.help</a>
• <a href="https://github.com/Laxilef/OTGateway/issues" target="_blank" class="secondary" data-i18n>nav.issues</a>
• <a href="https://github.com/Laxilef/OTGateway/releases" target="_blank" class="secondary" data-i18n>nav.releases</a>
</small>
</footer>
<script src="/static/app.js?{BUILD_TIME}"></script>
<script src="/static/settings-converter.js?{BUILD_TIME}"></script>
<script>
const setupRestoreBackupForm = (formSelector) => {
const form = document.querySelector(formSelector);
if (!form) {
return;
}
const button = form.querySelector('button[type="submit"]');
form.addEventListener('submit', (event) => {
event.preventDefault();
const defaultText = button ? button.textContent : '';
if (button) {
button.textContent = i18n('button.wait');
button.setAttribute('disabled', true);
button.setAttribute('aria-busy', true);
}
const onFinished = (success) => {
if (button) {
button.textContent = i18n(success ? 'button.restored' : 'button.error');
button.classList.add(success ? 'success' : 'failed');
button.removeAttribute('aria-busy');
setTimeout(() => {
button.removeAttribute('disabled');
button.classList.remove('success', 'failed');
button.textContent = defaultText;
}, 5000);
}
};
const files = form.querySelector('#restore-file').files;
if (files.length === 0) {
onFinished(false);
return;
}
const reader = new FileReader();
reader.onload = async (event) => {
try {
const data = convertLegacyBackup(JSON.parse(event.target.result));
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
if (!isObject(data)) {
throw new Error('Backup not valid');
}
let hasData = false;
if (data.settings !== undefined) {
if (!isObject(data.settings)) {
throw new Error('Backup settings not valid');
}
for (const section of Object.values(data.settings)) {
if (!isObject(section) || Object.keys(section).length === 0) {
throw new Error('Backup section not valid');
}
hasData = true;
}
}
if (data.sensors !== undefined) {
if (!isObject(data.sensors) && !Array.isArray(data.sensors)) {
throw new Error('Backup sensors not valid');
}
for (const [sensorId, sensor] of Object.entries(data.sensors)) {
if (!/^\d+$/.test(sensorId) || !isObject(sensor) || Object.keys(sensor).length === 0) {
throw new Error('Backup sensor not valid');
}
hasData = true;
}
}
if (data.network !== undefined) {
if (!isObject(data.network) || Object.keys(data.network).length === 0) {
throw new Error('Backup network not valid');
}
hasData = true;
}
if (!hasData) {
throw new Error('Backup is empty');
}
const headers = {
'Content-Type': 'application/json'
};
const post = async (url, payload) => {
const response = await fetch(url, {
method: 'POST',
cache: 'no-cache',
credentials: 'include',
headers,
body: payload === undefined ? undefined : JSON.stringify(payload)
});
if (!response.ok) {
throw new Error('Restore request failed');
}
return response;
};
if (data.settings !== undefined) {
// Units and regulator modes must precede temperature limits and targets.
const prioritySections = ['system', 'opentherm', 'equitherm', 'pid'];
for (const sectionName of prioritySections) {
if (data.settings[sectionName] === undefined) {
continue;
}
await post(`/api/settings/${sectionName}?noresponse`, data.settings[sectionName]);
}
for (const [sectionName, section] of Object.entries(data.settings)) {
if (sectionName === 'portal' || prioritySections.includes(sectionName)) {
continue;
}
await post(`/api/settings/${encodeURIComponent(sectionName)}?noresponse`, section);
}
// Restore targets again after mode and limit normalization.
for (const sectionName of ['emergency', 'heating', 'dhw']) {
const target = data.settings[sectionName]?.target;
if (target !== undefined) {
await post(`/api/settings/${sectionName}?noresponse`, {target});
}
}
}
if (data.sensors !== undefined) {
for (const [sensorId, sensor] of Object.entries(data.sensors)) {
await post(`/api/sensor?id=${encodeURIComponent(sensorId)}&noresponse`, sensor);
}
}
if (data.settings?.portal !== undefined) {
const response = await post('/api/settings/portal', data.settings.portal);
const portal = await response.json();
if (!isObject(portal) || typeof portal.auth !== 'boolean'
|| typeof portal.login !== 'string' || typeof portal.password !== 'string') {
throw new Error('Portal response not valid');
}
if (portal.auth) {
// Basic authentication encodes UTF-8 bytes, not UTF-16 code units.
const bytes = new TextEncoder().encode(`${portal.login}:${portal.password}`);
let credentials = '';
for (const byte of bytes) {
credentials += String.fromCharCode(byte);
}
headers.Authorization = `Basic ${btoa(credentials)}`;
}
}
if (data.network !== undefined) {
// Network requests are limited to 512 bytes, including escaped strings.
for (const [key, value] of Object.entries(data.network)) {
if (isObject(value)) {
for (const [field, fieldValue] of Object.entries(value)) {
await post('/api/network/settings?noreconnect&noresponse', {[key]: {[field]: fieldValue}});
}
} else {
await post('/api/network/settings?noreconnect&noresponse', {[key]: value});
}
}
}
await post('/api/restart');
onFinished(true);
} catch {
onFinished(false);
}
};
reader.onerror = () => {
onFinished(false);
};
reader.onabort = () => {
onFinished(false);
};
try {
reader.readAsText(files[0]);
} catch {
onFinished(false);
}
});
};
document.addEventListener('DOMContentLoaded', async () => {
const lang = new Lang(document.getElementById('lang'));
await lang.build();
setupRestoreBackupForm('#restore');
const upgradeForm = document.querySelector('#upgrade');
if (upgradeForm) {
upgradeForm.reset();
const statusToText = (status) => {
switch (status) {
case 0:
return "None";
case 1:
return "No file";
case 2:
return "Success";
case 3:
return "Prohibited";
case 4:
return "Size mismatch";
case 5:
return "Error on start";
case 6:
return "Error on write";
case 7:
return "Error on finish";
default:
return "Unknown";
}
};
upgradeForm.addEventListener('submit', async (event) => {
event.preventDefault();
hide('.fwResult');
hide('.fsResult');
let button = upgradeForm.querySelector('button[type="submit"]');
button.textContent = i18n('button.uploading');
button.setAttribute('disabled', true);
button.setAttribute('aria-busy', true);
try {
let fd = new FormData();
const fw = upgradeForm.querySelector("[name='fw']").files;
if (fw.length > 0) {
fd.append("fw_size", fw[0].size);
fd.append("fw", fw[0]);
}
const fs = upgradeForm.querySelector("[name='fs']").files;
if (fs.length > 0) {
fd.append("fs_size", fs[0].size);
fd.append("fs", fs[0]);
}
let response = await fetch(upgradeForm.action, {
method: "POST",
cache: "no-cache",
credentials: "include",
body: fd
});
if (response.status != 202 && response.status != 406) {
throw new Error('Response not valid');
}
const result = await response.json();
let resItem = upgradeForm.querySelector('.fwResult');
if (resItem && result.firmware.status > 1) {
resItem.textContent = statusToText(result.firmware.status);
resItem.classList.remove('hidden');
if (result.firmware.status == 2) {
resItem.classList.remove('failed');
resItem.classList.add('success');
} else {
resItem.classList.remove('success');
resItem.classList.add('failed');
if (result.firmware.error != "") {
resItem.textContent += `: ${result.firmware.error}`;
}
}
}
resItem = upgradeForm.querySelector('.fsResult');
if (resItem && result.filesystem.status > 1) {
resItem.textContent = statusToText(result.filesystem.status);
resItem.classList.remove('hidden');
if (result.filesystem.status == 2) {
resItem.classList.remove('failed');
resItem.classList.add('success');
} else {
resItem.classList.remove('success');
resItem.classList.add('failed');
if (result.filesystem.error != "") {
resItem.textContent += `: ${result.filesystem.error}`;
}
}
}
} catch (err) {
console.log(err);
button.textContent = i18n('button.error');
button.classList.add('failed');
} finally {
setTimeout(() => {
button.removeAttribute('aria-busy');
button.removeAttribute('disabled');
button.classList.remove('success', 'failed');
button.textContent = i18n(button.dataset.i18n);
upgradeForm.reset();
}, 10000);
}
});
}
});
</script>
</body>
</html>