Offline-Karten Runde 4: Minimal-Speicher-Modell (Modell Rene)

Funkloch-Gedaechtnis = Quelle der Wahrheit, Kacheln = ableitbarer Cache:
- Ephemeres Vorausladen: Prefetch-Kacheln werden bei Aufzeichnungsende
  geloescht, wenn die Runde kein Funkloch hatte (nur neue Keys)
- Netz-Probe bei Aufzeichnung (~2min, 6s-Timeout): erkennt Funkloecher auch
  in bereits gespeicherten Gebieten (dort kein Remote-Miss als Signal)
- clear() behaelt Zonen (filled=false) -> naechster Online-Start laedt
  Funkloch-Gebiete automatisch neu, auch nach 'Alles loeschen'
- Start-Check mit Position: nur Zonen im 50-km-Umkreis, naechste zuerst,
  Coverage-Verify (faengt Eviction ab); Pfote Segment 5 = alle Zonen gefuellt
- Coverage-Layer zweifarbig: Funkloch orange, manuell blau + Modal-Legende
- Stub-Tests Runde 4 (Prune/Probe/clear/Naehe/Verify/Faerbung) bestanden
Bump v1230
This commit is contained in:
rene 2026-06-06 12:46:12 +02:00
parent 763108fa7c
commit 53bc27faa3
10 changed files with 221 additions and 74 deletions

View file

@ -37,6 +37,7 @@ window.MapOffline = (function () {
}
var _get = function (k) { return _req(STORE, 'readonly', function (os) { return os.get(k); }); };
var _put = function (k, v) { return _req(STORE, 'readwrite', function (os) { os.put(v, k); }); };
var _del = function (k) { return _req(STORE, 'readwrite', function (os) { os.delete(k); }); };
var _count = function () { return _req(STORE, 'readonly', function (os) { return os.count(); }); };
var _metaGet = function (k) { return _req(META, 'readonly', function (os) { return os.get(k); }); };
var _metaPut = function (k, v) { return _req(META, 'readwrite', function (os) { os.put(v, k); }); };
@ -291,8 +292,8 @@ window.MapOffline = (function () {
.then(function (gb) { state.bytes += gb; return _cachePois(_bboxAround(lat, lon, radiusKm)); })
.then(function (pc) {
poiCount = pc;
return _addRegion({ lat: lat, lon: lon, radiusKm: radiusKm, tiles: state.stored,
bytes: state.bytes, pois: poiCount, savedAt: Date.now() });
return _addRegion({ type: opts.type || 'gebiet', lat: lat, lon: lon, radiusKm: radiusKm,
tiles: state.stored, bytes: state.bytes, pois: poiCount, savedAt: Date.now() });
})
.then(function () { return _bumpTotal(state.bytes); })
.then(function () { return { tiles: state.stored, bytes: state.bytes, pois: poiCount, radiusKm: radiusKm }; });
@ -311,7 +312,7 @@ window.MapOffline = (function () {
function _bumpTotal(bytes) {
if (!bytes) return Promise.resolve();
return _metaGet('totalBytes')
.then(function (t) { return _metaPut('totalBytes', (t || 0) + bytes); })
.then(function (t) { return _metaPut('totalBytes', Math.max(0, (t || 0) + bytes)); })
.catch(function () {});
}
function _overCap() {
@ -330,14 +331,23 @@ window.MapOffline = (function () {
} catch (e) {}
}
// {lat,lon} während aktiver Aufzeichnung, sonst null. Nebeneffekt (Runde 3):
// ROLLENDES VORAUSLADEN — solange Empfang da ist, alle ~400 m die fehlenden Kacheln
// um die aktuelle Position still mitnehmen. Deckt den Weg + die Anfahrt ab, BEVOR
// man ins Funkloch läuft (greift schon beim ersten Besuch, anders als das Gedächtnis).
// {lat,lon} während aktiver Aufzeichnung, sonst null. Nebeneffekte (Runde 3):
// 1. ROLLENDES VORAUSLADEN — solange Empfang da ist, alle ~400 m die fehlenden Kacheln
// um die aktuelle Position still mitnehmen. EPHEMER: Hatte die Runde KEIN Funkloch,
// wird das Vorausgeladene am Ende wieder gelöscht — gespeichert bleibt nur, was
// wegen fehlenden Netzes nötig ist + manuell Hinzugefügtes (Modell René 2026-06-08).
// 2. NETZ-PROBE — erkennt Funklöcher auch in BEREITS GESPEICHERTEN Gebieten (dort
// kommen Kacheln aus IndexedDB → kein Remote-Miss als Signal).
var _lastPre = null, _preActive = false;
var _preKeys = [], _preBytes = 0, _recHadDeadzone = false;
function setGps(pos) {
if (!pos) { // Aufzeichnung beendet
_gps = null; _lastPre = null;
_prunePrefetch();
return;
}
_gps = pos;
if (!pos) { _lastPre = null; return; }
_probeNet(pos);
if (_preActive || !navigator.onLine) return;
if (_lastPre && _distKm(_lastPre.lat, _lastPre.lon, pos.lat, pos.lon) < 0.4) return;
_preActive = true;
@ -349,6 +359,36 @@ window.MapOffline = (function () {
.then(function () { _preActive = false; });
}
// Aktive Netz-Probe (alle ~2 Min bei Aufzeichnung): kleiner Request mit Timeout.
// Fehlschlag = Funkloch an dieser Position — unabhängig davon, ob die Kacheln
// hier längst lokal liegen. (navigator.onLine lügt bei Schwachempfang.)
var _lastProbe = 0;
function _probeNet(pos) {
var now = Date.now();
if (now - _lastProbe < 120000) return;
_lastProbe = now;
var p = { lat: pos.lat, lon: pos.lon };
var ctrl = (typeof AbortController !== 'undefined') ? new AbortController() : null;
var timer = ctrl && setTimeout(function () { try { ctrl.abort(); } catch (e) {} }, 6000);
fetch('/api/version', { cache: 'no-store', signal: ctrl ? ctrl.signal : undefined })
.then(function (r) { if (!r.ok) throw new Error('probe ' + r.status); })
.catch(function () {
_recHadDeadzone = true;
markDeadZone(p.lat, p.lon);
})
.then(function () { if (timer) clearTimeout(timer); });
}
// Vorausgeladenes wieder entfernen, wenn die Runde KEIN Funkloch hatte.
function _prunePrefetch() {
var keys = _preKeys, bytes = _preBytes, had = _recHadDeadzone;
_preKeys = []; _preBytes = 0; _recHadDeadzone = false;
if (had || !keys.length) return Promise.resolve();
var chain = Promise.resolve();
keys.forEach(function (k) { chain = chain.then(function () { return _del(k).catch(function () {}); }); });
return chain.then(function () { return _bumpTotal(-bytes); });
}
// z14-Kacheln ±n um lat/lon (+ Eltern z1013) — NUR fehlende, still, ohne Region-Eintrag.
function _prefetchRing(lat, lon, n) {
var cx = _x(lon, MAXZOOM), cy = _y(lat, MAXZOOM), seen = {}, list = [];
@ -368,7 +408,12 @@ window.MapOffline = (function () {
var state = { done: 0, bytes: 0, stored: 0 };
return chain
.then(function () { return missing.length ? _fetchTiles(missing, state, null) : null; })
.then(function () { return _bumpTotal(state.bytes); });
.then(function () {
// Nur NEU gespeicherte Keys fürs Prune merken — Bestand bleibt unangetastet.
missing.forEach(function (t) { _preKeys.push(t[0] + '/' + t[1] + '/' + t[2]); });
_preBytes += state.bytes;
return _bumpTotal(state.bytes);
});
}
function _distKm(aLat, aLon, bLat, bLon) {
@ -378,6 +423,7 @@ window.MapOffline = (function () {
function _noteRemoteMiss() {
if (!_gps) return;
_recHadDeadzone = true; // Vorausgeladenes dieser Runde behalten
var now = Date.now();
if (now - _lastZoneNote < 120000) return; // max. 1 Eintrag / 2 Min
_lastZoneNote = now;
@ -395,32 +441,55 @@ window.MapOffline = (function () {
}).catch(function () {});
}
// Offene Funkloch-Zonen budget-getrieben nachladen (nur online sinnvoll).
// Dort wo Netz verfügbar ist, braucht man keine Offline-Karten — gespeichert
// wird gezielt da, wo es ausfiel. Liefert die Anzahl gefüllter Zonen.
// Funkloch-Zonen nachladen (nur online sinnvoll). Dort wo Netz verfügbar ist, braucht
// man keine Offline-Karten — gespeichert wird gezielt da, wo es ausfiel.
// opts {lat, lon, maxKm:50}: mit Position werden nur Zonen im Umkreis betrachtet
// (Speicher minimal halten) und nächstgelegene zuerst geladen. Gefüllte Zonen werden
// VERIFIZIERT (Zentrums-Kachel noch da?) — fängt „Alles löschen" und iOS-Eviction ab:
// die Gebiete kommen beim nächsten Start automatisch wieder (Modell René 2026-06-08).
// Liefert die Anzahl (neu) gefüllter Zonen.
var _autofillActive = false;
function autoFillDeadZones() {
function autoFillDeadZones(opts) {
opts = opts || {};
var maxKm = opts.maxKm || 50;
if (_autofillActive || !navigator.onLine) return Promise.resolve(0);
_autofillActive = true;
var filled = 0;
var filled = 0, all = null;
return _overCap().then(function (over) {
if (over) return null; // Speicher-Cap erreicht → kein automatisches Nachladen mehr
return _metaGet('deadzones');
}).then(function (zones) {
if (zones === null) return 0;
zones = zones || [];
var open = zones.filter(function (z) { return !z.filled; });
if (!open.length) return 0;
var chain = Promise.resolve();
open.forEach(function (z) {
chain = chain.then(function () {
return downloadAround(z.lat, z.lon, { budgetMB: 5 }).then(function (res) {
if (res.bytes > 0) { z.filled = true; filled++; }
}).catch(function () {});
if (zones === null || zones === undefined) return 0;
all = zones;
var cand = opts.lat == null ? zones.slice() : zones.filter(function (z) {
return _distKm(z.lat, z.lon, opts.lat, opts.lon) <= maxKm;
});
if (!cand.length) return 0;
// Verify: als gefüllt markierte Zonen ohne lokale Zentrums-Kachel wieder öffnen.
var verify = Promise.resolve();
cand.forEach(function (z) {
verify = verify.then(function () {
if (!z.filled) return;
return _get(MAXZOOM + '/' + _x(z.lon, MAXZOOM) + '/' + _y(z.lat, MAXZOOM))
.then(function (hit) { if (!hit) z.filled = false; });
});
});
return chain.then(function () { return _metaPut('deadzones', zones); })
.then(function () { return filled; });
return verify.then(function () {
var open = cand.filter(function (z) { return !z.filled; });
if (opts.lat != null) open.sort(function (a, b) {
return _distKm(a.lat, a.lon, opts.lat, opts.lon) - _distKm(b.lat, b.lon, opts.lat, opts.lon);
});
var chain = Promise.resolve();
open.forEach(function (z) {
chain = chain.then(function () {
return downloadAround(z.lat, z.lon, { budgetMB: 5, type: 'funkloch' }).then(function (res) {
if (res.bytes > 0) { z.filled = true; filled++; }
}).catch(function () {});
});
});
return chain.then(function () { return _metaPut('deadzones', all); })
.then(function () { return filled; });
});
}).then(function (n) { _autofillActive = false; return n; },
function () { _autofillActive = false; return 0; });
}
@ -521,25 +590,35 @@ window.MapOffline = (function () {
}
// ---- Coverage-Layer ----------------------------------------------------------
// GeoJSON der gespeicherten z14-Kacheln — zeigt auf der Karte, welche Bereiche offline da sind.
// GeoJSON der gespeicherten z14-Kacheln — zeigt auf der Karte, welche Bereiche offline
// da sind. properties.kind: 'funkloch' (Kachel liegt in einer Funkloch-Region, wird
// anders eingefärbt — Wunsch René 2026-06-08) oder 'manuell'.
function _tile2lat(y, z) {
var m = Math.PI - 2 * Math.PI * y / Math.pow(2, z);
return 180 / Math.PI * Math.atan(0.5 * (Math.exp(m) - Math.exp(-m)));
}
function coverage() {
var re = new RegExp('^' + MAXZOOM + '/(\\d+)/(\\d+)$');
return _req(STORE, 'readonly', function (os) { return os.getAllKeys(); }).then(function (keys) {
var feats = [];
(keys || []).forEach(function (k) {
var m = re.exec(k);
if (!m) return;
var x = +m[1], y = +m[2], n2 = Math.pow(2, MAXZOOM);
var west = x / n2 * 360 - 180, east = (x + 1) / n2 * 360 - 180;
var north = _tile2lat(y, MAXZOOM), south = _tile2lat(y + 1, MAXZOOM);
feats.push({ type: 'Feature', properties: {}, geometry: { type: 'Polygon',
coordinates: [[[west, south], [east, south], [east, north], [west, north], [west, south]]] } });
return _metaGet('regions').then(function (regions) {
var fz = (regions || []).filter(function (r) { return r.type === 'funkloch'; });
return _req(STORE, 'readonly', function (os) { return os.getAllKeys(); }).then(function (keys) {
var feats = [];
(keys || []).forEach(function (k) {
var m = re.exec(k);
if (!m) return;
var x = +m[1], y = +m[2], n2 = Math.pow(2, MAXZOOM);
var west = x / n2 * 360 - 180, east = (x + 1) / n2 * 360 - 180;
var north = _tile2lat(y, MAXZOOM), south = _tile2lat(y + 1, MAXZOOM);
var cLat = (north + south) / 2, cLon = (west + east) / 2;
var isFz = fz.some(function (r) {
return _distKm(r.lat, r.lon, cLat, cLon) <= (r.radiusKm || 5) + 0.8;
});
feats.push({ type: 'Feature', properties: { kind: isFz ? 'funkloch' : 'manuell' },
geometry: { type: 'Polygon',
coordinates: [[[west, south], [east, south], [east, north], [west, north], [west, south]]] } });
});
return { type: 'FeatureCollection', features: feats };
});
return { type: 'FeatureCollection', features: feats };
});
}
@ -555,9 +634,20 @@ window.MapOffline = (function () {
});
}
function hasRegion() { return stats().then(function (s) { return s.count > 0; }).catch(function () { return false; }); }
// „Alles löschen" entfernt Kacheln/Marker/Regionen — das FUNKLOCH-GEDÄCHTNIS bleibt
// (Quelle der Wahrheit, Modell René 2026-06-08): Zonen werden auf filled:false gesetzt
// und beim nächsten Online-Start in Positionsnähe automatisch neu geladen.
function clear() {
return _req(STORE, 'readwrite', function (os) { os.clear(); })
.then(function () { return _req(META, 'readwrite', function (os) { os.clear(); }); });
.then(function () { return _metaGet('deadzones'); })
.then(function (zones) {
return _req(META, 'readwrite', function (os) { os.clear(); }).then(function () {
if (zones && zones.length) {
zones.forEach(function (z) { z.filled = false; });
return _metaPut('deadzones', zones);
}
});
});
}
return {