feat(scale): auto-discover gateway on local network

- api/scale_discover.php: async TCP scan of whole /24 subnet on port 8765,
  confirms with WebSocket handshake, returns found ws:// URLs in ~1.5s
- index.html: '🔍 Auto' button next to gateway URL field
- app.js: discoverScaleGateway() — calls relay, fills URL field and
  auto-saves settings + reconnects on success
This commit is contained in:
dadaloop82
2026-04-15 20:56:54 +00:00
parent 099a6cc4e8
commit 55c5b34381
3 changed files with 186 additions and 2 deletions
+38
View File
@@ -247,6 +247,44 @@ function testScaleConnection() {
});
}
async function discoverScaleGateway() {
const btn = document.getElementById('btn-scale-discover');
const status = document.getElementById('scale-discover-status');
if (!btn || !status) return;
btn.disabled = true;
btn.textContent = '⏳';
status.style.display = 'block';
status.textContent = '🔍 Scanning local network for scale gateway…';
try {
const res = await fetch('/api/scale_discover.php', { signal: AbortSignal.timeout(8000) });
const data = await res.json();
if (data.error) {
status.textContent = '❌ ' + data.error;
} else if (data.found && data.found.length > 0) {
const url = data.found[0];
const urlEl = document.getElementById('setting-scale-url');
if (urlEl) urlEl.value = url;
status.textContent = '✅ Gateway found: ' + url + (data.found.length > 1 ? ' (+' + (data.found.length - 1) + ' more)' : '');
status.style.color = 'var(--color-success, #059669)';
// Auto-save
const s = getSettings();
s.scale_gateway_url = url;
saveSettingsToStorage(s);
scaleInit();
} else {
status.textContent = '❌ No gateway found on ' + (data.subnet || 'local network') + '. Make sure the Android app is running and on the same Wi-Fi.';
}
} catch(e) {
status.textContent = '❌ Discovery failed: ' + (e.message || 'timeout');
}
btn.disabled = false;
btn.textContent = '🔍 Auto';
}
// ===== i18n TRANSLATION SYSTEM =====
let _i18nStrings = null; // current language translations (flat)
let _i18nFallback = null; // Italian fallback (flat)