Bimba.git

ref: 7a1f29c024a31360e8b3e4d40df4cfed55ae9d15

app/src/main/java/xyz/apiote/bimba/czwek/api/transitousDepartures.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package xyz.apiote.bimba.czwek.api

import android.content.Context
import android.util.JsonReader
import android.util.JsonToken
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.responses.ErrorResponse
import xyz.apiote.bimba.czwek.repo.Colour
import xyz.apiote.bimba.czwek.repo.CongestionLevel
import xyz.apiote.bimba.czwek.repo.Event
import xyz.apiote.bimba.czwek.repo.LineStub
import xyz.apiote.bimba.czwek.repo.LineType
import xyz.apiote.bimba.czwek.repo.OccupancyStatus
import xyz.apiote.bimba.czwek.repo.Position
import xyz.apiote.bimba.czwek.repo.Stop
import xyz.apiote.bimba.czwek.repo.StopEvents
import xyz.apiote.bimba.czwek.repo.TrafficResponseException
import xyz.apiote.bimba.czwek.repo.Vehicle
import xyz.apiote.bimba.czwek.units.Mps
import java.security.MessageDigest
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZonedDateTime

@OptIn(ExperimentalStdlibApi::class)
suspend fun getTransitousDepartures(
	context: Context,
	stop: String,
	date: LocalDate?,
	limit: Int?
): StopEvents {
	if (!isNetworkAvailable(context)) {
		throw TrafficResponseException(0, "", Error(0, R.string.error_offline, R.drawable.error_net))
	}

	// TODO shouldn't it be start-of-day in stop's timezone?
	val timestamp =
		date?.atStartOfDay(ZoneId.systemDefault())?.toEpochSecond() ?: ZonedDateTime.now()
			.toEpochSecond()

	val json = """
		{
			"destination": {
				"type": "Module",
				"target": "/railviz/get_station"
			},
			"content_type": "RailVizStationRequest",
			"content": {
				"station_id": "$stop",
				"time": $timestamp,
				"event_count": ${limit ?: 12},
				"direction": "LATER",
				"by_schedule_time": false
			}
		}
	"""

	val result = askTransitous(context, json)

	if (result.error != null) {
		if (result.stream != null) {
			val response = withContext(Dispatchers.IO) { ErrorResponse.unmarshal(result.stream) }
			throw TrafficResponseException(result.error.statusCode, response.message, result.error)
		} else {
			throw TrafficResponseException(result.error.statusCode, "", result.error)
		}
	} else {
		return withContext(Dispatchers.IO) {
			val events = mutableListOf<Event>()
			var stopID = ""
			var stopName = ""
			var latitude = 0.0
			var longitude = 0.0
			val r = JsonReader(result.stream!!.bufferedReader())
			r.withObject { name ->
				if (name != "content") {
					r.skipValue()
					return@withObject
				}
				r.withObject { name ->
					when (name) {
						"events" -> {
							r.withArray {
								var reason = ""
								var eventTimestamp: Long = 0
								var valid = false
								var tripID = ""
								var idLineID = ""
								var stationID = ""
								var targetStationID = ""
								var targetTimestamp = 0
								var idTimestamp = 0
								var trainNr = 0
								var clasz = -1
								var direction = ""
								var lineID = ""
								var lineName = ""
								var lineColour = ""
								var eventType = ""
								r.withObject { name ->
									when (name) {
										"event" -> {
											r.withObject { name ->
												when (name) {
													"reason" -> reason = r.nextString()
													"schedule_time" -> r.skipValue()
													"schedule_track" -> r.skipValue()
													"time" -> eventTimestamp = r.nextLong()
													"track" -> r.skipValue()
													"valid" -> valid = r.nextBoolean()
													else -> r.skipValue()
												}
											}
										}

										"trips" -> {
											r.withArray { i ->
												if (i == 0) {
													r.withObject { name ->
														when (name) {
															"id" -> {
																r.withObject { name ->
																	when (name) {
																		"id" -> tripID = r.nextString()
																		"line_id" -> idLineID = r.nextString()
																		"station_id" -> stationID = r.nextString()
																		"target_station_id" -> targetStationID = r.nextString()
																		"target_time" -> targetTimestamp = r.nextInt()
																		"time" -> idTimestamp = r.nextInt()
																		"train_nr" -> trainNr = r.nextInt()
																		else -> r.skipValue()
																	}
																}
															}

															"transport" -> {
																r.withObject { name ->
																	Log.d("WithObject", "next is ${name}")
																	when (name) {
																		"clasz" -> clasz = r.nextInt()
																		"direction" -> direction = r.nextString()
																		"line_id" -> lineID = r.nextString()
																		"name" -> lineName = r.nextString()
																		"provider" -> r.skipValue()
																		"provider_url" -> r.skipValue()
																		"range" -> r.skipValue()
																		"route_color" -> lineColour = r.nextString()
																		"route_text_color" -> r.skipValue()
																		else -> r.skipValue()
																	}
																}
															}
															else -> r.skipValue()
														}
													}
												} else {
													r.skipValue()
												}
											}
										}

										"type" -> {
											eventType = r.nextString()
										}
										else -> r.skipValue()
									}
								}
								if (eventType == "ARR") {
									val hash = MessageDigest.getInstance("SHA-256").let {
										it.update(tripID.toByteArray())
										it.update(idLineID.toByteArray())
										it.update(stationID.toByteArray())
										it.update(targetStationID.toByteArray())
										it.update(targetTimestamp.toHexString().toByteArray())
										it.update(idTimestamp.toHexString().toByteArray())
										it.update(trainNr.toHexString().toByteArray())
										it.digest()
									}.toHexString()
									val t =
										ZonedDateTime.ofInstant(
											Instant.ofEpochSecond(eventTimestamp),
											ZoneId.systemDefault()
										)
									events.add(
										Event(
											id = hash,
											arrivalTime = Time(
												t.hour.toUInt(),
												t.minute.toUInt(),
												t.second.toUInt(),
												(t.dayOfYear - ZonedDateTime.now().dayOfYear).toByte(),
												ZoneId.systemDefault().id
											),
											departureTime = Time(
												t.hour.toUInt(),
												t.minute.toUInt(),
												t.second.toUInt(),
												(t.dayOfYear - ZonedDateTime.now().dayOfYear).toByte(),
												ZoneId.systemDefault().id
											),
											status = 0u,
											isRealtime = reason in setOf("IS", "PROPAGATION", "FORECAST"),
											vehicle = Vehicle(
												"",
												Position(0.0, 0.0),
												0u,
												Mps(0),
												LineStub(
													lineName,
													LineType.fromTransitous(clasz),
													Colour.fromHex(lineColour)
												),
												direction,
												CongestionLevel.UNKNOWN,
												OccupancyStatus.UNKNOWN
											),
											boarding = 0xffu,
											alerts = emptyList(),
											exact = true,
											terminusArrival = !valid
										)
									)
								}
							}
						}

						"station" -> {
							r.withObject { name ->
								when (name) {
									"id" -> stopID = r.nextString()
									"name" -> stopName = r.nextString()
									"pos" -> {
										r.withObject { posName ->
											when (posName) {
												"lat" -> latitude = r.nextDouble()
												"lng" -> longitude = r.nextDouble()
											}
										}
									}
								}
							}
						}
						else -> r.skipValue()
					}
				}
			}

			return@withContext StopEvents(
				events,
				Stop(stopID, stopName, stopName, "", "transitous", Position(latitude, longitude), listOf()),
				listOf()
			)
		}
	}
}

fun JsonReader.withObject(f: (String) -> Unit) {
	beginObject()
	while (hasNext()) {
		try {
			f(nextName())
		} catch (e: IllegalStateException) {
			val a: JsonToken = this.peek()
			Log.d("WithObject", "next is ${a.name} ${nextString()}")
			throw e
		}
	}
	endObject()
}

fun JsonReader.withArray(f: (Int) -> Unit) {
	beginArray()
	var i = 0
	while (hasNext()) {
		f(i)
		i++
	}
	endArray()
}