fix connnecting quality counter

This commit is contained in:
Andrei Vereha 2021-11-11 00:18:56 +01:00
parent d5ae4cabf2
commit 3a9213992b
4 changed files with 36 additions and 15 deletions

View file

@ -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)
}

View file

@ -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,

View file

@ -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

View file

@ -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 {