You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
package net.aiterp.git.ykonsole2.infrastructure.drivers
|
|
|
|
import net.aiterp.git.ykonsole2.domain.models.*
|
|
import net.aiterp.git.ykonsole2.domain.runtime.*
|
|
import net.aiterp.git.ykonsole2.infrastructure.drivers.abstracts.ReactiveDriver
|
|
|
|
class WorkoutWriter(
|
|
private val workoutRepo: WorkoutRepository,
|
|
private val stateRepo: WorkoutStateRepository,
|
|
) : ReactiveDriver() {
|
|
private var workout: Workout? = null
|
|
|
|
override suspend fun onEvent(event: Event, input: FlowBus<Command>) {
|
|
workout = workout ?: workoutRepo.findActive()
|
|
|
|
if (workout != null) {
|
|
val foundWorkout = workout!!
|
|
|
|
when (event) {
|
|
is ValuesReceived -> {
|
|
val newState = foundWorkout.makeState(event.values)
|
|
|
|
stateRepo.save(newState)
|
|
}
|
|
|
|
is Connected -> {
|
|
foundWorkout.status = WorkoutStatus.Connected
|
|
workoutRepo.save(foundWorkout)
|
|
}
|
|
|
|
Disconnected -> {
|
|
foundWorkout.status = WorkoutStatus.Disconnected
|
|
workoutRepo.save(foundWorkout)
|
|
|
|
workout = null
|
|
}
|
|
|
|
Started -> {
|
|
foundWorkout.status = WorkoutStatus.Started
|
|
workoutRepo.save(foundWorkout)
|
|
}
|
|
|
|
Stopped -> {
|
|
foundWorkout.status = WorkoutStatus.Stopped
|
|
workoutRepo.save(foundWorkout)
|
|
}
|
|
|
|
is MilestoneReached -> {
|
|
/* Do nothing */
|
|
}
|
|
|
|
is Skipped -> {
|
|
/* Do nothing */
|
|
}
|
|
|
|
is ErrorOccurred -> {
|
|
foundWorkout.message = "Error: ${event.message}"
|
|
workoutRepo.save(foundWorkout)
|
|
|
|
workout = null
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|