Refactor onOpen in device spec
This commit is contained in:
parent
a0fd8913eb
commit
68805d6f42
@ -6,7 +6,7 @@ plugins {
|
|||||||
id("space.kscience.gradle.project")
|
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 ktorVersion: String by extra(space.kscience.gradle.KScienceVersions.ktorVersion)
|
||||||
val rsocketVersion by extra("0.15.4")
|
val rsocketVersion by extra("0.15.4")
|
||||||
val xodusVersion by extra("2.0.1")
|
val xodusVersion by extra("2.0.1")
|
||||||
@ -14,6 +14,10 @@ val xodusVersion by extra("2.0.1")
|
|||||||
allprojects {
|
allprojects {
|
||||||
group = "space.kscience"
|
group = "space.kscience"
|
||||||
version = "0.1.1-SNAPSHOT"
|
version = "0.1.1-SNAPSHOT"
|
||||||
|
repositories{
|
||||||
|
mavenCentral()
|
||||||
|
mavenLocal()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ksciencePublish {
|
ksciencePublish {
|
||||||
|
@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.decodeFromJsonElement
|
import kotlinx.serialization.json.decodeFromJsonElement
|
||||||
import kotlinx.serialization.json.encodeToJsonElement
|
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.Meta
|
||||||
import space.kscience.dataforge.meta.toJson
|
import space.kscience.dataforge.meta.toJson
|
||||||
import space.kscience.dataforge.meta.toMeta
|
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.toMeta(): Meta = Json.encodeToJsonElement(this).toMeta()
|
||||||
|
|
||||||
public fun DeviceMessage.toEnvelope(): SimpleEnvelope = SimpleEnvelope(toMeta(), null)
|
public fun DeviceMessage.toEnvelope(): Envelope = Envelope(toMeta(), null)
|
||||||
|
@ -20,8 +20,8 @@ public abstract class DeviceBase<D : DeviceBase<D>>(
|
|||||||
override val meta: Meta = Meta.EMPTY
|
override val meta: Meta = Meta.EMPTY
|
||||||
) : Device {
|
) : Device {
|
||||||
|
|
||||||
public abstract val properties: Map<String, DevicePropertySpec<D, *>> //get() = spec.properties
|
public abstract val properties: Map<String, DevicePropertySpec<D, *>>
|
||||||
public abstract val actions: Map<String, DeviceActionSpec<D, *, *>> //get() = spec.actions
|
public abstract val actions: Map<String, DeviceActionSpec<D, *, *>>
|
||||||
|
|
||||||
override val propertyDescriptors: Collection<PropertyDescriptor>
|
override val propertyDescriptors: Collection<PropertyDescriptor>
|
||||||
get() = properties.values.map { it.descriptor }
|
get() = properties.values.map { it.descriptor }
|
||||||
@ -136,5 +136,15 @@ public open class DeviceBySpec<D : DeviceBySpec<D>>(
|
|||||||
) : DeviceBase<D>(context, meta) {
|
) : DeviceBase<D>(context, meta) {
|
||||||
override val properties: Map<String, DevicePropertySpec<D, *>> get() = spec.properties
|
override val properties: Map<String, DevicePropertySpec<D, *>> get() = spec.properties
|
||||||
override val actions: Map<String, DeviceActionSpec<D, *, *>> get() = spec.actions
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,14 @@ public abstract class DeviceSpec<D : Device> {
|
|||||||
private val _actions = HashMap<String, DeviceActionSpec<D, *, *>>()
|
private val _actions = HashMap<String, DeviceActionSpec<D, *, *>>()
|
||||||
public val actions: Map<String, DeviceActionSpec<D, *, *>> get() = _actions
|
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 {
|
public fun <T : Any, P : DevicePropertySpec<D, T>> registerProperty(deviceProperty: P): P {
|
||||||
_properties[deviceProperty.name] = deviceProperty
|
_properties[deviceProperty.name] = deviceProperty
|
||||||
return deviceProperty
|
return deviceProperty
|
||||||
|
@ -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 {
|
public fun <D : DeviceBase<D>> D.doRecurring(interval: Duration, task: suspend D.() -> Unit): Job = launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
|
@ -43,7 +43,7 @@ class DemoController : Controller(), ContextAware {
|
|||||||
plugin(DeviceManager)
|
plugin(DeviceManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val deviceManager = context.fetch(DeviceManager)
|
private val deviceManager = context.request(DeviceManager)
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
context.launch {
|
context.launch {
|
||||||
|
@ -11,7 +11,6 @@ import space.kscience.dataforge.meta.descriptors.value
|
|||||||
import space.kscience.dataforge.meta.transformations.MetaConverter
|
import space.kscience.dataforge.meta.transformations.MetaConverter
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import kotlin.time.ExperimentalTime
|
|
||||||
|
|
||||||
|
|
||||||
class DemoDevice(context: Context, meta: Meta) : DeviceBySpec<DemoDevice>(DemoDevice, context, meta) {
|
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 sinScaleState = 1.0
|
||||||
private var cosScaleState = 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> {
|
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) {
|
val resetScale by action(MetaConverter.meta, MetaConverter.meta) {
|
||||||
timeScale.write(5000.0)
|
timeScale.write(5000.0)
|
||||||
sinScale.write(1.0)
|
sinScale.write(1.0)
|
||||||
|
@ -55,6 +55,7 @@ suspend fun Trace.updateXYFrom(flow: Flow<Iterable<Pair<Double, Double>>>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Suppress("ExtractKtorModule")
|
||||||
suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEngine = embeddedServer(CIO, 9091) {
|
suspend fun MagixEndpoint.startDemoDeviceServer(): ApplicationEngine = embeddedServer(CIO, 9091) {
|
||||||
install(WebSockets)
|
install(WebSockets)
|
||||||
install(RSocketSupport)
|
install(RSocketSupport)
|
||||||
|
@ -70,6 +70,7 @@ public fun CoroutineScope.startMagixServer(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("ExtractKtorModule")
|
||||||
return embeddedServer(CIO, host = "localhost", port = port) {
|
return embeddedServer(CIO, host = "localhost", port = port) {
|
||||||
magixModule(magixFlow)
|
magixModule(magixFlow)
|
||||||
applicationConfiguration(magixFlow)
|
applicationConfiguration(magixFlow)
|
||||||
|
Loading…
Reference in New Issue
Block a user