Skip to content

Commit 75f235a

Browse files
authored
Merge pull request #49 from MrAlders0n/copilot/improve-gps-status-feedback
Add GPS status feedback and freshness validation for ping operations
2 parents 5c00154 + 2186af0 commit 75f235a

1 file changed

Lines changed: 63 additions & 20 deletions

File tree

content/wardrive.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -653,20 +653,65 @@ function scheduleApiPostAndMapRefresh(lat, lon, accuracy) {
653653
}
654654

655655
// ---- Ping ----
656+
/**
657+
* Acquire fresh GPS coordinates and update state
658+
* @returns {Promise<{lat: number, lon: number, accuracy: number}>} GPS coordinates
659+
* @throws {Error} If GPS position cannot be acquired
660+
*/
661+
async function acquireFreshGpsPosition() {
662+
const pos = await getCurrentPosition();
663+
const coords = {
664+
lat: pos.coords.latitude,
665+
lon: pos.coords.longitude,
666+
accuracy: pos.coords.accuracy
667+
};
668+
debugLog(`Fresh GPS acquired: lat=${coords.lat.toFixed(5)}, lon=${coords.lon.toFixed(5)}, accuracy=${coords.accuracy}m`);
669+
670+
state.lastFix = {
671+
lat: coords.lat,
672+
lon: coords.lon,
673+
accM: coords.accuracy,
674+
tsMs: Date.now()
675+
};
676+
updateGpsUi();
677+
678+
return coords;
679+
}
680+
656681
/**
657682
* Get GPS coordinates for ping operation
658683
* @param {boolean} isAutoMode - Whether this is an auto ping
659684
* @returns {Promise<{lat: number, lon: number, accuracy: number}|null>} GPS coordinates or null if unavailable
660685
*/
661686
async function getGpsCoordinatesForPing(isAutoMode) {
662687
if (isAutoMode) {
663-
// Auto mode: use GPS watch data only
688+
// Auto mode: validate GPS freshness before sending
664689
if (!state.lastFix) {
665690
debugWarn("Auto ping skipped: no GPS fix available yet");
666691
setStatus("Waiting for GPS fix...", STATUS_COLORS.warning);
667692
return null;
668693
}
669-
debugLog(`Using GPS watch data: lat=${state.lastFix.lat.toFixed(5)}, lon=${state.lastFix.lon.toFixed(5)}`);
694+
695+
// Check if GPS data is too old for auto ping
696+
const ageMs = Date.now() - state.lastFix.tsMs;
697+
const intervalMs = getSelectedIntervalMs();
698+
const maxAge = intervalMs + GPS_FRESHNESS_BUFFER_MS;
699+
700+
if (ageMs >= maxAge) {
701+
debugLog(`GPS data too old for auto ping (${ageMs}ms), attempting to refresh`);
702+
setStatus("GPS data old, trying to refresh position", STATUS_COLORS.warning);
703+
704+
try {
705+
return await acquireFreshGpsPosition();
706+
} catch (e) {
707+
debugError(`Could not refresh GPS position for auto ping: ${e.message}`, e);
708+
const intervalSec = Math.ceil(intervalMs / 1000);
709+
setStatus(`GPS could not refresh position, skipping ping. Next attempt (${intervalSec}s)`, STATUS_COLORS.error);
710+
return null;
711+
}
712+
}
713+
714+
debugLog(`Using GPS watch data: lat=${state.lastFix.lat.toFixed(5)}, lon=${state.lastFix.lon.toFixed(5)} (age: ${ageMs}ms)`);
670715
return {
671716
lat: state.lastFix.lat,
672717
lon: state.lastFix.lon,
@@ -707,27 +752,18 @@ async function getGpsCoordinatesForPing(isAutoMode) {
707752

708753
// Data exists but is too old
709754
debugLog(`GPS data too old (${ageMs}ms), requesting fresh position`);
755+
setStatus("GPS data too old, requesting fresh position", STATUS_COLORS.warning);
710756
}
711757

712758
// Get fresh GPS coordinates for manual ping
713759
debugLog("Requesting fresh GPS position for manual ping");
714-
const pos = await getCurrentPosition();
715-
const coords = {
716-
lat: pos.coords.latitude,
717-
lon: pos.coords.longitude,
718-
accuracy: pos.coords.accuracy
719-
};
720-
debugLog(`Fresh GPS acquired: lat=${coords.lat.toFixed(5)}, lon=${coords.lon.toFixed(5)}, accuracy=${coords.accuracy}m`);
721-
722-
state.lastFix = {
723-
lat: coords.lat,
724-
lon: coords.lon,
725-
accM: coords.accuracy,
726-
tsMs: Date.now()
727-
};
728-
updateGpsUi();
729-
730-
return coords;
760+
try {
761+
return await acquireFreshGpsPosition();
762+
} catch (e) {
763+
debugError(`Could not get fresh GPS location: ${e.message}`, e);
764+
// Note: "Error:" prefix is intentional per UX requirements for manual ping timeout
765+
throw new Error("Error: could not get fresh GPS location");
766+
}
731767
}
732768

733769
/**
@@ -776,7 +812,14 @@ async function sendPing(manual = false) {
776812

777813
// Get GPS coordinates
778814
const coords = await getGpsCoordinatesForPing(!manual && state.running);
779-
if (!coords) return; // GPS not available, message already shown
815+
if (!coords) {
816+
// GPS not available, message already shown
817+
// For auto mode, schedule next attempt
818+
if (!manual && state.running) {
819+
scheduleNextAutoPing();
820+
}
821+
return;
822+
}
780823

781824
const { lat, lon, accuracy } = coords;
782825

0 commit comments

Comments
 (0)