Navigation state & location observer

This commit is contained in:
Piotr Dec 2023-08-25 14:34:43 +02:00
parent b5cc580cf8
commit 9a553cf563
No known key found for this signature in database
GPG key ID: ABD6CB83D6110D27
5 changed files with 70 additions and 21 deletions

View file

@ -5,10 +5,12 @@ import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.util.Log
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.NavigationSessionState
import com.mapbox.navigation.ui.maneuver.model.Maneuver
import eu.ztsh.garmin.data.DataCache
import eu.ztsh.garmin.data.GarminMapper
import eu.ztsh.garmin.data.ManeuverMapper
import eu.ztsh.garmin.data.MapboxMapper
import java.io.IOException
import java.util.*
import java.util.concurrent.SynchronousQueue
@ -34,44 +36,56 @@ class Garmin(
}
fun process(maneuver: Maneuver) {
processing = ProcessingThread(maneuver)
processing = ManeuverProcessingThread(maneuver)
processing.start()
processing.join()
}
fun process(location: LocationMatcherResult) {
processing = LocationProcessingThread(location)
processing.start()
processing.join()
}
private inner class ProcessingThread(val maneuver: Maneuver) : Thread() {
fun process(navigationSessionState: NavigationSessionState) {
cache.update(navigationSessionState)
}
private inner class ManeuverProcessingThread(val maneuver: Maneuver) : ProcessingThread() {
override fun run() {
if (cache.hasChanged(maneuver)) {
cache.update(maneuver)
send(ManeuverMapper.apply(maneuver))
send(MapboxMapper.apply(maneuver))
}
}
}
private inner class LocationProcessingThread(val location: LocationMatcherResult) : ProcessingThread() {
override fun run() {
if (cache.hasChanged(location)) {
cache.update(location)
send(MapboxMapper.apply(location))
}
}
}
private open inner class ProcessingThread : Thread() {
fun send(incoming: eu.ztsh.garmin.data.State) {
if (cache.hasChanged(incoming.distance)) {
setDistance(incoming)
connection.enqueue(GarminMapper.setDistance(incoming))
}
if (cache.hasChanged(incoming.direction)) {
setDirection(incoming)
connection.enqueue(GarminMapper.setDirection(incoming))
}
cache.update(incoming)
}
private fun setLines(state: eu.ztsh.garmin.data.State) {
}
private fun setDistance(state: eu.ztsh.garmin.data.State) {
connection.enqueue(GarminMapper.setDistance(state))
}
private fun setDirection(direction: eu.ztsh.garmin.data.State) {
connection.enqueue(GarminMapper.setDirection(direction))
}
}
private inner class ConnectThread : Thread() {

View file

@ -12,6 +12,8 @@ import com.mapbox.navigation.core.formatter.MapboxDistanceFormatter
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.LocationObserver
import com.mapbox.navigation.core.trip.session.NavigationSessionState
import com.mapbox.navigation.core.trip.session.NavigationSessionStateObserver
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
import com.mapbox.navigation.ui.maneuver.api.MapboxManeuverApi
@ -48,12 +50,13 @@ class MapboxToolbox(lifecycle: Lifecycle, private val context: MainActivity) {
fun onStart() {
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
MapboxNavigationApp.current()?.registerLocationObserver(locationObserver)
// MapboxNavigationApp.current()?.registerNavigationSessionStateObserver()
MapboxNavigationApp.current()?.registerNavigationSessionStateObserver(navigationStateObserver)
}
fun onStop() {
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
MapboxNavigationApp.current()?.unregisterLocationObserver(locationObserver)
MapboxNavigationApp.current()?.unregisterNavigationSessionStateObserver(navigationStateObserver)
}
fun onDestroy() {
@ -81,11 +84,13 @@ class MapboxToolbox(lifecycle: Lifecycle, private val context: MainActivity) {
private val locationObserver = object : LocationObserver {
override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
Log.d("LOCATION", "")
context.garmin.process(locationMatcherResult)
}
override fun onNewRawLocation(rawLocation: Location) {
}
}
private val navigationStateObserver = NavigationSessionStateObserver { context.garmin.process(it) }
}

View file

@ -1,11 +1,15 @@
package eu.ztsh.garmin.data
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.NavigationSessionState
import com.mapbox.navigation.ui.maneuver.model.Maneuver
class DataCache {
private val stateCache: State = State()
private var maneuverCache: Maneuver? = null
private var locationCache: LocationMatcherResult? = null
private var session: NavigationSessionState? = null
// state
fun hasChanged(lanes: Lanes?): Boolean {
@ -63,4 +67,22 @@ class DataCache {
maneuverCache = maneuver
}
// location
fun hasChanged(locationMatcherResult: LocationMatcherResult): Boolean {
return locationCache == null || locationCache!! != locationMatcherResult
}
fun update(locationMatcherResult: LocationMatcherResult) {
locationCache = locationMatcherResult
}
// session
fun isActive(): Boolean {
return session != null && session is NavigationSessionState.ActiveGuidance
}
fun update(sessionState: NavigationSessionState) {
session = sessionState
}
}

View file

@ -1,8 +1,9 @@
package eu.ztsh.garmin.data
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.ui.maneuver.model.Maneuver
class ManeuverMapper {
class MapboxMapper {
companion object {
@ -59,6 +60,12 @@ class ManeuverMapper {
return state
}
fun apply(locationMatcherResult: LocationMatcherResult): State {
val state = State()
// TODO: speed, limit, location?, bearing
return state
}
}
}

View file

@ -106,6 +106,7 @@ class State {
var distance: Distance? = null
var speed: Speed? = null
var arrival: Arrival? = null
// TODO: Bearing
// TODO: support
var traffic: Boolean? = null
var flag: Boolean? = null