diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java index 2cb83980..3c98ad93 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java @@ -46,7 +46,7 @@ public class Talk { portName = args[0]; } PortHandler handler; - handler = PortFactory.buildPort(portName); + handler = PortFactory.getdPort(portName); handler.setPhraseCondition((String str) -> str.endsWith("\r")); // MagnetController controller = new MagnetController(handler, 1); diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/VirtualLambdaPort.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/VirtualLambdaPort.java index 4f93457c..f26043d2 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/VirtualLambdaPort.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/VirtualLambdaPort.java @@ -33,21 +33,27 @@ public class VirtualLambdaPort extends VirtualPort { private volatile int currentAddress = -1; private Map magnets = new HashMap<>(); + private final String virtualPortName; public VirtualLambdaPort(String portName, Map magnets) { - super(portName); + this.virtualPortName = portName; magnets.entrySet().stream().forEach((entry) -> { this.magnets.put(entry.getKey(), new VirtualMagnetStatus(entry.getValue())); }); } public VirtualLambdaPort(String portName, int... magnets) { - super(portName); + this.virtualPortName = portName; for (int magnet : magnets) { this.magnets.put(magnet, new VirtualMagnetStatus(0.01)); } } + @Override + public String getPortId() { + return virtualPortName; + } + @Override protected void evaluateRequest(String request) { String comand; diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java index 9312a269..891b61f2 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java @@ -71,7 +71,7 @@ public class MagnetControllerApp extends Application { if(portName.equals("virtual")){ handler = new VirtualLambdaPort("COM12", 1, 2, 3, 4); } else { - handler = PortFactory.buildPort(portName); + handler = PortFactory.getdPort(portName); //TODO add meta reader here } diff --git a/numass-control/msp/build.gradle b/numass-control/msp/build.gradle index 05e67f04..d856ec57 100644 --- a/numass-control/msp/build.gradle +++ b/numass-control/msp/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'application' -version = "0.2.2" +version = "0.2.3" if (!hasProperty('mainClass')) { ext.mainClass = 'inr.numass.control.msp.fx.MspApp' 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 a2207ff5..cdd18e20 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 @@ -71,7 +71,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po String ip = meta().getString("connection.ip", "127.0.0.1"); int port = meta().getInt("connection.port", 10014); getLogger().info("Connection to MKS mass-spectrometer on {}:{}...", ip, port); - handler = new TcpPortHandler(ip, port, "msp"); + handler = new TcpPortHandler(ip, port); handler.setDelimeter("\r\r"); handler.holdBy(this); setConnected(true); @@ -125,22 +125,20 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po } @Override - protected boolean applyState(String stateName, Value stateValue) throws ControlException { - switch (stateName) { - case "connected": - return setConnected(stateValue.booleanValue()); - case "filamentOn": - return setFileamentOn(stateValue.booleanValue()); + public void command(String commandName, Value argument) throws ControlException { + switch(commandName){ + case "connect": + setConnected(argument.booleanValue()); + case "setFilamentOn": + setFileamentOn(argument.booleanValue()); default: - return super.applyState(stateName, stateValue); + super.command(commandName, argument); } } /** * Startup MSP: get available sensors, select sensor and control. * - * @param measurement - * @throws hep.dataforge.exceptions.PortException */ public boolean setConnected(boolean connected) throws ControlException { String sensorName; @@ -217,8 +215,8 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po } /** - * Send specific command and wait for its results (the onResult must begin - * with command name) + * Send specific command and wait for its results (the result must begin + with command name) * * @param commandName * @param paremeters diff --git a/numass-control/vac/build.gradle b/numass-control/vac/build.gradle index 27375ddc..621360bf 100644 --- a/numass-control/vac/build.gradle +++ b/numass-control/vac/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'application' -version = "0.4.0-SNAPSHOT" +version = "0.4.0" if (!hasProperty('mainClass')) { ext.mainClass = 'inr.numass.readvac.Main' @@ -9,10 +9,7 @@ mainClassName = mainClass dependencies { - compile 'ch.qos.logback:logback-classic:1.1.0+' - compile 'org.scream3r:jssc:2.8.0' - compile 'commons-cli:commons-cli:1.3' + compile project(':numass-storage:numass-client') compile project(':dataforge-plots') - compile project(':dataforge-storage') compile project(':dataforge-control') } \ No newline at end of file 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 d51ecb36..541cf922 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 @@ -6,12 +6,15 @@ package inr.numass.readvac.app; import hep.dataforge.control.measurements.Sensor; +import hep.dataforge.io.MetaFileReader; +import hep.dataforge.meta.Meta; import inr.numass.readvac.devices.CM32Device; import inr.numass.readvac.devices.MKSBaratronDevice; import inr.numass.readvac.devices.MKSVacDevice; import inr.numass.readvac.devices.VITVacDevice; import inr.numass.readvac.devices.VacCollectorDevice; import inr.numass.readvac.fx.VacCollectorController; +import java.io.File; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -27,23 +30,39 @@ public class ReadVac extends Application { @Override public void start(Stage primaryStage) throws Exception { - Sensor p1 = new MKSVacDevice("com::/dev/ttyUSB0"); - p1.setName("P1"); + String configFileName = getParameters().getNamed().get("config"); + if (configFileName == null) { + configFileName = "vac-config.xml"; + } + File configFile = new File(configFileName); + Meta config; + if (configFile.exists()) { + config = MetaFileReader.read(configFile).build(); + } else { + config = Meta.empty(); + } + + Sensor p1 = new MKSVacDevice(config.getString("p1.port","com::/dev/ttyUSB0")); + p1.configure(config.getNode("p1",Meta.empty())); + p1.setName(config.getString("p1.name","P1")); p1.getConfig().putValue("powerButton", true); - Sensor p2 = new CM32Device("tcp::192.168.111.32:4002"); - p2.setName("P2"); - Sensor p3 = new CM32Device("tcp::192.168.111.32:4003"); - p3.setName("P3"); - Sensor px = new VITVacDevice("com::/dev/ttyUSB1"); - px.setName("Px"); - Sensor baratron = new MKSBaratronDevice("tcp::192.168.111.33:4004"); - baratron.setName("Baratron"); + Sensor p2 = new CM32Device(config.getString("p2.port","tcp::192.168.111.32:4002")); + p1.configure(config.getNode("p2",Meta.empty())); + p2.setName(config.getString("p2.name","P2")); + Sensor p3 = new CM32Device(config.getString("p3.port","tcp::192.168.111.32:4003")); + p1.configure(config.getNode("p3",Meta.empty())); + p3.setName(config.getString("p3.name","P3")); + Sensor px = new VITVacDevice(config.getString("px.port","com::/dev/ttyUSB1")); + p1.configure(config.getNode("px",Meta.empty())); + px.setName(config.getString("px.name","Px")); + Sensor baratron = new MKSBaratronDevice(config.getString("baratron.port","tcp::192.168.111.33:4004")); + baratron.setName(config.getString("baratron.name","Baratron")); + p1.configure(config.getNode("baratron",Meta.empty())); VacCollectorDevice collector = new VacCollectorDevice(); collector.setSensors(p1, p2, p3, px, baratron); -// collector.setSensors(baratron); collector.init(); - + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/VacCollector.fxml")); loader.load(); controller = loader.getController(); 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 e48fa372..06773828 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 @@ -5,8 +5,8 @@ */ package inr.numass.readvac.devices; +import hep.dataforge.control.devices.PortSensor; import hep.dataforge.control.measurements.Measurement; -import hep.dataforge.control.measurements.Sensor; import hep.dataforge.control.measurements.SimpleMeasurement; import hep.dataforge.control.ports.ComPortHandler; import hep.dataforge.control.ports.PortHandler; @@ -20,7 +20,7 @@ import hep.dataforge.exceptions.ControlException; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class CM32Device extends NumassVacDevice { +public class CM32Device extends PortSensor { public CM32Device(String portName) { super(portName); @@ -75,19 +75,19 @@ public class CM32Device extends NumassVacDevice { if (answer.isEmpty()) { this.onProgressUpdate("No signal"); - updateState("connection", false); + updateState(CONNECTION_STATE, false); return null; } else if (answer.indexOf("PM1:mbar") < -1) { this.onProgressUpdate("Wrong answer: " + answer); - updateState("connection", false); + updateState(CONNECTION_STATE, false); return null; } else if (answer.substring(14, 17).equals("OFF")) { this.onProgressUpdate("Off"); - updateState("connection", true); + updateState(CONNECTION_STATE, true); return null; } else { this.onProgressUpdate("OK"); - updateState("connection", true); + updateState(CONNECTION_STATE, true); return Double.parseDouble(answer.substring(14, 17) + answer.substring(19, 23)); } } 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 861d93a6..8040e648 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 @@ -5,6 +5,7 @@ */ package inr.numass.readvac.devices; +import hep.dataforge.control.devices.PortSensor; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.Sensor; import hep.dataforge.control.measurements.SimpleMeasurement; @@ -19,12 +20,8 @@ import java.util.regex.Pattern; * * @author Alexander Nozik */ -@ValueDef(name = "address") @ValueDef(name = "channel") -@ValueDef(name = "port") -@ValueDef(name = "delay") -@ValueDef(name = "timeout") -public class MKSBaratronDevice extends NumassVacDevice { +public class MKSBaratronDevice extends PortSensor { public MKSBaratronDevice(String portName) { super(portName); 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 74bc3cc2..6ab50903 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 @@ -5,9 +5,9 @@ */ package inr.numass.readvac.devices; +import hep.dataforge.control.devices.PortSensor; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.SimpleMeasurement; -import hep.dataforge.control.ports.ComPortHandler; import hep.dataforge.control.ports.PortHandler; import hep.dataforge.description.ValueDef; import hep.dataforge.exceptions.ControlException; @@ -21,12 +21,9 @@ import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder; * * @author Alexander Nozik */ -@ValueDef(name = "address") -@ValueDef(name = "channel") -@ValueDef(name = "port") -@ValueDef(name = "delay") -@ValueDef(name = "timeout") -public class MKSVacDevice extends NumassVacDevice { +@ValueDef(name = "address", def = "253") +@ValueDef(name = "channel", def = "5") +public class MKSVacDevice extends PortSensor { public MKSVacDevice(String portName) { super(portName); @@ -78,14 +75,12 @@ public class MKSVacDevice extends NumassVacDevice { } @Override - protected boolean applyState(String stateName, Value stateValue) throws ControlException { - switch (stateName) { - case "power": - boolean powerOn = stateValue.booleanValue(); - setPowerOn(powerOn); - return powerOn == isPowerOn(); - default: - return super.applyState(stateName, stateValue); + public void command(String commandName, Value argument) throws ControlException { + if (commandName.equals("setPower")) { + boolean powerOn = argument.booleanValue(); + setPowerOn(powerOn); + } else { + super.command(commandName, argument); } } @@ -102,14 +97,14 @@ public class MKSVacDevice extends NumassVacDevice { // } String ans = talk("FP!ON"); if (ans.equals("ON")) { - setState("power", true); + updateState("power", true); } else { this.notifyError("Failed to set power state", null); } } else { String ans = talk("FP!OFF"); if (ans.equals("OFF")) { - setState("power", false); + updateState("power", false); } else { this.notifyError("Failed to set power state", null); } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/devices/NumassVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/devices/NumassVacDevice.java deleted file mode 100644 index 3461f094..00000000 --- a/numass-control/vac/src/main/java/inr/numass/readvac/devices/NumassVacDevice.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package inr.numass.readvac.devices; - -import hep.dataforge.control.measurements.Sensor; -import hep.dataforge.control.ports.PortFactory; -import hep.dataforge.control.ports.PortHandler; -import hep.dataforge.exceptions.ControlException; - -/** - * - * @author darksnake - */ -public abstract class NumassVacDevice extends Sensor { - - private PortHandler handler; - private final String portName; - - public NumassVacDevice(String portName) { - this.portName = portName; - } - - - protected final void setHandler(PortHandler handler) { - this.handler = handler; - } - - public boolean isConnected() { - return getState("connection").booleanValue(); - } - - protected int timeout() { - return meta().getInt("timeout", 400); - } - - protected PortHandler buildHandler(String portName) throws ControlException { - getLogger().info("Connecting to port {}", portName); - return PortFactory.buildPort(portName); - } - - @Override - public void shutdown() throws ControlException { - super.shutdown(); - try { - handler.close(); - } catch (Exception ex) { - throw new ControlException(ex); - } - } - - /** - * @return the handler - * @throws hep.dataforge.exceptions.ControlException - */ - protected PortHandler getHandler() throws ControlException { - if (handler == null) { - String port = meta().getString("port", portName); - this.handler = buildHandler(port); - } - - if (!handler.isOpen()) { - handler.open(); - } - - return handler; - } - -} 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 74dfb17d..cc600fdd 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 @@ -5,12 +5,10 @@ */ package inr.numass.readvac.devices; +import hep.dataforge.control.devices.PortSensor; import hep.dataforge.control.measurements.Measurement; -import hep.dataforge.control.measurements.Sensor; import hep.dataforge.control.measurements.SimpleMeasurement; -import hep.dataforge.control.ports.ComPortHandler; import hep.dataforge.control.ports.PortHandler; -import hep.dataforge.description.ValueDef; import hep.dataforge.exceptions.ControlException; import java.math.BigDecimal; import java.math.RoundingMode; @@ -21,10 +19,7 @@ import java.util.regex.Pattern; * * @author Alexander Nozik */ -@ValueDef(name = "port") -@ValueDef(name = "delay") -@ValueDef(name = "timeout") -public class VITVacDevice extends NumassVacDevice { +public class VITVacDevice extends PortSensor { public VITVacDevice(String portName) { super(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 161d0877..22982172 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 @@ -7,13 +7,17 @@ package inr.numass.readvac.devices; import hep.dataforge.control.collectors.PointCollector; import hep.dataforge.control.collectors.ValueCollector; +import hep.dataforge.control.connections.Roles; +import hep.dataforge.control.devices.annotations.RoleDef; import hep.dataforge.control.measurements.AbstractMeasurement; import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.Sensor; import hep.dataforge.points.DataPoint; import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.MeasurementException; +import hep.dataforge.points.PointListener; import hep.dataforge.values.Value; +import java.time.Instant; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; @@ -27,6 +31,7 @@ import java.util.concurrent.TimeUnit; * * @author Alexander Nozik */ +@RoleDef(name = Roles.STORAGE_ROLE, objectType = PointListener.class, info = "Storage for acquired points") public class VacCollectorDevice extends Sensor { private Map sensorMap = new HashMap<>(); @@ -38,6 +43,13 @@ public class VacCollectorDevice extends Sensor { } } + public void setSensors(Iterable sensors) { + sensorMap = new LinkedHashMap<>(); + for (Sensor sensor : sensors) { + sensorMap.put(sensor.getName(), sensor); + } + } + @Override protected Object calculateState(String stateName) throws ControlException { //TODO add dot path notation for states @@ -68,10 +80,8 @@ public class VacCollectorDevice extends Sensor { super.shutdown(); for (Sensor sensor : getSensors()) { sensor.shutdown(); - } + } } - - public Collection getSensors() { return sensorMap.values(); @@ -79,7 +89,7 @@ public class VacCollectorDevice extends Sensor { private class VacuumMeasurement extends AbstractMeasurement { - private final ValueCollector collector = new PointCollector(this::onResult, sensorMap.keySet()); + private final ValueCollector collector = new PointCollector(this::result, sensorMap.keySet()); private ScheduledExecutorService executor; private ScheduledFuture currentTask; @@ -100,6 +110,14 @@ public class VacCollectorDevice extends Sensor { }, 0, meta().getInt("delay", 5000), TimeUnit.MILLISECONDS); } + @Override + protected synchronized void result(DataPoint result, Instant time) { + super.result(result, time); + forEachTypedConnection(Roles.STORAGE_ROLE, PointListener.class, (PointListener listener) -> { + listener.accept(result); + }); + } + @Override public boolean stop(boolean force) { boolean isRunning = currentTask != null; 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 4f277bb4..db63587b 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,9 +5,13 @@ */ package inr.numass.readvac.fx; +import hep.dataforge.exceptions.ControlException; +import hep.dataforge.values.Value; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; +import java.util.logging.Level; +import java.util.logging.Logger; import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -43,7 +47,11 @@ public class PoweredVacuumeterView extends VacuumeterView { unitLabel.setText(getDevice().meta().getString("units", "mbar")); deviceNameLabel.setText(getDevice().getName()); powerSwitch.selectedProperty().addListener((ObservableValue observable, Boolean oldValue, Boolean newValue) -> { - getDevice().setState("power", newValue); + try { + getDevice().command("setPower", Value.of(newValue)); + } catch (ControlException ex) { + Logger.getLogger(PoweredVacuumeterView.class.getName()).log(Level.SEVERE, null, ex); + } }); } 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 7af7333d..e7b2f9eb 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 @@ -5,6 +5,7 @@ */ package inr.numass.readvac.fx; +import hep.dataforge.control.connections.Connection; import hep.dataforge.control.connections.Roles; import hep.dataforge.control.devices.Device; import hep.dataforge.control.devices.DeviceListener; @@ -14,6 +15,7 @@ import hep.dataforge.control.measurements.Sensor; import hep.dataforge.points.DataPoint; import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.MeasurementException; +import hep.dataforge.exceptions.StorageException; import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaBuilder; import hep.dataforge.plots.PlotFrame; @@ -21,13 +23,24 @@ import hep.dataforge.plots.data.DynamicPlottable; import hep.dataforge.plots.data.DynamicPlottableSet; import hep.dataforge.plots.fx.PlotContainer; import hep.dataforge.plots.jfreechart.JFreeChartFrame; +import hep.dataforge.points.FormatBuilder; +import hep.dataforge.points.PointListener; +import hep.dataforge.storage.api.PointLoader; +import hep.dataforge.storage.api.Storage; +import hep.dataforge.storage.commons.LoaderFactory; +import hep.dataforge.storage.commons.StorageManager; import hep.dataforge.values.Value; +import hep.dataforge.values.ValueType; import inr.numass.readvac.devices.VacCollectorDevice; import java.net.URL; import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; +import java.util.stream.Collectors; import javafx.application.Platform; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -41,6 +54,7 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.util.Duration; import org.controlsfx.control.Notifications; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** @@ -50,9 +64,15 @@ import org.slf4j.LoggerFactory; */ public class VacCollectorController implements Initializable, DeviceListener, MeasurementListener { + private final Logger logger = LoggerFactory.getLogger("ValCollector"); + + private final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + private final String[] intervalNames = {"1 sec", "5 sec", "10 sec", "30 sec", "1 min"}; private final int[] intervals = {1000, 5000, 10000, 30000, 60000}; + private LoaderConnection storageConnection; + private VacCollectorDevice device; private final List views = new ArrayList<>(); private PlotContainer plotContainer; @@ -68,6 +88,10 @@ public class VacCollectorController implements Initializable, DeviceListener, Me private ChoiceBox intervalSelector; @FXML private ToggleButton startStopButton; + @FXML + private ToggleButton storeButton; + @FXML + private ToggleButton logButton; /** * Initializes the controller class. @@ -112,7 +136,7 @@ public class VacCollectorController implements Initializable, DeviceListener, Me if (plottables != null) { plottables.put(result); } - Platform.runLater(() -> timeLabel.setText(time.toString())); + Platform.runLater(() -> timeLabel.setText(TIME_FORMAT.format(LocalDateTime.ofInstant(time, ZoneId.systemDefault())))); } private void setupView() { @@ -168,7 +192,7 @@ public class VacCollectorController implements Initializable, DeviceListener, Me getDevice().stopMeasurement(false); for (Sensor sensor : getDevice().getSensors()) { sensor.stopMeasurement(false); - } + } } catch (ControlException ex) { throw new RuntimeException(ex); } @@ -185,4 +209,63 @@ public class VacCollectorController implements Initializable, DeviceListener, Me } } + @FXML + private void onStoreToggle(ActionEvent event) { + if (storeButton.isSelected()) { + try { + Meta storageConfig = device.meta().getNode("storage"); + Storage storage = StorageManager.buildFrom(device.getContext()) + .buildStorage(storageConfig); + storageConnection = new LoaderConnection(storage, device.meta().getString("storage.shelf", "")); + device.connect(storageConnection, Roles.STORAGE_ROLE); + } catch (Exception ex) { + logger.error("Failed to start data storing", ex); + storeButton.setSelected(false); + } + } else if (storageConnection != null) { + device.disconnect(storageConnection); + } + } + + @FXML + private void onLogToggle(ActionEvent event) { + } + + private class LoaderConnection implements PointListener, Connection { + + private final PointLoader loader; + + public LoaderConnection(Storage storage, String shelfName) throws StorageException { + this.loader = LoaderFactory.buildPointLoder(storage, "vactms", shelfName, "timestamp", + new FormatBuilder(device.getSensors().stream().map(sensor -> sensor.getName()).collect(Collectors.toList())) + .setFormat("timestamp", ValueType.TIME) + .build()); + } + + @Override + public void accept(DataPoint point) { + try { + loader.push(point); + } catch (StorageException ex) { + logger.error("Error while pushing data", ex); + } + } + + @Override + public boolean isOpen() { + return loader.isOpen(); + } + + @Override + public void open(Device object) throws Exception { + loader.open(); + } + + @Override + public void close() throws Exception { + loader.close(); + } + + } + } 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 a3c85aab..8ab03d73 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 @@ -17,6 +17,9 @@ import java.io.IOException; import java.net.URL; import java.text.DecimalFormat; import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ResourceBundle; import javafx.application.Platform; import javafx.fxml.FXML; @@ -33,6 +36,7 @@ import javafx.scene.paint.Color; public class VacuumeterView extends DeviceViewController implements MeasurementListener, Initializable, Named, Annotated { private static final DecimalFormat FORMAT = new DecimalFormat("0.##E0"); + private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_TIME; protected Node node; @@ -115,7 +119,7 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL String resString = FORMAT.format(result); Platform.runLater(() -> { valueLabel.setText(resString); - setStatus("OK: " + time.toString()); + setStatus("OK: " + TIME_FORMAT.format(LocalDateTime.ofInstant(time, ZoneId.systemDefault()))); }); } 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 b1621319..3b9833ee 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 @@ -6,7 +6,6 @@ package inr.numass.readvac.test; import hep.dataforge.control.measurements.Sensor; -import hep.dataforge.control.virtual.SensorFactory; import hep.dataforge.control.virtual.Virtual; import hep.dataforge.meta.MetaBuilder; import inr.numass.readvac.devices.VacCollectorDevice; @@ -31,29 +30,34 @@ public class TestVac extends Application { Sensor sensor1 = Virtual.randomDoubleSensor("vac1", Duration.ofMillis(200), 1e-5, 2e-6); Sensor sensor2 = Virtual.randomDoubleSensor("vac2", Duration.ofMillis(200), 2e-5, 2e-6); Sensor sensor3 = Virtual.randomDoubleSensor("vac3", Duration.ofMillis(200), 1e-7, 1e-8); - Sensor poweredSensor = new SensorFactory("vac4", (sensor) -> { - 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; - } - }) - .addState("power") - .setMeta(new MetaBuilder("device") - .setValue("color", "magenta") - .setValue("thickness", 3)) - .build(); +// Sensor poweredSensor = new VirtualSensorFactory( +// "vac4", +// (sensor) -> { +// if (sensor.getState("power").booleanValue()) { +// return 1e-6; +// } else { +// return null; +// } +// }) +// .addState("power") +// .setMeta(new MetaBuilder("device") +// .setValue("color", "magenta") +// .setValue("thickness", 3) +// .setValue("powerButton", true)) +// .addCommand("setPower", new BiConsumer, Value>() { +// @Override +// public void accept(Sensor sensor, Value power) { +// +// } +// }) +// .build(); VacCollectorDevice collector = new VacCollectorDevice(); - collector.setSensors(sensor1, sensor2, sensor3, poweredSensor); + collector.setSensors(sensor1, sensor2, sensor3); collector.init(); + collector.getConfig().putNode(new MetaBuilder("storage").putValue("path", "D:\\temp\\test").putValue("monitor", false)); + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/VacCollector.fxml")); loader.load(); controller = loader.getController(); @@ -64,7 +68,6 @@ public class TestVac extends Application { primaryStage.setTitle("Vacuum measurement test"); primaryStage.setScene(scene); primaryStage.show(); - controller.startMeasurement(); } catch (Exception ex) { throw new Error(ex); } diff --git a/numass-control/vac/src/main/resources/fxml/VacCollector.fxml b/numass-control/vac/src/main/resources/fxml/VacCollector.fxml index c148236c..dc1c7313 100644 --- a/numass-control/vac/src/main/resources/fxml/VacCollector.fxml +++ b/numass-control/vac/src/main/resources/fxml/VacCollector.fxml @@ -9,6 +9,7 @@ + @@ -29,16 +30,20 @@ + -