Bimba.git

ref: 3ad0d68a424da7ca2bb59c9ae179d372cfca9615

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
// 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.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.transitous.api.TimetableApi
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.math.BigDecimal
import java.time.Instant
import java.time.LocalDate
import java.time.OffsetDateTime
import java.time.ZoneId

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))
	}

	return withContext(Dispatchers.IO) {
		// TODO shouldn't it be start-of-day in stop's timezone?
		val datetime = date?.let {
			OffsetDateTime.of(date.atStartOfDay(), ZoneId.systemDefault().rules.getOffset(Instant.now()))
		}
		val times = TimetableApi().stoptimes(stop, limit ?: 12, datetime)
		var stopName = ""
		var latitude: BigDecimal = BigDecimal(0)
		var longitude: BigDecimal = BigDecimal(0)
		val departures = times.stopTimes.map {
			Log.i("stop", "stopID recvd: ${it.place.stopId}")
			if (it.place.arrival == null) {
				null
			} else {
				latitude = it.place.lat
				longitude = it.place.lon
				stopName = it.place.name
				Departure(
					it.tripId + it.source,
					Time.fromOffsetTime(it.place.arrival, ZoneId.systemDefault()),
					0u,
					it.realTime,
					Vehicle(
						it.tripId,
						Position(0.0, 0.0),
						0u,
						Mps(0),
						LineStub(it.routeShortName, LineType.fromTransitous2(it.mode), Colour.fromHex(it.routeColor)),
						it.headsign,
						CongestionLevel.UNKNOWN,
						OccupancyStatus.UNKNOWN
					),
					boarding = 0xffu,
					alerts = emptyList(),
					exact = true,
					terminusArrival = it.place.departure == null
				)
			}
		}.filterNotNull()
		Log.i("stop", "stopID asked: $stop")
		StopDepartures(
			departures,
			Stop(
				stop,
				stopName,
				stopName,
				"",
				"transitous",
				Position(latitude.toDouble(), longitude.toDouble()),
				listOf(),
				null
			),
			listOf()
		)
	}
}