Refactor onOpen in device spec

This commit is contained in:
Alexander Nozik 2023-02-25 10:53:53 +03:00
parent a0fd8913eb
commit 68805d6f42
9 changed files with 45 additions and 21 deletions

View File

@ -6,7 +6,7 @@ plugins {
id("space.kscience.gradle.project")
}
val dataforgeVersion: String by extra("0.6.0-dev-15")
val dataforgeVersion: String by extra("0.6.1-dev-4")
val ktorVersion: String by extra(space.kscience.gradle.KScienceVersions.ktorVersion)
val rsocketVersion by extra("0.15.4")
val xodusVersion by extra("2.0.1")
@ -14,6 +14,10 @@ val xodusVersion by extra("2.0.1")
allprojects {
group = "space.kscience"
version = "0.1.1-SNAPSHOT"
repositories{
mavenCentral()
mavenLocal()
}
}
ksciencePublish {

View File

@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import space.kscience.dataforge.io.SimpleEnvelope
import space.kscience.dataforge.io.Envelope
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.toJson
import space.kscience.dataforge.meta.toMeta
@ -221,4 +221,4 @@ public data class DeviceErrorMessage(
public fun DeviceMessage.toMeta(): Meta = Json.encodeToJsonElement(this).toMeta()
public fun DeviceMessage.toEnvelope(): SimpleEnvelope = SimpleEnvelope(toMeta(), null)
public fun DeviceMessage.toEnvelope(): Envelope = Envelope(toMeta(), null)

View File

@ -20,8 +20,8 @@ public abstract class DeviceBase<D : DeviceBase<D>>(
override val meta: Meta = Meta.EMPTY
) : Device {
public abstract val properties: Map<String, DevicePropertySpec<D, *>> //get() = spec.properties
public abstract val actions: Map<String, DeviceActionSpec<D, *, *>> //get() = spec.actions
public abstract val properties: Map<String, DevicePropertySpec<D, *>>
public abstract val actions: Map<String, DeviceActionSpec<D, *, *>>
override val propertyDescriptors: Collection<PropertyDescriptor>
get() = properties.values.map { it.descriptor }
@ -136,5 +136,15 @@ public open class DeviceBySpec<D : DeviceBySpec<D>>(
) : DeviceBase<D>(context, meta) {
override val properties: Map<String, DevicePropertySpec<D, *>> get() = spec.properties
override val actions: Map<String, DeviceActionSpec<D, *, *>> get() = spec.actions
override suspend fun open(): Unit = with(spec){
super.open()
self.onOpen()
}
override fun close(): Unit = with(spec){
self.onClose()
super.close()
}
}

View File

@ -23,6 +23,14 @@ public abstract class DeviceSpec<D : Device> {
private val _actions = HashMap<String, DeviceActionSpec<D, *, *>>()
public val actions: Map<String, DeviceActionSpec<D, *, *>> get() = _actions
public open suspend fun D.onOpen(){
}
public open fun D.onClose(){
}
public fun <T : Any, P : DevicePropertySpec<D, T>> registerProperty(deviceProperty: P): P {
_properties[deviceProperty.name] = deviceProperty
return deviceProperty

View File

@ -22,7 +22,7 @@ public fun <D : DeviceBase<D>, R> D.readRecurring(interval: Duration, reader: su
}
/**
* Do a recurring task on a device. The task could
* Do a recurring (with a fixed delay) task on a device.
*/
public fun <D : DeviceBase<D>> D.doRecurring(interval: Duration, task: suspend D.() -> Unit): Job = launch {
while (isActive) {

View File

@ -43,7 +43,7 @@ class DemoController : Controller(), ContextAware {
plugin(DeviceManager)
}
private val deviceManager = context.fetch(DeviceManager)
private val deviceManager = context.request(DeviceManager)
fun init() {
context.launch {

View File

@ -11,7 +11,6 @@ import space.kscience.dataforge.meta.descriptors.value
import space.kscience.dataforge.meta.transformations.MetaConverter
import java.time.Instant
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.ExperimentalTime
class DemoDevice(context: Context, meta: Meta) : DeviceBySpec<DemoDevice>(DemoDevice, context, meta) {
@ -19,19 +18,6 @@ class DemoDevice(context: Context, meta: Meta) : DeviceBySpec<DemoDevice>(DemoDe
private var sinScaleState = 1.0
private var cosScaleState = 1.0
@OptIn(ExperimentalTime::class)
override suspend fun open() {
super.open()
launch {
sinScale.read()
cosScale.read()
timeScale.read()
}
doRecurring(50.milliseconds) {
coordinates.read()
}
}
companion object : DeviceSpec<DemoDevice>(), Factory<DemoDevice> {
@ -73,6 +59,20 @@ class DemoDevice(context: Context, meta: Meta) : DeviceBySpec<DemoDevice>(DemoDe
}
}
override suspend fun DemoDevice.onOpen() {
launch {
sinScale.read()
cosScale.read()
timeScale.read()
}
doRecurring(50.milliseconds) {
sin.read()
cos.read()
coordinates.read()
}
}
val resetScale by action(MetaConverter.meta, MetaConverter.meta) {
timeScale.write(5000.0)
sinScale.write(1.0)

View File

@ -55,6 +55,7 @@ suspend fun Trace.updateXYFrom(flow: Flow<Iterable<Pair<Double, Double>>>) {
}
@Suppress("ExtractKtorModule")
suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEngine = embeddedServer(CIO, 9091) {
install(WebSockets)
install(RSocketSupport)

View File

@ -70,6 +70,7 @@ public fun CoroutineScope.startMagixServer(
)
}
@Suppress("ExtractKtorModule")
return embeddedServer(CIO, host = "localhost", port = port) {
magixModule(magixFlow)
applicationConfiguration(magixFlow)