Request first two and last location for openweathermap route forecast (#154)
This commit is contained in:
parent
533cb1e006
commit
b6d0acd62d
@ -232,7 +232,7 @@ class KarooHeadwindExtension : KarooExtension("karoo-headwind", BuildConfig.VERS
|
|||||||
response
|
response
|
||||||
}.retry(Long.MAX_VALUE) { e ->
|
}.retry(Long.MAX_VALUE) { e ->
|
||||||
Log.w(TAG, "Failed to get weather data", e)
|
Log.w(TAG, "Failed to get weather data", e)
|
||||||
delay(1.minutes); true
|
delay(2.minutes); true
|
||||||
}.collect { response ->
|
}.collect { response ->
|
||||||
try {
|
try {
|
||||||
saveCurrentData(applicationContext, response)
|
saveCurrentData(applicationContext, response)
|
||||||
|
|||||||
@ -206,8 +206,7 @@ abstract class LineGraphForecastDataType(private val karooSystem: KarooSystemSer
|
|||||||
val viewJob = CoroutineScope(Dispatchers.IO).launch {
|
val viewJob = CoroutineScope(Dispatchers.IO).launch {
|
||||||
emitter.onNext(ShowCustomStreamState("", null))
|
emitter.onNext(ShowCustomStreamState("", null))
|
||||||
|
|
||||||
dataFlow.filter { it.isVisible }.collect { (allData, settingsAndProfile, widgetSettings, headingResponse, actualUpcomingRoute) ->
|
dataFlow.filter { it.isVisible }.collect { (allData, settingsAndProfile, _, headingResponse, upcomingRoute) ->
|
||||||
val upcomingRoute = if (allData?.provider == WeatherDataProvider.OPEN_METEO) actualUpcomingRoute else null
|
|
||||||
Log.d(KarooHeadwindExtension.TAG, "Updating weather forecast view")
|
Log.d(KarooHeadwindExtension.TAG, "Updating weather forecast view")
|
||||||
|
|
||||||
if (allData?.data.isNullOrEmpty()){
|
if (allData?.data.isNullOrEmpty()){
|
||||||
|
|||||||
@ -15,8 +15,6 @@ import io.hammerhead.karooext.models.OnHttpResponse
|
|||||||
import io.hammerhead.karooext.models.UserProfile
|
import io.hammerhead.karooext.models.UserProfile
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.TimeoutCancellationException
|
import kotlinx.coroutines.TimeoutCancellationException
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.awaitAll
|
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.flow.callbackFlow
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
@ -51,7 +49,7 @@ data class Snow(
|
|||||||
|
|
||||||
class OpenWeatherMapWeatherProvider(private val apiKey: String) : WeatherProvider {
|
class OpenWeatherMapWeatherProvider(private val apiKey: String) : WeatherProvider {
|
||||||
companion object {
|
companion object {
|
||||||
private const val MAX_API_CALLS = 4
|
private const val MAX_API_CALLS = 3
|
||||||
|
|
||||||
fun convertWeatherCodeToOpenMeteo(owmCode: Int): Int {
|
fun convertWeatherCodeToOpenMeteo(owmCode: Int): Int {
|
||||||
// Mapping OpenWeatherMap to WMO OpenMeteo
|
// Mapping OpenWeatherMap to WMO OpenMeteo
|
||||||
@ -73,27 +71,30 @@ class OpenWeatherMapWeatherProvider(private val apiKey: String) : WeatherProvide
|
|||||||
settings: HeadwindSettings,
|
settings: HeadwindSettings,
|
||||||
profile: UserProfile?
|
profile: UserProfile?
|
||||||
): WeatherDataResponse = coroutineScope {
|
): 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")
|
Log.d(KarooHeadwindExtension.TAG, "OpenWeatherMap: searching for ${selectedCoordinates.size} locations from ${coordinates.size} total")
|
||||||
selectedCoordinates.forEachIndexed { index, coord ->
|
selectedCoordinates.forEachIndexed { index, coord ->
|
||||||
Log.d(KarooHeadwindExtension.TAG, "Point #$index: ${coord.lat}, ${coord.lon}, distance: ${coord.distanceAlongRoute}")
|
Log.d(KarooHeadwindExtension.TAG, "Point #$index: ${coord.lat}, ${coord.lon}, distance: ${coord.distanceAlongRoute}")
|
||||||
}
|
}
|
||||||
|
|
||||||
val weatherDataForSelectedLocations = selectedCoordinates.map { coordinate ->
|
val weatherDataForSelectedLocations = buildList {
|
||||||
async {
|
for (coordinate in selectedCoordinates){
|
||||||
val response = makeOpenWeatherMapRequest(karooSystem, coordinate, apiKey)
|
val response = makeOpenWeatherMapRequest(karooSystem, coordinate, apiKey)
|
||||||
val responseBody = response.body?.let { String(it) }
|
val responseBody = response.body?.let { String(it) }
|
||||||
?: throw WeatherProviderException(response.statusCode, "Null Response from OpenWeatherMap")
|
?: throw WeatherProviderException(response.statusCode, "Null Response from OpenWeatherMap")
|
||||||
|
|
||||||
val weatherData = jsonWithUnknownKeys.decodeFromString<OpenWeatherMapWeatherDataForLocation>(responseBody)
|
val weatherData = jsonWithUnknownKeys.decodeFromString<OpenWeatherMapWeatherDataForLocation>(responseBody)
|
||||||
coordinate to weatherData
|
|
||||||
}
|
|
||||||
}.awaitAll()
|
|
||||||
|
|
||||||
|
add(coordinate to weatherData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val allLocationData = coordinates.map { originalCoord ->
|
val allLocationData = coordinates.map { originalCoord ->
|
||||||
|
|
||||||
val directMatch = weatherDataForSelectedLocations.find { it.first == originalCoord }
|
val directMatch = weatherDataForSelectedLocations.find { it.first == originalCoord }
|
||||||
|
|
||||||
if (directMatch != null) {
|
if (directMatch != null) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user