Request first two and last location for openweathermap route forecast (#154)

This commit is contained in:
timklge 2025-06-11 20:54:53 +02:00 committed by GitHub
parent 533cb1e006
commit b6d0acd62d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 13 deletions

View File

@ -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)

View File

@ -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()){

View File

@ -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<OpenWeatherMapWeatherDataForLocation>(responseBody)
coordinate to weatherData
}
}.awaitAll()
add(coordinate to weatherData)
}
}
val allLocationData = coordinates.map { originalCoord ->
val directMatch = weatherDataForSelectedLocations.find { it.first == originalCoord }
if (directMatch != null) {