From ff6a6048744d34ee73ea99cda15cd7ecd8b563c7 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 12 Mar 2018 22:17:00 +0300 Subject: [PATCH] Control update --- .../numass/control/cryotemp/PKT8Channel.kt | 24 ++++++++-- .../inr/numass/control/cryotemp/PKT8Device.kt | 46 +++++++++---------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Channel.kt b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Channel.kt index a225f7be..d8876927 100644 --- a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Channel.kt +++ b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Channel.kt @@ -16,6 +16,8 @@ package inr.numass.control.cryotemp +import hep.dataforge.kodex.buildMeta +import hep.dataforge.kodex.stringValue import hep.dataforge.meta.Meta import hep.dataforge.meta.MetaBuilder import hep.dataforge.meta.Metoid @@ -48,14 +50,22 @@ internal fun createChannel(meta: Meta): PKT8Channel { } } +data class PKT8Result(val channel: String, val rawValue: Double, val temperature: Double) { + + val rawString: String = String.format("%.2f", rawValue) + + val temperatureString: String = String.format("%.2f", temperature) +} /** * Created by darksnake on 28-Sep-16. */ -class PKT8Channel(private val _meta: Meta, val func: (Double) -> Double) : Named, Metoid { +class PKT8Channel(private val _meta: Meta, private val func: (Double) -> Double) : Named, Metoid { + + private val _name: String by meta.stringValue() override fun getName(): String { - return getMeta().getString("name") + return _name } override fun getMeta(): Meta { @@ -63,7 +73,7 @@ class PKT8Channel(private val _meta: Meta, val func: (Double) -> Double) : Named } fun description(): String { - return getMeta().getString("description", "") + return meta.getString("description", "") } /** @@ -74,8 +84,12 @@ class PKT8Channel(private val _meta: Meta, val func: (Double) -> Double) : Named return func(r) } - fun evaluate(r: Double): PKT8Result { - return PKT8Result(name, r, getTemperature(r)) + fun evaluate(r: Double): Meta { + return buildMeta { + "channel" to name + "raw" to r + "temperature" to getTemperature(r) + } } } \ No newline at end of file diff --git a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt index 852daf6d..db055af9 100644 --- a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt +++ b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt @@ -18,6 +18,7 @@ package inr.numass.control.cryotemp import hep.dataforge.connections.RoleDef import hep.dataforge.connections.RoleDefs import hep.dataforge.context.Context +import hep.dataforge.control.collectors.RegularPointCollector import hep.dataforge.control.connections.Roles import hep.dataforge.control.devices.PortSensor import hep.dataforge.control.devices.stringState @@ -101,7 +102,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { override fun init() { //read channel configuration - if (getMeta().hasMeta("channel")) { + if (meta.hasMeta("channel")) { for (node in getMeta().getMetaList("channel")) { val designation = node.getString("designation", "default") this.channels.put(designation, createChannel(node)) @@ -109,7 +110,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { } else { //set default channel configuration for (designation in CHANNEL_DESIGNATIONS) { - this.channels.put(designation, createChannel(designation)) + this.channels[designation] = createChannel(designation) } logger.warn("No channels defined in configuration") } @@ -222,6 +223,21 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { } } + private val collector = RegularPointCollector(duration) { + notifyResult(produceResult(it)) + storageHelper?.push(it) + } + + private fun notifyChannelResult(designation: String, rawValue: Double) { + + val channel = channels[designation] + + if (channel != null) { + collector.put(channel.name, channel.getTemperature(rawValue)) + } else { + result(PKT8Result(designation, rawValue, -1.0)) + } + } override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) { if (oldMeta != null) { @@ -246,15 +262,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { val designation = trimmed.substring(0, 1) val rawValue = java.lang.Double.parseDouble(trimmed.substring(1)) / 100 - val channel = this@PKT8Device.channels[designation] - - if (channel != null) { - notifyResult() - result(channel.evaluate(rawValue)) - collector.put(channel.name, channel.getTemperature(rawValue)) - } else { - result(PKT8Result(designation, rawValue, -1.0)) - } + notifyChannelResult(designation, rawValue) } //send start signal @@ -269,12 +277,12 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { logger.info("Stopping measurement") val response = sendAndWait("p").trim() // Должно быть именно с большой буквы!!! - return "Stopped" == response || "stopped" == response + if ("Stopped" == response || "stopped" == response) { + notifyMeasurementState(MeasurementState.STOPPED) + } } catch (ex: Exception) { - onError("Failed to stop measurement", ex) - return false + notifyError("Failed to stop measurement", ex) } finally { - afterStop() connection?.removeErrorListener(this) connection?.removePhraseListener(this) collector.stop() @@ -306,12 +314,4 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) { private val CHANNEL_DESIGNATIONS = arrayOf("a", "b", "c", "d", "e", "f", "g", "h") } -} - - -data class PKT8Result(val channel: String, val rawValue: Double, val temperature: Double) { - - val rawString: String = String.format("%.2f", rawValue) - - val temperatureString: String = String.format("%.2f", temperature) } \ No newline at end of file