Move functionality to the Finite State Machine

On hindsight, the approach of letting the FSM handle all state transitions explicitly is much clearer
This commit is contained in:
Jaap van Ekris 2023-01-16 12:18:46 +01:00 committed by GitHub
parent 36953315bd
commit 6eed136caf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 15 deletions

View File

@ -93,11 +93,18 @@ function createRowingStatistics (config) {
sessionStatus = 'Paused'
pauseTraining()
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Recovery' && rower.strokeState() === 'Drive' && intervalTargetReached()):
case (sessionStatus === 'Rowing' && lastStrokeState === 'Recovery' && rower.strokeState() === 'Drive' && isIntervalTargetReached() && isNextIntervalAvailable()):
updateContinousMetrics()
updateCycleMetrics()
handleRecoveryEnd()
handleIntervalEnd()
activateNextIntervalParameters()
emitMetrics('intervalTargetReached')
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Recovery' && rower.strokeState() === 'Drive' && isIntervalTargetReached()):
updateContinousMetrics()
updateCycleMetrics()
handleRecoveryEnd()
stopTraining()
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Recovery' && rower.strokeState() === 'Drive'):
updateContinousMetrics()
@ -105,11 +112,18 @@ function createRowingStatistics (config) {
handleRecoveryEnd()
emitMetrics('recoveryFinished')
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Drive' && rower.strokeState() === 'Recovery' && intervalTargetReached()):
case (sessionStatus === 'Rowing' && lastStrokeState === 'Drive' && rower.strokeState() === 'Recovery' && isIntervalTargetReached() && isNextIntervalAvailable()):
updateContinousMetrics()
updateCycleMetrics()
handleDriveEnd()
handleIntervalEnd()
activateNextIntervalParameters()
emitMetrics('intervalTargetReached')
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Drive' && rower.strokeState() === 'Recovery' && isIntervalTargetReached()):
updateContinousMetrics()
updateCycleMetrics()
handleDriveEnd()
stopTraining()
break
case (sessionStatus === 'Rowing' && lastStrokeState === 'Drive' && rower.strokeState() === 'Recovery'):
updateContinousMetrics()
@ -117,9 +131,14 @@ function createRowingStatistics (config) {
handleDriveEnd()
emitMetrics('driveFinished')
break
case (sessionStatus === 'Rowing' && intervalTargetReached()):
case (sessionStatus === 'Rowing' && isIntervalTargetReached() && isNextIntervalAvailable()):
updateContinousMetrics()
handleIntervalEnd()
activateNextIntervalParameters()
emitMetrics('intervalTargetReached')
break
case (sessionStatus === 'Rowing' && isIntervalTargetReached()):
updateContinousMetrics()
stopTraining()
break
case (sessionStatus === 'Rowing'):
updateContinousMetrics()
@ -266,7 +285,7 @@ function createRowingStatistics (config) {
}
}
function intervalTargetReached () {
function isIntervalTargetReached () {
// This tests wether the end of the current interval is reached
if ((intervalTargetDistance > 0 && rower.totalLinearDistanceSinceStart() >= intervalTargetDistance) || (intervalTargetTime > 0 && rower.totalMovingTimeSinceStart() >= intervalTargetTime)) {
return true
@ -275,15 +294,12 @@ function createRowingStatistics (config) {
}
}
function handleIntervalEnd () {
// initiated when the state machine has concluded the interval has ended
if (intervalSettings.length > 0 && intervalSettings.length > (currentIntervalNumber + 1)) {
// There is a next interval available
emitMetrics('intervalTargetReached')
activateNextIntervalParameters()
function isNextIntervalAvailable () {
// This function tests whether there is a next interval available
if (currentIntervalNumber > -1 && intervalSettings.length > 0 && intervalSettings.length > (currentIntervalNumber + 1)) {
return true
} else {
// There is no additional interval available
stopTraining()
return false
}
}