Data cache

This commit is contained in:
Piotr Dec 2023-08-25 08:25:06 +02:00
parent 89e16fb805
commit b5cc580cf8
No known key found for this signature in database
GPG key ID: ABD6CB83D6110D27
5 changed files with 178 additions and 40 deletions

View file

@ -6,17 +6,13 @@ import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.util.Log
import com.mapbox.navigation.ui.maneuver.model.Maneuver
import eu.ztsh.garmin.data.Direction
import eu.ztsh.garmin.data.DataCache
import eu.ztsh.garmin.data.GarminMapper
import eu.ztsh.garmin.data.ManeuverMapper
import eu.ztsh.garmin.data.OutAngle
import eu.ztsh.garmin.data.OutType
import eu.ztsh.garmin.data.State
import java.io.IOException
import java.util.*
import java.util.concurrent.SynchronousQueue
@SuppressLint("MissingPermission")
class Garmin(
val context: MainActivity,
@ -26,7 +22,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()
@ -46,22 +42,24 @@ class Garmin(
private inner class ProcessingThread(val maneuver: Maneuver) : Thread() {
override fun run() {
// TODO: check for equality before mapping!
send(ManeuverMapper.apply(maneuver))
if (cache.hasChanged(maneuver)) {
cache.update(maneuver)
send(ManeuverMapper.apply(maneuver))
}
}
fun send(incoming: eu.ztsh.garmin.data.State) {
if (stateCache.distance != incoming.distance) {
if (cache.hasChanged(incoming.distance)) {
setDistance(incoming)
}
if (stateCache.direction != incoming.direction) {
setDirection(stateCache)
if (cache.hasChanged(incoming.direction)) {
setDirection(incoming)
}
stateCache = incoming
cache.update(incoming)
}
private fun setLines(state: eu.ztsh.garmin.data.State) {
}
@ -94,10 +92,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)
}
}
@ -138,6 +139,7 @@ class Garmin(
}
companion object {
fun prepareData(input: IntArray): IntArray {
val n = input.size
var crc = (0xeb + n + n).toUInt()