Navigation state & location observer
This commit is contained in:
parent
b5cc580cf8
commit
9a553cf563
5 changed files with 70 additions and 21 deletions
|
@ -5,10 +5,12 @@ import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
import android.bluetooth.BluetoothSocket
|
import android.bluetooth.BluetoothSocket
|
||||||
import android.util.Log
|
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 com.mapbox.navigation.ui.maneuver.model.Maneuver
|
||||||
import eu.ztsh.garmin.data.DataCache
|
import eu.ztsh.garmin.data.DataCache
|
||||||
import eu.ztsh.garmin.data.GarminMapper
|
import eu.ztsh.garmin.data.GarminMapper
|
||||||
import eu.ztsh.garmin.data.ManeuverMapper
|
import eu.ztsh.garmin.data.MapboxMapper
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.SynchronousQueue
|
import java.util.concurrent.SynchronousQueue
|
||||||
|
@ -34,44 +36,56 @@ class Garmin(
|
||||||
}
|
}
|
||||||
|
|
||||||
fun process(maneuver: Maneuver) {
|
fun process(maneuver: Maneuver) {
|
||||||
processing = ProcessingThread(maneuver)
|
processing = ManeuverProcessingThread(maneuver)
|
||||||
processing.start()
|
processing.start()
|
||||||
processing.join()
|
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() {
|
override fun run() {
|
||||||
if (cache.hasChanged(maneuver)) {
|
if (cache.hasChanged(maneuver)) {
|
||||||
cache.update(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) {
|
fun send(incoming: eu.ztsh.garmin.data.State) {
|
||||||
if (cache.hasChanged(incoming.distance)) {
|
if (cache.hasChanged(incoming.distance)) {
|
||||||
setDistance(incoming)
|
connection.enqueue(GarminMapper.setDistance(incoming))
|
||||||
}
|
}
|
||||||
if (cache.hasChanged(incoming.direction)) {
|
if (cache.hasChanged(incoming.direction)) {
|
||||||
setDirection(incoming)
|
connection.enqueue(GarminMapper.setDirection(incoming))
|
||||||
}
|
}
|
||||||
cache.update(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() {
|
private inner class ConnectThread : Thread() {
|
||||||
|
|
|
@ -12,6 +12,8 @@ import com.mapbox.navigation.core.formatter.MapboxDistanceFormatter
|
||||||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
||||||
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
|
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
|
||||||
import com.mapbox.navigation.core.trip.session.LocationObserver
|
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.core.trip.session.RouteProgressObserver
|
||||||
import com.mapbox.navigation.ui.maneuver.api.MapboxManeuverApi
|
import com.mapbox.navigation.ui.maneuver.api.MapboxManeuverApi
|
||||||
|
|
||||||
|
@ -48,12 +50,13 @@ class MapboxToolbox(lifecycle: Lifecycle, private val context: MainActivity) {
|
||||||
fun onStart() {
|
fun onStart() {
|
||||||
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
||||||
MapboxNavigationApp.current()?.registerLocationObserver(locationObserver)
|
MapboxNavigationApp.current()?.registerLocationObserver(locationObserver)
|
||||||
// MapboxNavigationApp.current()?.registerNavigationSessionStateObserver()
|
MapboxNavigationApp.current()?.registerNavigationSessionStateObserver(navigationStateObserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onStop() {
|
fun onStop() {
|
||||||
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
MapboxNavigationApp.current()?.unregisterRouteProgressObserver(routeProgressObserver)
|
||||||
MapboxNavigationApp.current()?.unregisterLocationObserver(locationObserver)
|
MapboxNavigationApp.current()?.unregisterLocationObserver(locationObserver)
|
||||||
|
MapboxNavigationApp.current()?.unregisterNavigationSessionStateObserver(navigationStateObserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onDestroy() {
|
fun onDestroy() {
|
||||||
|
@ -81,11 +84,13 @@ class MapboxToolbox(lifecycle: Lifecycle, private val context: MainActivity) {
|
||||||
|
|
||||||
private val locationObserver = object : LocationObserver {
|
private val locationObserver = object : LocationObserver {
|
||||||
override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
|
override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
|
||||||
Log.d("LOCATION", "")
|
context.garmin.process(locationMatcherResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNewRawLocation(rawLocation: Location) {
|
override fun onNewRawLocation(rawLocation: Location) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val navigationStateObserver = NavigationSessionStateObserver { context.garmin.process(it) }
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,11 +1,15 @@
|
||||||
package eu.ztsh.garmin.data
|
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
|
import com.mapbox.navigation.ui.maneuver.model.Maneuver
|
||||||
|
|
||||||
class DataCache {
|
class DataCache {
|
||||||
|
|
||||||
private val stateCache: State = State()
|
private val stateCache: State = State()
|
||||||
private var maneuverCache: Maneuver? = null
|
private var maneuverCache: Maneuver? = null
|
||||||
|
private var locationCache: LocationMatcherResult? = null
|
||||||
|
private var session: NavigationSessionState? = null
|
||||||
|
|
||||||
// state
|
// state
|
||||||
fun hasChanged(lanes: Lanes?): Boolean {
|
fun hasChanged(lanes: Lanes?): Boolean {
|
||||||
|
@ -63,4 +67,22 @@ class DataCache {
|
||||||
maneuverCache = maneuver
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,9 @@
|
||||||
package eu.ztsh.garmin.data
|
package eu.ztsh.garmin.data
|
||||||
|
|
||||||
|
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
|
||||||
import com.mapbox.navigation.ui.maneuver.model.Maneuver
|
import com.mapbox.navigation.ui.maneuver.model.Maneuver
|
||||||
|
|
||||||
class ManeuverMapper {
|
class MapboxMapper {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
@ -59,6 +60,12 @@ class ManeuverMapper {
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun apply(locationMatcherResult: LocationMatcherResult): State {
|
||||||
|
val state = State()
|
||||||
|
// TODO: speed, limit, location?, bearing
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -106,6 +106,7 @@ class State {
|
||||||
var distance: Distance? = null
|
var distance: Distance? = null
|
||||||
var speed: Speed? = null
|
var speed: Speed? = null
|
||||||
var arrival: Arrival? = null
|
var arrival: Arrival? = null
|
||||||
|
// TODO: Bearing
|
||||||
// TODO: support
|
// TODO: support
|
||||||
var traffic: Boolean? = null
|
var traffic: Boolean? = null
|
||||||
var flag: Boolean? = null
|
var flag: Boolean? = null
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue