Do not fall back to open-meteo if open weather map requests fail (#93)

This commit is contained in:
timklge 2025-04-17 23:08:33 +02:00 committed by GitHub
parent c7ceabc0e5
commit 78bc93b36e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 4 additions and 47 deletions

View File

@ -44,7 +44,7 @@ After installing this app on your Karoo and opening it once from the main menu,
The app can use OpenMeteo or OpenWeatherMap as providers for live weather data.
- OpenMeteo is the default provider and does not require any configuration.
- OpenWeatherMap can provide more accurate data for some locations. Forecasts along the loaded route are not available using OpenWeatherMap. OpenWeatherMap is free for personal use, but you need to register at https://openweathermap.org/home/sign_in and obtain a one call API key. If you use OpenWeatherMap without a correct key, the app will fall back to OpenMeteo.
- OpenWeatherMap can provide more accurate data for some locations. Forecasts along the loaded route are not available using OpenWeatherMap. OpenWeatherMap is free for personal use, but you need to register at https://openweathermap.org/home/sign_in and obtain a one call API key.
The app will automatically attempt to download weather data from the selected data provider once your device has acquired a GPS fix. Your location is rounded to approximately three kilometers to maintain privacy.
New weather data is downloaded when you ride more than three kilometers from the location where the weather data was downloaded for or after one hour at the latest.

View File

@ -284,7 +284,7 @@ fun WeatherProviderSection(
)
Text(
text = "If you want to use OpenWeatherMap, you need to provide an API key. If you don't provide a correct key, OpenMeteo is used as a fallback.",
text = "If you want to use OpenWeatherMap, you need to provide an API key.",
style = MaterialTheme.typography.bodySmall
)
}

View File

@ -3,23 +3,14 @@ package de.timklge.karooheadwind.weatherprovider
import android.util.Log
import de.timklge.karooheadwind.HeadwindSettings
import de.timklge.karooheadwind.KarooHeadwindExtension
import de.timklge.karooheadwind.weatherprovider.openmeteo.OpenMeteoWeatherProvider
import de.timklge.karooheadwind.weatherprovider.openweathermap.OpenWeatherMapWeatherProvider
import de.timklge.karooheadwind.WeatherDataProvider
import de.timklge.karooheadwind.datatypes.GpsCoordinates
import de.timklge.karooheadwind.weatherprovider.openmeteo.OpenMeteoWeatherProvider
import de.timklge.karooheadwind.weatherprovider.openweathermap.OpenWeatherMapWeatherProvider
import io.hammerhead.karooext.KarooSystemService
import io.hammerhead.karooext.models.UserProfile
import java.time.LocalDate
object WeatherProviderFactory {
private var openWeatherMapConsecutiveFailures = 0
private var openWeatherMapTotalFailures = 0
private var openMeteoSuccessfulAfterFailures = false
private var fallbackUntilDate: LocalDate? = null
private const val MAX_FAILURES_BEFORE_TEMP_FALLBACK = 3
private const val MAX_FAILURES_BEFORE_DAILY_FALLBACK = 20
suspend fun makeWeatherRequest(
karooSystemService: KarooSystemService,
gpsCoordinates: List<GpsCoordinates>,
@ -35,48 +26,14 @@ object WeatherProviderFactory {
} catch(e: Throwable){
Log.d(KarooHeadwindExtension.TAG, "Weather request failed: $e")
if (provider is OpenWeatherMapWeatherProvider && (e is WeatherProviderException && (e.statusCode == 401 || e.statusCode == 403))) {
handleOpenWeatherMapFailure()
}
throw e;
}
}
private fun getProvider(settings: HeadwindSettings): WeatherProvider {
val currentDate = LocalDate.now()
if (fallbackUntilDate != null && !currentDate.isAfter(fallbackUntilDate)) {
Log.d(KarooHeadwindExtension.TAG, "Using fallback OpenMeteo until $fallbackUntilDate")
return OpenMeteoWeatherProvider()
}
if (settings.weatherProvider == WeatherDataProvider.OPEN_WEATHER_MAP &&
openWeatherMapConsecutiveFailures >= MAX_FAILURES_BEFORE_TEMP_FALLBACK
) {
openWeatherMapConsecutiveFailures = 0
Log.d(KarooHeadwindExtension.TAG, "Using temporary fallback OpenMeteo")
return OpenMeteoWeatherProvider()
}
return when (settings.weatherProvider) {
WeatherDataProvider.OPEN_METEO -> OpenMeteoWeatherProvider()
WeatherDataProvider.OPEN_WEATHER_MAP -> OpenWeatherMapWeatherProvider(settings.openWeatherMapApiKey)
}
}
private fun handleOpenWeatherMapFailure() {
openWeatherMapConsecutiveFailures++
openWeatherMapTotalFailures++
Log.d(KarooHeadwindExtension.TAG, "OpenWeatherMap failed $openWeatherMapConsecutiveFailures times consecutive, $openWeatherMapTotalFailures total times")
if (openWeatherMapTotalFailures >= MAX_FAILURES_BEFORE_DAILY_FALLBACK && openMeteoSuccessfulAfterFailures) {
fallbackUntilDate = LocalDate.now()
Log.d(KarooHeadwindExtension.TAG, "Activated daily fallback OpenMeteo until $fallbackUntilDate")
}
}
}