From b4b534df1d55665fb440c373521aaf2e91162497 Mon Sep 17 00:00:00 2001
From: Alexander Nozik <altavir@gmail.com>
Date: Fri, 29 Nov 2024 12:58:24 +0300
Subject: [PATCH] Change property delegates names

---
 build.gradle.kts                              |  2 +-
 .../controls/spec/propertySpecDelegates.kt    | 43 +++++++++++++------
 .../sciprog/devices/mks/MksPdr900Device.kt    |  6 +--
 .../pimotionmaster/PiMotionMasterDevice.kt    |  6 +--
 4 files changed, 36 insertions(+), 21 deletions(-)

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 <T, D : Device> DeviceSpec<D>.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 <T, D : DeviceBase<D>> DeviceSpec<D>.logicalProperty(
+public fun <T, D : DeviceBase<D>> DeviceSpec<D>.property(
     converter: MetaConverter<T>,
     descriptorBuilder: PropertyDescriptor.() -> Unit = {},
     name: String? = null,
-): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, MutableDevicePropertySpec<D, T>>> =
-    mutableProperty(
+): PropertyDelegateProvider<DeviceSpec<D>, ReadOnlyProperty<DeviceSpec<D>, DevicePropertySpec<D, T>>> =
+    property(
         converter,
         descriptorBuilder,
         name,
         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(
     descriptorBuilder: PropertyDescriptor.() -> Unit = {},
     name: String? = null,
@@ -141,7 +140,25 @@ public fun <D : Device> DeviceSpec<D>.metaProperty(
 
 //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 = {},
     name: String? = null,
     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 = {},
     name: String? = null,
     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>>> =
     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 = {},
     name: String? = null,
     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>>> =
     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 = {},
     name: String? = null,
     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>>> =
     mutableProperty(MetaConverter.string, descriptorBuilder, name, read, write)
 
-public fun <D : Device> DeviceSpec<D>.metaProperty(
+public fun <D : Device> DeviceSpec<D>.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<MksPdr900Devi
 
         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 = {
             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")