Change property delegates names

This commit is contained in:
Alexander Nozik 2024-11-29 12:58:24 +03:00
parent 9a0c55b24a
commit b4b534df1d
4 changed files with 36 additions and 21 deletions

View File

@ -8,7 +8,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.4.0-dev-6" version = "0.4.0-dev-7"
repositories{ repositories{
google() google()
} }

View File

@ -41,25 +41,24 @@ public fun <T, D : Device> DeviceSpec<D>.mutableProperty(
write = { _, value: T -> readWriteProperty.set(this, value) } 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 <T, D : DeviceBase<D>> DeviceSpec<D>.logicalProperty( public fun <T, D : DeviceBase<D>> DeviceSpec<D>.property(
converter: MetaConverter<T>, converter: MetaConverter<T>,
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, T>>> = ): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, DevicePropertySpec<D, T>>> =
mutableProperty( property(
converter, converter,
descriptorBuilder, descriptorBuilder,
name, name,
read = { propertyName -> getProperty(propertyName)?.let(converter::readOrNull) }, read = { propertyName -> getProperty(propertyName)?.let(converter::readOrNull) },
write = { propertyName, value -> writeProperty(propertyName, converter.convert(value)) }
) )
//read only delegates
public fun <D : Device> DeviceSpec<D>.booleanProperty( public fun <D : Device> DeviceSpec<D>.booleanProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
@ -141,7 +140,25 @@ public fun <D : Device> DeviceSpec<D>.metaProperty(
//read-write delegates //read-write delegates
public fun <D : Device> DeviceSpec<D>.booleanProperty(
/**
* Register a mutable logical property
* (without a corresponding physical state or with a state that is updated asynchronously) for a device
*/
public fun <T, D : DeviceBase<D>> DeviceSpec<D>.mutableProperty(
converter: MetaConverter<T>,
descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null,
): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, T>>> =
mutableProperty(
converter,
descriptorBuilder,
name,
read = { propertyName -> getProperty(propertyName)?.let(converter::readOrNull) },
write = { propertyName, value -> writeProperty(propertyName, converter.convert(value)) }
)
public fun <D : Device> DeviceSpec<D>.mutableBooleanProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
read: suspend D.(propertyName: String) -> Boolean?, read: suspend D.(propertyName: String) -> Boolean?,
@ -161,7 +178,7 @@ public fun <D : Device> DeviceSpec<D>.booleanProperty(
) )
public fun <D : Device> DeviceSpec<D>.numberProperty( public fun <D : Device> DeviceSpec<D>.mutableNumberProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
read: suspend D.(propertyName: String) -> Number, read: suspend D.(propertyName: String) -> Number,
@ -169,7 +186,7 @@ public fun <D : Device> DeviceSpec<D>.numberProperty(
): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, Number>>> = ): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, Number>>> =
mutableProperty(MetaConverter.number, numberDescriptor(descriptorBuilder), name, read, write) mutableProperty(MetaConverter.number, numberDescriptor(descriptorBuilder), name, read, write)
public fun <D : Device> DeviceSpec<D>.doubleProperty( public fun <D : Device> DeviceSpec<D>.mutableDoubleProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
read: suspend D.(propertyName: String) -> Double, read: suspend D.(propertyName: String) -> Double,
@ -177,7 +194,7 @@ public fun <D : Device> DeviceSpec<D>.doubleProperty(
): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, Double>>> = ): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, Double>>> =
mutableProperty(MetaConverter.double, numberDescriptor(descriptorBuilder), name, read, write) mutableProperty(MetaConverter.double, numberDescriptor(descriptorBuilder), name, read, write)
public fun <D : Device> DeviceSpec<D>.stringProperty( public fun <D : Device> DeviceSpec<D>.mutableStringProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
read: suspend D.(propertyName: String) -> String, read: suspend D.(propertyName: String) -> String,
@ -185,7 +202,7 @@ public fun <D : Device> DeviceSpec<D>.stringProperty(
): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, String>>> = ): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, String>>> =
mutableProperty(MetaConverter.string, descriptorBuilder, name, read, write) mutableProperty(MetaConverter.string, descriptorBuilder, name, read, write)
public fun <D : Device> DeviceSpec<D>.metaProperty( public fun <D : Device> DeviceSpec<D>.mutableMetaProperty(
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
name: String? = null, name: String? = null,
read: suspend D.(propertyName: String) -> Meta, read: suspend D.(propertyName: String) -> Meta,

View File

@ -88,15 +88,15 @@ class MksPdr900Device(context: Context, meta: Meta) : DeviceBySpec<MksPdr900Devi
override fun build(context: Context, meta: Meta): MksPdr900Device = MksPdr900Device(context, meta) override fun build(context: Context, meta: Meta): MksPdr900Device = MksPdr900Device(context, meta)
val powerOn by booleanProperty(read = { readPowerOn() }, write = { _, value -> 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 = { val value by doubleProperty(read = {
readChannelData(getOrRead(channel)) readChannelData(getOrRead(channel))
}) })
val error by logicalProperty(MetaConverter.string) val error by property(MetaConverter.string)
override suspend fun MksPdr900Device.onClose() { override suspend fun MksPdr900Device.onClose() {

View File

@ -23,8 +23,6 @@ import space.kscience.dataforge.context.*
import space.kscience.dataforge.meta.* import space.kscience.dataforge.meta.*
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.parseAsName import space.kscience.dataforge.names.parseAsName
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.milliseconds
@ -238,7 +236,7 @@ class PiMotionMasterDevice(
private fun axisBooleanProperty( private fun axisBooleanProperty(
command: String, command: String,
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
) = booleanProperty( ) = mutableBooleanProperty(
read = { read = {
readAxisBoolean("$command?") readAxisBoolean("$command?")
}, },
@ -251,7 +249,7 @@ class PiMotionMasterDevice(
private fun axisNumberProperty( private fun axisNumberProperty(
command: String, command: String,
descriptorBuilder: PropertyDescriptor.() -> Unit = {}, descriptorBuilder: PropertyDescriptor.() -> Unit = {},
) = doubleProperty( ) = mutableDoubleProperty(
read = { read = {
mm.requestAndParse("$command?", axisId)[axisId]?.toDoubleOrNull() mm.requestAndParse("$command?", axisId)[axisId]?.toDoubleOrNull()
?: error("Malformed $command response. Should include float value for $axisId") ?: error("Malformed $command response. Should include float value for $axisId")