fix: Model rebuilt

This commit is contained in:
Piotr Dec 2024-07-31 20:16:44 +02:00
parent 75e10c1579
commit 785a35473e
Signed by: stawros
GPG key ID: F89F27AD8F881A91
6 changed files with 136 additions and 137 deletions

View file

@ -5,11 +5,11 @@ 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.tripdata.maneuver.model.Maneuver
import eu.ztsh.garmin.data.DataCache
import eu.ztsh.garmin.data.GarminManeuver
import eu.ztsh.garmin.data.GarminMapper
import eu.ztsh.garmin.data.GarminModelItem
import eu.ztsh.garmin.data.MapboxMapper
import java.io.IOException
import java.util.*
@ -39,46 +39,61 @@ class Garmin(
ManeuverProcessingThread(maneuver).start()
}
fun process(location: LocationMatcherResult) {
LocationProcessingThread(location).start()
}
// fun process(location: LocationMatcherResult) {
// LocationProcessingThread(location).start()
// }
fun process(navigationSessionState: NavigationSessionState) {
cache.update(navigationSessionState)
}
// fun process(navigationSessionState: NavigationSessionState) {
// cache.update(navigationSessionState)
// }
private inner class ManeuverProcessingThread(val maneuver: Maneuver) : ProcessingThread() {
private inner class ManeuverProcessingThread(val maneuver: Maneuver) : ProcessingThread<GarminManeuver>() {
override fun run() {
override fun process(): GarminManeuver? {
if (cache.hasChanged(maneuver)) {
cache.update(maneuver)
send(MapboxMapper.apply(maneuver))
return MapboxMapper.map(maneuver)
}
return null
}
override fun enqueue(item: GarminManeuver) {
if (cache.hasChanged(item.lanes)) {
connection.enqueue(GarminMapper.map(item.lanes))
}
if (cache.hasChanged(item.direction)) {
connection.enqueue(GarminMapper.map(item.direction))
}
if (cache.hasChanged(item.distance)) {
connection.enqueue(GarminMapper.map(item.distance))
}
// flag?
}
}
private inner class LocationProcessingThread(val location: LocationMatcherResult) : ProcessingThread() {
// private inner class LocationProcessingThread(val location: LocationMatcherResult) : ProcessingThread() {
//
// override fun process() {
// if (cache.hasChanged(location)) {
// cache.update(location)
// send(MapboxMapper.apply(location))
// }
// }
// }
private abstract inner class ProcessingThread<T : GarminModelItem> : Thread() {
abstract fun process(): T?
abstract fun enqueue(item: T)
override fun run() {
if (cache.hasChanged(location)) {
cache.update(location)
send(MapboxMapper.apply(location))
val processing = process()
if (processing != null) {
enqueue(processing)
cache.update(processing)
}
}
}
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)
observer.join(this)
}
@ -86,7 +101,7 @@ class Garmin(
private inner class ProcessingThreadObserver {
fun join(thread: ProcessingThread) {
fun <T : GarminModelItem> join(thread: ProcessingThread<T>) {
thread.join()
}