Compare commits
1 Commits
da8583bd3e
...
b4e5f42007
| Author | SHA1 | Date | |
|---|---|---|---|
| b4e5f42007 |
@ -2,6 +2,7 @@ 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
|
||||
@ -13,6 +14,7 @@ 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> {
|
||||
@ -26,6 +28,17 @@ 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 ->
|
||||
|
||||
@ -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,9 +108,8 @@ 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 && accuracy != null && accuracy < 500) {
|
||||
if (lat != null && lng != null) {
|
||||
emit(GpsCoordinates(lat, lng, orientation))
|
||||
|
||||
Log.i(KarooHeadwindExtension.TAG, "No last known position found, fetched initial GPS position")
|
||||
@ -131,21 +130,9 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat
|
||||
}
|
||||
}
|
||||
|
||||
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 gpsFlow = streamLocation()
|
||||
.filter { it.orientation != null }
|
||||
.map { GpsCoordinates(it.lat, it.lng, it.orientation) }
|
||||
|
||||
val concatenatedFlow = concatenate(initialFlow, gpsFlow)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user