From aabf2b85a495509dfaaa3f45ba4a8846dd61506d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 8 May 2023 21:25:25 +0300 Subject: [PATCH] Documentation and DeviceSpec fix --- README.md | 9 ++ .../controls/spec/DevicePropertySpec.kt | 1 - .../kscience/controls/spec/DeviceSpec.kt | 33 ++-- .../controls/spec/propertySpecDelegates.kt | 21 +-- controls-magix-client/README.md | 2 +- .../controls/demo/DemoControllerView.kt | 1 + .../kscience/controls/demo/DemoDevice.kt | 63 ++++---- demo/many-devices/README.md | 2 +- .../kscience/controls/demo/MassDevice.kt | 6 +- docs/Device and DeviceSpec.md | 147 ++++++++++++++++++ docs/templates/README-TEMPLATE.md | 4 + 11 files changed, 230 insertions(+), 59 deletions(-) create mode 100644 docs/Device and DeviceSpec.md diff --git a/README.md b/README.md index e52bdac..098569b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ Example view of a demo: ![](docs/pictures/demo-view.png) +## Documentation + +* [Creating a device](docs/Device%20and%20DeviceSpec.md) + ## Modules @@ -119,6 +123,11 @@ Example view of a demo: > > **Maturity**: EXPERIMENTAL +### [demo/many-devices](demo/many-devices) +> +> +> **Maturity**: EXPERIMENTAL + ### [demo/mks-pdr900](demo/mks-pdr900) > > diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DevicePropertySpec.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DevicePropertySpec.kt index 4feace7..078a43d 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DevicePropertySpec.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DevicePropertySpec.kt @@ -39,7 +39,6 @@ public interface DevicePropertySpec { */ @InternalDeviceAPI public suspend fun read(device: D): T? - } /** diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceSpec.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceSpec.kt index 966ee32..eb5c978 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceSpec.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceSpec.kt @@ -13,7 +13,6 @@ import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 - @OptIn(InternalDeviceAPI::class) public abstract class DeviceSpec { //initializing meta property for everyone @@ -38,20 +37,24 @@ public abstract class DeviceSpec { return deviceProperty } - public fun registerProperty( - converter: MetaConverter, - readOnlyProperty: KProperty1, - descriptorBuilder: PropertyDescriptor.() -> Unit = {}, - ): DevicePropertySpec { - val deviceProperty = object : DevicePropertySpec { - override val descriptor: PropertyDescriptor = - PropertyDescriptor(readOnlyProperty.name).apply(descriptorBuilder) - override val converter: MetaConverter = converter - override suspend fun read(device: D): T = - withContext(device.coroutineContext) { readOnlyProperty.get(device) } - } - return registerProperty(deviceProperty) - } +// public fun registerProperty( +// converter: MetaConverter, +// readOnlyProperty: KProperty1, +// descriptorBuilder: PropertyDescriptor.() -> Unit = {}, +// ): DevicePropertySpec { +// val deviceProperty = object : DevicePropertySpec { +// +// override val descriptor: PropertyDescriptor = PropertyDescriptor(readOnlyProperty.name) +// .apply(descriptorBuilder) +// +// override val converter: MetaConverter = converter +// +// override suspend fun read(device: D): T = withContext(device.coroutineContext) { +// readOnlyProperty.get(device) +// } +// } +// return registerProperty(deviceProperty) +// } public fun property( converter: MetaConverter, diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/propertySpecDelegates.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/propertySpecDelegates.kt index e2afcab..ff89662 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/propertySpecDelegates.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/propertySpecDelegates.kt @@ -1,5 +1,6 @@ package space.kscience.controls.spec +import space.kscience.controls.api.Device import space.kscience.controls.api.PropertyDescriptor import space.kscience.controls.api.metaDescriptor import space.kscience.dataforge.meta.Meta @@ -10,7 +11,7 @@ import kotlin.properties.ReadOnlyProperty //read only delegates -public fun > DeviceSpec.booleanProperty( +public fun DeviceSpec.booleanProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Boolean? @@ -35,7 +36,7 @@ private inline fun numberDescriptor( descriptorBuilder() } -public fun > DeviceSpec.numberProperty( +public fun DeviceSpec.numberProperty( name: String? = null, descriptorBuilder: PropertyDescriptor.() -> Unit = {}, read: suspend D.() -> Number? @@ -46,7 +47,7 @@ public fun > DeviceSpec.numberProperty( read ) -public fun > DeviceSpec.doubleProperty( +public fun DeviceSpec.doubleProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Double? @@ -57,7 +58,7 @@ public fun > DeviceSpec.doubleProperty( read ) -public fun > DeviceSpec.stringProperty( +public fun DeviceSpec.stringProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> String? @@ -73,7 +74,7 @@ public fun > DeviceSpec.stringProperty( read ) -public fun > DeviceSpec.metaProperty( +public fun DeviceSpec.metaProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Meta? @@ -91,7 +92,7 @@ public fun > DeviceSpec.metaProperty( //read-write delegates -public fun > DeviceSpec.booleanProperty( +public fun DeviceSpec.booleanProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Boolean?, @@ -111,7 +112,7 @@ public fun > DeviceSpec.booleanProperty( ) -public fun > DeviceSpec.numberProperty( +public fun DeviceSpec.numberProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Number, @@ -119,7 +120,7 @@ public fun > DeviceSpec.numberProperty( ): PropertyDelegateProvider, ReadOnlyProperty, WritableDevicePropertySpec>> = mutableProperty(MetaConverter.number, numberDescriptor(descriptorBuilder), name, read, write) -public fun > DeviceSpec.doubleProperty( +public fun DeviceSpec.doubleProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Double, @@ -127,7 +128,7 @@ public fun > DeviceSpec.doubleProperty( ): PropertyDelegateProvider, ReadOnlyProperty, WritableDevicePropertySpec>> = mutableProperty(MetaConverter.double, numberDescriptor(descriptorBuilder), name, read, write) -public fun > DeviceSpec.stringProperty( +public fun DeviceSpec.stringProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> String, @@ -135,7 +136,7 @@ public fun > DeviceSpec.stringProperty( ): PropertyDelegateProvider, ReadOnlyProperty, WritableDevicePropertySpec>> = mutableProperty(MetaConverter.string, descriptorBuilder, name, read, write) -public fun > DeviceSpec.metaProperty( +public fun DeviceSpec.metaProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.() -> Meta, diff --git a/controls-magix-client/README.md b/controls-magix-client/README.md index 2c1895e..9e3187b 100644 --- a/controls-magix-client/README.md +++ b/controls-magix-client/README.md @@ -1,6 +1,6 @@ # Module controls-magix-client - +Magix service for binding controls devices (both as RPC client and server ## Usage diff --git a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt index b464611..9eb8244 100644 --- a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt +++ b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt @@ -147,6 +147,7 @@ class DemoControllerView : View(title = " Demo controller remote") { } } + class DemoControllerApp : App(DemoControllerView::class) { private val controller: DemoController by inject() diff --git a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt index b576b9a..37b7b4b 100644 --- a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt +++ b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt @@ -1,6 +1,7 @@ package space.kscience.controls.demo import kotlinx.coroutines.launch +import space.kscience.controls.api.Device import space.kscience.controls.api.metaDescriptor import space.kscience.controls.spec.* import space.kscience.dataforge.context.Context @@ -10,39 +11,47 @@ import space.kscience.dataforge.meta.ValueType import space.kscience.dataforge.meta.descriptors.value import space.kscience.dataforge.meta.transformations.MetaConverter import java.time.Instant +import kotlin.math.cos +import kotlin.math.sin import kotlin.time.Duration.Companion.milliseconds -class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(DemoDevice, context, meta) { - private var timeScaleState = 5000.0 - private var sinScaleState = 1.0 - private var cosScaleState = 1.0 +interface IDemoDevice: Device { + var timeScaleState: Double + var sinScaleState: Double + var cosScaleState: Double + fun time(): Instant = Instant.now() + fun sinValue(): Double + fun cosValue(): Double +} - companion object : DeviceSpec(), Factory { +class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(Companion, context, meta), IDemoDevice { + override var timeScaleState = 5000.0 + override var sinScaleState = 1.0 + override var cosScaleState = 1.0 + + override fun sinValue(): Double = sin(time().toEpochMilli().toDouble() / timeScaleState) * sinScaleState + + override fun cosValue(): Double = cos(time().toEpochMilli().toDouble() / timeScaleState) * cosScaleState + + companion object : DeviceSpec(), Factory { override fun build(context: Context, meta: Meta): DemoDevice = DemoDevice(context, meta) // register virtual properties based on actual object state - val timeScale by mutableProperty(MetaConverter.double, DemoDevice::timeScaleState) { + val timeScale by mutableProperty(MetaConverter.double, IDemoDevice::timeScaleState) { metaDescriptor { type(ValueType.NUMBER) } info = "Real to virtual time scale" } - val sinScale by mutableProperty(MetaConverter.double, DemoDevice::sinScaleState) - val cosScale by mutableProperty(MetaConverter.double, DemoDevice::cosScaleState) + val sinScale by mutableProperty(MetaConverter.double, IDemoDevice::sinScaleState) + val cosScale by mutableProperty(MetaConverter.double, IDemoDevice::cosScaleState) - val sin by doubleProperty { - val time = Instant.now() - kotlin.math.sin(time.toEpochMilli().toDouble() / timeScaleState) * sinScaleState - } - - val cos by doubleProperty { - val time = Instant.now() - kotlin.math.cos(time.toEpochMilli().toDouble() / timeScaleState) * cosScaleState - } + val sin by doubleProperty(read = IDemoDevice::sinValue) + val cos by doubleProperty(read = IDemoDevice::cosValue) val coordinates by metaProperty( descriptorBuilder = { @@ -52,15 +61,21 @@ class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(DemoDe } ) { Meta { - val time = Instant.now() - "time" put time.toEpochMilli() + "time" put time().toEpochMilli() "x" put read(sin) "y" put read(cos) } } - override suspend fun DemoDevice.onOpen() { + val resetScale by action(MetaConverter.meta, MetaConverter.meta) { + write(timeScale, 5000.0) + write(sinScale, 1.0) + write(cosScale, 1.0) + null + } + + override suspend fun IDemoDevice.onOpen() { launch { read(sinScale) read(cosScale) @@ -72,13 +87,5 @@ class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(DemoDe read(coordinates) } } - - val resetScale by action(MetaConverter.meta, MetaConverter.meta) { - write(timeScale, 5000.0) - write(sinScale, 1.0) - write(cosScale, 1.0) - null - } - } } \ No newline at end of file diff --git a/demo/many-devices/README.md b/demo/many-devices/README.md index 5d9c12f..cd860b5 100644 --- a/demo/many-devices/README.md +++ b/demo/many-devices/README.md @@ -1,4 +1,4 @@ -# Module all-things +# Module many-devices diff --git a/demo/many-devices/src/main/kotlin/space/kscience/controls/demo/MassDevice.kt b/demo/many-devices/src/main/kotlin/space/kscience/controls/demo/MassDevice.kt index a62b928..36e8253 100644 --- a/demo/many-devices/src/main/kotlin/space/kscience/controls/demo/MassDevice.kt +++ b/demo/many-devices/src/main/kotlin/space/kscience/controls/demo/MassDevice.kt @@ -49,7 +49,7 @@ class MassDevice(context: Context, meta: Meta) : DeviceBySpec(MassDe val value by doubleProperty { randomValue } override suspend fun MassDevice.onOpen() { - doRecurring(200.milliseconds) { + doRecurring(2.milliseconds) { read(value) } } @@ -63,7 +63,7 @@ fun main() { RSocketMagixFlowPlugin() ) - val numDevices = 1000 + val numDevices = 100 repeat(numDevices) { val deviceContext = Context("Device${it}") { @@ -99,7 +99,7 @@ fun main() { }.launchIn(this) while (isActive) { - delay(1000) + delay(200) val now = Clock.System.now() x.strings = latest.keys y.numbers = latest.values.map { now.minus(it).inWholeMilliseconds / 1000.0 } diff --git a/docs/Device and DeviceSpec.md b/docs/Device and DeviceSpec.md new file mode 100644 index 0000000..37dbeda --- /dev/null +++ b/docs/Device and DeviceSpec.md @@ -0,0 +1,147 @@ +# Device and DeviceSpec - what is the difference? + +One of the problems with creating device servers is that one needs device properties to be accessible both in static and dynamic mode. For example, consider a property: + +```kotlin +var property: Double = 1.0 + +``` + +We can change the state of the property, but neither propagate this change to the device, nor observe changes made to the property value by the device. The propagation to the device state could be added via custom getters and setters: + +```kotlin +var property: Double + get() = device.read(...) + set(value){ + device.write(..., value) + } +``` + +But this approach does not solve the observability problem. Neither it exposes the property to be automatically collected from the outside of the device + +The next stop is to use Kotlin delegates: + +```kotlin +var property by property( + read = { device.read(...)}, + write = {value-> device.write(..., value)} +) +``` + +Delegate solves almost all problems: it allows reading and writing the hardware, also it allows registering observation handles to listen to property changes externally (one needs to use [delegate providers](https://kotlinlang.org/docs/delegated-properties.html#providing-a-delegate) to register properties eagerly on instance creation. The only problem left is that properties registered this way are created on object instance creation and not accessible without creating the device instance. + +In order to solve this problem `Controls-kt` allows to separate device properties specification from the device itself. + +Check [DemoDevice](../demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoDevice.kt) for an example of a device with a specification. + +```kotlin +interface IDemoDevice: Device { + var timeScaleState: Double + var sinScaleState: Double + var cosScaleState: Double + + fun time(): Instant = Instant.now() + fun sinValue(): Double + fun cosValue(): Double +} + +class DemoDevice(context: Context, meta: Meta) : DeviceBySpec(Companion, context, meta), IDemoDevice { + override var timeScaleState = 5000.0 + override var sinScaleState = 1.0 + override var cosScaleState = 1.0 + + override fun sinValue(): Double = sin(time().toEpochMilli().toDouble() / timeScaleState) * sinScaleState + + override fun cosValue(): Double = cos(time().toEpochMilli().toDouble() / timeScaleState) * cosScaleState + + companion object : DeviceSpec(), Factory { + + override fun build(context: Context, meta: Meta): DemoDevice = DemoDevice(context, meta) + + // register virtual properties based on actual object state + val timeScale by mutableProperty(MetaConverter.double, IDemoDevice::timeScaleState) { + metaDescriptor { + type(ValueType.NUMBER) + } + info = "Real to virtual time scale" + } + + val sinScale by mutableProperty(MetaConverter.double, IDemoDevice::sinScaleState) + val cosScale by mutableProperty(MetaConverter.double, IDemoDevice::cosScaleState) + + val sin by doubleProperty(read = IDemoDevice::sinValue) + val cos by doubleProperty(read = IDemoDevice::cosValue) + + val coordinates by metaProperty( + descriptorBuilder = { + metaDescriptor { + value("time", ValueType.NUMBER) + } + } + ) { + Meta { + "time" put time().toEpochMilli() + "x" put read(sin) + "y" put read(cos) + } + } + + + val resetScale by action(MetaConverter.meta, MetaConverter.meta) { + write(timeScale, 5000.0) + write(sinScale, 1.0) + write(cosScale, 1.0) + null + } + + override suspend fun IDemoDevice.onOpen() { + launch { + read(sinScale) + read(cosScale) + read(timeScale) + } + doRecurring(50.milliseconds) { + read(sin) + read(cos) + read(coordinates) + } + } + } +} +``` + +## Device body + +Device inherits the class `DeviceBySpec` and takes the specification as an argument. The device itself contains hardware logic, but not communication logic. For example, it does not define properties exposed to the external observers. In the given example, it stores states for virtual properties (states) and contains logic to request current values for two properties. + +States for logical properties could also be stored via device mechanics without explicit state variables. + +## Device specification + +Specification is an object (singleton) that defines property scheme for external communication. Specification could define the following components: + +* Properties specifications via `property` delegate or specialized delegate variants. +* Action specification via `action` delegate or specialized delegates. +* Initialization logic (override `onOpen`). +* Finalization logic (override `onClose`). + +Properties can reference properties and method of the device. They also could contain device-independent logic or manipulate properties (like `coordinates` property in the example does). It is not recommended to implement direct device integration from the spec (yet it is possible). + +## Device specification abstraction + +In the example, the specification is a companion for `DemoDevice` and could be used as a factory for the device. Yet it works with the abstraction `IDemoDevice`. It is done to demonstrate that the device logic could be separated from the hardware logic. For example, one could swap a real device or a virtual device anytime without changing integrations anywhere. There could be also layers of abstractions for a device. + +## Access to properties + +In order to access property values, one needs to use both the device instance and property descriptor from the spec like follows: +```kotlin +val device = DemoDevice.build() + +val res = device.read(DemoDevice.sin) + +``` + +## Other ways to create a device + +It is not obligatory to use `DeviceBySpec` to define a `Device`. One could directly implement the `Device` interface or use intermediate abstraction `DeviceBase`, which uses properties schema but allows to define it manually. + diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md index 57f30c1..11e0905 100644 --- a/docs/templates/README-TEMPLATE.md +++ b/docs/templates/README-TEMPLATE.md @@ -35,6 +35,10 @@ Example view of a demo: ![](docs/pictures/demo-view.png) +## Documentation + +* [Creating a device](docs/Device%20and%20DeviceSpec.md) + ## Modules ${modules}