From 66148d66f47dc188bf3774c7355a682c2d789ef0 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 2 Jun 2017 14:23:22 +0300 Subject: [PATCH] Moving msp to kotlin --- .../numass/control/cryotemp/PKT8Device.java | 2 +- .../control/cryotemp/PKT8DeviceFactory.java | 27 ---- .../control/cryotemp/PKT8DeviceFactory.kt | 24 ++++ numass-control/msp/build.gradle | 2 +- .../inr/numass/control/msp/MspDevice.java | 2 + .../numass/control/msp/MspDeviceFactory.java | 28 ---- .../kotlin/inr/numass/control/msp/MspApp.kt | 5 +- .../numass/control/msp/MspDeviceFactory.kt | 25 ++++ .../numass/control/msp/MspViewConnection.kt | 120 +++++++----------- .../numass/control/DeviceViewConnection.kt | 8 +- 10 files changed, 111 insertions(+), 132 deletions(-) delete mode 100644 numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8DeviceFactory.java create mode 100644 numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8DeviceFactory.kt delete mode 100644 numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java create mode 100644 numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDeviceFactory.kt diff --git a/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8Device.java b/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8Device.java index 6be434c2..e838765f 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8Device.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8Device.java @@ -55,7 +55,7 @@ import java.util.stream.Collectors; @RoleDef(name = Roles.STORAGE_ROLE) @RoleDef(name = Roles.VIEW_ROLE) @ValueDef(name = "port", def = "virtual", info = "The name of the port for this PKT8") -@StateDef(name = "storing") +@StateDef(@ValueDef(name = "storing")) public class PKT8Device extends PortSensor { public static final String PKT8_DEVICE_TYPE = "numass:pkt8"; diff --git a/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8DeviceFactory.java b/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8DeviceFactory.java deleted file mode 100644 index a2448205..00000000 --- a/numass-control/cryotemp/src/main/java/inr/numass/control/cryotemp/PKT8DeviceFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -package inr.numass.control.cryotemp; - -import hep.dataforge.context.Context; -import hep.dataforge.control.devices.Device; -import hep.dataforge.meta.Meta; -import inr.numass.control.DeviceViewConnection; -import inr.numass.control.DeviceViewFactory; - -/** - * Created by darksnake on 09-May-17. - */ -public class PKT8DeviceFactory implements DeviceViewFactory { - @Override - public String getType() { - return PKT8Device.PKT8_DEVICE_TYPE; - } - - @Override - public PKT8Device build(Context context, Meta meta) { - return new PKT8Device(context, meta); - } - - @Override - public DeviceViewConnection buildView(Device device) { - return new PKT8ViewConnection(); - } -} diff --git a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8DeviceFactory.kt b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8DeviceFactory.kt new file mode 100644 index 00000000..aec5aaf0 --- /dev/null +++ b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8DeviceFactory.kt @@ -0,0 +1,24 @@ +package inr.numass.control.cryotemp + +import hep.dataforge.context.Context +import hep.dataforge.control.devices.Device +import hep.dataforge.meta.Meta +import inr.numass.control.DeviceViewConnection +import inr.numass.control.DeviceViewFactory + +/** + * Created by darksnake on 09-May-17. + */ +class PKT8DeviceFactory : DeviceViewFactory { + override fun getType(): String { + return PKT8Device.PKT8_DEVICE_TYPE + } + + override fun build(context: Context, meta: Meta): PKT8Device { + return PKT8Device(context, meta) + } + + override fun buildView(device: Device): DeviceViewConnection<*> { + return PKT8ViewConnection() + } +} diff --git a/numass-control/msp/build.gradle b/numass-control/msp/build.gradle index 0f60d11a..c60bd867 100644 --- a/numass-control/msp/build.gradle +++ b/numass-control/msp/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'application' version = "0.4.0" if (!hasProperty('mainClass')) { - ext.mainClass = 'inr.numass.control.msp.fx.MspApp' + ext.mainClass = 'inr.numass.control.msp.MspApp' } mainClassName = mainClass diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/MspDevice.java b/numass-control/msp/src/main/java/inr/numass/control/msp/MspDevice.java index a49f8494..47599628 100644 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/MspDevice.java +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/MspDevice.java @@ -149,8 +149,10 @@ public class MspDevice extends Sensor implements PortHandler.PortCont switch (stateName) { case PortSensor.CONNECTED_STATE: setConnected(value.booleanValue()); + break; case "filamentOn": setFilamentOn(value.booleanValue()); + break; default: super.requestStateChange(stateName, value); } diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java b/numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java deleted file mode 100644 index 6d517d70..00000000 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -package inr.numass.control.msp; - -import hep.dataforge.context.Context; -import hep.dataforge.control.devices.Device; -import hep.dataforge.meta.Meta; -import inr.numass.control.DeviceViewConnection; -import inr.numass.control.DeviceViewFactory; - -/** - * Created by darksnake on 09-May-17. - */ -public class MspDeviceFactory implements DeviceViewFactory { - @Override - public String getType() { - return MspDevice.MSP_DEVICE_TYPE; - } - - @Override - public MspDevice build(Context context, Meta config) { - MspDevice device = new MspDevice(context,config); - return device; - } - - @Override - public DeviceViewConnection buildView(Device device) { - return MspViewConnection.Companion.build(device.getContext()); - } -} diff --git a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspApp.kt b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspApp.kt index 85686bad..b07b35d3 100644 --- a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspApp.kt +++ b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspApp.kt @@ -15,6 +15,7 @@ */ package inr.numass.control.msp +import hep.dataforge.control.connections.Roles import hep.dataforge.control.devices.DeviceFactory import hep.dataforge.meta.Meta import inr.numass.control.DeviceViewConnection @@ -27,7 +28,9 @@ import javafx.stage.Stage class MspApp : NumassControlApplication() { override fun buildView(device: MspDevice): DeviceViewConnection { - return MspViewConnection.build(device.context) + return MspViewConnection().apply { + device.connect(this, Roles.VIEW_ROLE) + } } override val deviceFactory: DeviceFactory = MspDeviceFactory() diff --git a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDeviceFactory.kt b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDeviceFactory.kt new file mode 100644 index 00000000..489889c4 --- /dev/null +++ b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDeviceFactory.kt @@ -0,0 +1,25 @@ +package inr.numass.control.msp + +import hep.dataforge.context.Context +import hep.dataforge.control.devices.Device +import hep.dataforge.meta.Meta +import inr.numass.control.DeviceViewConnection +import inr.numass.control.DeviceViewFactory + +/** + * Created by darksnake on 09-May-17. + */ +class MspDeviceFactory : DeviceViewFactory { + override fun getType(): String { + return MspDevice.MSP_DEVICE_TYPE + } + + override fun build(context: Context, config: Meta): MspDevice { + val device = MspDevice(context, config) + return device + } + + override fun buildView(device: Device): DeviceViewConnection<*> { + return MspViewConnection() + } +} diff --git a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt index 32a25f01..1f93b6f1 100644 --- a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt +++ b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspViewConnection.kt @@ -16,7 +16,6 @@ package inr.numass.control.msp import hep.dataforge.control.NamedValueListener -import hep.dataforge.control.devices.Device import hep.dataforge.control.devices.DeviceListener import hep.dataforge.control.devices.PortSensor import hep.dataforge.control.devices.Sensor @@ -34,8 +33,9 @@ import hep.dataforge.plots.jfreechart.JFreeChartFrame import hep.dataforge.values.Value import inr.numass.control.DeviceViewConnection import inr.numass.control.deviceStateIndicator -import javafx.application.Platform import javafx.beans.property.SimpleObjectProperty +import javafx.collections.FXCollections +import javafx.collections.MapChangeListener import javafx.geometry.Insets import javafx.geometry.Orientation import javafx.scene.Node @@ -56,6 +56,8 @@ import tornadofx.* class MspViewConnection : DeviceViewConnection(), DeviceListener, NamedValueListener { private val mspView by lazy { MspView() } + private val table = FXCollections.observableHashMap() + override fun getBoardView(): Parent { return VBox().apply { this += super.getBoardView() @@ -70,17 +72,7 @@ class MspViewConnection : DeviceViewConnection(), DeviceListener, Nam } override fun pushValue(valueName: String, value: Value) { - val pl = plottables.get(valueName) - if (pl != null) { - if (value.doubleValue() > 0) { - pl.put(value) - } else { - pl.put(Value.NULL) - } - val titleBase = pl.config.getString("titleBase") - val title = String.format("%s (%.4g)", titleBase, value.doubleValue()) - pl.configureValue("title", title) - } + table.put(valueName, value) } @@ -102,7 +94,26 @@ class MspViewConnection : DeviceViewConnection(), DeviceListener, Nam configure(plotFrameMeta) } } - val plottables: TimePlottableGroup = TimePlottableGroup() + val plottables: TimePlottableGroup = TimePlottableGroup().apply { + if (plotFrameMeta.hasMeta("peakJump.peak")) { + for (peakMeta in plotFrameMeta.getMetaList("peakJump.peak")) { + val mass = peakMeta.getString("mass") + if (!this.has(mass)) { + val newPlottable = TimePlottable(mass, mass) + newPlottable.configure(peakMeta) + newPlottable.setMaxItems(1000) + newPlottable.setPrefItems(400) + newPlottable.configureValue("titleBase", peakMeta.getString("title", mass)) + add(newPlottable) + } else { + get(mass).configure(peakMeta) + } + } + } else { + showError("No peaks defined in config") + throw RuntimeException() + } + } private var logButton: ToggleButton by singleAssign() @@ -136,7 +147,7 @@ class MspViewConnection : DeviceViewConnection(), DeviceListener, Nam .bind(getStateBinding(MEASURING_STATE).booleanBinding { it!!.booleanValue() }) } add(ToggleSwitch().apply { - padding = Insets(11.0, 0.0, 0.0, 0.0) + padding = Insets(5.0, 0.0, 0.0, 0.0) disableProperty() .bind(getStateBinding(PortSensor.CONNECTED_STATE).booleanBinding { !it!!.booleanValue() }) bindBooleanToState("filamentOn", selectedProperty()) @@ -146,7 +157,7 @@ class MspViewConnection : DeviceViewConnection(), DeviceListener, Nam "ON" -> Paint.valueOf("red") "OFF" -> Paint.valueOf("blue") "WARM-UP", "COOL-DOWN" -> Paint.valueOf("yellow") - else -> error("Unknown filament state") + else -> Paint.valueOf("grey") } } @@ -179,66 +190,33 @@ class MspViewConnection : DeviceViewConnection(), DeviceListener, Nam } PlotContainer.centerIn(this).plot = plotFrame } - - private var plot: JFreeChartFrame? = null - private var logFragment: LogFragment? = null - - @Throws(Exception::class) - override fun open(device: MspDevice) { - super.open(device) - updatePlot() - - bindBooleanToState("connected", connectButton!!.selectedProperty()) - } - - private fun initPlot() { - val plotConfig = MetaBuilder("plotFrame") - .setNode(MetaBuilder("yAxis") - .setValue("type", "log") - .setValue("axisTitle", "partial pressure") - .setValue("axisUnits", "mbar") - ) - .setValue("xAxis.type", "time") - - this.plot = JFreeChartFrame(plotConfig) - val container = PlotContainer.centerIn(plotPane) - container.plot = plot - } - - private fun updatePlot() { - if (plot == null) { - initPlot() - } - if (plotFrameMeta.hasMeta("plotFrame")) { - this.plot!!.configure(plotFrameMeta.getMeta("plotFrame")) - } - if (plotFrameMeta.hasMeta("peakJump.peak")) { - for (peakMeta in plotFrameMeta.getMetaList("peakJump.peak")) { - val mass = peakMeta.getString("mass") - if (!this.plottables.has(mass)) { - val newPlottable = TimePlottable(mass, mass) - newPlottable.configure(peakMeta) - newPlottable.setMaxItems(1000) - newPlottable.setPrefItems(400) - newPlottable.configureValue("titleBase", peakMeta.getString("title", mass)) - this.plottables.add(newPlottable) - plot!!.add(newPlottable) - } else { - plottables.get(mass).configure(peakMeta) + init{ + table.addListener {change: MapChangeListener.Change -> + if (change.wasAdded()) { + val pl = plottables.get(change.key) + val value = change.valueAdded + if (pl != null) { + if (value.doubleValue() > 0) { + pl.put(value) + } else { + pl.put(Value.NULL) + } + val titleBase = pl.config.getString("titleBase") + val title = String.format("%s (%.4g)", titleBase, value.doubleValue()) + pl.configureValue("title", title) } } - } else { - showError("No peaks defined in config") - throw RuntimeException() + } } - override fun evaluateDeviceException(device: Device, message: String, exception: Throwable) { - Platform.runLater { - logFragment!!.appendLine("ERROR: " + message) - showError(message) - } - } + +// override fun evaluateDeviceException(device: Device, message: String, exception: Throwable) { +// Platform.runLater { +// logFragment!!.appendLine("ERROR: " + message) +// showError(message) +// } +// } private fun showError(message: String) { val alert = Alert(Alert.AlertType.ERROR) diff --git a/numass-control/src/main/kotlin/inr/numass/control/DeviceViewConnection.kt b/numass-control/src/main/kotlin/inr/numass/control/DeviceViewConnection.kt index d95c2415..a468c245 100644 --- a/numass-control/src/main/kotlin/inr/numass/control/DeviceViewConnection.kt +++ b/numass-control/src/main/kotlin/inr/numass/control/DeviceViewConnection.kt @@ -9,6 +9,7 @@ import hep.dataforge.fx.FXObject import hep.dataforge.fx.fragments.FXFragment import hep.dataforge.fx.fragments.FragmentWindow import hep.dataforge.values.Value +import javafx.application.Platform import javafx.beans.binding.ObjectBinding import javafx.beans.property.BooleanProperty import javafx.beans.value.ObservableValue @@ -46,8 +47,8 @@ abstract class DeviceViewConnection : DeviceConnection(), DeviceL } } - fun getBooleanStateBinding(state: String): ObservableValue{ - return getStateBinding(state).booleanBinding{it!!.booleanValue()} + fun getBooleanStateBinding(state: String): ObservableValue { + return getStateBinding(state).booleanBinding { it!!.booleanValue() } } /** @@ -65,7 +66,8 @@ abstract class DeviceViewConnection : DeviceConnection(), DeviceL } property.addListener { observable, oldValue, newValue -> if (isOpen && oldValue != newValue) { - device.setState(state, newValue) + val result = device.setState(state, newValue).get().booleanValue(); + Platform.runLater { property.set(result) } } } }