mirror of
https://github.com/ev-map/EVMap.git
synced 2026-06-11 07:49:43 -04:00
Nobil: Add real-time availability support
This commit is contained in:
committed by
Johan von Forstner
parent
fc22b16111
commit
141b2c76b1
@@ -175,6 +175,7 @@ class AvailabilityRepository(context: Context) {
|
||||
RheinenergieAvailabilityDetector(okhttp),
|
||||
teslaOwnerAvailabilityDetector,
|
||||
TeslaGuestAvailabilityDetector(okhttp),
|
||||
NobilAvailabilityDetector(okhttp, context),
|
||||
EnBwAvailabilityDetector(okhttp),
|
||||
NewMotionAvailabilityDetector(okhttp)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package net.vonforst.evmap.api.availability
|
||||
|
||||
import android.content.Context
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import net.vonforst.evmap.R
|
||||
import net.vonforst.evmap.model.ChargeLocation
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.Path
|
||||
import java.time.Instant
|
||||
|
||||
internal class InstantStringAdapter {
|
||||
@FromJson
|
||||
fun fromJson(value: String?): Instant? = value?.let {
|
||||
Instant.parse(value)
|
||||
}
|
||||
|
||||
@ToJson
|
||||
fun toJson(value: Instant?): String? = value?.toString()
|
||||
}
|
||||
|
||||
interface NobilRealtimeApi {
|
||||
@GET("{nobilId}")
|
||||
suspend fun getAvailability(
|
||||
@Path("nobilId") nobilId: String,
|
||||
@Header("X-Api-Key") apiKey: String
|
||||
): List<NobilChargepointState>
|
||||
|
||||
companion object {
|
||||
fun create(client: OkHttpClient): NobilRealtimeApi {
|
||||
val retrofit = Retrofit.Builder()
|
||||
.baseUrl("https://api.ev-map.app/nobil/api/realtime/")
|
||||
.addConverterFactory(
|
||||
MoshiConverterFactory.create(
|
||||
Moshi.Builder().add(InstantStringAdapter()).build()
|
||||
)
|
||||
)
|
||||
.client(client)
|
||||
.build()
|
||||
return retrofit.create(NobilRealtimeApi::class.java)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class NobilChargepointState(
|
||||
val evseUid: String,
|
||||
val status: String,
|
||||
val timestamp: Instant
|
||||
)
|
||||
|
||||
class NobilAvailabilityDetector(client: OkHttpClient, context: Context) :
|
||||
BaseAvailabilityDetector(client) {
|
||||
val api = NobilRealtimeApi.create(client)
|
||||
val apiKey = context.getString(R.string.evmap_key)
|
||||
|
||||
override suspend fun getAvailability(location: ChargeLocation): ChargeLocationStatus {
|
||||
val nobilId = when (location.address?.country) {
|
||||
"Norway" -> "NOR"
|
||||
"Sweden" -> "SWE"
|
||||
else -> throw AvailabilityDetectorException("nobil: unsupported country")
|
||||
} + "_%05d".format(location.id)
|
||||
|
||||
val availability = api.getAvailability(nobilId, apiKey)
|
||||
if (availability.isEmpty()) {
|
||||
throw AvailabilityDetectorException("nobil: no real-time data available")
|
||||
}
|
||||
return ChargeLocationStatus(
|
||||
location.chargepointsMerged.associateWith { cp ->
|
||||
cp.evseUIds!!.map { evseUId ->
|
||||
when (availability.find { it.evseUid == evseUId }?.status) {
|
||||
"AVAILABLE" -> ChargepointStatus.AVAILABLE
|
||||
"BLOCKED" -> ChargepointStatus.OCCUPIED
|
||||
"CHARGING" -> ChargepointStatus.CHARGING
|
||||
"INOPERATIVE" -> ChargepointStatus.FAULTED
|
||||
"OUTOFORDER" -> ChargepointStatus.FAULTED
|
||||
"PLANNED" -> ChargepointStatus.FAULTED
|
||||
"REMOVED" -> ChargepointStatus.FAULTED
|
||||
"RESERVED" -> ChargepointStatus.OCCUPIED
|
||||
"UNKNOWN" -> ChargepointStatus.UNKNOWN
|
||||
else -> ChargepointStatus.UNKNOWN
|
||||
}
|
||||
}
|
||||
},
|
||||
"Nobil",
|
||||
location.chargepointsMerged.associateWith { cp ->
|
||||
if (cp.evseIds != null) cp.evseIds.map { it ?: "??" } else listOf()
|
||||
},
|
||||
lastChange = location.chargepointsMerged.associateWith { cp ->
|
||||
cp.evseUIds!!.map { evseUId ->
|
||||
availability.find { it.evseUid == evseUId }?.timestamp
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
override fun isChargerSupported(charger: ChargeLocation): Boolean {
|
||||
return when (charger.dataSource) {
|
||||
"nobil" -> charger.chargepoints.any { it.evseUIds?.isNotEmpty() == true }
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,9 +279,10 @@ data class NobilChargerStation(
|
||||
|
||||
val connectionVoltage = if (attribs["12"]?.attrVal is String) attribs["12"]?.attrVal.toString().toDoubleOrNull() else null
|
||||
val connectionCurrent = if (attribs["31"]?.attrVal is String) attribs["31"]?.attrVal.toString().toDoubleOrNull() else null
|
||||
val evseUId = if (attribs["27"]?.attrVal is String) listOf(attribs["27"]?.attrVal.toString()) else null
|
||||
val evseId = if (attribs["28"]?.attrVal is String) listOf(attribs["28"]?.attrVal.toString()) else null
|
||||
|
||||
return Chargepoint(connectionType, connectionPower, 1, connectionCurrent, connectionVoltage, evseId)
|
||||
return Chargepoint(connectionType, connectionPower, 1, connectionCurrent, connectionVoltage, evseId, evseUId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,10 +140,12 @@ data class ChargeLocation(
|
||||
.filter { it.type == variant.type && it.power == variant.power }
|
||||
val count = filtered.sumOf { it.count }
|
||||
val mergedEvseIds = filtered.map { if (it.evseIds == null) List(it.count) {null} else it.evseIds }.flatten()
|
||||
val mergedEvseUIds = filtered.map { if (it.evseUIds == null) List(it.count) {null} else it.evseUIds }.flatten()
|
||||
Chargepoint(variant.type, variant.power, count,
|
||||
filtered.map { it.current }.distinct().singleOrNull(),
|
||||
filtered.map { it.voltage }.distinct().singleOrNull(),
|
||||
if (mergedEvseIds.all { it == null }) null else mergedEvseIds
|
||||
if (mergedEvseIds.all { it == null }) null else mergedEvseIds,
|
||||
if (mergedEvseUIds.all { it == null }) null else mergedEvseUIds
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -425,7 +427,9 @@ data class Chargepoint(
|
||||
// (each of the three can be separately limited)
|
||||
val voltage: Double? = null,
|
||||
// Electric Vehicle Supply Equipment Ids for this Chargepoint's plugs/sockets
|
||||
val evseIds: List<String?>? = null
|
||||
val evseIds: List<String?>? = null,
|
||||
// Electric Vehicle Supply Equipment Unique Ids for this Chargepoint's plugs/sockets
|
||||
val evseUIds: List<String?>? = null
|
||||
) : Equatable, Parcelable {
|
||||
fun hasKnownPower(): Boolean = power != null
|
||||
fun hasKnownVoltageAndCurrent(): Boolean = voltage != null && current != null
|
||||
|
||||
@@ -40,7 +40,7 @@ import net.vonforst.evmap.model.SliderFilterValue
|
||||
OCMOperator::class,
|
||||
OSMNetwork::class,
|
||||
SavedRegion::class
|
||||
], version = 27
|
||||
], version = 28
|
||||
)
|
||||
@TypeConverters(Converters::class, GeometryConverters::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
@@ -85,7 +85,7 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
MIGRATION_12, MIGRATION_13, MIGRATION_14, MIGRATION_15, MIGRATION_16,
|
||||
MIGRATION_17, MIGRATION_18, MIGRATION_19, MIGRATION_20, MIGRATION_21,
|
||||
MIGRATION_22, MIGRATION_23, MIGRATION_24, MIGRATION_25, MIGRATION_26,
|
||||
MIGRATION_27
|
||||
MIGRATION_27, MIGRATION_28
|
||||
)
|
||||
.addCallback(object : Callback() {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
@@ -547,6 +547,14 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
db.execSQL("ALTER TABLE `ChargeLocation` ADD `accessibility` TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
private val MIGRATION_28 = object : Migration(27, 28) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// Force nobil data refresh to fetch EVSE UId attributes needed for real-time data
|
||||
db.execSQL("DELETE FROM SavedRegion WHERE `dataSource` = 'nobil'")
|
||||
db.execSQL("DELETE FROM ChargeLocation WHERE `dataSource` = 'nobil'")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user