From 0ebe97b18ee15029f0d39795bba0207efa4767fa Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 9 May 2017 22:20:33 +0300 Subject: [PATCH] Working on devices --- .../java/inr/numass/cryotemp/PKT8App.java | 37 ++--- .../inr/numass/cryotemp/PKT8Controller.java | 30 ++-- .../java/inr/numass/cryotemp/PKT8Device.java | 15 +- .../numass/cryotemp/PKT8DeviceFactory.java | 33 ++++ .../inr/numass/control/msp/MspDevice.java | 24 ++- .../numass/control/msp/MspDeviceFactory.java | 23 +++ .../inr/numass/control/msp/MspListener.java | 2 +- .../java/inr/numass/control/msp/MspTest.java | 32 ++-- .../inr/numass/control/msp/fx/MspApp.java | 60 +++++-- .../control/msp/fx/MspViewController.java | 154 +++++++----------- .../src/main/resources/config/msp-config.xml | 38 ++--- .../inr/numass/control/NumassConnections.java | 40 +++++ .../java/inr/numass/readvac/app/ReadVac.java | 27 +-- .../numass/readvac/devices/CM32Device.java | 4 - .../readvac/devices/MKSBaratronDevice.java | 4 - .../numass/readvac/devices/MKSVacDevice.java | 5 - .../numass/readvac/devices/VITVacDevice.java | 4 - .../readvac/devices/VacCollectorDevice.java | 2 +- .../readvac/fx/PoweredVacuumeterView.java | 18 +- .../inr/numass/readvac/fx/VacuumeterView.java | 6 +- 20 files changed, 317 insertions(+), 241 deletions(-) create mode 100644 numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8DeviceFactory.java create mode 100644 numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java create mode 100644 numass-control/src/main/java/inr/numass/control/NumassConnections.java 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 0cd47794..0b8e956e 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 @@ -16,17 +16,12 @@ package inr.numass.cryotemp; import ch.qos.logback.classic.Level; -import hep.dataforge.control.connections.Roles; -import hep.dataforge.control.connections.StorageConnection; import hep.dataforge.exceptions.ControlException; -import hep.dataforge.exceptions.StorageException; import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaUtils; -import hep.dataforge.storage.api.Storage; -import hep.dataforge.storage.commons.StorageFactory; import hep.dataforge.storage.commons.StorageManager; -import inr.numass.client.ClientUtils; +import inr.numass.control.NumassConnections; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; @@ -40,10 +35,13 @@ import java.io.IOException; import java.net.URISyntaxException; import java.text.ParseException; +import static hep.dataforge.control.devices.PortSensor.PORT_NAME_KEY; + /** * @author darksnake */ public class PKT8App extends Application { + public static final String DEFAULT_CONFIG_LOCATION = "numass-devices.xml"; @@ -77,23 +75,11 @@ public class PKT8App extends Application { device = setupDevice(deviceName, config); // setting up storage connections - if (config.hasMeta("storage")) { - String numassRun = ClientUtils.getRunName(config); - config.getMetaList("storage").forEach(node -> { - Storage storage = StorageFactory.buildStorage(device.getContext(), node); - if (!numassRun.isEmpty()) { - try { - storage = storage.buildShelf(numassRun, Meta.empty()); - } catch (StorageException e) { - LoggerFactory.getLogger(getClass()).error("Failed to build shelf"); - } - } - device.connect(new StorageConnection(storage), Roles.STORAGE_ROLE); - }); - } + NumassConnections.connectStorage(device, config); FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/PKT8Indicator.fxml")); - PKT8Controller controller = new PKT8Controller(device); + PKT8Controller controller = new PKT8Controller(); + device.connect(controller, "view"); loader.setController(controller); Parent parent = loader.load(); @@ -111,7 +97,6 @@ public class PKT8App extends Application { Platform.runLater(() -> { try { device.init(); -// controller.start(); } catch (ControlException e) { e.printStackTrace(); throw new RuntimeException(e); @@ -137,10 +122,14 @@ public class PKT8App extends Application { deviceMeta = config; } - PKT8Device device = new PKT8Device(deviceMeta.getString("port", "virtual")); - + PKT8Device device = new PKT8Device(); device.configure(deviceMeta); + if(!deviceMeta.hasValue(PORT_NAME_KEY)){ + device.getLogger().warn("Port name not provided, will try to use emulation port"); + } + + return device; } diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Controller.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Controller.java index 621f9dab..910d8421 100644 --- a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Controller.java +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8Controller.java @@ -1,5 +1,6 @@ package inr.numass.cryotemp; +import hep.dataforge.control.connections.DeviceConnection; import hep.dataforge.control.devices.Device; import hep.dataforge.control.devices.DeviceListener; import hep.dataforge.control.measurements.Measurement; @@ -26,11 +27,11 @@ import java.util.ResourceBundle; /** * Created by darksnake on 07-Oct-16. */ -public class PKT8Controller implements Initializable, DeviceListener, MeasurementListener { +public class PKT8Controller extends DeviceConnection implements Initializable, DeviceListener, MeasurementListener { - private final PKT8Device device; private LogFragment logFragment; private PKT8PlotFragment plotFragment; + @FXML private ToggleButton startStopButton; @FXML @@ -42,35 +43,28 @@ public class PKT8Controller implements Initializable, DeviceListener, Measuremen @FXML private TableView table; - @FXML private TableColumn, String> sensorColumn; - @FXML private TableColumn, Double> resColumn; - @FXML private TableColumn, String> tempColumn; - public PKT8Controller(PKT8Device device) { - this.device = device; - } - @Override public void initialize(URL location, ResourceBundle resources) { this.logFragment = new LogFragment(); - logFragment.addLogHandler(device.getContext().getLogger()); + logFragment.addLogHandler(getDevice().getContext().getLogger()); //TODO to be removed later logFragment.hookStd(); new FragmentWindow(logFragment).bindTo(consoleButton); - plotFragment = new PKT8PlotFragment(device); + plotFragment = new PKT8PlotFragment(getDevice()); new FragmentWindow(plotFragment).bindTo(plotButton); sensorColumn.setCellValueFactory(new PropertyValueFactory<>("channel")); resColumn.setCellValueFactory(new PropertyValueFactory<>("rawString")); tempColumn.setCellValueFactory(new PropertyValueFactory<>("temperatureString")); - startStopButton.selectedProperty().setValue(device.isMeasuring()); + startStopButton.selectedProperty().setValue(getDevice().isMeasuring()); } @Override @@ -100,20 +94,20 @@ public class PKT8Controller implements Initializable, DeviceListener, Measuremen public void start() throws MeasurementException { - device.startMeasurement().addListener(this); + getDevice().startMeasurement().addListener(this); } public void stop() throws MeasurementException { - if (device.isMeasuring()) { - device.getMeasurement().removeListener(this); - device.stopMeasurement(false); + if (getDevice().isMeasuring()) { + getDevice().getMeasurement().removeListener(this); + getDevice().stopMeasurement(false); } } @FXML private void onStartStopClick(ActionEvent event) { - if (device != null) { + if (getDevice() != null) { try { if (startStopButton.isSelected()) { start(); @@ -122,7 +116,7 @@ public class PKT8Controller implements Initializable, DeviceListener, Measuremen stop(); } } catch (ControlException ex) { - evaluateDeviceException(device, "Failed to start or stop device", ex); + evaluateDeviceException(getDevice(), "Failed to start or stop device", ex); } } } 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 e228f7c4..d17c6600 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 @@ -15,6 +15,7 @@ */ package inr.numass.cryotemp; +import hep.dataforge.context.Context; import hep.dataforge.control.collectors.RegularPointCollector; import hep.dataforge.control.connections.LoaderConnection; import hep.dataforge.control.connections.PointListenerConnection; @@ -25,6 +26,7 @@ import hep.dataforge.control.devices.annotations.RoleDef; import hep.dataforge.control.measurements.AbstractMeasurement; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.ports.PortHandler; +import hep.dataforge.description.ValueDef; import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.MeasurementException; import hep.dataforge.exceptions.PortException; @@ -49,7 +51,10 @@ import java.util.stream.Collectors; */ @RoleDef(name = Roles.STORAGE_ROLE) @RoleDef(name = Roles.POINT_LISTENER_ROLE) +@RoleDef(name = Roles.VIEW_ROLE, objectType = PKT8Controller.class) +@ValueDef(name = "port",def = "virtual", info = "The name of the port for this PKT8") public class PKT8Device extends PortSensor { + public static final String PKT8_DEVICE_TYPE = "numass:pkt8"; public static final String PGA = "pga"; public static final String SPS = "sps"; @@ -61,8 +66,12 @@ public class PKT8Device extends PortSensor { private final Map channels = new HashMap<>(); private RegularPointCollector collector; - public PKT8Device(String portName) { - super(portName); + public PKT8Device() { + } + + + public PKT8Device(Context context, Meta meta) { + } @Override @@ -277,7 +286,7 @@ public class PKT8Device extends PortSensor { // setting up the collector Duration duration = Duration.parse(meta().getString("averagingDuration", "PT30S")); collector = new RegularPointCollector((DataPoint dp) -> { - forEachTypedConnection(Roles.POINT_LISTENER_ROLE, PointListener.class, listener -> { + forEachConnection(Roles.POINT_LISTENER_ROLE, PointListener.class, listener -> { getLogger().debug("Point measurement complete. Pushing..."); listener.accept(dp); }); diff --git a/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8DeviceFactory.java b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8DeviceFactory.java new file mode 100644 index 00000000..b0bc25f2 --- /dev/null +++ b/numass-control/cryotemp/src/main/java/inr/numass/cryotemp/PKT8DeviceFactory.java @@ -0,0 +1,33 @@ +package inr.numass.cryotemp; + +import hep.dataforge.context.Context; +import hep.dataforge.control.connections.Connection; +import hep.dataforge.control.connections.Roles; +import hep.dataforge.control.devices.DeviceFactory; +import hep.dataforge.meta.Meta; + +import java.util.Objects; + +/** + * Created by darksnake on 09-May-17. + */ +public class PKT8DeviceFactory implements DeviceFactory { + @Override + public String getType() { + return PKT8Device.PKT8_DEVICE_TYPE; + } + + @Override + public PKT8Device build(Context context, Meta meta) { + return new PKT8Device(context, meta); + } + + @Override + public Connection buildConnection(String role, Context context, Meta meta) { + if(Objects.equals(role, Roles.VIEW_ROLE)){ + return new PKT8Controller(); + } else { + return DeviceFactory.super.buildConnection(role, context, meta); + } + } +} 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 6957ea95..8980c155 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 @@ -48,8 +48,9 @@ import java.util.function.Consumer; */ @RoleDef(name = Roles.STORAGE_ROLE, objectType = StorageConnection.class) public class MspDevice extends SingleMeasurementDevice implements PortHandler.PortController { + public static final String MSP_DEVICE_TYPE = "msp"; -// private static final String PEAK_SET_PATH = "peakJump.peak"; + // private static final String PEAK_SET_PATH = "peakJump.peak"; private static final int TIMEOUT = 200; boolean connected = false; boolean selected = false; @@ -81,8 +82,8 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po super.stopMeasurement(true); setFileamentOn(false); setConnected(false); - handler.unholdBy(this); - handler.close(); + getHandler().unholdBy(this); + getHandler().close(); } @Override @@ -184,7 +185,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po } } - public void setListener(MspListener listener) { + public void setMspListener(MspListener listener) { this.mspListener = listener; } @@ -200,7 +201,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po if (mspListener != null) { mspListener.acceptRequest(request); } - handler.send(request); + getHandler().send(request); } /** @@ -235,7 +236,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po mspListener.acceptRequest(request); } - String response = handler.sendAndWait( + String response = getHandler().sendAndWait( request, (String str) -> str.trim().startsWith(commandName), TIMEOUT @@ -305,7 +306,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po updateState("filamentOn", status.equals("ON")); updateState("filamentStatus", status); if (mspListener != null) { - mspListener.acceptFillamentStateChange(status); + mspListener.acceptFilamentStateChange(status); } break; } @@ -325,6 +326,13 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po } } + private TcpPortHandler getHandler() { + if(handler == null){ + throw new RuntimeException("Device not initialized"); + } + return handler; + } + /** * The MKS response as two-dimensional array of strings */ @@ -504,7 +512,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po if (isFilamentOn()) { mspListener.acceptScan(measurement); - forEachTypedConnection(Roles.STORAGE_ROLE, StorageConnection.class, (StorageConnection connection) -> { + forEachConnection(Roles.STORAGE_ROLE, StorageConnection.class, (StorageConnection connection) -> { PointLoader pl = loaderMap.computeIfAbsent(connection, con -> makeLoader(con)); try { pl.push(point.build()); 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 new file mode 100644 index 00000000..b9aa98d6 --- /dev/null +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/MspDeviceFactory.java @@ -0,0 +1,23 @@ +package inr.numass.control.msp; + +import hep.dataforge.context.Context; +import hep.dataforge.control.devices.DeviceFactory; +import hep.dataforge.meta.Meta; + +/** + * Created by darksnake on 09-May-17. + */ +public class MspDeviceFactory implements DeviceFactory { + @Override + public String getType() { + return MspDevice.MSP_DEVICE_TYPE; + } + + @Override + public MspDevice build(Context context, Meta config) { + MspDevice device = new MspDevice(); + device.setContext(context); + device.configure(config); + return device; + } +} diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/MspListener.java b/numass-control/msp/src/main/java/inr/numass/control/msp/MspListener.java index fc98c35e..35a7201c 100644 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/MspListener.java +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/MspListener.java @@ -30,7 +30,7 @@ public interface MspListener { void acceptMessage(String message); void acceptRequest(String message); - default void acceptFillamentStateChange(String fillamentState){ + default void acceptFilamentStateChange(String fillamentState){ } } diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/MspTest.java b/numass-control/msp/src/main/java/inr/numass/control/msp/MspTest.java index d2571990..7e1f3fae 100644 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/MspTest.java +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/MspTest.java @@ -1,25 +1,25 @@ -/* - * Copyright 2015 Alexander Nozik. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +/* + * Copyright 2015 Alexander Nozik. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package inr.numass.control.msp; import hep.dataforge.exceptions.PortException; + import java.io.IOException; /** - * * @author darksnake */ public class MspTest { diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspApp.java b/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspApp.java index d5b9b657..af2b7fc0 100644 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspApp.java +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspApp.java @@ -17,11 +17,16 @@ package inr.numass.control.msp.fx; import ch.qos.logback.classic.Level; import hep.dataforge.context.Global; +import hep.dataforge.control.connections.Roles; +import hep.dataforge.exceptions.ControlException; import hep.dataforge.io.MetaFileReader; import hep.dataforge.io.XMLMetaReader; import hep.dataforge.meta.Meta; import hep.dataforge.storage.commons.StorageManager; +import inr.numass.control.msp.MspDevice; +import inr.numass.control.msp.MspDeviceFactory; import javafx.application.Application; +import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; @@ -31,13 +36,16 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.util.Locale; +import static inr.numass.control.msp.MspDevice.MSP_DEVICE_TYPE; + /** - * * @author darksnake */ public class MspApp extends Application { + public static final String DEFAULT_CONFIG_LOCATION = "msp-config.xml"; + + private MspDevice device; - MspViewController controller; /** * @param args the command line arguments @@ -55,41 +63,67 @@ public class MspApp extends Application { String configFileName = getParameters().getNamed().get("config"); if (configFileName == null) { - configFileName = "msp-config.xml"; + configFileName = DEFAULT_CONFIG_LOCATION; } File configFile = new File(configFileName); Meta config; if (configFile.exists()) { config = MetaFileReader.read(configFile).build(); } else { -// throw new RuntimeException("Configuration file not found"); - config = new XMLMetaReader().read(MspApp.class.getClassLoader().getResourceAsStream("config/msp-config.xml"), -1); + config = new XMLMetaReader().read(getClass().getResourceAsStream("/config/msp-config.xml")); } FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/MspView.fxml")); Parent parent = loader.load(); - controller = loader.getController(); + MspViewController controller = loader.getController(); - controller.setDeviceConfig(Global.instance(), config); - - Scene scene = new Scene(parent, 600, 400); + Scene scene = new Scene(parent, 800, 600); primaryStage.setTitle("Numass mass-spectrometer view"); primaryStage.setScene(scene); primaryStage.setMinHeight(400); primaryStage.setMinWidth(600); -// primaryStage.setResizable(false); + Platform.runLater(()->{ + try { + device = new MspDeviceFactory().build(Global.instance(), getMspConfig(config)); + device.init(); + device.connect(controller, Roles.VIEW_ROLE); + } catch (ControlException e) { + throw new RuntimeException("Failed to build device", e); + } + }); primaryStage.show(); - } + private Meta getMspConfig(Meta config) { + Meta mspConfig = null; + if (config.hasMeta("device")) { + for (Meta d : config.getMetaList("device")) { + if (d.getString("type", "unknown").equals(MSP_DEVICE_TYPE)) { + mspConfig = d; + } + } + } else if (config.hasMeta("peakJump")) { + mspConfig = config; + } + return mspConfig; + } + + +// showError(String.format("Can't connect to %s:%d. The port is either busy or not the MKS mass-spectrometer port", +// device.meta().getString("connection.ip", "127.0.0.1"), +// device.meta().getInt("connection.port", 10014))); +// throw new RuntimeException("Can't connect to device"); + + @Override public void stop() throws Exception { super.stop(); - controller.shutdown(); -// System.exit(0); + if (device != null) { + device.shutdown(); + } } } diff --git a/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspViewController.java b/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspViewController.java index 1c2c3956..7d4be673 100644 --- a/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspViewController.java +++ b/numass-control/msp/src/main/java/inr/numass/control/msp/fx/MspViewController.java @@ -15,16 +15,16 @@ */ package inr.numass.control.msp.fx; -import hep.dataforge.context.Context; -import hep.dataforge.context.Global; +import hep.dataforge.control.connections.DeviceConnection; import hep.dataforge.control.connections.Roles; import hep.dataforge.control.connections.StorageConnection; +import hep.dataforge.control.devices.Device; +import hep.dataforge.control.devices.DeviceListener; import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.PortException; import hep.dataforge.exceptions.StorageException; import hep.dataforge.fx.fragments.FragmentWindow; import hep.dataforge.fx.fragments.LogFragment; -import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.ConfigChangeListener; import hep.dataforge.meta.Configuration; import hep.dataforge.meta.Meta; @@ -59,10 +59,7 @@ import org.controlsfx.control.ToggleSwitch; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; import java.net.URL; -import java.text.ParseException; import java.util.List; import java.util.Map; import java.util.ResourceBundle; @@ -72,18 +69,14 @@ import java.util.ResourceBundle; * * @author darksnake */ -public class MspViewController implements Initializable, MspListener { +public class MspViewController extends DeviceConnection implements DeviceListener, Initializable, MspListener { - public static final String MSP_DEVICE_TYPE = "msp"; - - public static final String DEFAULT_CONFIG_LOCATION = "msp-config.xml"; private final PlottableGroup plottables = new PlottableGroup<>(); - private final String mspName = "msp"; - private MspDevice device; private Configuration viewConfig; private JFreeChartFrame plot; private LogFragment logArea; private StorageConnection connection; + @FXML private Slider autoRangeSlider; @FXML @@ -128,7 +121,7 @@ public class MspViewController implements Initializable, MspListener { fillamentSelector.setConverter(new StringConverter() { @Override public String toString(Integer object) { - return "Fillament " + object; + return "Filament " + object; } @Override @@ -143,7 +136,7 @@ public class MspViewController implements Initializable, MspListener { fillamentSelector.setDisable(newValue); getDevice().setFileamentOn(newValue); } catch (PortException ex) { - device.getLogger().error("Failed to toggle fillaments"); + getDevice().getLogger().error("Failed to toggle filaments"); } }); } @@ -162,69 +155,22 @@ public class MspViewController implements Initializable, MspListener { this.viewConfig.addObserver(viewConfigObserver); } - private MspDevice getDevice() { - if (this.device == null) { - showError("Device configuration not found. Using default configuration."); - Meta defaultDeviceConfig; - try { - defaultDeviceConfig = MetaFileReader - .read(new File(getClass().getResource("/config/msp-config.xml").toURI())); - } catch (IOException | URISyntaxException | ParseException ex) { - throw new Error(ex); - } - setDeviceConfig(Global.instance(), defaultDeviceConfig); - } - return device; - } - - public void setDeviceConfig(Context context, Meta config) { - Meta mspConfig = null; - if (config.hasMeta("device")) { - for (Meta d : config.getMetaList("device")) { - if (d.getString("type", "unknown").equals(MSP_DEVICE_TYPE) - && d.getString("name", "msp").equals(this.mspName)) { - mspConfig = d; - } - } - } else if (config.hasMeta("peakJump")) { - mspConfig = config; - } - - if (mspConfig != null) { - this.device = new MspDevice(); - device.setName(mspName); - device.setContext(context); - device.configure(mspConfig); - - try { - getDevice().setListener(this); - getDevice().init(); - } catch (ControlException ex) { - showError(String.format("Can't connect to %s:%d. The port is either busy or not the MKS mass-spectrometer port", - device.meta().getString("connection.ip", "127.0.0.1"), - device.meta().getInt("connection.port", 10014))); - throw new RuntimeException("Can't connect to device"); - } - } else { - showError("Can't find device description in given confgiuration"); - throw new RuntimeException(); - } - - if (config.hasMeta("plots.msp")) { - setViewConfig(config.getMeta("plots.msp")); - } - + @Override + public void open(MspDevice device) throws Exception { + super.open(device); + getDevice().setMspListener(this); + device.getConfig().optMeta("plot").ifPresent(this::setViewConfig); updatePlot(); } - public void setDeviceConfig(Context context, File cfgFile) { - try { - Meta deviceConfig = MetaFileReader.instance().read(context, cfgFile, null); - setDeviceConfig(context, deviceConfig); - } catch (IOException | ParseException ex) { - showError("Can't load configuration file"); - } - } +// public void setDeviceConfig(Context context, File cfgFile) { +// try { +// Meta deviceConfig = MetaFileReader.instance().read(context, cfgFile, null); +// setDeviceConfig(context, deviceConfig); +// } catch (IOException | ParseException ex) { +// showError("Can't load configuration file"); +// } +// } public void initPlot() { Meta plotConfig = new MetaBuilder("plotFrame") @@ -279,7 +225,7 @@ public class MspViewController implements Initializable, MspListener { val = Double.NaN; } TimePlottable pl = plottables.get(Integer.toString(entry.getKey())); - if(pl!= null){ + if (pl != null) { pl.put(Value.of(val)); } } @@ -310,7 +256,7 @@ public class MspViewController implements Initializable, MspListener { @FXML private void onAutoRangeChange(DragEvent event) { - plottables.setValue(TimePlottable.MAX_AGE_KEY, this.autoRangeSlider.getValue()*60000); + plottables.setValue(TimePlottable.MAX_AGE_KEY, this.autoRangeSlider.getValue() * 60000); } @FXML @@ -335,12 +281,8 @@ public class MspViewController implements Initializable, MspListener { } - public void shutdown() throws IOException, ControlException { - getDevice().shutdown(); - } - @Override - public void acceptFillamentStateChange(String fillamentState) { + public void acceptFilamentStateChange(String fillamentState) { Platform.runLater(() -> { switch (fillamentState) { case "ON": @@ -362,32 +304,32 @@ public class MspViewController implements Initializable, MspListener { private void onStoreButtonClick(ActionEvent event) { if (storeButton.isSelected()) { - if (!device.meta().hasMeta("storage")) { - device.getLogger().info("Storage not defined. Starting storage selection dialog"); + if (!getDevice().meta().hasMeta("storage")) { + getDevice().getLogger().info("Storage not defined. Starting storage selection dialog"); DirectoryChooser chooser = new DirectoryChooser(); File storageDir = chooser.showDialog(this.plotPane.getScene().getWindow()); if (storageDir == null) { storeButton.setSelected(false); throw new RuntimeException("User canceled directory selection"); } - device.getConfig().putNode(new MetaBuilder("storage") + getDevice().getConfig().putNode(new MetaBuilder("storage") .putValue("path", storageDir.getAbsolutePath())); } - Meta storageConfig = device.meta().getMeta("storage"); - Storage localStorage = StorageManager.buildFrom(device.getContext()) + Meta storageConfig = getDevice().meta().getMeta("storage"); + Storage localStorage = StorageManager.buildFrom(getDevice().getContext()) .buildStorage(storageConfig); - String runName = device.meta().getString("numass.run", ""); - Meta meta = device.meta(); + String runName = getDevice().meta().getString("numass.run", ""); + Meta meta = getDevice().meta(); if (meta.hasMeta("numass")) { try { - device.getLogger().info("Obtaining run information from cetral server..."); + getDevice().getLogger().info("Obtaining run information from cetral server..."); NumassClient client = new NumassClient(meta.getString("numass.ip", "192.168.111.1"), meta.getInt("numass.port", 8335)); runName = client.getCurrentRun().getString("path", ""); - device.getLogger().info("Run name is '{}'", runName); + getDevice().getLogger().info("Run name is '{}'", runName); } catch (Exception ex) { - device.getLogger().warn("Failed to download current run information", ex); + getDevice().getLogger().warn("Failed to download current run information", ex); } } @@ -395,15 +337,39 @@ public class MspViewController implements Initializable, MspListener { try { localStorage = localStorage.buildShelf(runName, null); } catch (StorageException ex) { - device.getLogger().error("Failed to create storage shelf. Using root storage instead"); + getDevice().getLogger().error("Failed to create storage shelf. Using root storage instead"); } } connection = new StorageConnection(localStorage); - device.connect(connection, Roles.STORAGE_ROLE); + getDevice().connect(connection, Roles.STORAGE_ROLE); } else if (connection != null) { - device.disconnect(connection); + getDevice().disconnect(connection); } } + @Override + public void notifyDeviceInitialized(Device device) { + + } + + @Override + public void notifyDeviceShutdown(Device device) { + + } + + @Override + public void notifyDeviceStateChanged(Device device, String name, Value state) { + + } + + @Override + public void notifyDeviceConfigChanged(Device device) { + + } + + @Override + public void evaluateDeviceException(Device device, String message, Throwable exception) { + + } } diff --git a/numass-control/msp/src/main/resources/config/msp-config.xml b/numass-control/msp/src/main/resources/config/msp-config.xml index 4e92c734..e92dc598 100644 --- a/numass-control/msp/src/main/resources/config/msp-config.xml +++ b/numass-control/msp/src/main/resources/config/msp-config.xml @@ -1,19 +1,19 @@ - + @@ -30,15 +30,13 @@ limitations under the License. - - - + - - + + \ No newline at end of file diff --git a/numass-control/src/main/java/inr/numass/control/NumassConnections.java b/numass-control/src/main/java/inr/numass/control/NumassConnections.java new file mode 100644 index 00000000..ad6a7f88 --- /dev/null +++ b/numass-control/src/main/java/inr/numass/control/NumassConnections.java @@ -0,0 +1,40 @@ +package inr.numass.control; + +import hep.dataforge.control.connections.Roles; +import hep.dataforge.control.connections.StorageConnection; +import hep.dataforge.control.devices.Device; +import hep.dataforge.exceptions.StorageException; +import hep.dataforge.meta.Meta; +import hep.dataforge.storage.api.Storage; +import hep.dataforge.storage.commons.StorageFactory; +import inr.numass.client.ClientUtils; + +/** + * Created by darksnake on 08-May-17. + */ +public class NumassConnections { + + /** + * Create a single or multiple storage connections for a device + * @param device + * @param config + */ + public static void connectStorage(Device device, Meta config) { + //TODO add on reset listener + if (config.hasMeta("storage")) { + String numassRun = ClientUtils.getRunName(config); + config.getMetaList("storage").forEach(node -> { + Storage storage = StorageFactory.buildStorage(device.getContext(), node); + if (!numassRun.isEmpty()) { + try { + storage = storage.buildShelf(numassRun, Meta.empty()); + } catch (StorageException e) { + device.getContext().getLogger().error("Failed to build shelf", e); + } + } + device.connect(new StorageConnection(storage), Roles.STORAGE_ROLE); + }); + } + } + +} diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/app/ReadVac.java b/numass-control/vac/src/main/java/inr/numass/readvac/app/ReadVac.java index f79fcc91..f7e152a9 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/app/ReadVac.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/app/ReadVac.java @@ -9,6 +9,7 @@ import hep.dataforge.control.measurements.Sensor; import hep.dataforge.exceptions.StorageException; import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.Meta; +import hep.dataforge.meta.MetaBuilder; import hep.dataforge.storage.api.PointLoader; import hep.dataforge.storage.api.Storage; import hep.dataforge.storage.commons.LoaderFactory; @@ -27,7 +28,6 @@ import org.slf4j.LoggerFactory; import java.io.File; /** - * * @author Alexander Nozik */ public class ReadVac extends Application { @@ -56,20 +56,25 @@ public class ReadVac extends Application { config = Meta.empty(); } - Sensor p1 = new MKSVacDevice(config.getString("p1.port", "com::/dev/ttyUSB0")); - p1.configure(config.getMeta("p1", Meta.empty())); + Sensor p1 = new MKSVacDevice(); + p1.configure(config.getMeta("p1", + () -> new MetaBuilder("p1").setValue("port", "com::/dev/ttyUSB0"))); p1.setName(config.getString("p1.name", "P1")); - Sensor p2 = new CM32Device(config.getString("p2.port", "tcp::192.168.111.32:4002")); - p2.configure(config.getMeta("p2", Meta.empty())); + Sensor p2 = new CM32Device(); + p2.configure(config.getMeta("p2", + () -> new MetaBuilder("p2").setValue("port", "tcp::192.168.111.32:4002"))); p2.setName(config.getString("p2.name", "P2")); - Sensor p3 = new CM32Device(config.getString("p3.port", "tcp::192.168.111.32:4003")); - p3.configure(config.getMeta("p3", Meta.empty())); + Sensor p3 = new CM32Device(); + p3.configure(config.getMeta("p3", + () -> new MetaBuilder("p3").setValue("port", "tcp::192.168.111.32:4003"))); p3.setName(config.getString("p3.name", "P3")); - Sensor px = new VITVacDevice(config.getString("px.port", "com::/dev/ttyUSB1")); - px.configure(config.getMeta("px", Meta.empty())); + Sensor px = new VITVacDevice(); + px.configure(config.getMeta("px", + () -> new MetaBuilder("px").setValue("port", "tcp::192.168.111.32:4003"))); px.setName(config.getString("px.name", "Px")); - Sensor baratron = new MKSBaratronDevice(config.getString("baratron.port", "tcp::192.168.111.33:4004")); - baratron.configure(config.getMeta("baratron", Meta.empty())); + Sensor baratron = new MKSBaratronDevice(); + baratron.configure(config.getMeta("baratron", + () -> new MetaBuilder("baratron").setValue("port", "tcp::192.168.111.33:4004"))); baratron.setName(config.getString("baratron.name", "Baratron")); VacCollectorDevice collector = new VacCollectorDevice(); diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/CM32Device.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/CM32Device.java index 83308a18..67d3c607 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/CM32Device.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/CM32Device.java @@ -23,10 +23,6 @@ import hep.dataforge.exceptions.ControlException; @ValueDef(name = "timeout") public class CM32Device extends PortSensor { - public CM32Device(String portName) { - super(portName); - } - @Override protected PortHandler buildHandler(String portName) throws ControlException { getLogger().info("Connecting to port {}", portName); diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java index 4bcf1949..aa878e9c 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSBaratronDevice.java @@ -6,7 +6,6 @@ package inr.numass.readvac.devices; import hep.dataforge.control.devices.PortSensor; -import static hep.dataforge.control.devices.PortSensor.CONNECTION_STATE; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.SimpleMeasurement; import hep.dataforge.control.ports.PortHandler; @@ -20,9 +19,6 @@ import hep.dataforge.exceptions.ControlException; @ValueDef(name = "channel") public class MKSBaratronDevice extends PortSensor { - public MKSBaratronDevice(String portName) { - super(portName); - } @Override protected Measurement createMeasurement() { diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java index 036952d1..0c8a2800 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/MKSVacDevice.java @@ -26,11 +26,6 @@ import java.util.regex.Pattern; @ValueDef(name = "powerButton", type = "BOOLEAN", def = "true") public class MKSVacDevice extends PortSensor { - public MKSVacDevice(String portName) { - super(portName); - super.getConfig().setValue("powerButton", true); - } - private String talk(String requestContent) throws ControlException { String answer = getHandler().sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout()); diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/VITVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/VITVacDevice.java index 69c0c310..b6ff7482 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/VITVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/VITVacDevice.java @@ -22,10 +22,6 @@ import java.util.regex.Pattern; */ public class VITVacDevice extends PortSensor { - public VITVacDevice(String portName) { - super(portName); - } - @Override protected PortHandler buildHandler(String portName) throws ControlException { PortHandler newHandler = super.buildHandler(portName); 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 c0bb4889..ba32cbcf 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 @@ -128,7 +128,7 @@ public class VacCollectorDevice extends Sensor { @Override protected synchronized void result(DataPoint result, Instant time) { super.result(result, time); - forEachTypedConnection(Roles.STORAGE_ROLE, PointListener.class, (PointListener listener) -> { + forEachConnection(Roles.STORAGE_ROLE, PointListener.class, (PointListener listener) -> { listener.accept(result); }); } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/fx/PoweredVacuumeterView.java b/numass-control/vac/src/main/java/inr/numass/readvac/fx/PoweredVacuumeterView.java index a8c1df1a..604877a7 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/fx/PoweredVacuumeterView.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/fx/PoweredVacuumeterView.java @@ -5,7 +5,6 @@ */ package inr.numass.readvac.fx; -import hep.dataforge.exceptions.ControlException; import hep.dataforge.values.Value; import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; @@ -16,19 +15,16 @@ import org.controlsfx.control.ToggleSwitch; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; /** - * * @author Alexander Nozik */ public class PoweredVacuumeterView extends VacuumeterView { @FXML ToggleSwitch powerSwitch; - - + + @Override public Node getComponent() { if (node == null) { @@ -41,19 +37,15 @@ public class PoweredVacuumeterView extends VacuumeterView { } } return node; - } + } @Override public void initialize(URL location, ResourceBundle resources) { unitLabel.setText(getDevice().meta().getString("units", "mbar")); deviceNameLabel.setText(getDevice().getName()); powerSwitch.selectedProperty().addListener((ObservableValue observable, Boolean oldValue, Boolean newValue) -> { - try { - getDevice().command("setPower", Value.of(newValue)); - } catch (ControlException ex) { - Logger.getLogger(PoweredVacuumeterView.class.getName()).log(Level.SEVERE, null, ex); - } + getDevice().setState("power", Value.of(newValue)); }); } - + } 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 a46528b8..54692bee 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 @@ -5,8 +5,10 @@ */ package inr.numass.readvac.fx; -import hep.dataforge.control.connections.DeviceViewController; +import hep.dataforge.control.connections.DeviceConnection; +import hep.dataforge.control.connections.MeasurementConsumer; 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.meta.Meta; @@ -35,7 +37,7 @@ import java.util.ResourceBundle; /** * @author Alexander Nozik */ -public class VacuumeterView extends DeviceViewController implements MeasurementListener, Initializable, Named, Metoid { +public class VacuumeterView extends DeviceConnection implements DeviceListener, MeasurementConsumer, MeasurementListener, Initializable, Named, Metoid { private static final DecimalFormat FORMAT = new DecimalFormat("0.###E0"); private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_TIME;