From a33457d025df2f37cf53b41102b8564620c4c068 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 29 Sep 2016 13:52:24 +0300 Subject: [PATCH] PKT8 - working --- numass-control/cryotemp/build.gradle | 10 +- .../java/inr/numass/cryotemp/PKT8App.java | 11 +- .../java/inr/numass/cryotemp/PKT8Device.java | 109 ++++++++---------- .../cryotemp/PKT8MainViewController.java | 104 +++++++---------- .../inr/numass/cryotemp/PKT8VirtualPort.java | 27 +---- .../main/resources/config/defaultConfig.xml | 2 +- .../src/main/resources/fxml/PKT8MainView.fxml | 42 +++---- 7 files changed, 128 insertions(+), 177 deletions(-) diff --git a/numass-control/cryotemp/build.gradle b/numass-control/cryotemp/build.gradle index 6a97c860..09b1b135 100644 --- a/numass-control/cryotemp/build.gradle +++ b/numass-control/cryotemp/build.gradle @@ -12,5 +12,13 @@ dependencies { compile 'de.jensd:shichimifx:1.0.5' compile project(':dataforge-control') compile project(':dataforge-storage') - compile project(':dataforge-plots') + compile project(':dataforge-plots:plots-jfc') +} + +task debug(dependsOn: classes, type: JavaExec) { + main mainClass + args "--debug=true" + classpath = sourceSets.main.runtimeClasspath + description "Start application in debug mode with default virtual port" + group "debug" } \ No newline at end of file diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8App.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8App.java index 3c6dd857..d4039b12 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8App.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8App.java @@ -17,6 +17,7 @@ package inr.numass.cryotemp; import ch.qos.logback.classic.Level; import hep.dataforge.exceptions.ControlException; +import hep.dataforge.io.MetaFileReader; import hep.dataforge.storage.commons.StorageManager; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -25,7 +26,9 @@ import javafx.scene.Scene; import javafx.stage.Stage; import org.slf4j.LoggerFactory; +import java.io.File; import java.io.IOException; +import java.text.ParseException; import java.util.Locale; /** @@ -43,7 +46,7 @@ public class PKT8App extends Application { } @Override - public void start(Stage primaryStage) throws IOException, ControlException { + public void start(Stage primaryStage) throws IOException, ControlException, ParseException { Locale.setDefault(Locale.US);// чтобы отделение десятичных знаков было точкой ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(Level.INFO); @@ -68,6 +71,12 @@ public class PKT8App extends Application { // primaryStage.setResizable(false); primaryStage.show(); + + if(getParameters().getNamed().containsKey("cfgFile")){ + controller.setConfig(MetaFileReader.read(new File(getParameters().getNamed().get("cfgFile")))); + } else if (Boolean.parseBoolean(getParameters().getNamed().getOrDefault("debug","false"))){ + controller.loadTestConfig(); + } } @Override diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Device.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Device.java index 6da81180..9a83ccf5 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Device.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Device.java @@ -76,6 +76,7 @@ public class PKT8Device extends PortSensor { getLogger().warn("No channels defined in configuration"); } + super.init(); //update parameters from meta if (meta().hasValue("pga")) { getLogger().info("Setting dynamic range to " + meta().getInt("pga")); @@ -90,22 +91,27 @@ public class PKT8Device extends PortSensor { setSPS(meta().getInt("sps", 0)); setBUF(meta().getInt("abuf", 100)); - super.init(); + } + + @Override + public void shutdown() throws ControlException { + super.shutdown(); + collector.clear(); + collector = null; } @Override protected PortHandler buildHandler(String portName) throws ControlException { PortHandler handler; //setup connection - if (meta().hasNode("debug")) { - handler = new PKT8VirtualPort("PKT8", meta().getNode("debug")); + if ("virtual".equals(portName)) { + getLogger().info("Starting {} using virtual debug port", getName()); + handler = new PKT8VirtualPort("PKT8", meta().getNodeOrEmpty("debug")); } else { handler = super.buildHandler(portName); } handler.setDelimeter("\n"); - //clearing PKT queue - handler.send("p"); - handler.sendAndWait("p", null, 1000); + return handler; } @@ -115,7 +121,13 @@ public class PKT8Device extends PortSensor { private void setBUF(int buf) throws ControlException { getLogger().info("Setting avaraging buffer size to " + buf); - String response = getHandler().sendAndWait("b" + buf, null, 400).trim(); + String response; + try { + response = getHandler().sendAndWait("b" + buf, null, 400).trim(); + } catch (Exception ex) { + response = ex.getMessage(); + } + if (response.contains("=")) { updateState(ABUF, Integer.parseInt(response.substring(14))); // getLogger().info("successfully set buffer size to {}", this.abuf); @@ -168,11 +180,11 @@ public class PKT8Device extends PortSensor { * '0' : ± 5 В '1' : ± 2,5 В '2' : ± 1,25 В '3' : ± 0,625 В '4' : ± 312.5 мВ * '5' : ± 156,25 мВ '6' : ± 78,125 мВ * - * @param sps + * @param pga * @return */ - private String pgaToStr(int sps) { - switch (sps) { + private String pgaToStr(int pga) { + switch (pga) { case 0: return "± 5 V"; case 1: @@ -198,7 +210,12 @@ public class PKT8Device extends PortSensor { private void setSPS(int sps) throws ControlException { getLogger().info("Setting sampling rate to " + spsToStr(sps)); - String response = getHandler().sendAndWait("v" + sps, null, 400).trim(); + String response; + try { + response = getHandler().sendAndWait("v" + sps, null, 400).trim(); + } catch (Exception ex) { + response = ex.getMessage(); + } if (response.contains("=")) { updateState(SPS, Integer.parseInt(response.substring(4))); // getLogger().info("successfully sampling rate to {}", spsToStr(this.sps)); @@ -216,7 +233,7 @@ public class PKT8Device extends PortSensor { } private void setupStorage() { - if (meta().hasNode("storage") && collector != null) { + if (meta().hasNode("storage")) { try { Storage storage = StorageFactory.buildStorage(getContext(), meta().getNode("storage", Meta.empty())); String suffix = Integer.toString((int) Instant.now().toEpochMilli()); @@ -253,16 +270,29 @@ public class PKT8Device extends PortSensor { @Override protected Measurement createMeasurement() throws MeasurementException { - try { - return new PKT8Measurement(getHandler()); - } catch (ControlException e) { - throw new MeasurementException(e); + if (this.getMeasurement() != null) { + return this.getMeasurement(); + } else { + try { + return new PKT8Measurement(getHandler()); + } catch (ControlException e) { + throw new MeasurementException(e); + } } } @Override public Measurement startMeasurement() throws MeasurementException { - setupStorage(); + //clearing PKT queue + try { + getHandler().send("p"); + getHandler().sendAndWait("p", null, 1000); + } catch (ControlException e) { + // throw new MeasurementException(e); + } + if(collector == null){ + setupStorage(); + } return super.startMeasurement(); } @@ -282,7 +312,7 @@ public class PKT8Device extends PortSensor { handler.send("s"); afterStart(); } catch (PortException ex) { - throw new RuntimeException(ex); + error("Failed to start measurement", ex); } } @@ -291,7 +321,7 @@ public class PKT8Device extends PortSensor { try { getHandler().send("p"); if (collector != null) { - collector.cancel(); + collector.clear(); } return true; } catch (Exception ex) { @@ -324,7 +354,7 @@ public class PKT8Device extends PortSensor { } else { result(new PKT8Result(designation, rawValue, -1)); } - + setMeasurementState(MeasurementState.OK); } } } @@ -334,41 +364,4 @@ public class PKT8Device extends PortSensor { super.error(error); } } -} - -/* - - //setup storage - - try { - Storage storage = getPrimaryStorage(measurement); - String suffix = Integer.toString((int) Instant.now().toEpochMilli()); - - // Building data format - TableFormatBuilder TableFormatBuilder = new TableFormatBuilder() - .addTime("timestamp"); - List names = new ArrayList<>(); - - for (PKT8Channel channel : channels.values()) { - TableFormatBuilder.addNumber(channel.getName()); - names.add(channel.getName()); - } - - this.pointLoader = LoaderFactory.buildPointLoder(storage, "cryotemp_" + suffix, "", "timestamp", TableFormatBuilder.build()); - - Duration duration = Duration.parse(meta().getString("averagingDuration", "PT30S")); - - collector = new RegularPointCollector((dp) -> { - if (pointLoader != null) { - try { - getLogger().debug("Point measurement complete. Pushing..."); - pointLoader.push(dp); - } catch (StorageException ex) { - getLogger().error("Error while pushing point to loader", ex); - } - } - }, duration, names); - } catch (StorageException ex) { - getLogger().error("Can't setup storage", ex); - } - */ \ No newline at end of file +} \ No newline at end of file diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8MainViewController.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8MainViewController.java index 7525a7a8..ae569d17 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8MainViewController.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8MainViewController.java @@ -15,31 +15,28 @@ */ package inr.numass.cryotemp; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.Appender; -import ch.qos.logback.core.AppenderBase; -import de.jensd.shichimifx.utils.SplitPaneDividerSlider; import hep.dataforge.context.GlobalContext; import hep.dataforge.control.devices.Device; import hep.dataforge.control.devices.DeviceListener; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.MeasurementListener; import hep.dataforge.exceptions.ControlException; +import hep.dataforge.fx.ConsoleFragment; import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.Meta; -import hep.dataforge.meta.MetaBuilder; import hep.dataforge.meta.MetaUtils; +import hep.dataforge.plots.PlotUtils; import hep.dataforge.plots.data.TimePlottable; import hep.dataforge.plots.data.TimePlottableGroup; +import hep.dataforge.plots.data.XYPlottable; +import hep.dataforge.plots.fx.FXPlotFrame; +import hep.dataforge.plots.fx.PlotContainer; import hep.dataforge.plots.jfreechart.JFreeChartFrame; import hep.dataforge.values.Value; -import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; -import javafx.scene.control.SplitPane; -import javafx.scene.control.TextArea; import javafx.scene.control.ToggleButton; import javafx.scene.layout.AnchorPane; import javafx.stage.FileChooser; @@ -62,28 +59,24 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me public static final String DEFAULT_CONFIG_LOCATION = "devices.xml"; private PKT8Device device; - private JFreeChartFrame plotFrame; + private FXPlotFrame plotFrame; private TimePlottableGroup plottables; private Meta currentPlotConfig; + ConsoleFragment consoleFragment; + @FXML private Button loadConfigButton; @FXML - private SplitPane consoleSplitPane; - @FXML - private TextArea logArea; - @FXML private ToggleButton startStopButton; @FXML private AnchorPane plotArea; - @FXML private ToggleButton consoleButton; @Override public void close() throws Exception { if (device != null) { - device.stopMeasurement(true); device.shutdown(); } } @@ -94,11 +87,8 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me @Override public void initialize(URL url, ResourceBundle rb) { setupPlotFrame(null); - SplitPaneDividerSlider slider = new SplitPaneDividerSlider(consoleSplitPane, 0, SplitPaneDividerSlider.Direction.DOWN); - consoleButton.selectedProperty().addListener((ObservableValue observable, Boolean oldValue, Boolean newValue) -> { - slider.setAimContentVisible(newValue); - }); - slider.setAimContentVisible(false); + this.consoleFragment = new ConsoleFragment(); + consoleFragment.bindTo(consoleButton); } @FXML @@ -117,7 +107,7 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me } } - private void loadTestConfig() throws ControlException { + public void loadTestConfig() throws ControlException { try { Meta testConfig = MetaFileReader .read(new File(getClass().getResource("/config/defaultConfig.xml").toURI())); @@ -157,16 +147,10 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me private void setupPlotFrame(Meta plotFrameMeta) { plottables = new TimePlottableGroup(); plotArea.getChildren().clear(); - Meta plotConfig; - if (plotFrameMeta != null) { - plotConfig = new MetaBuilder(plotFrameMeta) - .setValue("xAxis.timeAxis", true); - } else { - plotConfig = new MetaBuilder("plotFrame") - .setValue("xAxis.timeAxis", true); - } - plotFrame = new JFreeChartFrame(plotConfig); - plotFrame.display(plotArea); + plotFrame = new JFreeChartFrame(plotFrameMeta); + PlotUtils.setXAxis(plotFrame, "timestamp", null, "time"); + PlotContainer container = PlotContainer.anchorTo(plotArea); + container.setPlot(plotFrame); } public void setupDevice(Meta deviceMeta) throws ControlException { @@ -175,27 +159,12 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me device.shutdown(); } - this.device = new PKT8Device("PKT8", GlobalContext.instance(), deviceMeta); + this.device = new PKT8Device(deviceMeta.getString("port", "virtual")); + + device.configure(deviceMeta); + device.addDeviceListener(this); - device.addMeasurementListener(this); - - logArea.appendText("Starting log...\n"); - - Appender appender = new AppenderBase() { - // private final DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME; - @Override - protected void append(ILoggingEvent e) { - logArea.appendText(String.format("%s > (%s) [%s] %s%n", - e.getLoggerName(), - Instant.now().toString(), - e.getLevel(), - e.getFormattedMessage())); - } - }; - - appender.start(); - - device.getLogger().addAppender(appender); + consoleFragment.addLogHandler(device.getLogger()); device.init(); } @@ -203,18 +172,22 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me @Override public void notifyDeviceInitialized(Device device) { Collection channels = this.device.getChanels(); - for (PKT8Channel channel : channels) { - if (!plottables.hasPlottable(channel.getName())) { - //plot config from device configuration - Meta deviceLineMeta = channel.meta().getNode("plot", channel.meta()); + //plot config from device configuration + //Do not use view config here, it is applyed separately + channels.stream() + .filter(channel -> !plottables.hasPlottable(channel.getName())) + .forEach(channel -> { - //Do not use view config here, it is applyed separately - TimePlottable plottable = new TimePlottable(channel.getName(), deviceLineMeta); - plottables.addPlottable(plottable); - plotFrame.add(plottable); - } - } + //plot config from device configuration + Meta deviceLineMeta = channel.meta().getNode("plot", channel.meta()); + + //Do not use view config here, it is applyed separately + TimePlottable plottable = new TimePlottable(channel.getName()); + plottable.configure(deviceLineMeta); + plottables.addPlottable(plottable); + plotFrame.add(plottable); + }); startStopButton.setDisable(false); if (currentPlotConfig != null) { @@ -259,16 +232,19 @@ public class PKT8MainViewController implements Initializable, DeviceListener, Me } - @FXML private void onStartStopClick(ActionEvent event) { if (device != null) { try { if (startStopButton.isSelected()) { - device.startMeasurement(); + device.startMeasurement() + .addListener(this); } else { //in case device started - device.stopMeasurement(false); + if (device.isMeasuring()) { + device.getMeasurement().removeListener(this); + device.stopMeasurement(false); + } } } catch (ControlException ex) { diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8VirtualPort.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8VirtualPort.java index f5c6efc1..888d4c51 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8VirtualPort.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8VirtualPort.java @@ -6,7 +6,6 @@ package inr.numass.cryotemp; import hep.dataforge.control.ports.VirtualPort; -import hep.dataforge.exceptions.PortException; import hep.dataforge.meta.Annotated; import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaUtils; @@ -21,18 +20,10 @@ import java.util.Random; */ public class PKT8VirtualPort extends VirtualPort implements Annotated { - private final Meta meta; private final Random generator = new Random(); - private final String id; public PKT8VirtualPort(String portName, Meta meta) { - id = portName; - this.meta = meta; - } - - @Override - public Meta meta() { - return meta; + super.configure(meta).configureValue("id", portName); } @Override @@ -70,24 +61,10 @@ public class PKT8VirtualPort extends VirtualPort implements Annotated { } } - @Override - public void open() throws PortException { - - } - - @Override - public boolean isOpen() { - return true; - } - - @Override - public String getPortId() { - return id; - } - @Override public void close() throws Exception { cancelByTag("measurement"); + super.close(); } } diff --git a/numass-control/cryotemp/src/main/resources/config/defaultConfig.xml b/numass-control/cryotemp/src/main/resources/config/defaultConfig.xml index 01795727..75112b8b 100644 --- a/numass-control/cryotemp/src/main/resources/config/defaultConfig.xml +++ b/numass-control/cryotemp/src/main/resources/config/defaultConfig.xml @@ -1,7 +1,7 @@ - + 120 diff --git a/numass-control/cryotemp/src/main/resources/fxml/PKT8MainView.fxml b/numass-control/cryotemp/src/main/resources/fxml/PKT8MainView.fxml index 7fd4796f..c7f5b76a 100644 --- a/numass-control/cryotemp/src/main/resources/fxml/PKT8MainView.fxml +++ b/numass-control/cryotemp/src/main/resources/fxml/PKT8MainView.fxml @@ -16,34 +16,22 @@ See the License for the specific language governing permissions and limitations under the License. --> + - - - - + + + +
+ +
+ + - - - -