feat: ETA

This commit is contained in:
Piotr Dec 2024-08-02 23:28:56 +02:00
parent 028f1b6082
commit c329cffeb7
Signed by: stawros
GPG key ID: F89F27AD8F881A91
5 changed files with 77 additions and 6 deletions

View file

@ -6,6 +6,7 @@ import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.util.Log
import com.mapbox.navigation.tripdata.maneuver.model.Maneuver
import com.mapbox.navigation.tripdata.progress.model.TripProgressUpdateValue
import eu.ztsh.garmin.data.Arrows
import eu.ztsh.garmin.data.DataCache
import eu.ztsh.garmin.data.GarminMapper
@ -25,9 +26,11 @@ class Garmin(
private lateinit var connection: ConnectThread
private lateinit var maneuvers: ManeuverProcessingThread
private lateinit var trips: TripProgressProcessingThread
private val cache = DataCache()
private val maneuversPool = Executors.newFixedThreadPool(4)
private val tripPool = Executors.newFixedThreadPool(4)
fun start() {
connection = ConnectThread()
@ -35,6 +38,9 @@ class Garmin(
maneuvers = ManeuverProcessingThread()
maneuvers.start()
trips = TripProgressProcessingThread()
trips.start()
}
fun close() {
@ -43,13 +49,42 @@ class Garmin(
maneuvers.interrupt()
maneuvers.join(0)
trips.interrupt()
trips.join(0)
maneuversPool.shutdown()
tripPool.shutdown()
}
fun process(maneuver: Maneuver) {
maneuversPool.submit{maneuvers.enqueue(maneuver)}
}
fun process(tripProgressUpdateValue: TripProgressUpdateValue) {
maneuversPool.submit{trips.enqueue(tripProgressUpdateValue)}
}
private inner class TripProgressProcessingThread : ProcessingThread<TripProgressUpdateValue>() {
override fun mapAndSend(maybeItem: TripProgressUpdateValue?): TripProgressUpdateValue? {
if (maybeItem != null) {
// it is much simplier to parse and compare model object
val value = MapboxMapper.asEta(maybeItem)
if (cache.hasChanged(value)) {
// TODO: traffic
send(GarminMapper.setTime(value.hours, value.minutes))
cache.update(value)
}
}
return null
}
override fun updateCache(item: TripProgressUpdateValue) {
// won't be used
}
}
private inner class ManeuverProcessingThread : ProcessingThread<Maneuver>() {
override fun mapAndSend(maybeItem: Maneuver?): Maneuver? {

View file

@ -12,6 +12,7 @@ class DataCache {
private var maneuverCache: Maneuver? = null
private var locationCache: LocationMatcherResult? = null
private var session: NavigationSessionState? = null
private var eta: Arrival? = null
// maneuver
fun hasChanged(guidance: Lane?): Boolean {
@ -30,6 +31,15 @@ class DataCache {
maneuverCache = maneuver
}
// eta
fun hasChanged(eta: Arrival): Boolean {
return this.eta.let { it == null || it != eta }
}
fun update(eta: Arrival) {
this.eta = eta
}
// location
fun hasChanged(locationMatcherResult: LocationMatcherResult): Boolean {
return locationCache == null || locationCache!! != locationMatcherResult

View file

@ -2,6 +2,7 @@ package eu.ztsh.garmin.data
import com.mapbox.navigation.tripdata.maneuver.model.Maneuver
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.tripdata.progress.model.TripProgressUpdateValue
class MapboxMapper {
@ -111,6 +112,13 @@ class MapboxMapper {
}
fun asEta(trip: TripProgressUpdateValue): Arrival {
val eta = trip.formatter
.getEstimatedTimeToArrival(trip.estimatedTimeToArrival)
.toString().split(":")
return Arrival(eta[0].toInt(), eta[1].toInt())
}
}
}

View file

@ -101,7 +101,24 @@ class Distance(val distance: Double, val unit: Unit) {
class Speed(val speed: Int, val limit: Int)
class Arrival(val hours: Int, val minutes: Int)
class Arrival(val hours: Int, val minutes: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Arrival
if (hours != other.hours) return false
if (minutes != other.minutes) return false
return true
}
override fun hashCode(): Int {
return 61 * hours + minutes
}
}
class Direction(
var angle: OutAngle = OutAngle.AsDirection,

View file

@ -91,7 +91,7 @@ class RouteControl(private val mapControl: MapControl, ui: UI, private val conte
PercentDistanceTraveledFormatter()
)
.estimatedTimeToArrivalFormatter(
EstimatedTimeToArrivalFormatter(context, TimeFormat.NONE_SPECIFIED)
EstimatedTimeToArrivalFormatter(context, TimeFormat.TWENTY_FOUR_HOURS)
)
.build()
)
@ -172,10 +172,11 @@ class RouteControl(private val mapControl: MapControl, ui: UI, private val conte
}
)
// update bottom trip progress summary
mapControl.ui.tripProgressView.render(
tripProgressApi.getTripProgress(routeProgress)
)
// update bottom trip progress summary and send to HUD
tripProgressApi.getTripProgress(routeProgress).let {
mapControl.ui.tripProgressView.render(it)
Garmin.instance.process(it)
}
}
/**