Only update position if the estimated accuracy is within 500 meters (#172)

This commit is contained in:
timklge 2025-08-30 12:16:44 +02:00 committed by GitHub
parent d56a220ae1
commit b809a48631
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package de.timklge.karooheadwind
import io.hammerhead.karooext.KarooSystemService import io.hammerhead.karooext.KarooSystemService
import io.hammerhead.karooext.models.ActiveRidePage import io.hammerhead.karooext.models.ActiveRidePage
import io.hammerhead.karooext.models.OnLocationChanged
import io.hammerhead.karooext.models.OnNavigationState import io.hammerhead.karooext.models.OnNavigationState
import io.hammerhead.karooext.models.OnStreamState import io.hammerhead.karooext.models.OnStreamState
import io.hammerhead.karooext.models.RideState import io.hammerhead.karooext.models.RideState
@ -14,7 +13,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.transform import kotlinx.coroutines.flow.transform
fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow<StreamState> { fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow<StreamState> {
@ -28,17 +26,6 @@ fun KarooSystemService.streamDataFlow(dataTypeId: String): Flow<StreamState> {
} }
} }
fun KarooSystemService.streamLocation(): Flow<OnLocationChanged> {
return callbackFlow {
val listenerId = addConsumer { event: OnLocationChanged ->
trySendBlocking(event)
}
awaitClose {
removeConsumer(listenerId)
}
}
}
fun KarooSystemService.streamNavigationState(): Flow<OnNavigationState> { fun KarooSystemService.streamNavigationState(): Flow<OnNavigationState> {
return callbackFlow { return callbackFlow {
val listenerId = addConsumer { event: OnNavigationState -> val listenerId = addConsumer { event: OnNavigationState ->

View File

@ -12,11 +12,11 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
sealed class HeadingResponse { sealed class HeadingResponse {
data object NoGps: HeadingResponse() data object NoGps: HeadingResponse()
@ -108,8 +108,9 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat
val lat = dataPoint.values[DataType.Field.LOC_LATITUDE] val lat = dataPoint.values[DataType.Field.LOC_LATITUDE]
val lng = dataPoint.values[DataType.Field.LOC_LONGITUDE] val lng = dataPoint.values[DataType.Field.LOC_LONGITUDE]
val orientation = dataPoint.values[DataType.Field.LOC_BEARING] val orientation = dataPoint.values[DataType.Field.LOC_BEARING]
val accuracy = dataPoint.values[DataType.Field.LOC_ACCURACY]
if (lat != null && lng != null) { if (lat != null && lng != null && accuracy != null && accuracy < 500) {
emit(GpsCoordinates(lat, lng, orientation)) emit(GpsCoordinates(lat, lng, orientation))
Log.i(KarooHeadwindExtension.TAG, "No last known position found, fetched initial GPS position") Log.i(KarooHeadwindExtension.TAG, "No last known position found, fetched initial GPS position")
@ -130,9 +131,21 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat
} }
} }
val gpsFlow = streamLocation() val gpsFlow = streamDataFlow(DataType.Type.LOCATION).mapNotNull { it as? StreamState.Streaming }
.filter { it.orientation != null } .mapNotNull { dataPoint ->
.map { GpsCoordinates(it.lat, it.lng, it.orientation) } val lat = dataPoint.dataPoint.values[DataType.Field.LOC_LATITUDE]
val lng = dataPoint.dataPoint.values[DataType.Field.LOC_LONGITUDE]
val orientation = dataPoint.dataPoint.values[DataType.Field.LOC_BEARING]
val accuracy = dataPoint.dataPoint.values[DataType.Field.LOC_ACCURACY]
Log.i(KarooHeadwindExtension.TAG, "Received GPS update: lat=$lat, lng=$lng, accuracy=$accuracy, orientation=$orientation")
if (lat != null && lng != null && accuracy != null && accuracy < 500) {
GpsCoordinates(lat, lng, orientation)
} else {
null
}
}
val concatenatedFlow = concatenate(initialFlow, gpsFlow) val concatenatedFlow = concatenate(initialFlow, gpsFlow)