This commit is contained in:
Milos Kozak 2022-11-25 14:21:44 +01:00
commit 681fe52d80
2 changed files with 26 additions and 19 deletions

View file

@ -7,7 +7,7 @@ android {
namespace 'info.nightscout.comboctl' namespace 'info.nightscout.comboctl'
sourceSets { sourceSets {
main { main {
kotlin.srcDirs += ['src/commonMain', 'src/androidMain'] kotlin.srcDirs += ['src/commonMain/kotlin', 'src/androidMain/kotlin']
manifest.srcFile 'src/androidMain/AndroidManifest.xml' manifest.srcFile 'src/androidMain/AndroidManifest.xml'
} }
} }

View file

@ -20,7 +20,14 @@ import kotlin.reflect.KClassifier
private val logger = Logger.get("RTNavigation") private val logger = Logger.get("RTNavigation")
private const val WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS = 110L // There are _two_ waiting periods during RT button presses. The minimum one is there
// in case a new RT arrives very quickly; it is then necessary to wait a bit before
// sending the next button press packet to the Combo to avoid overflow. The maximum
// one is there if there is no screen update until we send the next button press
// packet to the Combo. Without the maximum period, we'd then end up in a deadlock.
// In other words, the maximum waiting period functions as a timeout.
private const val MINIMUM_WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS = 110L
private const val MAXIMUM_WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS = 600L
private const val MAX_NUM_SAME_QUANTITY_OBSERVATIONS = 10 private const val MAX_NUM_SAME_QUANTITY_OBSERVATIONS = 10
/** /**
@ -390,7 +397,7 @@ suspend fun longPressRTButtonUntil(
// both cases), keep pressing the button. // both cases), keep pressing the button.
val parsedDisplayFrame = try { val parsedDisplayFrame = try {
withTimeout( withTimeout(
timeMillis = WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS timeMillis = MAXIMUM_WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS
) { ) {
rtNavigationContext.getParsedDisplayFrame(filterDuplicates = true) rtNavigationContext.getParsedDisplayFrame(filterDuplicates = true)
} }
@ -414,8 +421,8 @@ suspend fun longPressRTButtonUntil(
// to be a phenomenon that is separate to the packet overflow // to be a phenomenon that is separate to the packet overflow
// that is documented in TransportLayer.IO.sendInternal().) // that is documented in TransportLayer.IO.sendInternal().)
val elapsedTime = getElapsedTimeInMs() - timestampBeforeDisplayFrameRetrieval val elapsedTime = getElapsedTimeInMs() - timestampBeforeDisplayFrameRetrieval
if (elapsedTime < WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS) { if (elapsedTime < MINIMUM_WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS) {
val waitingPeriodInMs = WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS - elapsedTime val waitingPeriodInMs = MINIMUM_WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS - elapsedTime
logger(LogLevel.VERBOSE) { "Waiting $waitingPeriodInMs milliseconds before continuing button long-press" } logger(LogLevel.VERBOSE) { "Waiting $waitingPeriodInMs milliseconds before continuing button long-press" }
delay(timeMillis = waitingPeriodInMs) delay(timeMillis = waitingPeriodInMs)
} }
@ -723,15 +730,15 @@ suspend fun adjustQuantityOnScreen(
rtNavigationContext, rtNavigationContext,
if (needToIncrement) incrementButton else decrementButton if (needToIncrement) incrementButton else decrementButton
) { parsedScreen -> ) { parsedScreen ->
val currentQuantity = getQuantity(parsedScreen) val currentQuantityOnScreen = getQuantity(parsedScreen)
logger(LogLevel.VERBOSE) { "Current quantity in first phase: $currentQuantity; need to increment: $needToIncrement" } logger(LogLevel.VERBOSE) { "Current quantity in first phase: $currentQuantityOnScreen; need to increment: $needToIncrement" }
if (currentQuantity == null) { if (currentQuantityOnScreen == null) {
LongPressRTButtonsCommand.ContinuePressingButton LongPressRTButtonsCommand.ContinuePressingButton
} else { } else {
if (currentQuantity != targetQuantity) { if (currentQuantityOnScreen != targetQuantity) {
if (checkIfQuantityUnexpectedlyNotChanging(currentQuantity)) { if (checkIfQuantityUnexpectedlyNotChanging(currentQuantityOnScreen)) {
logger(LogLevel.ERROR) { "Quantity unexpectedly not changing" } logger(LogLevel.ERROR) { "Quantity unexpectedly not changing" }
throw QuantityNotChangingException(targetQuantity = targetQuantity, hitLimitAt = currentQuantity) throw QuantityNotChangingException(targetQuantity = targetQuantity, hitLimitAt = currentQuantityOnScreen)
} }
} }
@ -749,12 +756,12 @@ suspend fun adjustQuantityOnScreen(
// will be set to false, indicating that the long RT // will be set to false, indicating that the long RT
// button press needs to stop. // button press needs to stop.
val keepPressing = val keepPressing =
if (currentQuantity == targetQuantity) if (currentQuantityOnScreen == targetQuantity)
false false
else if (needToIncrement) else if (needToIncrement)
checkIfNeedsToIncrement(currentQuantity) checkIfNeedsToIncrement(currentQuantityOnScreen)
else else
!checkIfNeedsToIncrement(currentQuantity) !checkIfNeedsToIncrement(currentQuantityOnScreen)
if (keepPressing) if (keepPressing)
LongPressRTButtonsCommand.ContinuePressingButton LongPressRTButtonsCommand.ContinuePressingButton
@ -789,20 +796,20 @@ suspend fun adjustQuantityOnScreen(
// is pretty much what we are waiting for. // is pretty much what we are waiting for.
val parsedDisplayFrame = rtNavigationContext.getParsedDisplayFrame(filterDuplicates = false) ?: continue val parsedDisplayFrame = rtNavigationContext.getParsedDisplayFrame(filterDuplicates = false) ?: continue
val parsedScreen = parsedDisplayFrame.parsedScreen val parsedScreen = parsedDisplayFrame.parsedScreen
val currentQuantity = getQuantity(parsedScreen) val currentQuantityOnScreen = getQuantity(parsedScreen)
logger(LogLevel.DEBUG) { logger(LogLevel.DEBUG) {
"Observed quantity after long-pressing RT button: " + "Observed quantity after long-pressing RT button: " +
"last / current quantity: $lastQuantity / $currentQuantity" "last / current quantity: $lastQuantity / $currentQuantityOnScreen"
} }
if (currentQuantity != null) { if (currentQuantityOnScreen != null) {
if (currentQuantity == lastQuantity) { if (currentQuantityOnScreen == lastQuantity) {
sameQuantityObservedCount++ sameQuantityObservedCount++
if (sameQuantityObservedCount >= 3) if (sameQuantityObservedCount >= 3)
break break
} else { } else {
lastQuantity = currentQuantity lastQuantity = currentQuantityOnScreen
sameQuantityObservedCount = 0 sameQuantityObservedCount = 0
} }
} }