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.bluetooth.BluetoothSocket
import android.util.Log import android.util.Log
import com.mapbox.navigation.tripdata.maneuver.model.Maneuver 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.Arrows
import eu.ztsh.garmin.data.DataCache import eu.ztsh.garmin.data.DataCache
import eu.ztsh.garmin.data.GarminMapper import eu.ztsh.garmin.data.GarminMapper
@ -25,9 +26,11 @@ class Garmin(
private lateinit var connection: ConnectThread private lateinit var connection: ConnectThread
private lateinit var maneuvers: ManeuverProcessingThread private lateinit var maneuvers: ManeuverProcessingThread
private lateinit var trips: TripProgressProcessingThread
private val cache = DataCache() private val cache = DataCache()
private val maneuversPool = Executors.newFixedThreadPool(4) private val maneuversPool = Executors.newFixedThreadPool(4)
private val tripPool = Executors.newFixedThreadPool(4)
fun start() { fun start() {
connection = ConnectThread() connection = ConnectThread()
@ -35,6 +38,9 @@ class Garmin(
maneuvers = ManeuverProcessingThread() maneuvers = ManeuverProcessingThread()
maneuvers.start() maneuvers.start()
trips = TripProgressProcessingThread()
trips.start()
} }
fun close() { fun close() {
@ -43,13 +49,42 @@ class Garmin(
maneuvers.interrupt() maneuvers.interrupt()
maneuvers.join(0) maneuvers.join(0)
trips.interrupt()
trips.join(0)
maneuversPool.shutdown() maneuversPool.shutdown()
tripPool.shutdown()
} }
fun process(maneuver: Maneuver) { fun process(maneuver: Maneuver) {
maneuversPool.submit{maneuvers.enqueue(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>() { private inner class ManeuverProcessingThread : ProcessingThread<Maneuver>() {
override fun mapAndSend(maybeItem: Maneuver?): Maneuver? { override fun mapAndSend(maybeItem: Maneuver?): Maneuver? {

View file

@ -12,6 +12,7 @@ class DataCache {
private var maneuverCache: Maneuver? = null private var maneuverCache: Maneuver? = null
private var locationCache: LocationMatcherResult? = null private var locationCache: LocationMatcherResult? = null
private var session: NavigationSessionState? = null private var session: NavigationSessionState? = null
private var eta: Arrival? = null
// maneuver // maneuver
fun hasChanged(guidance: Lane?): Boolean { fun hasChanged(guidance: Lane?): Boolean {
@ -30,6 +31,15 @@ class DataCache {
maneuverCache = maneuver maneuverCache = maneuver
} }
// eta
fun hasChanged(eta: Arrival): Boolean {
return this.eta.let { it == null || it != eta }
}
fun update(eta: Arrival) {
this.eta = eta
}
// location // location
fun hasChanged(locationMatcherResult: LocationMatcherResult): Boolean { fun hasChanged(locationMatcherResult: LocationMatcherResult): Boolean {
return locationCache == null || locationCache!! != locationMatcherResult 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.tripdata.maneuver.model.Maneuver
import com.mapbox.navigation.core.trip.session.LocationMatcherResult import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.tripdata.progress.model.TripProgressUpdateValue
class MapboxMapper { 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 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( class Direction(
var angle: OutAngle = OutAngle.AsDirection, var angle: OutAngle = OutAngle.AsDirection,

View file

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