Author: Adam <git@apiote.xyz>
add batong responses
app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt | 10 app/src/main/java/xyz/apiote/bimba/czwek/api/Responses.kt | 157 app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt | 24
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt index 651daf551fd70c738ca448781bb530606a32ee95..91b191f7296b03dbcedf0726ab8ba89fefd714fd 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt @@ -65,11 +65,11 @@ val params = mutableMapOf("q" to query) if (limit != null) { params["limit"] = limit.toString() } - return request(server, "queryables", null, params, cm, arrayOf(1u, 0u), null) + return request(server, "queryables", null, params, cm, arrayOf(1u, 2u), null) } suspend fun locateQueryables(cm: ConnectivityManager, server: Server, near: PositionV1): Result { - return request(server, "queryables", null, mapOf("near" to near.toString()), cm, arrayOf(1u, 0u), null) + return request(server, "queryables", null, mapOf("near" to near.toString()), cm, arrayOf(1u, 2u), null) } suspend fun getLocatablesIn( @@ -81,13 +81,13 @@ "locatables", null, mapOf("lb" to bl.toString(), "rt" to tr.toString()), cm, - arrayOf(1u, 0u), + arrayOf(1u, 2u), null ) } suspend fun getLine(cm: ConnectivityManager, server: Server, feedID: String, line: String): Result { - return request(server, "lines", line, mapOf(), cm, arrayOf(1u, 0u), feedID) + return request(server, "lines", line, mapOf(), cm, arrayOf(1u), feedID) } suspend fun getDepartures( @@ -97,7 +97,7 @@ val params = mutableMapOf("code" to stop) if (line != null) { params["line"] = line } - return request(server, "departures", null, params, cm, arrayOf(1u, 0u), feedID) + return request(server, "departures", null, params, cm, arrayOf(1u, 2u), feedID) } suspend fun rawRequest( diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Responses.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Responses.kt index 857ffaa118e479acfaf37876af655c1f64f962ee..4946e573114915e4a9f6bfa963cbf71f2040e447 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Responses.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Responses.kt @@ -3,7 +3,8 @@ import xyz.apiote.fruchtfleisch.Reader import java.io.InputStream -class UnknownResponseVersion(val resource: String, val version: ULong) : Exception() +class UnknownResponseVersion(resource: String, version: ULong) : + Exception("Unknown resource $resource in version $version") interface LineResponse { companion object { @@ -43,18 +44,38 @@ companion object { fun unmarshal(stream: InputStream): DeparturesResponse { val reader = Reader(stream) return when (val v = reader.readUInt().toULong()) { - 0UL -> { - DeparturesResponseDev.unmarshal(stream) - } + 0UL -> DeparturesResponseDev.unmarshal(stream) + 1UL -> DeparturesResponseV1.unmarshal(stream) + 2UL -> DeparturesResponseV2.unmarshal(stream) + else -> throw UnknownResponseVersion("Departures", v) + } + } + } +} - 1UL -> { - DeparturesResponseV1.unmarshal(stream) - } +data class DeparturesResponseDev( + val alerts: List<AlertV1>, + val departures: List<DepartureV2>, + val stop: StopV2 +) : DeparturesResponse { + companion object { + fun unmarshal(stream: InputStream): DeparturesResponseDev { + val alerts = mutableListOf<AlertV1>() + val departures = mutableListOf<DepartureV2>() - else -> { - throw UnknownResponseVersion("Departures", v) - } + val reader = Reader(stream) + val alertsNum = reader.readUInt().toULong() + for (i in 0UL until alertsNum) { + val alert = AlertV1.unmarshal(stream) + alerts.add(alert) } + val departuresNum = reader.readUInt().toULong() + for (i in 0UL until departuresNum) { + val departure = DepartureV2.unmarshal(stream) + departures.add(departure) + } + + return DeparturesResponseDev(alerts, departures, StopV2.unmarshal(stream)) } } } @@ -86,13 +107,13 @@ } } } -data class DeparturesResponseDev( +data class DeparturesResponseV2( val alerts: List<AlertV1>, val departures: List<DepartureV2>, val stop: StopV2 ) : DeparturesResponse { companion object { - fun unmarshal(stream: InputStream): DeparturesResponseDev { + fun unmarshal(stream: InputStream): DeparturesResponseV2 { val alerts = mutableListOf<AlertV1>() val departures = mutableListOf<DepartureV2>() @@ -108,7 +129,7 @@ val departure = DepartureV2.unmarshal(stream) departures.add(departure) } - return DeparturesResponseDev(alerts, departures, StopV2.unmarshal(stream)) + return DeparturesResponseV2(alerts, departures, StopV2.unmarshal(stream)) } } } @@ -118,18 +139,37 @@ companion object { fun unmarshal(stream: InputStream): QueryablesResponse { val reader = Reader(stream) return when (val v = reader.readUInt().toULong()) { - 0UL -> { - QueryablesResponseDev.unmarshal(stream) - } + 0UL -> QueryablesResponseDev.unmarshal(stream) + 1UL -> QueryablesResponseV1.unmarshal(stream) + 2UL -> QueryablesResponseV2.unmarshal(stream) + else -> throw UnknownResponseVersion("Queryables", v) + } + } + } +} + +data class QueryablesResponseDev(val queryables: List<QueryableV2>) : QueryablesResponse { + companion object { + fun unmarshal(stream: InputStream): QueryablesResponseDev { + val queryables = mutableListOf<QueryableV2>() + val reader = Reader(stream) + val n = reader.readUInt().toULong() + for (i in 0UL until n) { + when (val r = reader.readUInt().toULong()) { + 0UL -> { + queryables.add(StopV2.unmarshal(stream)) + } - 1UL -> { - QueryablesResponseV1.unmarshal(stream) - } + 1UL -> { + queryables.add(LineV1.unmarshal(stream)) + } - else -> { - throw UnknownResponseVersion("Queryables", v) + else -> { + throw UnknownResourceVersionException("Queryable/$r", 1u) + } } } + return QueryablesResponseDev(queryables) } } } @@ -156,9 +196,9 @@ } } } -data class QueryablesResponseDev(val queryables: List<QueryableV2>) : QueryablesResponse { +data class QueryablesResponseV2(val queryables: List<QueryableV2>) : QueryablesResponse { companion object { - fun unmarshal(stream: InputStream): QueryablesResponseDev { + fun unmarshal(stream: InputStream): QueryablesResponseV2 { val queryables = mutableListOf<QueryableV2>() val reader = Reader(stream) val n = reader.readUInt().toULong() @@ -177,7 +217,7 @@ throw UnknownResourceVersionException("Queryable/$r", 1u) } } } - return QueryablesResponseDev(queryables) + return QueryablesResponseV2(queryables) } } } @@ -187,17 +227,9 @@ companion object { fun unmarshal(stream: InputStream): FeedsResponse { val reader = Reader(stream) return when (val v = reader.readUInt().toULong()) { - 0UL -> { - FeedsResponseDev.unmarshal(stream) - } - - 1UL -> { - FeedsResponseV1.unmarshal(stream) - } - - else -> { - throw UnknownResponseVersion("Feeds", v) - } + 0UL -> FeedsResponseDev.unmarshal(stream) + 1UL -> FeedsResponseV1.unmarshal(stream) + else -> throw UnknownResponseVersion("Feeds", v) } } } @@ -240,18 +272,37 @@ companion object { fun unmarshal(stream: InputStream): LocatablesResponse { val reader = Reader(stream) return when (val v = reader.readUInt().toULong()) { - 0UL -> { - LocatablesResponseDev.unmarshal(stream) - } + 0UL -> LocatablesResponseDev.unmarshal(stream) + 1UL -> LocatablesResponseV1.unmarshal(stream) + 2UL -> LocatablesResponseV2.unmarshal(stream) + else -> throw UnknownResponseVersion("Locatables", v) + } + } + } +} - 1UL -> { - LocatablesResponseV1.unmarshal(stream) - } +data class LocatablesResponseDev(val locatables: List<LocatableV2>) : LocatablesResponse { + companion object { + fun unmarshal(stream: InputStream): LocatablesResponseDev { + val locatables = mutableListOf<LocatableV2>() + val reader = Reader(stream) + val n = reader.readUInt().toULong() + for (i in 0UL until n) { + when (val r = reader.readUInt().toULong()) { + 0UL -> { + locatables.add(StopV2.unmarshal(stream)) + } - else -> { - throw UnknownResponseVersion("Locatables", v) + 1UL -> { + locatables.add(VehicleV2.unmarshal(stream)) + } + + else -> { + throw UnknownResourceVersionException("Locatable/$r", 1u) + } } } + return LocatablesResponseDev(locatables) } } } @@ -282,28 +333,20 @@ } } } -data class LocatablesResponseDev(val locatables: List<LocatableV2>) : LocatablesResponse { +data class LocatablesResponseV2(val locatables: List<LocatableV2>) : LocatablesResponse { companion object { - fun unmarshal(stream: InputStream): LocatablesResponseDev { + fun unmarshal(stream: InputStream): LocatablesResponseV2 { val locatables = mutableListOf<LocatableV2>() val reader = Reader(stream) val n = reader.readUInt().toULong() for (i in 0UL until n) { when (val r = reader.readUInt().toULong()) { - 0UL -> { - locatables.add(StopV2.unmarshal(stream)) - } - - 1UL -> { - locatables.add(VehicleV2.unmarshal(stream)) - } - - else -> { - throw UnknownResourceVersionException("Locatable/$r", 1u) - } + 0UL -> locatables.add(StopV2.unmarshal(stream)) + 1UL -> locatables.add(VehicleV2.unmarshal(stream)) + else -> throw UnknownResourceVersionException("Locatable/$r", 1u) } } - return LocatablesResponseDev(locatables) + return LocatablesResponseV2(locatables) } } } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt index 42539d9c5f88ab95a59d71f63069c00b436a10df..5c94740846682be05460fe8c9dce46bc21ae6228 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OnlineRepository.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.withContext import xyz.apiote.bimba.czwek.api.DeparturesResponse import xyz.apiote.bimba.czwek.api.DeparturesResponseDev import xyz.apiote.bimba.czwek.api.DeparturesResponseV1 +import xyz.apiote.bimba.czwek.api.DeparturesResponseV2 import xyz.apiote.bimba.czwek.api.ErrorResponse import xyz.apiote.bimba.czwek.api.LineResponse import xyz.apiote.bimba.czwek.api.LineResponseDev @@ -15,10 +16,12 @@ import xyz.apiote.bimba.czwek.api.LineV1 import xyz.apiote.bimba.czwek.api.LocatablesResponse import xyz.apiote.bimba.czwek.api.LocatablesResponseDev import xyz.apiote.bimba.czwek.api.LocatablesResponseV1 +import xyz.apiote.bimba.czwek.api.LocatablesResponseV2 import xyz.apiote.bimba.czwek.api.PositionV1 import xyz.apiote.bimba.czwek.api.QueryablesResponse import xyz.apiote.bimba.czwek.api.QueryablesResponseDev import xyz.apiote.bimba.czwek.api.QueryablesResponseV1 +import xyz.apiote.bimba.czwek.api.QueryablesResponseV2 import xyz.apiote.bimba.czwek.api.Server import xyz.apiote.bimba.czwek.api.StopV1 import xyz.apiote.bimba.czwek.api.StopV2 @@ -58,6 +61,11 @@ response.departures.map { Departure(it) }, Stop(response.stop), response.alerts.map { Alert(it) }) + is DeparturesResponseV2 -> StopDepartures( + response.departures.map { Departure(it) }, + Stop(response.stop), + response.alerts.map { Alert(it) }) + else -> null } } @@ -101,6 +109,14 @@ else -> throw UnknownResourceException("locatables", it::class) } } + is LocatablesResponseV2 -> response.locatables.map { + when (it) { + is StopV2 -> Stop(it) + is VehicleV2 -> Vehicle(it) + else -> throw UnknownResourceException("locatables", it::class) + } + } + else -> null } } @@ -175,6 +191,14 @@ is QueryablesResponseV1 -> response.queryables.map { when (it) { is StopV1 -> Stop(it) else -> throw UnknownResourceException("queryablesV1", it::class) + } + } + + is QueryablesResponseV2 -> response.queryables.map { + when (it) { + is StopV2 -> Stop(it) + is LineV1 -> Line(it) + else -> throw UnknownResourceException("queryablesV2", it::class) } }