From ac7577a43011475f3200b4c88290bbb229281a63 Mon Sep 17 00:00:00 2001 From: darksnake Date: Tue, 2 Feb 2016 17:00:01 +0300 Subject: [PATCH] [no commit message] --- .../numass/readvac/devices/CM32Device.java | 20 +-- .../numass/readvac/devices/MKSVacDevice.java | 24 +--- .../numass/readvac/devices/VITVacDevice.java | 124 ++++++++++++++++++ .../readvac/devices/VacCollectorDevice.java | 81 ++++++++++++ .../plotfit/PlotFitResultAction.java | 51 ++++--- 5 files changed, 241 insertions(+), 59 deletions(-) create mode 100644 numass-control/vac/src/main/java/inr/numass/readvac/devices/VITVacDevice.java create mode 100644 numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java 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 b8f044e1..9c6dd1be 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 @@ -6,16 +6,14 @@ package inr.numass.readvac.devices; import hep.dataforge.context.Context; -import hep.dataforge.control.measurements.RegularMeasurement; -import hep.dataforge.control.measurements.SingleMeasurementDevice; +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 hep.dataforge.meta.Meta; -import hep.dataforge.values.Value; -import java.time.Duration; -import java.time.temporal.ChronoUnit; /** * @@ -24,7 +22,7 @@ import java.time.temporal.ChronoUnit; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class CM32Device extends SingleMeasurementDevice> { +public class CM32Device extends Sensor { private PortHandler handler; @@ -47,7 +45,7 @@ public class CM32Device extends SingleMeasurementDevice createMeasurement() { + protected Measurement createMeasurement() { return new CMVacMeasurement(); } @@ -83,7 +81,7 @@ public class CM32Device extends SingleMeasurementDevice { + private class CMVacMeasurement extends SimpleMeasurement { private static final String CM32_QUERY = "MES R PM 1\r\n"; @@ -110,12 +108,6 @@ public class CM32Device extends SingleMeasurementDevice> { +public class MKSVacDevice extends Sensor { // private static final String DELIMETER = ";FF"; private PortHandler handler; @@ -60,7 +59,7 @@ public class MKSVacDevice extends SingleMeasurementDevice createMeasurement() { + protected Measurement createMeasurement() { return new MKSVacMeasurement(); } @@ -172,7 +171,7 @@ public class MKSVacDevice extends SingleMeasurementDevice { + private class MKSVacMeasurement extends SimpleMeasurement { @Override protected Double doMeasurement() throws Exception { @@ -196,16 +195,5 @@ public class MKSVacDevice extends SingleMeasurementDevice { + + private PortHandler handler; + + public VITVacDevice(String name, Context context, Meta meta) { + super(name, context, meta); + } + + /** + * @return the handler + */ + private PortHandler getHandler() throws ControlException { + if (handler == null || !handler.isOpen()) { + String port = meta().getString("port"); + getLogger().info("Connecting to port {}", port); + handler = new ComPortHandler(port, 2400, 8, 1, 0); + handler.setDelimeter("\r\n"); + handler.open(); + } + return handler; + } + + @Override + protected Measurement createMeasurement() { + return new CMVacMeasurement(); + } + + @Override + public String type() { + return meta().getString("type", "Vit vacuumeter"); + } + + @Override + protected Object calculateState(String stateName) throws ControlException { + if (getHandler() == null) { + notifyError("No port connection", null); + return null; + } + + notifyError("State not found: " + stateName, null); + return null; + //TODO add connection check here +// switch (stateName) { +// case "connection": +// return !talk("T?").isEmpty(); +// default: +// notifyError("State not found: " + stateName, null); +// return null; +// } + } + + public boolean isConnected() { + return getState("connection").booleanValue(); + } + + private int timeout() { + return meta().getInt("timeout", 400); + } + + private class CMVacMeasurement extends SimpleMeasurement { + + private static final String VIT_QUERY = ":010300000002FA\r\n"; + + @Override + protected Double doMeasurement() throws Exception { + + String answer = handler.sendAndWait(VIT_QUERY, timeout()); + + if (answer.isEmpty()) { + this.progressUpdate("No signal"); + updateState("connection", false); + return null; + } else { + Matcher match = Pattern.compile(":010304(\\w{4})(\\w{4})..\r\n").matcher(answer); + + if (match.matches()) { + double base = (double) (Integer.parseInt(match.group(1), 16)) / 10d; + int exp = Integer.parseInt(match.group(2), 16); + if (exp > 32766) { + exp = exp - 65536; + } + BigDecimal res = BigDecimal.valueOf(base * Math.pow(10, exp)); + res = res.setScale(4, RoundingMode.CEILING); + this.progressUpdate("OK"); + updateState("connection", true); + return res.doubleValue(); + } else { + this.progressUpdate("Wrong answer: " + answer); + updateState("connection", false); + return null; + } + } + } + } + +} 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 new file mode 100644 index 00000000..adbfaf20 --- /dev/null +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/VacCollectorDevice.java @@ -0,0 +1,81 @@ +/* + * 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.context.Context; +import hep.dataforge.control.collectors.PointCollector; +import hep.dataforge.control.collectors.ValueCollector; +import hep.dataforge.control.measurements.AbstractMeasurement; +import hep.dataforge.control.measurements.Measurement; +import hep.dataforge.control.measurements.Sensor; +import hep.dataforge.data.DataPoint; +import hep.dataforge.exceptions.ControlException; +import hep.dataforge.meta.Meta; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; +import javafx.util.Pair; + +/** + * + * @author Alexander Nozik + */ +public class VacCollectorDevice extends Sensor { + + private final Map> sensorMap; + + public VacCollectorDevice(String name, Context context, Meta meta, Sensor... sensors) { + super(name, context, meta); + sensorMap = new HashMap<>(sensors.length); + for (Sensor sensor : sensors) { + sensorMap.put(sensor.getName(), sensor); + } + //TODO add automatic construction from meta using deviceManager + } + + @Override + protected Object calculateState(String stateName) throws ControlException { + //TODO add dot path notation for states + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + protected Measurement createMeasurement() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public String type() { + return "Numass vacuum"; + } + + private class VacuumMeasurement extends AbstractMeasurement { + + ValueCollector collector = new PointCollector(this::result, sensorMap.keySet()); + + @Override + protected Pair doGet() throws Exception { + sensorMap.values().stream().parallel().forEach(action); + } + + @Override + public boolean isFinished() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void start() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean stop(boolean force) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + } + +} diff --git a/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java b/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java index 10c55898..8a7075c3 100644 --- a/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java +++ b/numass-main/src/main/java/hep/dataforge/plotfit/PlotFitResultAction.java @@ -16,24 +16,21 @@ package hep.dataforge.plotfit; import hep.dataforge.actions.OneToOneAction; -import hep.dataforge.meta.Meta; import hep.dataforge.context.Context; -import hep.dataforge.data.DataPoint; import hep.dataforge.data.DataSet; import hep.dataforge.data.XYDataAdapter; import hep.dataforge.datafitter.FitState; import hep.dataforge.datafitter.models.XYModel; import hep.dataforge.description.NodeDef; -import hep.dataforge.description.ValueDef; import hep.dataforge.description.TypedActionDef; +import hep.dataforge.description.ValueDef; import hep.dataforge.exceptions.ContentException; import hep.dataforge.io.log.Logable; +import hep.dataforge.meta.Meta; import hep.dataforge.plots.PlotsPlugin; import hep.dataforge.plots.XYPlotFrame; import hep.dataforge.plots.data.PlottableData; import hep.dataforge.plots.data.PlottableFunction; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.math3.analysis.UnivariateFunction; /** @@ -75,32 +72,32 @@ public class PlotFitResultAction extends OneToOneAction { metaData.getNode("plot", null)); //JFreeChartFrame.drawFrame(reader.getString("plotTitle", "Fit result plot for "+input.getName()), null); - double[] x = new double[data.size()]; +// double[] x = new double[data.size()]; // double[] y = new double[data.size()]; - double xMin = Double.POSITIVE_INFINITY; +// double xMin = Double.POSITIVE_INFINITY; +// +// double xMax = Double.NEGATIVE_INFINITY; +// +// List points = new ArrayList<>(); +// +// for (int i = 0; i < data.size(); i++) { +// x[i] = adapter.getX(data.get(i)).doubleValue(); +//// y[i] = adapter.getY(data.get(i)); +// +// points.add(adapter.mapToDefault(data.get(i))); +// if (x[i] < xMin) { +// xMin = x[i]; +// } +// +// if (x[i] > xMax) { +// xMax = x[i]; +// } +// } - double xMax = Double.NEGATIVE_INFINITY; + frame.add(new PlottableFunction("fit", null, function, data, adapter));//FIXME replace by helper - List points = new ArrayList<>(); - - for (int i = 0; i < data.size(); i++) { - x[i] = adapter.getX(data.get(i)).doubleValue(); -// y[i] = adapter.getY(data.get(i)); - - points.add(adapter.mapToDefault(data.get(i))); - if (x[i] < xMin) { - xMin = x[i]; - } - - if (x[i] > xMax) { - xMax = x[i]; - } - } - - frame.add(new PlottableFunction("fit", null, function, points, "x")); - - frame.add(PlottableData.plot("data", adapter, points)); + frame.add(PlottableData.plot("data", adapter, data)); return input; }