Fronyx predictions: Only load data for supported chargers

(CCS and CHAdeMO located in Germany)
This commit is contained in:
johan12345
2022-10-05 22:21:30 +02:00
committed by Johan von Forstner
parent 172e66fe15
commit 9891cf8e88
4 changed files with 48 additions and 4 deletions

View File

@@ -4,6 +4,8 @@ import android.content.Context
import com.facebook.stetho.okhttp3.StethoInterceptor
import com.squareup.moshi.Moshi
import net.vonforst.evmap.BuildConfig
import net.vonforst.evmap.model.ChargeLocation
import net.vonforst.evmap.model.Chargepoint
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
@@ -56,5 +58,30 @@ interface FronyxApi {
.build()
return retrofit.create(FronyxApi::class.java)
}
/**
* Checks if a chargepoint is supported by Fronyx.
*
* This function just applies some heuristics on the charger's data without making API
* calls. If it returns true, that is not a guarantee that Fronyx will have information
* on this chargepoint. But if it is false, it is pretty unlikely that Fronyx will have
* useful data, so we do not try to load the data in this case.
*/
fun isChargepointSupported(charger: ChargeLocation, chargepoint: Chargepoint): Boolean {
if (charger.address?.country !in listOf("Deutschland", "Germany")) {
// fronyx only predicts for chargers in Germany for now
return false
}
if (chargepoint.type !in listOf(
Chargepoint.CCS_UNKNOWN,
Chargepoint.CCS_TYPE_2,
Chargepoint.CHADEMO
)
) {
// fronyx only predicts DC chargers for now
return false
}
return true
}
}
}

View File

@@ -215,11 +215,14 @@ class MapViewModel(application: Application, private val state: SavedStateHandle
val prediction: LiveData<Resource<List<FronyxEvseIdResponse>>> by lazy {
availability.switchMap { av ->
av?.data?.evseIds?.let { evseIds ->
av.data?.evseIds?.let { evseIds ->
liveData {
emit(Resource.loading(null))
val allEvseIds = evseIds.flatMap { it.value }
val charger = charger.value?.data ?: return@liveData
val allEvseIds =
evseIds.filterKeys { FronyxApi.isChargepointSupported(charger, it) }
.flatMap { it.value }
try {
val result = allEvseIds.map {
@@ -268,12 +271,21 @@ class MapViewModel(application: Application, private val state: SavedStateHandle
it.value.count {
it.second == FronyxStatus.UNAVAILABLE
}
}
}.ifEmpty { null }
}
}
}
}
val predictionMaxValue: LiveData<Int> by lazy {
charger.map {
it.data?.let { charger ->
charger.chargepoints.filter { FronyxApi.isChargepointSupported(charger, it) }
.sumOf { it.count }
} ?: 0
}
}
val myLocationEnabled: MutableLiveData<Boolean> by lazy {
MutableLiveData<Boolean>()
}

View File

@@ -47,6 +47,10 @@
name="predictionGraph"
type="Map&lt;ZonedDateTime, Integer&gt;" />
<variable
name="predictionMaxValue"
type="Integer" />
<variable
name="filteredAvailability"
type="Resource&lt;ChargeLocationStatus&gt;" />
@@ -388,7 +392,7 @@
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/textView8"
app:maxValue="@{charger.data.totalChargepoints}"
app:maxValue="@{predictionMaxValue}"
tools:itemCount="3"
tools:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_connector"

View File

@@ -196,6 +196,7 @@
app:availability="@{vm.availability}"
app:filteredAvailability="@{vm.filteredAvailability}"
app:predictionGraph="@{vm.predictionGraph}"
app:predictionMaxValue="@{vm.predictionMaxValue}"
app:chargeCards="@{vm.chargeCardMap}"
app:filteredChargeCards="@{vm.filteredChargeCards}"
app:distance="@{vm.chargerDistance}"