Compare commits

..

2 Commits

Author SHA1 Message Date
da8583bd3e Update changelog
All checks were successful
Build / build (push) Successful in 7m57s
2025-09-01 18:42:56 +02:00
timklge
b809a48631
Only update position if the estimated accuracy is within 500 meters (#172) 2025-08-30 12:16:44 +02:00
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.models.ActiveRidePage
import io.hammerhead.karooext.models.OnLocationChanged
import io.hammerhead.karooext.models.OnNavigationState
import io.hammerhead.karooext.models.OnStreamState
import io.hammerhead.karooext.models.RideState
@ -14,7 +13,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.transform
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> {
return callbackFlow {
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.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
sealed class 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 lng = dataPoint.values[DataType.Field.LOC_LONGITUDE]
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))
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()
.filter { it.orientation != null }
.map { GpsCoordinates(it.lat, it.lng, it.orientation) }
val gpsFlow = streamDataFlow(DataType.Type.LOCATION).mapNotNull { it as? StreamState.Streaming }
.mapNotNull { dataPoint ->
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)