diff --git a/android/app/src/main/kotlin/dev/fiatcode/tracpulse/service/LocationTrackingService.kt b/android/app/src/main/kotlin/dev/fiatcode/tracpulse/service/LocationTrackingService.kt index e66c453..282602b 100644 --- a/android/app/src/main/kotlin/dev/fiatcode/tracpulse/service/LocationTrackingService.kt +++ b/android/app/src/main/kotlin/dev/fiatcode/tracpulse/service/LocationTrackingService.kt @@ -323,24 +323,47 @@ class LocationTrackingService : Service() { } if (finalSpeed == null || finalSpeed <= 0) { - logEvent("SPEED_CALC", "Speed calculation failed - insufficient data") - return + logEvent("SPEED_CALC", "Speed calculation failed - sending without speed") + // Fallback: send location without speed rather than losing it + } else { + logEvent("LOCATION", "Lat: ${location.latitude}, Lon: ${location.longitude}, Calc Speed: ${finalSpeed}") } + val speedToSend = if (finalSpeed != null && finalSpeed > 0) finalSpeed else null + val traccarLocation = TraccarLocation( timestamp = location.time, latitude = location.latitude, longitude = location.longitude, accuracy = if (location.hasAccuracy()) location.accuracy else null, - speed = finalSpeed, + speed = speedToSend, heading = if (location.hasBearing()) location.bearing else null, altitude = if (location.hasAltitude()) location.altitude else null, - isMoving = finalSpeed > 1.0f + isMoving = (speedToSend ?: 0f) > 1.0f ) - logEvent("LOCATION", "Lat: ${location.latitude}, Lon: ${location.longitude}, Calc Speed: ${finalSpeed}") + updateNotificationSpeed(location, speedToSend) - updateNotificationSpeed(location, finalSpeed) + BridgeModule.eventSink?.success(mapOf( + "latitude" to location.latitude, + "longitude" to location.longitude, + "accuracy" to if (location.hasAccuracy()) location.accuracy else null, + "speed" to speedToSend, + "heading" to if (location.hasBearing()) location.bearing else null, + "altitude" to if (location.hasAltitude()) location.altitude else null, + "timestamp" to location.time + )) + + prefs.edit().apply { + putFloat("last_latitude", location.latitude.toFloat()) + putFloat("last_longitude", location.longitude.toFloat()) + if (location.hasAccuracy()) putFloat("last_accuracy", location.accuracy) + if (speedToSend != null) putFloat("last_speed", speedToSend) + if (location.hasBearing() && location.bearing >= 0) putFloat("last_heading", location.bearing) + if (location.hasAltitude() && location.altitude != 0.0) putFloat("last_altitude", location.altitude.toFloat()) + putLong("last_timestamp", location.time) + apply() + } if (isNetworkAvailable) { sendLocationToServer(traccarLocation) @@ -480,10 +503,14 @@ class LocationTrackingService : Service() { notificationManager.notify(NOTIFICATION_ID, notification) } - private fun updateNotificationSpeed(location: Location, speed: Float) { + private fun updateNotificationSpeed(location: Location, speed: Float?) { val content = buildString { append("Lat: %.4f Lon: %.4f".format(location.latitude, location.longitude)) - append("\nSpeed: %.0f km/h (calc)".format(speed * 3.6)) + if (speed != null && speed > 0) { + append("\nSpeed: %.0f km/h (calc)".format(speed * 3.6)) + } else { + append("\nSpeed: -- km/h") + } val time = java.text.SimpleDateFormat("HH:mm:ss", java.util.Locale.getDefault()) .format(java.util.Date(location.time)) append("\nLast: $time")