diff --git a/demo/car/build.gradle.kts b/demo/car/build.gradle.kts index 1de811f..9242dc7 100644 --- a/demo/car/build.gradle.kts +++ b/demo/car/build.gradle.kts @@ -16,6 +16,9 @@ val rsocketVersion: String by rootProject.extra dependencies { implementation(projects.controlsCore) implementation(projects.magix.magixApi) + implementation(projects.magix.magixServer) + implementation(projects.magix.magixRsocket) + implementation(projects.controlsMagixClient) implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1") implementation("no.tornado:tornadofx:1.7.20") diff --git a/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/IVirtualCar.kt b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/IVirtualCar.kt new file mode 100644 index 0000000..b7a2a97 --- /dev/null +++ b/demo/car/src/main/kotlin/ru/mipt/npm/controls/demo/car/IVirtualCar.kt @@ -0,0 +1,27 @@ +package ru.mipt.npm.controls.demo.car + +import ru.mipt.npm.controls.api.Device +import ru.mipt.npm.controls.spec.DeviceSpec + +interface IVirtualCar : Device { + var speedState: Vector2D + var locationState: Vector2D + var accelerationState: Vector2D + + companion object : DeviceSpec() { + /** + * Read-only speed + */ + val speed by property(Vector2D, IVirtualCar::speedState) + + /** + * Read-only location + */ + val location by property(Vector2D, IVirtualCar::locationState) + + /** + * writable acceleration + */ + val acceleration by mutableProperty(Vector2D, IVirtualCar::accelerationState) + } +} \ No newline at end of file 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 5df6a1f..4c04190 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 @@ -6,9 +6,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.datetime.Clock import kotlinx.datetime.Instant -import ru.mipt.npm.controls.api.Device import ru.mipt.npm.controls.spec.DeviceBySpec -import ru.mipt.npm.controls.spec.DeviceSpec import ru.mipt.npm.controls.spec.doRecurring import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Factory @@ -40,13 +38,7 @@ data class Vector2D(var x: Double = 0.0, var y: Double = 0.0) : MetaRepr { } } -interface IVirtualCar : Device { - var speedState: Vector2D - var locationState: Vector2D - var accelerationState: Vector2D -} - -class VirtualCar(context: Context, meta: Meta) : DeviceBySpec(VirtualCar, context, meta), IVirtualCar { +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 @@ -85,14 +77,14 @@ class VirtualCar(context: Context, meta: Meta) : DeviceBySpec(Virtua //TODO apply friction. One can introduce rotation of the cabin and different friction coefficients along the axis launch { //update logical states - location.read() - speed.read() - acceleration.read() + IVirtualCar.location.read() + IVirtualCar.speed.read() + IVirtualCar.acceleration.read() } } - public fun applyForce(force: Vector2D, duration: Duration) { + fun applyForce(force: Vector2D, duration: Duration) { launch { update() accelerationState = force / mass @@ -116,22 +108,7 @@ class VirtualCar(context: Context, meta: Meta) : DeviceBySpec(Virtua } } - companion object : DeviceSpec(), Factory { + companion object : Factory { override fun invoke(meta: Meta, context: Context): VirtualCar = VirtualCar(context, meta) - - /** - * Read-only speed - */ - val speed by property(Vector2D, IVirtualCar::speedState) - - /** - * Read-only location - */ - val location by property(Vector2D, IVirtualCar::locationState) - - /** - * writable acceleration - */ - val acceleration by mutableProperty(Vector2D, IVirtualCar::accelerationState) } } 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 1d63d49..5b0f3ff 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 @@ -1,20 +1,27 @@ package ru.mipt.npm.controls.demo.car +import io.ktor.server.engine.* import javafx.beans.property.DoubleProperty import javafx.scene.Parent import javafx.scene.control.TextField import javafx.scene.layout.Priority import javafx.stage.Stage import kotlinx.coroutines.launch +import ru.mipt.npm.controls.api.DeviceMessage +import ru.mipt.npm.controls.client.connectToMagix import ru.mipt.npm.controls.controllers.DeviceManager import ru.mipt.npm.controls.controllers.install -import ru.mipt.npm.controls.demo.car.VirtualCar.Companion.acceleration +import ru.mipt.npm.controls.demo.car.IVirtualCar.Companion.acceleration +import ru.mipt.npm.magix.api.MagixEndpoint +import ru.mipt.npm.magix.rsocket.rSocketWithTcp +import ru.mipt.npm.magix.server.startMagixServer import space.kscience.dataforge.context.* import tornadofx.* class VirtualCarController : Controller(), ContextAware { var device: VirtualCar? = null + var magixServer: ApplicationEngine? = null override val context = Context("demoDevice") { plugin(DeviceManager) @@ -25,11 +32,18 @@ class VirtualCarController : Controller(), ContextAware { fun init() { context.launch { device = deviceManager.install("virtual-car", VirtualCar) + //starting magix event loop + magixServer = startMagixServer(enableRawRSocket = true, enableZmq = true) + //Launch device client and connect it to the server + val deviceEndpoint = MagixEndpoint.rSocketWithTcp("localhost", DeviceMessage.serializer()) + deviceManager.connectToMagix(deviceEndpoint) } } fun shutdown() { logger.info { "Shutting down..." } + magixServer?.stop(1000, 5000) + logger.info { "Magix server stopped" } device?.close() logger.info { "Device server stopped" } context.close()