Refactor MagixVirtualCar.kt
This commit is contained in:
parent
dcd496660c
commit
039e0d083b
@ -4,21 +4,18 @@ import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.mipt.npm.controls.api.DeviceMessage
|
||||
import ru.mipt.npm.controls.api.PropertyChangedMessage
|
||||
import ru.mipt.npm.controls.spec.DeviceBySpec
|
||||
import ru.mipt.npm.controls.spec.doRecurring
|
||||
import ru.mipt.npm.magix.api.MagixEndpoint
|
||||
import ru.mipt.npm.magix.rsocket.rSocketWithWebSockets
|
||||
import space.kscience.dataforge.context.Context
|
||||
import space.kscience.dataforge.context.Factory
|
||||
import space.kscience.dataforge.meta.Meta
|
||||
import space.kscience.dataforge.meta.get
|
||||
import space.kscience.dataforge.meta.string
|
||||
import space.kscience.dataforge.names.Name
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
class MagixVirtualCar(private val magixEndpoint: MagixEndpoint<DeviceMessage>, context: Context, meta: Meta)
|
||||
: DeviceBySpec<MagixVirtualCar>(IVirtualCar, context, meta), IVirtualCar {
|
||||
override var speedState: Vector2D = Vector2D()
|
||||
override var locationState: Vector2D = Vector2D()
|
||||
override var accelerationState: Vector2D = Vector2D()
|
||||
class MagixVirtualCar(context: Context, meta: Meta)
|
||||
: VirtualCar(context, meta) {
|
||||
|
||||
private suspend fun MagixEndpoint<DeviceMessage>.startMagixVirtualCarUpdate() {
|
||||
launch {
|
||||
@ -26,9 +23,7 @@ class MagixVirtualCar(private val magixEndpoint: MagixEndpoint<DeviceMessage>, c
|
||||
(magix.payload as? PropertyChangedMessage)?.let { message ->
|
||||
if (message.sourceDevice == Name.parse("virtual-car")) {
|
||||
when (message.property) {
|
||||
"speed" -> speedState = Vector2D.metaToObject(message.value)
|
||||
"location" -> locationState = Vector2D.metaToObject(message.value)
|
||||
"acceleration" -> accelerationState = Vector2D.metaToObject(message.value)
|
||||
"acceleration" -> IVirtualCar.acceleration.write(Vector2D.metaToObject(message.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -38,21 +33,19 @@ class MagixVirtualCar(private val magixEndpoint: MagixEndpoint<DeviceMessage>, c
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
override suspend fun open() {
|
||||
super<DeviceBySpec>.open()
|
||||
super.open()
|
||||
|
||||
val magixEndpoint = MagixEndpoint.rSocketWithWebSockets(
|
||||
meta["magixServerHost"].string ?: "localhost",
|
||||
DeviceMessage.serializer()
|
||||
)
|
||||
|
||||
launch {
|
||||
magixEndpoint.startMagixVirtualCarUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
//starting regular updates
|
||||
doRecurring(Duration.milliseconds(100)) {
|
||||
IVirtualCar.speed.read()
|
||||
IVirtualCar.location.read()
|
||||
IVirtualCar.acceleration.read()
|
||||
}
|
||||
companion object : Factory<MagixVirtualCar> {
|
||||
override fun invoke(meta: Meta, context: Context): MagixVirtualCar = MagixVirtualCar(context, meta)
|
||||
}
|
||||
}
|
||||
|
||||
class MagixVirtualCarFactory(private val magixEndpoint: MagixEndpoint<DeviceMessage>) : Factory<MagixVirtualCar> {
|
||||
override fun invoke(meta: Meta, context: Context): MagixVirtualCar = MagixVirtualCar(magixEndpoint, context, meta)
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ data class Vector2D(var x: Double = 0.0, var y: Double = 0.0) : MetaRepr {
|
||||
}
|
||||
}
|
||||
|
||||
class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(IVirtualCar, context, meta), IVirtualCar {
|
||||
open class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(IVirtualCar, context, meta), IVirtualCar {
|
||||
private val timeScale = 1e-3
|
||||
|
||||
private val mass by meta.double(1000.0) // mass in kilograms
|
||||
|
@ -36,8 +36,7 @@ class VirtualCarController : Controller(), ContextAware {
|
||||
virtualCar = deviceManager.install("virtual-car", VirtualCar)
|
||||
//starting magix event loop
|
||||
magixServer = startMagixServer(enableRawRSocket = true, enableZmq = true)
|
||||
val magixEndpoint = MagixEndpoint.rSocketWithWebSockets("localhost", DeviceMessage.serializer())
|
||||
magixVirtualCar = deviceManager.install("magix-virtual-car", MagixVirtualCarFactory(magixEndpoint))
|
||||
magixVirtualCar = deviceManager.install("magix-virtual-car", MagixVirtualCar)
|
||||
//Launch device client and connect it to the server
|
||||
val deviceEndpoint = MagixEndpoint.rSocketWithTcp("localhost", DeviceMessage.serializer())
|
||||
deviceManager.connectToMagix(deviceEndpoint)
|
||||
|
Loading…
Reference in New Issue
Block a user