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.
77 lines
2.0 KiB
77 lines
2.0 KiB
package net.aiterp.git.ykonsole2.infrastructure.testing
|
|
|
|
import net.aiterp.git.ykonsole2.domain.runtime.*
|
|
import net.aiterp.git.ykonsole2.infrastructure.drivers.abstracts.ActiveDriver
|
|
import kotlinx.coroutines.delay
|
|
import net.aiterp.git.ykonsole2.application.logging.log
|
|
import kotlin.random.Random
|
|
import kotlin.random.nextInt
|
|
import kotlin.time.Duration
|
|
import kotlin.time.Duration.Companion.milliseconds
|
|
|
|
class TestDriver(private val secondLength: Duration) : ActiveDriver() {
|
|
private var time = 0
|
|
private var calories = 0.0
|
|
private var distance = 0.0
|
|
private var level = 0
|
|
private var running = false
|
|
private var connected = false
|
|
|
|
private val currentState
|
|
get() = listOf(
|
|
Level(level),
|
|
Time(time),
|
|
Calories(calories.toInt()),
|
|
Distance(distance.toInt()),
|
|
)
|
|
|
|
override suspend fun onCommand(command: Command, output: FlowBus<Event>) {
|
|
log.info("Received command: $command")
|
|
val ev = when (command) {
|
|
is ConnectCommand -> {
|
|
if (command.device.connectionString.startsWith("test:")) {
|
|
delay(Random.nextInt(100..300).milliseconds)
|
|
connected = true
|
|
Connected
|
|
} else {
|
|
log.info("Ignoring device: ${command.device.name}")
|
|
null
|
|
}
|
|
}
|
|
|
|
DisconnectCommand -> Disconnected.also {
|
|
connected = false
|
|
time = 0
|
|
calories = 0.0
|
|
distance = 0.0
|
|
}
|
|
|
|
is SetValueCommand -> when (command.value) {
|
|
is Level -> {
|
|
level = command.value.raw
|
|
ValuesReceived(currentState)
|
|
}
|
|
else -> null
|
|
}
|
|
|
|
SkipCommand -> null
|
|
|
|
StartCommand -> if (connected) Started.also { running = true } else null
|
|
StopCommand -> if (connected) Stopped.also { running = false } else null
|
|
}
|
|
|
|
ev?.let { output.emit(it) }
|
|
}
|
|
|
|
override suspend fun onTick(output: FlowBus<Event>) {
|
|
if (running && connected) {
|
|
time += 1
|
|
calories += Random.nextDouble(0.2, 0.8)
|
|
distance += 1.8
|
|
|
|
output.emit(ValuesReceived(currentState))
|
|
}
|
|
|
|
delay(secondLength)
|
|
}
|
|
}
|