Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/org/mtransit/android/commons/TimeUtilsK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ object TimeUtilsK {
if (negative) insert(0, "-")
}.trim()

fun currentInstant() = TimeUtils.currentTimeMillis().toInstant()
fun currentInstant() = TimeUtils.currentTimeMillis().millisToInstant()
}

fun Long.toInstant() = Instant.fromEpochMilliseconds(this)
fun Long.millisToInstant() = Instant.fromEpochMilliseconds(this)

fun Long.secsToInstant() = Instant.fromEpochSeconds(this)
Comment thread
mmathieum marked this conversation as resolved.

fun Instant.toMillis() = this.toEpochMilliseconds()

fun Instant.toSecs() = this.epochSeconds
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.mtransit.android.commons.provider.gtfs.routeIdCleanupPattern
import org.mtransit.android.commons.provider.gtfs.tripIdCleanupPattern
import org.mtransit.android.commons.provider.vehiclelocations.VehicleLocationProvider.Companion.getCachedVehicleLocationsS
import org.mtransit.android.commons.provider.vehiclelocations.model.VehicleLocation
import org.mtransit.android.commons.secsToInstant
import java.net.HttpURLConnection
import java.net.SocketException
import java.net.UnknownHostException
Expand Down Expand Up @@ -250,7 +251,7 @@ object GTFSRealTimeVehiclePositionsProvider {
//
vehicleId = gVehiclePosition.optVehicle?.optId,
vehicleLabel = gVehiclePosition.optVehicle?.optLabel,
reportTimestamp = gVehiclePosition.optTimestamp?.seconds,
reportTimestamp = gVehiclePosition.optTimestamp?.secsToInstant(),
latitude = gVehiclePosition.optPosition?.optLatitude ?: return null,
longitude = gVehiclePosition.optPosition?.optLongitude ?: return null,
bearingDegrees = gVehiclePosition.optPosition?.optBearing?.toInt(), // in degrees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.mtransit.android.commons.Constants
import org.mtransit.android.commons.MTLog
import org.mtransit.android.commons.SecurityUtils
import org.mtransit.android.commons.TimeUtils
import org.mtransit.android.commons.TimeUtilsK
import org.mtransit.android.commons.data.Route
import org.mtransit.android.commons.data.RouteDirection
import org.mtransit.android.commons.data.RouteDirectionStop
Expand All @@ -17,16 +18,16 @@ import org.mtransit.android.commons.provider.nextbus.api.NextBusApi
import org.mtransit.android.commons.provider.nextbus.api.VehicleLocationsResponse
import org.mtransit.android.commons.provider.vehiclelocations.VehicleLocationProvider.Companion.getCachedVehicleLocationsS
import org.mtransit.android.commons.provider.vehiclelocations.model.VehicleLocation
import org.mtransit.android.commons.toMillis
import java.net.HttpURLConnection
import java.net.SocketException
import java.net.UnknownHostException
import javax.net.ssl.SSLHandshakeException
import kotlin.math.min
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Instant

object NextBusVehicleLocationsProvider {

Expand Down Expand Up @@ -153,8 +154,8 @@ object NextBusVehicleLocationsProvider {
.getVehicleLocations(agencyTag = agencyTag)
.execute()
NextBusStorage.saveVehicleLocationLastUpdateCode(context, response.code())
val newLastUpdate = TimeUtils.currentTimeMillis().milliseconds
NextBusStorage.saveVehicleLocationLastUpdateMs(context, newLastUpdate.inWholeMilliseconds)
val newLastUpdate = TimeUtilsK.currentInstant()
NextBusStorage.saveVehicleLocationLastUpdateMs(context, newLastUpdate.toMillis())
when (response.code()) {
HttpURLConnection.HTTP_OK -> {
val vehicleLocations = mutableListOf<VehicleLocation>()
Expand Down Expand Up @@ -217,7 +218,7 @@ object NextBusVehicleLocationsProvider {
}

private fun NextBusProvider.processVehiclePositions(
newLastUpdate: Duration,
newLastUpdate: Instant,
nVehicle: VehicleLocationsResponse.Vehicle,
): Set<VehicleLocation>? {
val targetUUIDs = parseProviderTargetUUID(nVehicle)?.takeIf { it.isNotBlank() } ?: return null
Expand All @@ -226,7 +227,7 @@ object NextBusVehicleLocationsProvider {
authority = this.authority,
targetUUID = targetUUIDs,
targetTripId = null, // no GTFS trip.id info returned
lastUpdateInMs = newLastUpdate.inWholeMilliseconds,
lastUpdateInMs = newLastUpdate.toMillis(),
maxValidityInMs = this@processVehiclePositions.vehicleLocationMaxValidityInMs,
//
vehicleId = nVehicle.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package org.mtransit.android.commons.provider.vehiclelocations.model
import android.content.ContentValues
import android.database.Cursor
import org.mtransit.android.commons.TimeUtils
import org.mtransit.android.commons.TimeUtilsK
import org.mtransit.android.commons.getFloat
import org.mtransit.android.commons.getLong
import org.mtransit.android.commons.getString
import org.mtransit.android.commons.optInt
import org.mtransit.android.commons.optLong
import org.mtransit.android.commons.optString
import org.mtransit.android.commons.provider.vehiclelocations.VehicleLocationProviderContract
import org.mtransit.android.commons.secsToInstant
import org.mtransit.android.commons.toMillis
import org.mtransit.android.commons.toSecs
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Instant

/**
* See [VehicleLocationProviderContract]
Expand All @@ -27,19 +30,19 @@ data class VehicleLocation(
//
val vehicleId: String?, // not user visible
val vehicleLabel: String?, // user visible
val reportTimestamp: Duration?, // in SECONDS
val reportTimestamp: Instant?,
val latitude: Float,
val longitude: Float,
val bearingDegrees: Int?, // in degrees
val speedMetersPerSecond: Int?, // in m/s
) {

val reportTimestampSec: Long? get() = reportTimestamp?.inWholeSeconds
val reportTimestampSec: Long? get() = reportTimestamp?.toSecs()

@Suppress("unused")
val reportTimestampMs: Long? get() = reportTimestamp?.inWholeMilliseconds
val reportTimestampMs: Long? get() = reportTimestamp?.toMillis()

val reportTimestampCountdown: Duration? get() = reportTimestamp?.let { (TimeUtils.currentTimeMillis().milliseconds - it) }
val reportTimestampCountdown: Duration? get() = reportTimestamp?.let { (TimeUtilsK.currentInstant() - it) }

private val _uid: String? = this.vehicleId ?: this.vehicleLabel

Expand All @@ -57,7 +60,7 @@ data class VehicleLocation(
//
vehicleId = cursor.optString(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_VEHICLE_ID),
vehicleLabel = cursor.optString(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_VEHICLE_LABEL),
reportTimestamp = cursor.optLong(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_VEHICLE_REPORT_TIMESTAMP)?.seconds,
reportTimestamp = cursor.optLong(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_VEHICLE_REPORT_TIMESTAMP)?.secsToInstant(),
latitude = cursor.getFloat(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_LATITUDE),
longitude = cursor.getFloat(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_LONGITUDE),
bearingDegrees = cursor.optInt(VehicleLocationProviderContract.Columns.T_VEHICLE_LOCATION_K_BEARING),
Expand Down