Merge pull request #906 from 0pen-dash/avereha/connQuality

dash: connection quality
This commit is contained in:
Milos Kozak 2021-11-12 08:19:09 +01:00 committed by GitHub
commit 50861f237d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 19 deletions

View file

@ -60,7 +60,6 @@ class QueueThread internal constructor(
//write time //write time
sp.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis()) sp.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis())
//toggle BT //toggle BT
pump.stopConnecting()
pump.disconnect("watchdog") pump.disconnect("watchdog")
SystemClock.sleep(1000) SystemClock.sleep(1000)
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
@ -112,14 +111,18 @@ class QueueThread internal constructor(
// Pickup 1st command and set performing variable // Pickup 1st command and set performing variable
if (queue.size() > 0) { if (queue.size() > 0) {
queue.pickup() queue.pickup()
if (queue.performing() != null) { val cont = queue.performing()?.let {
aapsLogger.debug(LTag.PUMPQUEUE, "performing " + queue.performing()?.status()) aapsLogger.debug(LTag.PUMPQUEUE, "performing " + it.status())
rxBus.send(EventQueueChanged()) rxBus.send(EventQueueChanged())
queue.performing()?.execute() rxBus.send(EventPumpStatusChanged(it.status()))
it.execute()
queue.resetPerforming() queue.resetPerforming()
rxBus.send(EventQueueChanged()) rxBus.send(EventQueueChanged())
lastCommandTime = System.currentTimeMillis() lastCommandTime = System.currentTimeMillis()
SystemClock.sleep(100) SystemClock.sleep(100)
true
} ?: false
if (cont) {
continue continue
} }
} }

View file

@ -235,7 +235,6 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun connect(reason: String) { override fun connect(reason: String) {
aapsLogger.info(LTag.PUMP, "connect reason=$reason") aapsLogger.info(LTag.PUMP, "connect reason=$reason")
podStateManager.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING podStateManager.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
synchronized(this) { synchronized(this) {
stopConnecting?.let { stopConnecting?.let {
aapsLogger.warn(LTag.PUMP, "Already connecting: $stopConnecting") aapsLogger.warn(LTag.PUMP, "Already connecting: $stopConnecting")
@ -244,7 +243,6 @@ class OmnipodDashPumpPlugin @Inject constructor(
val stop = CountDownLatch(1) val stop = CountDownLatch(1)
stopConnecting = stop stopConnecting = stop
} }
thread( thread(
start = true, start = true,
name = "ConnectionThread", name = "ConnectionThread",
@ -253,6 +251,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
stopConnecting?.let { stopConnecting?.let {
val error = omnipodManager.connect(it).ignoreElements().blockingGet() val error = omnipodManager.connect(it).ignoreElements().blockingGet()
aapsLogger.info(LTag.PUMPCOMM, "connect error=$error") aapsLogger.info(LTag.PUMPCOMM, "connect error=$error")
if (error == null) {
podStateManager.incrementSuccessfulConnectionAttemptsAfterRetries()
}
} }
} finally { } finally {
synchronized(this) { synchronized(this) {
@ -270,6 +271,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun stopConnecting() { override fun stopConnecting() {
aapsLogger.info(LTag.PUMP, "stopConnecting") aapsLogger.info(LTag.PUMP, "stopConnecting")
podStateManager.incrementFailedConnectionsAfterRetries()
stopConnecting?.countDown() stopConnecting?.countDown()
omnipodManager.disconnect(true) omnipodManager.disconnect(true)
} }

View file

@ -33,6 +33,8 @@ interface OmnipodDashPodStateManager {
var bluetoothConnectionState: BluetoothConnectionState var bluetoothConnectionState: BluetoothConnectionState
var connectionAttempts: Int var connectionAttempts: Int
var successfulConnections: Int var successfulConnections: Int
val successfulConnectionAttemptsAfterRetries: Int
val failedConnectionsAfterRetries: Int
var timeZone: TimeZone var timeZone: TimeZone
val sameTimeZone: Boolean // The TimeZone is the same on the phone and on the pod val sameTimeZone: Boolean // The TimeZone is the same on the phone and on the pod
@ -85,6 +87,8 @@ interface OmnipodDashPodStateManager {
fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun updateFromPairing(uniqueId: Id, pairResult: PairResult)
fun reset() fun reset()
fun connectionSuccessRatio(): Float fun connectionSuccessRatio(): Float
fun incrementSuccessfulConnectionAttemptsAfterRetries()
fun incrementFailedConnectionsAfterRetries()
fun createActiveCommand( fun createActiveCommand(
historyId: String, historyId: String,

View file

@ -114,6 +114,23 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
podState.successfulConnections = value podState.successfulConnections = value
} }
override val successfulConnectionAttemptsAfterRetries: Int
@Synchronized
get() = podState.successfulConnectionAttemptsAfterRetries
@Synchronized
override fun incrementSuccessfulConnectionAttemptsAfterRetries() {
podState.successfulConnectionAttemptsAfterRetries++
}
override val failedConnectionsAfterRetries: Int
@Synchronized
get() = podState.failedConnectionsAfterRetries
override fun incrementFailedConnectionsAfterRetries() {
podState.failedConnectionsAfterRetries++
}
override var timeZone: TimeZone override var timeZone: TimeZone
get() = TimeZone.getTimeZone(podState.timeZone) get() = TimeZone.getTimeZone(podState.timeZone)
set(tz) { set(tz) {
@ -129,10 +146,12 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
val podOffset = timeZone.getOffset(now) val podOffset = timeZone.getOffset(now)
logger.debug( logger.debug(
LTag.PUMPCOMM, LTag.PUMPCOMM,
"sameTimeZone currentTimezone=${currentTimezone.getDisplayName( "sameTimeZone currentTimezone=${
currentTimezone.getDisplayName(
true, true,
TimeZone.SHORT TimeZone.SHORT
)} " + )
} " +
"currentOffset=$currentOffset " + "currentOffset=$currentOffset " +
"podOffset=$podOffset" "podOffset=$podOffset"
) )
@ -627,13 +646,10 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
} }
override fun connectionSuccessRatio(): Float { override fun connectionSuccessRatio(): Float {
if (connectionAttempts == 0) { if (failedConnectionsAfterRetries + successfulConnectionAttemptsAfterRetries == 0) {
return 0.0F return 0.0F
} else if (connectionAttempts <= successfulConnections) {
// Prevent bogus quality > 1 during initialisation
return 1.0F
} }
return successfulConnections.toFloat() / connectionAttempts.toFloat() return successfulConnectionAttemptsAfterRetries.toFloat() / (successfulConnectionAttemptsAfterRetries + failedConnectionsAfterRetries)
} }
override fun reset() { override fun reset() {
@ -675,6 +691,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
var connectionAttempts = 0 var connectionAttempts = 0
var successfulConnections = 0 var successfulConnections = 0
var successfulConnectionAttemptsAfterRetries = 0
var failedConnectionsAfterRetries = 0
var messageSequenceNumber: Short = 0 var messageSequenceNumber: Short = 0
var sequenceNumberOfLastProgrammingCommand: Short? = null var sequenceNumberOfLastProgrammingCommand: Short? = null
var activationTime: Long? = null var activationTime: Long? = null
@ -682,7 +700,6 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
var bluetoothAddress: String? = null var bluetoothAddress: String? = null
var ltk: ByteArray? = null var ltk: ByteArray? = null
var eapAkaSequenceNumber: Long = 1 var eapAkaSequenceNumber: Long = 1
var bolusPulsesRemaining: Short = 0
var timeZone: String = "" // TimeZone ID (e.g. "Europe/Amsterdam") var timeZone: String = "" // TimeZone ID (e.g. "Europe/Amsterdam")
var alarmSynced: Boolean = false var alarmSynced: Boolean = false

View file

@ -50,6 +50,7 @@ import org.apache.commons.lang3.StringUtils
import java.time.Duration import java.time.Duration
import java.time.ZonedDateTime import java.time.ZonedDateTime
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -213,6 +214,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
disposables += rxBus disposables += rxBus
.toObservable(EventPumpStatusChanged::class.java) .toObservable(EventPumpStatusChanged::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.delay(30, TimeUnit.MILLISECONDS, aapsSchedulers.main)
.subscribe( .subscribe(
{ {
updateBluetoothConnectionStatus(it) updateBluetoothConnectionStatus(it)
@ -254,17 +256,18 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
?: PLACEHOLDER ?: PLACEHOLDER
val connectionSuccessPercentage = podStateManager.connectionSuccessRatio() * 100 val connectionSuccessPercentage = podStateManager.connectionSuccessRatio() * 100
val connectionAttempts = podStateManager.failedConnectionsAfterRetries + podStateManager.successfulConnectionAttemptsAfterRetries
val successPercentageString = String.format("%.2f %%", connectionSuccessPercentage) val successPercentageString = String.format("%.2f %%", connectionSuccessPercentage)
val quality = val quality =
"${podStateManager.successfulConnections}/${podStateManager.connectionAttempts} :: $successPercentageString" "${podStateManager.successfulConnectionAttemptsAfterRetries}/$connectionAttempts :: $successPercentageString"
bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.text = quality bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.text = quality
val connectionStatsColor = when { val connectionStatsColor = when {
connectionSuccessPercentage > 90 -> connectionSuccessPercentage < 70 && podStateManager.successfulConnectionAttemptsAfterRetries > 50 ->
Color.WHITE Color.RED
connectionSuccessPercentage > 60 -> connectionSuccessPercentage < 90 && podStateManager.successfulConnectionAttemptsAfterRetries > 50 ->
Color.YELLOW Color.YELLOW
else -> else ->
Color.RED Color.WHITE
} }
bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.setTextColor(connectionStatsColor) bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.setTextColor(connectionStatsColor)
bluetoothStatusBinding.omnipodDashDeliveryStatus.text = podStateManager.deliveryStatus?.let { bluetoothStatusBinding.omnipodDashDeliveryStatus.text = podStateManager.deliveryStatus?.let {