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