Add magix for debugging and moved DeviceSpec to IVirtualCar

This commit is contained in:
Atos1337 2021-11-02 22:21:13 +03:00
parent 4dc33c012d
commit 289ff6ffd0
4 changed files with 51 additions and 30 deletions

View File

@ -16,6 +16,9 @@ val rsocketVersion: String by rootProject.extra
dependencies { dependencies {
implementation(projects.controlsCore) implementation(projects.controlsCore)
implementation(projects.magix.magixApi) 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("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
implementation("no.tornado:tornadofx:1.7.20") implementation("no.tornado:tornadofx:1.7.20")

View File

@ -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<IVirtualCar>() {
/**
* 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)
}
}

View File

@ -6,9 +6,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.datetime.Clock import kotlinx.datetime.Clock
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import ru.mipt.npm.controls.api.Device
import ru.mipt.npm.controls.spec.DeviceBySpec import ru.mipt.npm.controls.spec.DeviceBySpec
import ru.mipt.npm.controls.spec.DeviceSpec
import ru.mipt.npm.controls.spec.doRecurring import ru.mipt.npm.controls.spec.doRecurring
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.Factory 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 { class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(IVirtualCar, context, meta), IVirtualCar {
var speedState: Vector2D
var locationState: Vector2D
var accelerationState: Vector2D
}
class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(VirtualCar, context, meta), IVirtualCar {
private val timeScale = 1e-3 private val timeScale = 1e-3
private val mass by meta.double(1000.0) // mass in kilograms private val mass by meta.double(1000.0) // mass in kilograms
@ -85,14 +77,14 @@ class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(Virtua
//TODO apply friction. One can introduce rotation of the cabin and different friction coefficients along the axis //TODO apply friction. One can introduce rotation of the cabin and different friction coefficients along the axis
launch { launch {
//update logical states //update logical states
location.read() IVirtualCar.location.read()
speed.read() IVirtualCar.speed.read()
acceleration.read() IVirtualCar.acceleration.read()
} }
} }
public fun applyForce(force: Vector2D, duration: Duration) { fun applyForce(force: Vector2D, duration: Duration) {
launch { launch {
update() update()
accelerationState = force / mass accelerationState = force / mass
@ -116,22 +108,7 @@ class VirtualCar(context: Context, meta: Meta) : DeviceBySpec<VirtualCar>(Virtua
} }
} }
companion object : DeviceSpec<IVirtualCar>(), Factory<VirtualCar> { companion object : Factory<VirtualCar> {
override fun invoke(meta: Meta, context: Context): VirtualCar = VirtualCar(context, meta) 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)
} }
} }

View File

@ -1,20 +1,27 @@
package ru.mipt.npm.controls.demo.car package ru.mipt.npm.controls.demo.car
import io.ktor.server.engine.*
import javafx.beans.property.DoubleProperty import javafx.beans.property.DoubleProperty
import javafx.scene.Parent import javafx.scene.Parent
import javafx.scene.control.TextField import javafx.scene.control.TextField
import javafx.scene.layout.Priority import javafx.scene.layout.Priority
import javafx.stage.Stage import javafx.stage.Stage
import kotlinx.coroutines.launch 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.DeviceManager
import ru.mipt.npm.controls.controllers.install 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 space.kscience.dataforge.context.*
import tornadofx.* import tornadofx.*
class VirtualCarController : Controller(), ContextAware { class VirtualCarController : Controller(), ContextAware {
var device: VirtualCar? = null var device: VirtualCar? = null
var magixServer: ApplicationEngine? = null
override val context = Context("demoDevice") { override val context = Context("demoDevice") {
plugin(DeviceManager) plugin(DeviceManager)
@ -25,11 +32,18 @@ class VirtualCarController : Controller(), ContextAware {
fun init() { fun init() {
context.launch { context.launch {
device = deviceManager.install("virtual-car", VirtualCar) 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() { fun shutdown() {
logger.info { "Shutting down..." } logger.info { "Shutting down..." }
magixServer?.stop(1000, 5000)
logger.info { "Magix server stopped" }
device?.close() device?.close()
logger.info { "Device server stopped" } logger.info { "Device server stopped" }
context.close() context.close()