diff --git a/build.gradle.kts b/build.gradle.kts index 619a512..7e06cfa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { allprojects { group = "space.kscience" - version = "0.4.0-dev-6" + version = "0.4.0-dev-7" repositories{ google() } 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 8bd22b6..efb66a7 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 @@ -41,25 +41,24 @@ public fun DeviceSpec.mutableProperty( write = { _, value: T -> readWriteProperty.set(this, value) } ) +//read only delegates + /** - * Register a mutable logical property (without a corresponding physical state) for a device + * Register a read-only logical property + * (without a corresponding physical state or with a state that is updated asynchronously) for a device */ -public fun > DeviceSpec.logicalProperty( +public fun > DeviceSpec.property( converter: MetaConverter, descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, -): PropertyDelegateProvider, ReadOnlyProperty, MutableDevicePropertySpec>> = - mutableProperty( +): PropertyDelegateProvider, ReadOnlyProperty, DevicePropertySpec>> = + property( converter, descriptorBuilder, name, read = { propertyName -> getProperty(propertyName)?.let(converter::readOrNull) }, - write = { propertyName, value -> writeProperty(propertyName, converter.convert(value)) } ) - -//read only delegates - public fun DeviceSpec.booleanProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, @@ -141,7 +140,25 @@ public fun DeviceSpec.metaProperty( //read-write delegates -public fun DeviceSpec.booleanProperty( + +/** + * Register a mutable logical property + * (without a corresponding physical state or with a state that is updated asynchronously) for a device + */ +public fun > DeviceSpec.mutableProperty( + converter: MetaConverter, + descriptorBuilder: PropertyDescriptor.() -> Unit = {}, + name: String? = null, +): PropertyDelegateProvider, ReadOnlyProperty, MutableDevicePropertySpec>> = + mutableProperty( + converter, + descriptorBuilder, + name, + read = { propertyName -> getProperty(propertyName)?.let(converter::readOrNull) }, + write = { propertyName, value -> writeProperty(propertyName, converter.convert(value)) } + ) + +public fun DeviceSpec.mutableBooleanProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.(propertyName: String) -> Boolean?, @@ -161,7 +178,7 @@ public fun DeviceSpec.booleanProperty( ) -public fun DeviceSpec.numberProperty( +public fun DeviceSpec.mutableNumberProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.(propertyName: String) -> Number, @@ -169,7 +186,7 @@ public fun DeviceSpec.numberProperty( ): PropertyDelegateProvider, ReadOnlyProperty, MutableDevicePropertySpec>> = mutableProperty(MetaConverter.number, numberDescriptor(descriptorBuilder), name, read, write) -public fun DeviceSpec.doubleProperty( +public fun DeviceSpec.mutableDoubleProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.(propertyName: String) -> Double, @@ -177,7 +194,7 @@ public fun DeviceSpec.doubleProperty( ): PropertyDelegateProvider, ReadOnlyProperty, MutableDevicePropertySpec>> = mutableProperty(MetaConverter.double, numberDescriptor(descriptorBuilder), name, read, write) -public fun DeviceSpec.stringProperty( +public fun DeviceSpec.mutableStringProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.(propertyName: String) -> String, @@ -185,7 +202,7 @@ public fun DeviceSpec.stringProperty( ): PropertyDelegateProvider, ReadOnlyProperty, MutableDevicePropertySpec>> = mutableProperty(MetaConverter.string, descriptorBuilder, name, read, write) -public fun DeviceSpec.metaProperty( +public fun DeviceSpec.mutableMetaProperty( descriptorBuilder: PropertyDescriptor.() -> Unit = {}, name: String? = null, read: suspend D.(propertyName: String) -> Meta, diff --git a/demo/mks-pdr900/src/main/kotlin/center/sciprog/devices/mks/MksPdr900Device.kt b/demo/mks-pdr900/src/main/kotlin/center/sciprog/devices/mks/MksPdr900Device.kt index d28acc0..892d465 100644 --- a/demo/mks-pdr900/src/main/kotlin/center/sciprog/devices/mks/MksPdr900Device.kt +++ b/demo/mks-pdr900/src/main/kotlin/center/sciprog/devices/mks/MksPdr900Device.kt @@ -88,15 +88,15 @@ class MksPdr900Device(context: Context, meta: Meta) : DeviceBySpec writePowerOn(value) }) + val powerOn by mutableBooleanProperty(read = { readPowerOn() }, write = { _, value -> writePowerOn(value) }) - val channel by logicalProperty(MetaConverter.int) + val channel by property(MetaConverter.int) val value by doubleProperty(read = { readChannelData(getOrRead(channel)) }) - val error by logicalProperty(MetaConverter.string) + val error by property(MetaConverter.string) override suspend fun MksPdr900Device.onClose() { diff --git a/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt b/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt index 04e03df..4324b9b 100644 --- a/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt +++ b/demo/motors/src/main/kotlin/ru/mipt/npm/devices/pimotionmaster/PiMotionMasterDevice.kt @@ -23,8 +23,6 @@ import space.kscience.dataforge.context.* import space.kscience.dataforge.meta.* import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.parseAsName -import kotlin.collections.component1 -import kotlin.collections.component2 import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds @@ -238,7 +236,7 @@ class PiMotionMasterDevice( private fun axisBooleanProperty( command: String, descriptorBuilder: PropertyDescriptor.() -> Unit = {}, - ) = booleanProperty( + ) = mutableBooleanProperty( read = { readAxisBoolean("$command?") }, @@ -251,7 +249,7 @@ class PiMotionMasterDevice( private fun axisNumberProperty( command: String, descriptorBuilder: PropertyDescriptor.() -> Unit = {}, - ) = doubleProperty( + ) = mutableDoubleProperty( read = { mm.requestAndParse("$command?", axisId)[axisId]?.toDoubleOrNull() ?: error("Malformed $command response. Should include float value for $axisId")