diff --git a/numass-control/magnet/src/main/kotlin/inr/numass/control/magnet/LambdaMagnet.kt b/numass-control/magnet/src/main/kotlin/inr/numass/control/magnet/LambdaMagnet.kt index 5684bc1c..70c1afb0 100644 --- a/numass-control/magnet/src/main/kotlin/inr/numass/control/magnet/LambdaMagnet.kt +++ b/numass-control/magnet/src/main/kotlin/inr/numass/control/magnet/LambdaMagnet.kt @@ -23,7 +23,6 @@ import hep.dataforge.control.ports.PortFactory import hep.dataforge.control.ports.PortTimeoutException import hep.dataforge.description.ValueDef import hep.dataforge.exceptions.ControlException -import hep.dataforge.exceptions.NameNotFoundException import hep.dataforge.exceptions.PortException import hep.dataforge.meta.Meta import hep.dataforge.utils.DateTimeUtils @@ -104,7 +103,7 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La override fun computeState(stateName: String): Any { - when (stateName) { + return when (stateName) { "current" -> controller.talk(address, timeout) { s2d(getParameter("MC")) } "voltage" -> controller.talk(address, timeout) { s2d(getParameter("MV")) } "targetCurrent" -> controller.talk(address, timeout) { s2d(getParameter("PC")) } @@ -112,42 +111,36 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La "output" -> controller.talk(address, timeout) { talk("OUT?") == "OK" } "monitoring" -> monitorTask != null "updating" -> updateTask != null - else -> getDefaultState(stateName) - + else -> getLogicalState(stateName) } } - override fun requestStateChange(stateName: String, value: Value): Any { - return when (stateName) { + override fun requestStateChange(stateName: String, value: Value) { + when (stateName) { "targetCurrent" -> { - if (!setParameter("PC", value.doubleValue())) { - reportError("Can't set the target current", null) - Value.NULL - } else { + if (setParameter("PC", value.doubleValue())) { lastUpdate = DateTimeUtils.now() - value + } else { + reportError("Can't set the target current", null) } } "targetVoltage" -> { if (!setParameter("PV", value.doubleValue())) { reportError("Can't set the target voltage", null) - Value.NULL - } else { - value } } - "speed" -> value - "lastUpdate" -> value "updating" -> if (value.booleanValue()) { startUpdateTask() - true } else { stopUpdateTask() - false } - "monitoring" -> i - "output" -> - else -> throw NameNotFoundException("State not found", stateName) + "monitoring" -> if (value.booleanValue()) { + startMonitorTask() + } else { + stopMonitorTask() + } + "output" -> setOutputMode(value.booleanValue()) + else -> setLogicalState(stateName, value); } } @@ -270,8 +263,8 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La * @param targetI * @param delay */ - @JvmOverloads - fun startUpdateTask(delay: Int = DEFAULT_DELAY) { + + private fun startUpdateTask(delay: Int = DEFAULT_DELAY) { assert(delay > 0) stopUpdateTask() val call = { @@ -295,26 +288,22 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La updateTask = scheduler.scheduleWithFixedDelay(call, 0, delay.toLong(), TimeUnit.MILLISECONDS) listener?.updateTaskStateChanged(getName(), true) + setLogicalState("updating", Value.of(true)) } @Throws(PortException::class) - fun setOutputMode(out: Boolean) { - controller.talk(address, timeout) { - val outState: Int = if (out) { - 1 - } else { - 0 - } - if (!setParameter("OUT", outState)) { - listener?.error(getName(), "Can't set output mode", null) - } else { - listener?.outputModeChanged(getName(), out) - } + private fun setOutputMode(out: Boolean) { + val outState: Int = if (out) 1 else 0 + if (!setParameter("OUT", outState)) { + listener?.error(getName(), "Can't set output mode", null) + } else { + requestStateChange("output", Value.of(out)) + listener?.outputModeChanged(getName(), out) } } private fun nextI(measuredI: Double, targetI: Double): Double { - assert(measuredI != targetI) +// assert(measuredI != targetI) var step = if (lastUpdate == null) { MIN_UP_STEP_SIZE @@ -345,12 +334,13 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La /** * Cancel current monitoring task */ - fun stopMonitorTask() { + private fun stopMonitorTask() { monitorTask?.let { it.cancel(true) listener?.monitorTaskStateChanged(getName(), false) monitorTask = null } + setLogicalState("output", Value.of(false)) } override fun getName(): String { diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt index 4a70790f..39bea920 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt @@ -9,6 +9,7 @@ import hep.dataforge.context.Context import hep.dataforge.control.devices.Device import hep.dataforge.control.devices.PortSensor import hep.dataforge.control.devices.StateDef +import hep.dataforge.control.devices.StateDefs import hep.dataforge.control.measurements.Measurement import hep.dataforge.control.measurements.SimpleMeasurement import hep.dataforge.control.ports.Port @@ -19,11 +20,7 @@ import hep.dataforge.meta.Meta import hep.dataforge.values.Value import hep.dataforge.values.ValueType.BOOLEAN import inr.numass.control.DeviceView -import javafx.beans.property.BooleanProperty -import javafx.beans.property.SimpleBooleanProperty -import javafx.beans.property.SimpleIntegerProperty -import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder -import tornadofx.* +import java.lang.Double.parseDouble import java.util.regex.Pattern /** @@ -34,20 +31,22 @@ import java.util.regex.Pattern ValueDef(name = "channel", def = "5"), ValueDef(name = "powerButton", type = arrayOf(BOOLEAN), def = "true") ) -@StateDef(value = ValueDef(name = "power", info = "Device powered up"), writable = true) +@StateDefs( + StateDef(value = ValueDef(name = "power", info = "Device powered up"), writable = true) +// StateDef(value = ValueDef(name = "channel", info = "Measurement channel", type = arrayOf(NUMBER), def = "5"), writable = true) +) @DeviceView(VacDisplay::class) class MKSVacDevice(context: Context, meta: Meta) : PortSensor(context, meta) { - private val deviceAddress: String - get() = getMeta().getString("address", "253") + private val deviceAddress: String = meta.getString("address", "253") - val isPowerOnProperty = SimpleBooleanProperty() - var isPowerOn by isPowerOnProperty +// val isPowerOnProperty = SimpleBooleanProperty() +// var isPowerOn by isPowerOnProperty - val channelProperty = SimpleIntegerProperty(meta.getInt("channel", 5)) - var channel by channelProperty +// val channelProperty = SimpleIntegerProperty(meta.getInt("channel", 5)) +// var channel by channelProperty @Throws(ControlException::class) @@ -78,9 +77,9 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor(context, m } @Throws(ControlException::class) - override fun requestStateChange(stateName: String, value: Value): Any { + override fun requestStateChange(stateName: String, value: Value) { when (stateName) { - "power" -> isPowerOn = value.booleanValue() + "power" -> setPower(value.booleanValue()) else -> super.requestStateChange(stateName, value) } @@ -89,24 +88,46 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor(context, m @Throws(ControlException::class) override fun shutdown() { if (isConnected) { - isPowerOn = false + setState("power",false) } super.shutdown() } - fun powerOnProperty(): BooleanProperty { - try { - return JavaBeanBooleanPropertyBuilder().bean(this) - .name("powerOn").getter("isPowerOn").setter("setPowerOn").build() - } catch (ex: NoSuchMethodException) { - throw Error(ex) +// fun powerOnProperty(): BooleanProperty { +// try { +// return JavaBeanBooleanPropertyBuilder().bean(this) +// .name("powerOn").getter("isPowerOn").setter("setPower").build() +// } catch (ex: NoSuchMethodException) { +// throw Error(ex) +// } +// +// } + + private fun setPower(powerOn: Boolean) { + if (powerOn != getLogicalState("power").booleanValue()) { + if (powerOn) { + // String ans = talkMKS(p1Port, "@253ENC!OFF;FF"); + // if (!ans.equals("OFF")) { + // LoggerFactory.getLogger(getClass()).warn("The @253ENC!OFF;FF command is not working"); + // } + val ans = talk("FP!ON") + if (ans == "ON") { + setLogicalState("power", true) + } else { + this.notifyError("Failed to set power state", null) + } + } else { + val ans = talk("FP!OFF") + if (ans == "OFF") { + setLogicalState("power", false) + } else { + this.notifyError("Failed to set power state", null) + } + } } - } - override fun getType(): String { - return getMeta().getString("type", "MKS vacuumeter") - } + override fun getType(): String = meta.getString("type", "MKS vacuumeter") private inner class MKSVacMeasurement : SimpleMeasurement() { @@ -114,13 +135,14 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor(context, m @Throws(Exception::class) override fun doMeasure(): Double? { // if (getState("power").booleanValue()) { + val channel = meta.getInt("channel", 5) val answer = talk("PR$channel?") if (answer == null || answer.isEmpty()) { updateState(PortSensor.CONNECTED_STATE, false) this.updateMessage("No connection") return null } - val res = java.lang.Double.parseDouble(answer) + val res = parseDouble(answer) return if (res <= 0) { this.updateMessage("No power") invalidateState("power") diff --git a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt index 9f1488a3..c72b9fdc 100644 --- a/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt +++ b/numass-viewer/src/main/kotlin/inr/numass/viewer/StorageView.kt @@ -224,29 +224,27 @@ class StorageView(private val context: Context = Global.instance()) : View(title } - private fun buildContainer(content: Any, parent: Container): Container { - return when (content) { - is Storage -> { - Container(content.fullName.toString(), content) - } - is NumassSet -> { - val id = if (content is NumassDataLoader) { - content.path - } else { - content.name + private fun buildContainer(content: Any, parent: Container): Container = + when (content) { + is Storage -> { + Container(content.fullName.toString(), content) } - Container(id.toString(), content) + is NumassSet -> { + val id: String = if (content is NumassDataLoader) { + content.path.toString() + } else { + content.name + } + Container(id.toString(), content) + } + is NumassPoint -> { + Container("${parent.id}/${content.voltage}".replace(".", "_"), content) + } + is Loader -> { + Container(content.path.toString(), content); + } + else -> throw IllegalArgumentException("Unknown content type: ${content::class.java}"); } - is NumassPoint -> { - Container("${parent.id}/${content.voltage}".replace(".", "_"), content) - } - is Loader -> { - Container(content.path.toString(), content); - } - else -> throw IllegalArgumentException("Unknown content type: ${content::class.java}"); - } - - } // private fun getSetName(value: NumassSet): String {