From a337daee9319ab2de541dbe9caa0f9ba0e64be76 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 24 Sep 2023 13:21:01 +0300 Subject: [PATCH] Add read-after-write for DeviceBase property writers --- .../kscience/controls/spec/DeviceBase.kt | 21 ++++++++++++------- .../kscience/controls/spec/DeviceSpec.kt | 2 +- .../sciprog/devices/mks/MksPdr900Device.kt | 14 ++++++------- .../pimotionmaster/PiMotionMasterDevice.kt | 4 ++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceBase.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceBase.kt index 56f5aa9..dc43637 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceBase.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/spec/DeviceBase.kt @@ -87,7 +87,7 @@ public abstract class DeviceBase( /** * Update logical property state and notify listeners */ - protected suspend fun updateLogical(propertyName: String, value: Meta?) { + protected suspend fun propertyChanged(propertyName: String, value: Meta?) { if (value != logicalState[propertyName]) { stateLock.withLock { logicalState[propertyName] = value @@ -99,10 +99,10 @@ public abstract class DeviceBase( } /** - * Update logical state using given [spec] and its convertor + * Notify the device that a property with [spec] value is changed */ - public suspend fun updateLogical(spec: DevicePropertySpec, value: T) { - updateLogical(spec.name, spec.converter.objectToMeta(value)) + protected suspend fun propertyChanged(spec: DevicePropertySpec, value: T) { + propertyChanged(spec.name, spec.converter.objectToMeta(value)) } /** @@ -112,7 +112,7 @@ public abstract class DeviceBase( override suspend fun readProperty(propertyName: String): Meta { val spec = properties[propertyName] ?: error("Property with name $propertyName not found") val meta = spec.readMeta(self) ?: error("Failed to read property $propertyName") - updateLogical(propertyName, meta) + propertyChanged(propertyName, meta) return meta } @@ -122,7 +122,7 @@ public abstract class DeviceBase( public suspend fun readPropertyOrNull(propertyName: String): Meta? { val spec = properties[propertyName] ?: return null val meta = spec.readMeta(self) ?: return null - updateLogical(propertyName, meta) + propertyChanged(propertyName, meta) return meta } @@ -137,13 +137,18 @@ public abstract class DeviceBase( override suspend fun writeProperty(propertyName: String, value: Meta): Unit { when (val property = properties[propertyName]) { null -> { - //If there is a physical property with a given name, invalidate logical property and write physical one - updateLogical(propertyName, value) + //If there are no physical properties with given name, write a logical one. + propertyChanged(propertyName, value) } is WritableDevicePropertySpec -> { + //if there is a writeable property with a given name, invalidate logical and write physical invalidate(propertyName) property.writeMeta(self, value) + // perform read after writing if the writer did not set the value + if (logicalState[propertyName] == null) { + readPropertyOrNull(propertyName) + } } else -> { 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 d05bf30..17c3ecb 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 @@ -22,7 +22,7 @@ public val MetaConverter.Companion.unit: MetaConverter get() = UnitMetaCon @OptIn(InternalDeviceAPI::class) public abstract class DeviceSpec { - //initializing meta property for everyone + //initializing the metadata property for everyone private val _properties = hashMapOf>( DeviceMetaPropertySpec.name to DeviceMetaPropertySpec ) 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 949ced9..7771709 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 @@ -49,16 +49,16 @@ class MksPdr900Device(context: Context, meta: Meta) : DeviceBySpec