* fix #8: Use new location change event * Skip location values without orientation, remove averaging
This commit is contained in:
parent
d0089e755b
commit
0869121176
@ -15,8 +15,8 @@ android {
|
|||||||
applicationId = "de.timklge.karooheadwind"
|
applicationId = "de.timklge.karooheadwind"
|
||||||
minSdk = 26
|
minSdk = 26
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 9
|
versionCode = 10
|
||||||
versionName = "1.2.1"
|
versionName = "1.2.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
"packageName": "de.timklge.karooheadwind",
|
"packageName": "de.timklge.karooheadwind",
|
||||||
"iconUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/karoo-headwind.png",
|
"iconUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/karoo-headwind.png",
|
||||||
"latestApkUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/app-release.apk",
|
"latestApkUrl": "https://github.com/timklge/karoo-headwind/releases/latest/download/app-release.apk",
|
||||||
"latestVersion": "1.2.1",
|
"latestVersion": "1.2.2",
|
||||||
"latestVersionCode": 9,
|
"latestVersionCode": 10,
|
||||||
"developer": "timklge",
|
"developer": "timklge",
|
||||||
"description": "Provides headwind direction, wind speed and other weather data fields",
|
"description": "Provides headwind direction, wind speed and other weather data fields",
|
||||||
"releaseNotes": "Add tailwind and ride speed data field, reduce font size in tailwind indicators"
|
"releaseNotes": "Reduce font size in tailwind indicators, update gps bearing retrieval"
|
||||||
}
|
}
|
||||||
@ -32,6 +32,7 @@ import io.hammerhead.karooext.KarooSystemService
|
|||||||
import io.hammerhead.karooext.models.DataType
|
import io.hammerhead.karooext.models.DataType
|
||||||
import io.hammerhead.karooext.models.HttpResponseState
|
import io.hammerhead.karooext.models.HttpResponseState
|
||||||
import io.hammerhead.karooext.models.OnHttpResponse
|
import io.hammerhead.karooext.models.OnHttpResponse
|
||||||
|
import io.hammerhead.karooext.models.OnLocationChanged
|
||||||
import io.hammerhead.karooext.models.OnStreamState
|
import io.hammerhead.karooext.models.OnStreamState
|
||||||
import io.hammerhead.karooext.models.StreamState
|
import io.hammerhead.karooext.models.StreamState
|
||||||
import io.hammerhead.karooext.models.UserProfile
|
import io.hammerhead.karooext.models.UserProfile
|
||||||
@ -72,6 +73,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<T> Flow<T>.throttle(timeout: Long): Flow<T> = flow {
|
fun<T> Flow<T>.throttle(timeout: Long): Flow<T> = flow {
|
||||||
var lastEmissionTime = 0L
|
var lastEmissionTime = 0L
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ 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.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
@ -57,26 +58,6 @@ fun KarooSystemService.getHeadingFlow(context: Context): Flow<HeadingResponse> {
|
|||||||
headingValue ?: HeadingResponse.NoGps
|
headingValue ?: HeadingResponse.NoGps
|
||||||
}
|
}
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.scan(emptyList<HeadingResponse>()) { acc, value -> /* Average over 3 values */
|
|
||||||
if (value !is HeadingResponse.Value) return@scan listOf(value)
|
|
||||||
|
|
||||||
val newAcc = acc + value
|
|
||||||
if (newAcc.size > 3) newAcc.drop(1) else newAcc
|
|
||||||
}
|
|
||||||
.map { data ->
|
|
||||||
Log.i(KarooHeadwindExtension.TAG, "Heading value: $data")
|
|
||||||
|
|
||||||
if (data.isEmpty()) return@map HeadingResponse.NoGps
|
|
||||||
if (data.firstOrNull() !is HeadingResponse.Value) return@map data.first()
|
|
||||||
|
|
||||||
val avgValues = data.mapNotNull { (it as? HeadingResponse.Value)?.diff }
|
|
||||||
|
|
||||||
if (avgValues.isEmpty()) return@map HeadingResponse.NoGps
|
|
||||||
|
|
||||||
val avg = avgValues.average()
|
|
||||||
|
|
||||||
HeadingResponse.Value(avg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> concatenate(vararg flows: Flow<T>) = flow {
|
fun <T> concatenate(vararg flows: Flow<T>) = flow {
|
||||||
@ -115,24 +96,13 @@ fun KarooSystemService.getGpsCoordinateFlow(context: Context): Flow<GpsCoordinat
|
|||||||
|
|
||||||
val initialFlow = flow {
|
val initialFlow = flow {
|
||||||
val lastKnownPosition = context.getLastKnownPosition()
|
val lastKnownPosition = context.getLastKnownPosition()
|
||||||
if (lastKnownPosition != null) emit(lastKnownPosition)
|
|
||||||
|
emit(lastKnownPosition)
|
||||||
}
|
}
|
||||||
|
|
||||||
val gpsFlow = streamDataFlow(DataType.Type.LOCATION)
|
val gpsFlow = streamLocation()
|
||||||
.map { (it as? StreamState.Streaming)?.dataPoint?.values }
|
.filter { it.orientation != null }
|
||||||
.map { values ->
|
.map { GpsCoordinates(it.lat, it.lng, it.orientation) }
|
||||||
val lat = values?.get(DataType.Field.LOC_LATITUDE)
|
|
||||||
val lon = values?.get(DataType.Field.LOC_LONGITUDE)
|
|
||||||
val bearing = values?.get(DataType.Field.LOC_BEARING)
|
|
||||||
|
|
||||||
if (lat != null && lon != null){
|
|
||||||
// Log.d(KarooHeadwindExtension.TAG, "Updated gps coordinates: $lat $lon")
|
|
||||||
GpsCoordinates(lat, lon, bearing)
|
|
||||||
} else {
|
|
||||||
// Log.w(KarooHeadwindExtension.TAG, "Gps unavailable: $values")
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val concatenatedFlow = concatenate(initialFlow, gpsFlow)
|
val concatenatedFlow = concatenate(initialFlow, gpsFlow)
|
||||||
|
|
||||||
|
|||||||
@ -41,7 +41,7 @@ import kotlin.math.absoluteValue
|
|||||||
import kotlin.time.Duration.Companion.hours
|
import kotlin.time.Duration.Companion.hours
|
||||||
import kotlin.time.Duration.Companion.minutes
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
|
||||||
class KarooHeadwindExtension : KarooExtension("karoo-headwind", "1.2.1") {
|
class KarooHeadwindExtension : KarooExtension("karoo-headwind", "1.2.2") {
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "karoo-headwind"
|
const val TAG = "karoo-headwind"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "
|
|||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
|
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
|
||||||
hammerhead-karoo-ext = { group = "io.hammerhead", name = "karoo-ext", version = "1.1.2" }
|
hammerhead-karoo-ext = { group = "io.hammerhead", name = "karoo-ext", version = "1.1.3" }
|
||||||
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }
|
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }
|
||||||
|
|
||||||
# compose
|
# compose
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user