Fix observer location persistence and APRS defaults

This commit is contained in:
Smittix
2026-03-15 17:49:46 +00:00
parent 6b9c4ebebd
commit b5115d4aa1
5 changed files with 256 additions and 71 deletions

View File

@@ -1,9 +1,6 @@
// Shared observer location helper for map-based modules.
// Default: shared location enabled unless explicitly disabled via config.
window.ObserverLocation = (function() {
const DEFAULT_LOCATION = (window.INTERCEPT_DEFAULT_LAT && window.INTERCEPT_DEFAULT_LON)
? { lat: window.INTERCEPT_DEFAULT_LAT, lon: window.INTERCEPT_DEFAULT_LON }
: { lat: 51.5074, lon: -0.1278 };
const SHARED_KEY = 'observerLocation';
const AIS_KEY = 'ais_observerLocation';
const LEGACY_LAT_KEY = 'observerLat';
@@ -21,6 +18,9 @@ window.ObserverLocation = (function() {
return { lat: latNum, lon: lonNum };
}
const DEFAULT_LOCATION = normalize(window.INTERCEPT_DEFAULT_LAT, window.INTERCEPT_DEFAULT_LON)
|| { lat: 51.5074, lon: -0.1278 };
function parseLocation(raw) {
if (!raw) return null;
try {
@@ -39,7 +39,7 @@ window.ObserverLocation = (function() {
function readLegacyLatLon() {
const lat = localStorage.getItem(LEGACY_LAT_KEY);
const lon = localStorage.getItem(LEGACY_LON_KEY);
if (!lat || !lon) return null;
if (lat === null || lon === null) return null;
return normalize(lat, lon);
}
@@ -60,11 +60,12 @@ window.ObserverLocation = (function() {
}
function setShared(location, options = {}) {
if (!location) return;
localStorage.setItem(SHARED_KEY, JSON.stringify(location));
const normalized = location ? normalize(location.lat, location.lon) : null;
if (!normalized) return;
localStorage.setItem(SHARED_KEY, JSON.stringify(normalized));
if (options.updateLegacy !== false) {
localStorage.setItem(LEGACY_LAT_KEY, location.lat.toString());
localStorage.setItem(LEGACY_LON_KEY, location.lon.toString());
localStorage.setItem(LEGACY_LAT_KEY, normalized.lat.toString());
localStorage.setItem(LEGACY_LON_KEY, normalized.lon.toString());
}
}
@@ -84,16 +85,17 @@ window.ObserverLocation = (function() {
}
function setForModule(moduleKey, location, options = {}) {
if (!location) return;
const normalized = location ? normalize(location.lat, location.lon) : null;
if (!normalized) return;
if (isSharedEnabled()) {
setShared(location, options);
setShared(normalized, options);
return;
}
if (moduleKey) {
localStorage.setItem(moduleKey, JSON.stringify(location));
localStorage.setItem(moduleKey, JSON.stringify(normalized));
} else if (options.fallbackToLatLon) {
localStorage.setItem(LEGACY_LAT_KEY, location.lat.toString());
localStorage.setItem(LEGACY_LON_KEY, location.lon.toString());
localStorage.setItem(LEGACY_LAT_KEY, normalized.lat.toString());
localStorage.setItem(LEGACY_LON_KEY, normalized.lon.toString());
}
}

View File

@@ -896,23 +896,26 @@ function loadObserverLocation() {
lon = shared.lon.toString();
}
const hasLat = lat !== undefined && lat !== null && lat !== '';
const hasLon = lon !== undefined && lon !== null && lon !== '';
const latInput = document.getElementById('observerLatInput');
const lonInput = document.getElementById('observerLonInput');
const currentLatDisplay = document.getElementById('currentLatDisplay');
const currentLonDisplay = document.getElementById('currentLonDisplay');
if (latInput && lat) latInput.value = lat;
if (lonInput && lon) lonInput.value = lon;
if (latInput && hasLat) latInput.value = lat;
if (lonInput && hasLon) lonInput.value = lon;
if (currentLatDisplay) {
currentLatDisplay.textContent = lat ? parseFloat(lat).toFixed(4) + '°' : 'Not set';
currentLatDisplay.textContent = hasLat ? parseFloat(lat).toFixed(4) + '°' : 'Not set';
}
if (currentLonDisplay) {
currentLonDisplay.textContent = lon ? parseFloat(lon).toFixed(4) + '°' : 'Not set';
currentLonDisplay.textContent = hasLon ? parseFloat(lon).toFixed(4) + '°' : 'Not set';
}
// Sync dashboard-specific location keys for backward compatibility
if (lat !== undefined && lat !== null && lat !== '' && lon !== undefined && lon !== null && lon !== '') {
if (hasLat && hasLon) {
const locationObj = JSON.stringify({ lat: parseFloat(lat), lon: parseFloat(lon) });
if (!localStorage.getItem('observerLocation')) {
localStorage.setItem('observerLocation', locationObj);
@@ -1011,9 +1014,9 @@ function detectLocationGPS(btn) {
}
/**
* Save observer location to localStorage
* Save observer location to localStorage and persist defaults to .env
*/
function saveObserverLocation() {
async function saveObserverLocation() {
const latInput = document.getElementById('observerLatInput');
const lonInput = document.getElementById('observerLonInput');
@@ -1056,19 +1059,40 @@ function saveObserverLocation() {
if (currentLatDisplay) currentLatDisplay.textContent = lat.toFixed(4) + '°';
if (currentLonDisplay) currentLonDisplay.textContent = lon.toFixed(4) + '°';
if (typeof showNotification === 'function') {
showNotification('Location', 'Observer location saved');
}
if (window.observerLocation) {
window.observerLocation.lat = lat;
window.observerLocation.lon = lon;
}
let notificationMessage = 'Observer location saved';
try {
const response = await fetch('/settings/observer-location', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ lat, lon }),
});
const data = await response.json().catch(() => ({}));
if (!response.ok || data.status === 'error') {
throw new Error(data.message || 'Failed to save observer location to .env');
}
window.INTERCEPT_DEFAULT_LAT = lat;
window.INTERCEPT_DEFAULT_LON = lon;
notificationMessage = 'Observer location saved to settings and .env';
} catch (error) {
notificationMessage = `Observer location saved for this browser, but .env update failed: ${error.message}`;
}
// Refresh SSTV ISS schedule if available
if (typeof SSTV !== 'undefined' && typeof SSTV.loadIssSchedule === 'function') {
SSTV.loadIssSchedule();
}
if (typeof showNotification === 'function') {
showNotification('Location', notificationMessage);
}
}
// =============================================================================