Merge remote-tracking branch 'origin/mapbox' into mapbox

This commit is contained in:
Piotr Dec 2024-07-30 18:01:30 +02:00
commit 73719bbedf
Signed by: stawros
GPG key ID: F89F27AD8F881A91
15 changed files with 961 additions and 268 deletions

View file

@ -5,7 +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.MapboxMapper
import java.io.IOException
import java.util.*
import java.util.concurrent.SynchronousQueue
@ -19,7 +24,7 @@ class Garmin(
private lateinit var connection: ConnectThread
private lateinit var processing: ProcessingThread
private var stateCache: State = State()
private val cache = DataCache()
fun start() {
connection = ConnectThread()
@ -31,58 +36,55 @@ 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()
}
fun process(navigationSessionState: NavigationSessionState) {
cache.update(navigationSessionState)
}
private inner class ManeuverProcessingThread(val maneuver: Maneuver) : ProcessingThread() {
private inner class ProcessingThread(val maneuver: Maneuver) : Thread() {
override fun run() {
send(ManeuverMapper.apply(maneuver))
if (cache.hasChanged(maneuver)) {
cache.update(maneuver)
send(MapboxMapper.apply(maneuver))
}
}
fun send(incoming: eu.ztsh.garmin.State) {
if (stateCache.distance != incoming.distance) {
setDistance(incoming)
}
private inner class LocationProcessingThread(val location: LocationMatcherResult) : ProcessingThread() {
override fun run() {
if (cache.hasChanged(location)) {
cache.update(location)
send(MapboxMapper.apply(location))
}
if (stateCache.direction != incoming.direction) {
setDirection(stateCache.direction)
}
stateCache = incoming
}
private fun setDistance(state: eu.ztsh.garmin.State) {
connection.enqueue(intArrayOf(
0x03, asDigit(state.distance / 1000), asDigit(state.distance / 100), asDigit(state.distance / 10),
0x00, asDigit(state.distance), state.unit.data
))
}
private open inner class ProcessingThread : Thread() {
fun send(incoming: eu.ztsh.garmin.data.State) {
if (cache.hasChanged(incoming.distance)) {
connection.enqueue(GarminMapper.setDistance(incoming))
}
if (cache.hasChanged(incoming.direction)) {
connection.enqueue(GarminMapper.setDirection(incoming))
}
cache.update(incoming)
}
private fun setDirection(direction: Direction) {
val param1 = when (direction.outAngle) {
OutAngle.LeftDown -> 0x10
OutAngle.RightDown -> 0x20
else -> direction.outType.data
}
val param2: Int = if (direction.outType == OutType.RightRoundabout
|| direction.outType == OutType.LeftRoundabout) {
if (direction.roundabout == OutAngle.AsDirection) direction.outAngle.data else direction.roundabout.data
} else {
0x00
}
val param3: Int = if (direction.outAngle == OutAngle.LeftDown || direction.outAngle == OutAngle.RightDown) 0x00 else direction.outAngle.data
connection.enqueue(intArrayOf(0x01, param1, param2, param3))
}
private fun asDigit(n: Int): Int {
if (n == 0) {
return 0
}
val m = n % 10
return if (m == 0) 10 else m
}
}
private inner class ConnectThread : Thread() {
@ -105,10 +107,13 @@ class Garmin(
sleep(3000)
readAll()
while (true) {
val newCurrent = Optional.ofNullable(queue.poll()).orElse(current)
current = newCurrent
val newCurrent = queue.poll()
if (newCurrent == null) {
sleep(500)
} else {
current = newCurrent
}
send(current)
sleep(900)
}
} catch (e: IOException) {
Log.d(TAG, "Not connected", e)