91 lines
3.3 KiB
Kotlin
91 lines
3.3 KiB
Kotlin
/*
|
|
* Garmin HUD Companion Application
|
|
* Copyright (C) 2022 Piotr Dec / ztsh.eu
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package eu.ztsh.garmin.mock
|
|
|
|
import com.mapbox.api.directions.v5.models.DirectionsRoute
|
|
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
|
|
import com.mapbox.navigation.core.MapboxNavigation
|
|
import com.mapbox.navigation.core.replay.route.ReplayProgressObserver
|
|
import com.mapbox.navigation.core.replay.route.ReplayRouteMapper
|
|
import eu.ztsh.garmin.mapbox.MapControl
|
|
import java.util.*
|
|
|
|
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
|
|
class ReplayResources(private val mapControl: MapControl) {
|
|
|
|
/**
|
|
* Debug observer that makes sure the replayer has always an up-to-date information to generate mock updates.
|
|
*/
|
|
private lateinit var replayProgressObserver: ReplayProgressObserver
|
|
|
|
/**
|
|
* Debug object that converts a route into events that can be replayed to navigate a route.
|
|
*/
|
|
private val replayRouteMapper = ReplayRouteMapper()
|
|
|
|
fun replayOriginLocation() {
|
|
with(mapControl.mapboxNavigation().mapboxReplayer) {
|
|
play()
|
|
pushEvents(
|
|
listOf(
|
|
ReplayRouteMapper.mapToUpdateLocation(
|
|
Date().time.toDouble(),
|
|
com.mapbox.geojson.Point.fromLngLat(18.531478, 50.088155)
|
|
)
|
|
)
|
|
)
|
|
playFirstLocation()
|
|
}
|
|
}
|
|
|
|
fun startSimulation(route: DirectionsRoute) {
|
|
with(mapControl.mapboxNavigation()) {
|
|
mapboxReplayer.stop()
|
|
mapboxReplayer.clearEvents()
|
|
val replayData = replayRouteMapper.mapDirectionsRouteGeometry(route)
|
|
mapboxReplayer.pushEvents(replayData)
|
|
mapboxReplayer.seekTo(replayData[0])
|
|
mapboxReplayer.play()
|
|
}
|
|
}
|
|
|
|
fun stopSimulation() {
|
|
with(mapControl.mapboxNavigation()) {
|
|
mapboxReplayer.stop()
|
|
mapboxReplayer.clearEvents()
|
|
}
|
|
}
|
|
|
|
fun onAttached(mapboxNavigation: MapboxNavigation) {
|
|
replayProgressObserver = ReplayProgressObserver(mapboxNavigation.mapboxReplayer)
|
|
mapboxNavigation.registerRouteProgressObserver(replayProgressObserver)
|
|
|
|
// Start the trip session to being receiving location updates in free drive
|
|
// and later when a route is set also receiving route progress updates.
|
|
// In case of `startReplayTripSession`,
|
|
// location events are emitted by the `MapboxReplayer`
|
|
mapboxNavigation.startReplayTripSession()
|
|
}
|
|
|
|
fun onDetached(mapboxNavigation: MapboxNavigation) {
|
|
mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver)
|
|
mapboxNavigation.mapboxReplayer.finish()
|
|
}
|
|
|
|
}
|