diff --git a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/MagixVirtualCar.kt b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/MagixVirtualCar.kt index e56ddf5..3d0c97c 100644 --- a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/MagixVirtualCar.kt +++ b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/MagixVirtualCar.kt @@ -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, context: Context, meta: Meta) - : DeviceBySpec(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.startMagixVirtualCarUpdate() { launch { @@ -26,9 +23,7 @@ class MagixVirtualCar(private val magixEndpoint: MagixEndpoint, 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, c @OptIn(ExperimentalTime::class) override suspend fun open() { - super.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 { + override fun invoke(meta: Meta, context: Context): MagixVirtualCar = MagixVirtualCar(context, meta) } } - -class MagixVirtualCarFactory(private val magixEndpoint: MagixEndpoint) : Factory { - override fun invoke(meta: Meta, context: Context): MagixVirtualCar = MagixVirtualCar(magixEndpoint, context, meta) -} diff --git a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCar.kt b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCar.kt index 4c04190..2cfea25 100644 --- a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCar.kt +++ b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCar.kt @@ -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(IVirtualCar, context, meta), IVirtualCar { +open class VirtualCar(context: Context, meta: Meta) : DeviceBySpec(IVirtualCar, context, meta), IVirtualCar { private val timeScale = 1e-3 private val mass by meta.double(1000.0) // mass in kilograms diff --git a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCarController.kt b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCarController.kt index 6b16b91..974c2ac 100644 --- a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCarController.kt +++ b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/VirtualCarController.kt @@ -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)