Moving msp to kotlin

This commit is contained in:
Alexander Nozik 2017-06-02 14:23:22 +03:00
parent 1d82ccf123
commit 66148d66f4
10 changed files with 111 additions and 132 deletions

View File

@ -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<PKT8Result> {
public static final String PKT8_DEVICE_TYPE = "numass:pkt8";

View File

@ -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();
}
}

View File

@ -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()
}
}

View File

@ -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

View File

@ -149,8 +149,10 @@ public class MspDevice extends Sensor<DataPoint> implements PortHandler.PortCont
switch (stateName) {
case PortSensor.CONNECTED_STATE:
setConnected(value.booleanValue());
break;
case "filamentOn":
setFilamentOn(value.booleanValue());
break;
default:
super.requestStateChange(stateName, value);
}

View File

@ -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());
}
}

View File

@ -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<MspDevice>() {
override fun buildView(device: MspDevice): DeviceViewConnection<MspDevice> {
return MspViewConnection.build(device.context)
return MspViewConnection().apply {
device.connect(this, Roles.VIEW_ROLE)
}
}
override val deviceFactory: DeviceFactory = MspDeviceFactory()

View File

@ -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()
}
}

View File

@ -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<MspDevice>(), DeviceListener, NamedValueListener {
private val mspView by lazy { MspView() }
private val table = FXCollections.observableHashMap<String, Value>()
override fun getBoardView(): Parent {
return VBox().apply {
this += super.getBoardView()
@ -70,17 +72,7 @@ class MspViewConnection : DeviceViewConnection<MspDevice>(), 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<MspDevice>(), 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<MspDevice>(), 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<MspDevice>(), 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,67 +190,34 @@ class MspViewConnection : DeviceViewConnection<MspDevice>(), 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)
init{
table.addListener {change: MapChangeListener.Change<out String, out Value> ->
if (change.wasAdded()) {
val pl = plottables.get(change.key)
val value = change.valueAdded
if (pl != null) {
if (value.doubleValue() > 0) {
pl.put(value)
} else {
plottables.get(mass).configure(peakMeta)
pl.put(Value.NULL)
}
}
} else {
showError("No peaks defined in config")
throw RuntimeException()
val titleBase = pl.config.getString("titleBase")
val title = String.format("%s (%.4g)", titleBase, value.doubleValue())
pl.configureValue("title", title)
}
}
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)
alert.title = "Error!"

View File

@ -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
@ -65,7 +66,8 @@ abstract class DeviceViewConnection<D : Device> : DeviceConnection<D>(), 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) }
}
}
}