From d5ae4cabf282d7a3f18e573cc622fc33bf0bc320 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Wed, 10 Nov 2021 23:10:26 +0100 Subject: [PATCH 1/5] call stopConnecting only once --- .../main/java/info/nightscout/androidaps/queue/QueueThread.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt index 1bdbdc7d64..df3ce83881 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt @@ -60,7 +60,6 @@ class QueueThread internal constructor( //write time sp.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis()) //toggle BT - pump.stopConnecting() pump.disconnect("watchdog") SystemClock.sleep(1000) val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() From 3a9213992b9332cc6cb22381cd05e7fc200e752e Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 11 Nov 2021 00:18:56 +0100 Subject: [PATCH 2/5] fix connnecting quality counter --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 4 +-- .../pod/state/OmnipodDashPodStateManager.kt | 4 +++ .../state/OmnipodDashPodStateManagerImpl.kt | 33 ++++++++++++++----- .../dash/ui/OmnipodDashOverviewFragment.kt | 10 +++--- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index ce3a4a3e7b..3e8d4920bb 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -235,7 +235,6 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun connect(reason: String) { aapsLogger.info(LTag.PUMP, "connect reason=$reason") podStateManager.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING - synchronized(this) { stopConnecting?.let { aapsLogger.warn(LTag.PUMP, "Already connecting: $stopConnecting") @@ -244,7 +243,7 @@ class OmnipodDashPumpPlugin @Inject constructor( val stop = CountDownLatch(1) stopConnecting = stop } - + podStateManager.incrementConnectionAttemptsWithRetries() thread( start = true, name = "ConnectionThread", @@ -270,6 +269,7 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun stopConnecting() { aapsLogger.info(LTag.PUMP, "stopConnecting") + podStateManager.incrementFailedConnectionsAfterRetries() stopConnecting?.countDown() omnipodManager.disconnect(true) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 6f5ab0ee62..652819fa11 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -33,6 +33,8 @@ interface OmnipodDashPodStateManager { var bluetoothConnectionState: BluetoothConnectionState var connectionAttempts: Int var successfulConnections: Int + val connectionAttemptsWithRetries: Int + val failedConnectionsAfterRetries: Int var timeZone: TimeZone 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 reset() fun connectionSuccessRatio(): Float + fun incrementConnectionAttemptsWithRetries() + fun incrementFailedConnectionsAfterRetries() fun createActiveCommand( historyId: String, diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 07404b8607..9ab82212c1 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -114,6 +114,23 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( podState.successfulConnections = value } + override val connectionAttemptsWithRetries: Int + @Synchronized + get() = podState.connectionAttemptsWithRetries + + @Synchronized + override fun incrementConnectionAttemptsWithRetries() { + podState.connectionAttemptsWithRetries++ + } + + override val failedConnectionsAfterRetries: Int + @Synchronized + get() = podState.failedConnectionsAfterRetries + + override fun incrementFailedConnectionsAfterRetries() { + podState.failedConnectionsAfterRetries++ + } + override var timeZone: TimeZone get() = TimeZone.getTimeZone(podState.timeZone) set(tz) { @@ -129,10 +146,12 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( val podOffset = timeZone.getOffset(now) logger.debug( LTag.PUMPCOMM, - "sameTimeZone currentTimezone=${currentTimezone.getDisplayName( + "sameTimeZone currentTimezone=${ + currentTimezone.getDisplayName( true, TimeZone.SHORT - )} " + + ) + } " + "currentOffset=$currentOffset " + "podOffset=$podOffset" ) @@ -627,13 +646,10 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( } override fun connectionSuccessRatio(): Float { - if (connectionAttempts == 0) { + if (connectionAttemptsWithRetries == 0) { return 0.0F - } else if (connectionAttempts <= successfulConnections) { - // Prevent bogus quality > 1 during initialisation - return 1.0F } - return successfulConnections.toFloat() / connectionAttempts.toFloat() + return 1 - (failedConnectionsAfterRetries.toFloat() / connectionAttemptsWithRetries) } override fun reset() { @@ -675,6 +691,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED var connectionAttempts = 0 var successfulConnections = 0 + var connectionAttemptsWithRetries = 0 + var failedConnectionsAfterRetries = 0 var messageSequenceNumber: Short = 0 var sequenceNumberOfLastProgrammingCommand: Short? = null var activationTime: Long? = null @@ -682,7 +700,6 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( var bluetoothAddress: String? = null var ltk: ByteArray? = null var eapAkaSequenceNumber: Long = 1 - var bolusPulsesRemaining: Short = 0 var timeZone: String = "" // TimeZone ID (e.g. "Europe/Amsterdam") var alarmSynced: Boolean = false diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 4703b82d0c..3f87aad83d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -256,15 +256,15 @@ class OmnipodDashOverviewFragment : DaggerFragment() { val connectionSuccessPercentage = podStateManager.connectionSuccessRatio() * 100 val successPercentageString = String.format("%.2f %%", connectionSuccessPercentage) val quality = - "${podStateManager.successfulConnections}/${podStateManager.connectionAttempts} :: $successPercentageString" + "${podStateManager.connectionAttemptsWithRetries - podStateManager.failedConnectionsAfterRetries}/${podStateManager.connectionAttemptsWithRetries} :: $successPercentageString" bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.text = quality val connectionStatsColor = when { - connectionSuccessPercentage > 90 -> - Color.WHITE - connectionSuccessPercentage > 60 -> + connectionSuccessPercentage < 70 && podStateManager.connectionAttemptsWithRetries > 50 -> + Color.RED + connectionSuccessPercentage < 90 && podStateManager.connectionAttemptsWithRetries > 50 -> Color.YELLOW else -> - Color.RED + Color.WHITE } bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.setTextColor(connectionStatsColor) bluetoothStatusBinding.omnipodDashDeliveryStatus.text = podStateManager.deliveryStatus?.let { From 6ab2a7bb7ee19f9ec55f676e938c1867bb30a72d Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 11 Nov 2021 17:41:19 +0100 Subject: [PATCH 3/5] display the performed operation --- .../info/nightscout/androidaps/queue/QueueThread.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt index df3ce83881..c285b9d338 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt @@ -111,14 +111,18 @@ class QueueThread internal constructor( // Pickup 1st command and set performing variable if (queue.size() > 0) { queue.pickup() - if (queue.performing() != null) { - aapsLogger.debug(LTag.PUMPQUEUE, "performing " + queue.performing()?.status()) + val cont = queue.performing()?.let { + aapsLogger.debug(LTag.PUMPQUEUE, "performing " + it.status()) rxBus.send(EventQueueChanged()) - queue.performing()?.execute() + rxBus.send(EventPumpStatusChanged(it.status())) + it.execute() queue.resetPerforming() rxBus.send(EventQueueChanged()) lastCommandTime = System.currentTimeMillis() SystemClock.sleep(100) + true + } ?: false + if (cont) { continue } } From 7826c4183432996ee67fca075015490c7e6ecdb7 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 11 Nov 2021 17:41:37 +0100 Subject: [PATCH 4/5] delay observing for EventPumpStatusChanged --- .../plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 3f87aad83d..0a77ac2d14 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -50,6 +50,7 @@ import org.apache.commons.lang3.StringUtils import java.time.Duration import java.time.ZonedDateTime import java.util.* +import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.collections.ArrayList @@ -213,6 +214,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() { disposables += rxBus .toObservable(EventPumpStatusChanged::class.java) .observeOn(aapsSchedulers.main) + .delay(30, TimeUnit.MILLISECONDS, aapsSchedulers.main) .subscribe( { updateBluetoothConnectionStatus(it) From 59c7955126659aa98ae67881fbb34422a6a02a7f Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 11 Nov 2021 19:04:28 +0100 Subject: [PATCH 5/5] connection quality: count connections after retries --- .../pump/omnipod/dash/OmnipodDashPumpPlugin.kt | 4 +++- .../driver/pod/state/OmnipodDashPodStateManager.kt | 4 ++-- .../pod/state/OmnipodDashPodStateManagerImpl.kt | 14 +++++++------- .../omnipod/dash/ui/OmnipodDashOverviewFragment.kt | 7 ++++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 3e8d4920bb..405e7da295 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -243,7 +243,6 @@ class OmnipodDashPumpPlugin @Inject constructor( val stop = CountDownLatch(1) stopConnecting = stop } - podStateManager.incrementConnectionAttemptsWithRetries() thread( start = true, name = "ConnectionThread", @@ -252,6 +251,9 @@ class OmnipodDashPumpPlugin @Inject constructor( stopConnecting?.let { val error = omnipodManager.connect(it).ignoreElements().blockingGet() aapsLogger.info(LTag.PUMPCOMM, "connect error=$error") + if (error == null) { + podStateManager.incrementSuccessfulConnectionAttemptsAfterRetries() + } } } finally { synchronized(this) { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 652819fa11..6927e63195 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -33,7 +33,7 @@ interface OmnipodDashPodStateManager { var bluetoothConnectionState: BluetoothConnectionState var connectionAttempts: Int var successfulConnections: Int - val connectionAttemptsWithRetries: Int + val successfulConnectionAttemptsAfterRetries: Int val failedConnectionsAfterRetries: Int var timeZone: TimeZone @@ -87,7 +87,7 @@ interface OmnipodDashPodStateManager { fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun reset() fun connectionSuccessRatio(): Float - fun incrementConnectionAttemptsWithRetries() + fun incrementSuccessfulConnectionAttemptsAfterRetries() fun incrementFailedConnectionsAfterRetries() fun createActiveCommand( diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 9ab82212c1..fe63757d17 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -114,13 +114,13 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( podState.successfulConnections = value } - override val connectionAttemptsWithRetries: Int + override val successfulConnectionAttemptsAfterRetries: Int @Synchronized - get() = podState.connectionAttemptsWithRetries + get() = podState.successfulConnectionAttemptsAfterRetries @Synchronized - override fun incrementConnectionAttemptsWithRetries() { - podState.connectionAttemptsWithRetries++ + override fun incrementSuccessfulConnectionAttemptsAfterRetries() { + podState.successfulConnectionAttemptsAfterRetries++ } override val failedConnectionsAfterRetries: Int @@ -646,10 +646,10 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( } override fun connectionSuccessRatio(): Float { - if (connectionAttemptsWithRetries == 0) { + if (failedConnectionsAfterRetries + successfulConnectionAttemptsAfterRetries == 0) { return 0.0F } - return 1 - (failedConnectionsAfterRetries.toFloat() / connectionAttemptsWithRetries) + return successfulConnectionAttemptsAfterRetries.toFloat() / (successfulConnectionAttemptsAfterRetries + failedConnectionsAfterRetries) } override fun reset() { @@ -691,7 +691,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED var connectionAttempts = 0 var successfulConnections = 0 - var connectionAttemptsWithRetries = 0 + var successfulConnectionAttemptsAfterRetries = 0 var failedConnectionsAfterRetries = 0 var messageSequenceNumber: Short = 0 var sequenceNumberOfLastProgrammingCommand: Short? = null diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 0a77ac2d14..698381365e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -256,14 +256,15 @@ class OmnipodDashOverviewFragment : DaggerFragment() { ?: PLACEHOLDER val connectionSuccessPercentage = podStateManager.connectionSuccessRatio() * 100 + val connectionAttempts = podStateManager.failedConnectionsAfterRetries + podStateManager.successfulConnectionAttemptsAfterRetries val successPercentageString = String.format("%.2f %%", connectionSuccessPercentage) val quality = - "${podStateManager.connectionAttemptsWithRetries - podStateManager.failedConnectionsAfterRetries}/${podStateManager.connectionAttemptsWithRetries} :: $successPercentageString" + "${podStateManager.successfulConnectionAttemptsAfterRetries}/$connectionAttempts :: $successPercentageString" bluetoothStatusBinding.omnipodDashBluetoothConnectionQuality.text = quality val connectionStatsColor = when { - connectionSuccessPercentage < 70 && podStateManager.connectionAttemptsWithRetries > 50 -> + connectionSuccessPercentage < 70 && podStateManager.successfulConnectionAttemptsAfterRetries > 50 -> Color.RED - connectionSuccessPercentage < 90 && podStateManager.connectionAttemptsWithRetries > 50 -> + connectionSuccessPercentage < 90 && podStateManager.successfulConnectionAttemptsAfterRetries > 50 -> Color.YELLOW else -> Color.WHITE