diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java index fd573d0c..c73569f7 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java @@ -5,7 +5,6 @@ */ package inr.numass.readvac.devices; -import hep.dataforge.context.Context; import hep.dataforge.control.collectors.PointCollector; import hep.dataforge.control.collectors.ValueCollector; import hep.dataforge.control.measurements.AbstractMeasurement; @@ -14,7 +13,6 @@ import hep.dataforge.control.measurements.Sensor; import hep.dataforge.data.DataPoint; import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.MeasurementException; -import hep.dataforge.meta.Meta; import hep.dataforge.values.Value; import java.util.Collection; import java.util.HashMap; @@ -33,24 +31,8 @@ public class VacCollectorDevice extends Sensor { private Map sensorMap = new HashMap<>(); -// /** -// * Sensors in reversed order -// * -// * @param name -// * @param context -// * @param meta -// * @param sensors -// */ -// public VacCollectorDevice(String name, Context context, Meta meta, Sensor... sensors) { -// sensorMap = new LinkedHashMap<>(sensors.length); -// for (Sensor sensor : sensors) { -// sensorMap.put(sensor.getName(), sensor); -// } -// //TODO add automatic construction from meta using deviceManager -// } - - public void setSensors(Sensor... sensors){ - sensorMap = new LinkedHashMap<>(sensors.length); + public void setSensors(Sensor... sensors) { + sensorMap = new LinkedHashMap<>(sensors.length); for (Sensor sensor : sensors) { sensorMap.put(sensor.getName(), sensor); } @@ -73,6 +55,24 @@ public class VacCollectorDevice extends Sensor { return "Numass vacuum"; } + public void setDelay(int delay) throws MeasurementException { + getConfig().setValue("delay", delay); + if (isMeasuring()) { + getMeasurement().stop(false); + getMeasurement().start(); + } + } + + @Override + public void shutdown() throws ControlException { + super.shutdown(); + for (Sensor sensor : getSensors()) { + sensor.shutdown(); + } + } + + + public Collection getSensors() { return sensorMap.values(); } @@ -86,22 +86,18 @@ public class VacCollectorDevice extends Sensor { @Override public void start() { executor = Executors - .newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "VacuumMeasurement thread")); + .newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "VacuumMeasurement thread")); currentTask = executor.scheduleWithFixedDelay(() -> { sensorMap.entrySet().stream().parallel().forEach((entry) -> { try { Object value = entry.getValue().read(); collector.put(entry.getKey(), value); - } catch (MeasurementException ex) { + } catch (Exception ex) { onError(ex); collector.put(entry.getKey(), Value.NULL); } }); - }, 0, getDelay(), TimeUnit.MILLISECONDS); - } - - private int getDelay() { - return meta().getInt("delay", 5000); + }, 0, meta().getInt("delay", 5000), TimeUnit.MILLISECONDS); } @Override diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacCollectorController.java b/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacCollectorController.java index b5a56ce1..d7045773 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacCollectorController.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacCollectorController.java @@ -13,6 +13,7 @@ import hep.dataforge.control.measurements.MeasurementListener; import hep.dataforge.control.measurements.Sensor; import hep.dataforge.data.DataPoint; import hep.dataforge.exceptions.ControlException; +import hep.dataforge.exceptions.MeasurementException; import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaBuilder; import hep.dataforge.plots.PlotFrame; @@ -28,11 +29,18 @@ import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; import javafx.application.Platform; +import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; +import javafx.scene.control.ToggleButton; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; +import javafx.util.Duration; +import org.controlsfx.control.Notifications; import org.slf4j.LoggerFactory; /** @@ -42,6 +50,9 @@ import org.slf4j.LoggerFactory; */ public class VacCollectorController implements Initializable, DeviceListener, MeasurementListener { + private final String[] intervalNames = {"1 sec", "5 sec", "10 sec", "30 sec", "1 min"}; + private final int[] intervals = {1000, 5000, 10000, 30000, 60000}; + private VacCollectorDevice device; private final List views = new ArrayList<>(); private PlotContainer plotContainer; @@ -53,11 +64,10 @@ public class VacCollectorController implements Initializable, DeviceListener, Me private VBox vacBoxHolder; @FXML private Label timeLabel; - - @Override - public void evaluateDeviceException(Device device, String message, Throwable exception) { - - } + @FXML + private ChoiceBox intervalSelector; + @FXML + private ToggleButton startStopButton; /** * Initializes the controller class. @@ -65,6 +75,22 @@ public class VacCollectorController implements Initializable, DeviceListener, Me @Override public void initialize(URL url, ResourceBundle rb) { plotContainer = PlotContainer.anchorTo(plotHolder); + intervalSelector.setItems(FXCollections.observableArrayList(intervalNames)); + intervalSelector.getSelectionModel().select(1); + intervalSelector.getSelectionModel().selectedIndexProperty().addListener((ObservableValue observable, Number oldValue, Number newValue) -> { + if (getDevice() != null) { + try { + getDevice().setDelay(intervals[newValue.intValue()]); + } catch (MeasurementException ex) { + evaluateDeviceException(getDevice(), "Failed to restart measurement", null); + } + } + }); + } + + @Override + public void evaluateDeviceException(Device device, String message, Throwable exception) { + Notifications.create().darkStyle().hideAfter(Duration.seconds(2d)).text(message).showError(); } public VacCollectorDevice getDevice() { @@ -78,7 +104,7 @@ public class VacCollectorController implements Initializable, DeviceListener, Me @Override public void onMeasurementFailed(Measurement measurement, Throwable exception) { - LoggerFactory.getLogger(getClass()).error("Exception during measurement", exception); + LoggerFactory.getLogger(getClass()).debug("Exception during measurement: {}", exception.getMessage()); } @Override @@ -87,7 +113,6 @@ public class VacCollectorController implements Initializable, DeviceListener, Me plottables.put(result); } Platform.runLater(() -> timeLabel.setText(time.toString())); - } private void setupView() { @@ -135,17 +160,29 @@ public class VacCollectorController implements Initializable, DeviceListener, Me public void startMeasurement() throws ControlException { getDevice().startMeasurement().addListener(this); + startStopButton.setSelected(true); } public void stopMeasurement() { try { - getDevice().stopMeasurement(true); + getDevice().stopMeasurement(false); for (Sensor sensor : getDevice().getSensors()) { - sensor.shutdown(); - } + sensor.stopMeasurement(false); + } } catch (ControlException ex) { throw new RuntimeException(ex); } } + @FXML + private void onStartStopToggle(ActionEvent event) throws ControlException { + if (startStopButton.isSelected() != getDevice().isMeasuring()) { + if (startStopButton.isSelected()) { + startMeasurement(); + } else { + stopMeasurement(); + } + } + } + } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacuumeterView.java b/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacuumeterView.java index 02e2d4eb..f73fd5ab 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacuumeterView.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/fx/VacuumeterView.java @@ -25,7 +25,6 @@ import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.control.Label; import javafx.scene.paint.Color; -import org.controlsfx.control.StatusBar; /** * @@ -40,14 +39,14 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL @FXML Label deviceNameLabel; - @FXML - StatusBar status; - @FXML Label unitLabel; @FXML Label valueLabel; + + @FXML + Label status; @Override @SuppressWarnings("unchecked") @@ -60,7 +59,7 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL @Override public void evaluateDeviceException(Device device, String message, Throwable exception) { - Platform.runLater(() -> status.setText("ERROR: " + message)); + Platform.runLater(() -> setStatus("ERROR: " + message)); } public Node getComponent() { @@ -91,7 +90,14 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL @Override public void onMeasurementFailed(Measurement measurement, Throwable exception) { - Platform.runLater(() -> valueLabel.setText("Err")); + Platform.runLater(() -> { + valueLabel.setText("Err"); + setStatus("Error: " + exception.getMessage()); + }); + } + + private void setStatus(String text) { + status.setText(text); } @Override @@ -101,13 +107,16 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL @Override public void onMeasurementProgress(Measurement measurement, double progress) { - Platform.runLater(() -> status.setProgress(progress)); +// Platform.runLater(() -> status.setProgress(progress)); } @Override public void onMeasurementResult(Measurement measurement, Double result, Instant time) { String resString = FORMAT.format(result); - Platform.runLater(() -> valueLabel.setText(resString)); + Platform.runLater(() -> { + valueLabel.setText(resString); + setStatus("OK: " + time.toString()); + }); } @Override diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/test/TestVac.java b/numass-control/vac/src/main/java/inr/numass/readvac/test/TestVac.java index 2ffa8a86..18f647d7 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/test/TestVac.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/test/TestVac.java @@ -13,10 +13,13 @@ import hep.dataforge.meta.MetaBuilder; import inr.numass.readvac.devices.VacCollectorDevice; import inr.numass.readvac.fx.VacCollectorController; import java.time.Duration; +import java.util.logging.Level; +import java.util.logging.Logger; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; +import org.slf4j.LoggerFactory; /** * @@ -36,6 +39,12 @@ public class TestVac extends Application { if (sensor.getState("power").booleanValue()) { return 1e-6; } else { +// throw new RuntimeException("not connected"); +// try { +// Thread.sleep(2000); +// } catch (InterruptedException ex) { +// LoggerFactory.getLogger(getClass()).info("Sleep interrupted on demo device"); +// } return null; } }) diff --git a/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml b/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml index da965b02..786dd820 100644 --- a/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml +++ b/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml @@ -1,32 +1,27 @@ - + - - + - + - - - - - - - - + + + + @@ -40,12 +35,20 @@ - + - + - + + + + + + + + diff --git a/numass-control/vac/src/main/resources/fxml/VacBox.fxml b/numass-control/vac/src/main/resources/fxml/VacBox.fxml index 3cc92c45..6504316c 100644 --- a/numass-control/vac/src/main/resources/fxml/VacBox.fxml +++ b/numass-control/vac/src/main/resources/fxml/VacBox.fxml @@ -1,33 +1,28 @@ - + - - + - - - - - - - - + + + + - + @@ -38,7 +33,18 @@ - + + + + + + + + + + + diff --git a/numass-control/vac/src/main/resources/fxml/VacCollector.fxml b/numass-control/vac/src/main/resources/fxml/VacCollector.fxml index 43ecf083..c148236c 100644 --- a/numass-control/vac/src/main/resources/fxml/VacCollector.fxml +++ b/numass-control/vac/src/main/resources/fxml/VacCollector.fxml @@ -1,39 +1,55 @@ + + + + - + - + + + + - + + + + + + + + - - diff --git a/numass-control/vac/src/main/resources/styles/vacstyles.css b/numass-control/vac/src/main/resources/styles/vacstyles.css index d8e62ae2..48982a2e 100644 --- a/numass-control/vac/src/main/resources/styles/vacstyles.css +++ b/numass-control/vac/src/main/resources/styles/vacstyles.css @@ -2,6 +2,10 @@ * Empty Stylesheet file. */ +.vacBox { + -fx-border-color: blue; +} + .vacBox #pressure{ -fx-font-size: 24; -fx-font-weight: bold @@ -13,7 +17,12 @@ .vacBox #name{ -fx-font-size: 18; -} + -fx-font-weight: bold +} + +.vacBox .namePane{ + -fx-background-color: lavender +} .beveled{ -fx-border-color: white #676767 #676767 white;