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