fix: retain last valid gps data

ref:
This commit is contained in:
Janez T
2026-02-28 14:29:46 +01:00
parent 880cee5477
commit d587b1eeb1
2 changed files with 133 additions and 6 deletions

View File

@@ -335,12 +335,9 @@ class ContactsProvider with ChangeNotifier {
debugPrint(' ✅ Parsed new telemetry');
debugPrint(' New telemetry timestamp: ${telemetry.timestamp}');
// Ignore placeholder GPS coordinates (0,0) so we don't overwrite a
// previously known/saved position with invalid telemetry data.
if (_isInvalidTelemetryGps(telemetry.gpsLocation)) {
debugPrint(
' ⚠️ Ignoring invalid telemetry GPS coordinates: ${telemetry.gpsLocation}',
);
final incomingGps = telemetry.gpsLocation;
if (_isInvalidTelemetryGps(incomingGps)) {
// Always sanitize invalid placeholder GPS to null first.
telemetry = ContactTelemetry(
gpsLocation: null,
batteryPercentage: telemetry.batteryPercentage,
@@ -353,6 +350,25 @@ class ContactsProvider with ChangeNotifier {
);
}
// Keep last valid GPS for router/chat/room contacts when current
// telemetry does not provide a valid GPS fix.
if (_shouldRetainLastValidGps(contact, telemetry.gpsLocation)) {
debugPrint(
' ⚠️ Retaining last valid GPS. Incoming telemetry GPS is invalid/missing: $incomingGps',
);
final previousGps = _getValidGpsOrNull(contact.telemetry?.gpsLocation);
telemetry = ContactTelemetry(
gpsLocation: previousGps,
batteryPercentage: telemetry.batteryPercentage,
batteryMilliVolts: telemetry.batteryMilliVolts,
temperature: telemetry.temperature,
timestamp: telemetry.timestamp,
humidity: telemetry.humidity,
pressure: telemetry.pressure,
extraSensorData: telemetry.extraSensorData,
);
}
// Update contact with new telemetry AND last seen time
// lastAdvert is Unix timestamp in seconds
final currentTimestamp = (DateTime.now().millisecondsSinceEpoch / 1000)
@@ -390,6 +406,29 @@ class ContactsProvider with ChangeNotifier {
return lat.abs() < epsilon && lon.abs() < epsilon;
}
LatLng? _getValidGpsOrNull(LatLng? location) {
if (location == null || _isInvalidTelemetryGps(location)) {
return null;
}
return location;
}
bool _shouldRetainLastValidGps(Contact contact, LatLng? incomingGps) {
final isSupportedType =
contact.isChat || contact.isRepeater || contact.isRoom;
if (!isSupportedType) {
return false;
}
final hasPreviousValidGps =
_getValidGpsOrNull(contact.telemetry?.gpsLocation) != null;
if (!hasPreviousValidGps) {
return false;
}
return incomingGps == null;
}
/// Find contact by public key prefix (6 bytes)
Contact? _findContactByPrefix(Uint8List prefix) {
if (prefix.length < 6) return null;