From b6d0acd62d4cdedb1efa2e658b8297294e56aeaf Mon Sep 17 00:00:00 2001 From: timklge <2026103+timklge@users.noreply.github.com> Date: Wed, 11 Jun 2025 20:54:53 +0200 Subject: [PATCH] Request first two and last location for openweathermap route forecast (#154) --- .../karooheadwind/KarooHeadwindExtension.kt | 2 +- .../datatypes/LineGraphForecastDataType.kt | 3 +-- .../OpenWeatherMapWeatherProvider.kt | 21 ++++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt index 31dc8ec..49ccdee 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/KarooHeadwindExtension.kt @@ -232,7 +232,7 @@ class KarooHeadwindExtension : KarooExtension("karoo-headwind", BuildConfig.VERS response }.retry(Long.MAX_VALUE) { e -> Log.w(TAG, "Failed to get weather data", e) - delay(1.minutes); true + delay(2.minutes); true }.collect { response -> try { saveCurrentData(applicationContext, response) diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/LineGraphForecastDataType.kt b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/LineGraphForecastDataType.kt index 7f1909c..764f2a2 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/LineGraphForecastDataType.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/datatypes/LineGraphForecastDataType.kt @@ -206,8 +206,7 @@ abstract class LineGraphForecastDataType(private val karooSystem: KarooSystemSer val viewJob = CoroutineScope(Dispatchers.IO).launch { emitter.onNext(ShowCustomStreamState("", null)) - dataFlow.filter { it.isVisible }.collect { (allData, settingsAndProfile, widgetSettings, headingResponse, actualUpcomingRoute) -> - val upcomingRoute = if (allData?.provider == WeatherDataProvider.OPEN_METEO) actualUpcomingRoute else null + dataFlow.filter { it.isVisible }.collect { (allData, settingsAndProfile, _, headingResponse, upcomingRoute) -> Log.d(KarooHeadwindExtension.TAG, "Updating weather forecast view") if (allData?.data.isNullOrEmpty()){ diff --git a/app/src/main/kotlin/de/timklge/karooheadwind/weatherprovider/openweathermap/OpenWeatherMapWeatherProvider.kt b/app/src/main/kotlin/de/timklge/karooheadwind/weatherprovider/openweathermap/OpenWeatherMapWeatherProvider.kt index ac8d593..e2c6279 100644 --- a/app/src/main/kotlin/de/timklge/karooheadwind/weatherprovider/openweathermap/OpenWeatherMapWeatherProvider.kt +++ b/app/src/main/kotlin/de/timklge/karooheadwind/weatherprovider/openweathermap/OpenWeatherMapWeatherProvider.kt @@ -15,8 +15,6 @@ import io.hammerhead.karooext.models.OnHttpResponse import io.hammerhead.karooext.models.UserProfile import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.TimeoutCancellationException -import kotlinx.coroutines.async -import kotlinx.coroutines.awaitAll import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.callbackFlow @@ -51,7 +49,7 @@ data class Snow( class OpenWeatherMapWeatherProvider(private val apiKey: String) : WeatherProvider { companion object { - private const val MAX_API_CALLS = 4 + private const val MAX_API_CALLS = 3 fun convertWeatherCodeToOpenMeteo(owmCode: Int): Int { // Mapping OpenWeatherMap to WMO OpenMeteo @@ -73,27 +71,30 @@ class OpenWeatherMapWeatherProvider(private val apiKey: String) : WeatherProvide settings: HeadwindSettings, profile: UserProfile? ): WeatherDataResponse = coroutineScope { - val selectedCoordinates = coordinates.take(4) + val selectedCoordinates = coordinates.take((MAX_API_CALLS - 1).coerceAtLeast(1)).toMutableList() + + if (coordinates.isNotEmpty() && !selectedCoordinates.contains(coordinates.last())){ + selectedCoordinates.add(coordinates.last()) + } Log.d(KarooHeadwindExtension.TAG, "OpenWeatherMap: searching for ${selectedCoordinates.size} locations from ${coordinates.size} total") selectedCoordinates.forEachIndexed { index, coord -> Log.d(KarooHeadwindExtension.TAG, "Point #$index: ${coord.lat}, ${coord.lon}, distance: ${coord.distanceAlongRoute}") } - val weatherDataForSelectedLocations = selectedCoordinates.map { coordinate -> - async { + val weatherDataForSelectedLocations = buildList { + for (coordinate in selectedCoordinates){ val response = makeOpenWeatherMapRequest(karooSystem, coordinate, apiKey) val responseBody = response.body?.let { String(it) } ?: throw WeatherProviderException(response.statusCode, "Null Response from OpenWeatherMap") val weatherData = jsonWithUnknownKeys.decodeFromString(responseBody) - coordinate to weatherData - } - }.awaitAll() + add(coordinate to weatherData) + } + } val allLocationData = coordinates.map { originalCoord -> - val directMatch = weatherDataForSelectedLocations.find { it.first == originalCoord } if (directMatch != null) {