Bimba.git

ref: 2919df3a7321d4e904d6ed9cdd1c3c8c32a5ef6c

app/src/main/java/ml/adamsprogs/bimba/api/Responses.kt


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package ml.adamsprogs.bimba.api

import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream

interface DeparturesResponse {
	companion object {
		fun printable(it: String) = it.map {
			when (Character.getType(it).toByte()) {
				Character.CONTROL, Character.FORMAT, Character.PRIVATE_USE,
				Character.SURROGATE, Character.UNASSIGNED, Character.OTHER_SYMBOL
				-> "\\u%04x".format(it.code)
				else -> it.toString()
			}
		}.joinToString("")

		fun unmarshall(stream: InputStream): DeparturesResponse {
			val reader = Reader(stream)
			when (reader.readUInt()) {
				0UL -> {
					TODO("error response")
				}
				1UL -> {
					return DeparturesSuccess.unmarshall(stream)
				}
				else -> {
					TODO("throw unknown tag")
				}
			}
		}
	}
}

data class DeparturesSuccess(
	val alerts: List<Alert>,
	val departures: List<Departure>,
	val stop: Stop
) : DeparturesResponse {
	companion object {
		fun unmarshall(stream: InputStream): DeparturesSuccess {
			val alerts = mutableListOf<Alert>()
			val departures = mutableListOf<Departure>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt()
			for (i in 0UL until alertsNum) {
				val alert = Alert.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt()
			for (i in 0UL until departuresNum) {
				val departure = Departure.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesSuccess(alerts, departures, Stop.unmarshal(stream))
		}
	}
}

interface ItemsResponse {
	companion object {
		fun unmarshall(stream: InputStream): ItemsResponse {
			val reader = Reader(stream)
			when (reader.readUInt()) {
				0UL -> {
					TODO("error response")
				}
				1UL -> {
					return ItemsSuccess.unmarshall(stream)
				}
				else -> {
					TODO("throw unknown tag")
				}
			}
		}
	}
}

data class ItemsSuccess(val items: List<Item>) : ItemsResponse {
	companion object {
		fun unmarshall(stream: InputStream): ItemsSuccess {
			val items = mutableListOf<Item>()
			val reader = Reader(stream)
			val itemsNum = reader.readUInt()
			for (i in 0UL until itemsNum) {
				when (reader.readUInt()) {
					0UL -> {
						items.add(Stop.unmarshal(stream))
					}
					1UL -> {
						items.add(Line.unmarshal(stream))
					}
					else -> {
						TODO("throw unknown tag")
					}
				}
			}
			return ItemsSuccess(items)
		}
	}
}

data class Error(val message: String) : ItemsResponse, DeparturesResponse {

}