comboctl-main: Check if RT quantity adjustment hits an unexpected limit

This is important for example if the Combo is not properly configured
and the user tries to set a TBR percentage beyond the configured limit.
The RT UI will then clamp the percentage to that limit, and the quantity
won't increase anymore. Without this new check, this causes the code
to be stuck in an endless loop.

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2022-11-09 23:24:16 +01:00
parent c3c894cccb
commit ced93e4a53

View file

@ -21,6 +21,7 @@ import kotlin.reflect.KClassifier
private val logger = Logger.get("RTNavigation")
private const val WAIT_PERIOD_DURING_LONG_RT_BUTTON_PRESS_IN_MS = 110L
private const val MAX_NUM_SAME_QUANTITY_OBSERVATIONS = 10
/**
* RT navigation buttons.
@ -243,6 +244,21 @@ class CouldNotRecognizeAnyRTScreenException : RTNavigationException("Could not r
*/
class NoUsableRTScreenException : RTNavigationException("No usable RT screen available")
/**
* Exception thrown when [adjustQuantityOnScreen] attempts to adjust the shown quantity but hits an unexpected limit.
*
* For example, if the quantity shall be set to 500, but after incrementing it, it
* suddenly stops incrementing at 200, this exception is thrown to alert the user
* about this unexpected behavior.
*
* @param targetQuantity Quantity that was supposed to be reached.
* @param hitLimitAt The quantity at which point adjustments stopped changing the quantity.
*/
class QuantityNotChangingException(
val targetQuantity: Int,
val hitLimitAt: Int
) : RTNavigationException("Attempted to adjust quantity to target value $targetQuantity, but hit limit at $hitLimitAt")
/**
* Remote terminal (RT) navigation context.
*
@ -638,6 +654,25 @@ suspend fun adjustQuantityOnScreen(
"cyclicQuantityRange = $cyclicQuantityRange"
}
var previouslySeenQuantity: Int? = null
var seenSameQuantityCount = 0
fun checkIfQuantityUnexpectedlyNotChanging(currentQuantity: Int): Boolean {
// If the quantity stops changing, and is not the target quantity,
// something is wrong. Keep observing until MAX_NUM_SAME_QUANTITY_OBSERVATIONS
// observations are made where the quantity remained unchanged, and then
// report the situation as bogus (= return false).
if ((previouslySeenQuantity == null) || (previouslySeenQuantity != currentQuantity)) {
previouslySeenQuantity = currentQuantity
seenSameQuantityCount = 0
return false
}
seenSameQuantityCount++
return (seenSameQuantityCount >= MAX_NUM_SAME_QUANTITY_OBSERVATIONS)
}
val initialQuantity: Int
rtNavigationContext.resetDuplicate()
@ -682,6 +717,13 @@ suspend fun adjustQuantityOnScreen(
if (currentQuantity == null) {
LongPressRTButtonsCommand.ContinuePressingButton
} else {
if (currentQuantity != targetQuantity) {
if (checkIfQuantityUnexpectedlyNotChanging(currentQuantity)) {
logger(LogLevel.ERROR) { "Quantity unexpectedly not changing" }
throw QuantityNotChangingException(targetQuantity = targetQuantity, hitLimitAt = currentQuantity)
}
}
// If we are incrementing, and did not yet reach the
// quantity, then we expect checkIfNeedsToIncrement()
// to indicate that further incrementing is required.