From 2c58a16faca1d85775568ad9eefcf8a67e51ef76 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 18 May 2017 12:53:21 +0300 Subject: [PATCH] vacuum fix --- numass-control/build.gradle | 2 +- .../inr/numass/control/msp/MspDevice.java | 11 +++-- .../control/NumassControlApplication.java | 4 +- .../numass/control/NumassControlUtils.java | 7 ++- .../java/inr/numass/readvac/CM32Device.java | 31 ++----------- .../inr/numass/readvac/MKSBaratronDevice.java | 4 +- .../java/inr/numass/readvac/MKSVacDevice.java | 22 +++------ .../inr/numass/readvac/MeradatVacDevice.java | 6 +-- .../numass/readvac/VacCollectorDevice.java | 46 ++++++++++--------- .../inr/numass/readvac/fx/VacuumeterView.java | 16 ++++--- .../vac/src/main/resources/config/devices.xml | 4 +- .../main/resources/fxml/PoweredVacBox.fxml | 19 ++------ 12 files changed, 71 insertions(+), 101 deletions(-) diff --git a/numass-control/build.gradle b/numass-control/build.gradle index bb526c88..f8584abf 100644 --- a/numass-control/build.gradle +++ b/numass-control/build.gradle @@ -37,7 +37,7 @@ task installAll(type: Copy) { } } if (!configRoot.children().isEmpty()) { - File outFile = file("$buildDir/install/numass-control/config/devices.xml") + File outFile = file("$buildDir/install/numass-control/bin/numass-control.xml") outFile.getParentFile().mkdirs(); outFile.createNewFile(); new XmlNodePrinter(outFile.newPrintWriter()).print(configRoot) 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 8bbc46be..95b02d13 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 @@ -19,6 +19,7 @@ import hep.dataforge.context.Context; import hep.dataforge.control.RoleDef; import hep.dataforge.control.connections.Roles; import hep.dataforge.control.connections.StorageConnection; +import hep.dataforge.control.devices.PortSensor; import hep.dataforge.control.devices.SingleMeasurementDevice; import hep.dataforge.control.devices.StateDef; import hep.dataforge.control.measurements.AbstractMeasurement; @@ -51,7 +52,7 @@ import java.util.function.Consumer; */ @RoleDef(name = Roles.STORAGE_ROLE, objectType = StorageConnection.class) @RoleDef(name = Roles.VIEW_ROLE) -@StateDef(name = "connected", writable = true, info = "Connection with the device itself") +@StateDef(name = PortSensor.CONNECTED_STATE, writable = true, info = "Connection with the device itself") @StateDef(name = "storing", writable = true, info = "Define if this device is currently writes to storage") @StateDef(name = "filamentOn", writable = true, info = "Mass-spectrometer filament on") @StateDef(name = "filamentStatus", info = "Filament status") @@ -134,7 +135,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po case "storing": return false; default: - throw new ControlException("State not defined"); + return super.computeState(stateName); } } @@ -146,7 +147,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po @Override protected void requestStateChange(String stateName, Value value) throws ControlException { switch (stateName) { - case "connected": + case PortSensor.CONNECTED_STATE: setConnected(value.booleanValue()); case "filamentOn": setFileamentOn(value.booleanValue()); @@ -195,7 +196,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po return false; } // connected = true; - updateState("connected", true); + updateState(PortSensor.CONNECTED_STATE, true); return true; } else { handler.unholdBy(this); @@ -267,7 +268,7 @@ public class MspDevice extends SingleMeasurementDevice implements PortHandler.Po } public boolean isConnected() { - return getState("connected").booleanValue(); + return getState(PortSensor.CONNECTED_STATE).booleanValue(); } public boolean isSelected() { diff --git a/numass-control/src/main/java/inr/numass/control/NumassControlApplication.java b/numass-control/src/main/java/inr/numass/control/NumassControlApplication.java index 16184d29..d3cd8751 100644 --- a/numass-control/src/main/java/inr/numass/control/NumassControlApplication.java +++ b/numass-control/src/main/java/inr/numass/control/NumassControlApplication.java @@ -62,12 +62,12 @@ public abstract class NumassControlApplication extends Applica .orElseGet(() -> NumassControlUtils.readResourceMeta("/config/devices.xml")); Context ctx = NumassControlUtils.setupContext(config); - Meta mspConfig = NumassControlUtils.findDeviceMeta(config, this::acceptDevice) + Meta deviceConfig = NumassControlUtils.findDeviceMeta(config, this::acceptDevice) .orElseThrow(() -> new RuntimeException("Device configuration not found")); try { - D d = getDeviceFactory().build(ctx, mspConfig); + D d = getDeviceFactory().build(ctx, deviceConfig); d.init(); NumassControlUtils.connectStorage(d, config); Platform.runLater(() -> { diff --git a/numass-control/src/main/java/inr/numass/control/NumassControlUtils.java b/numass-control/src/main/java/inr/numass/control/NumassControlUtils.java index 8ccbc216..1b742bdf 100644 --- a/numass-control/src/main/java/inr/numass/control/NumassControlUtils.java +++ b/numass-control/src/main/java/inr/numass/control/NumassControlUtils.java @@ -14,6 +14,8 @@ import hep.dataforge.storage.commons.StorageFactory; import hep.dataforge.storage.commons.StorageManager; import inr.numass.client.ClientUtils; import javafx.application.Application; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; @@ -38,7 +40,7 @@ public class NumassControlUtils { if (config.hasMeta("storage") && device.acceptsRole(Roles.STORAGE_ROLE)) { String numassRun = ClientUtils.getRunName(config); config.getMetaList("storage").forEach(node -> { - device.getContext().getLogger().debug("Creating storage for device with meta: {}", node); + device.getContext().getLogger().info("Creating storage for device with meta: {}", node); //building storage in a separate thread new Thread(() -> { Storage storage = StorageFactory.buildStorage(device.getContext(), node); @@ -65,7 +67,9 @@ public class NumassControlUtils { public static Optional getConfig(Application app) { String configFileName = app.getParameters().getNamed().get("config"); + Logger logger = LoggerFactory.getLogger(app.getClass()); if (configFileName == null) { + logger.info("Configuration path not defined. Loading configuration from {}",DEFAULT_CONFIG_LOCATION); configFileName = DEFAULT_CONFIG_LOCATION; } File configFile = new File(configFileName); @@ -78,6 +82,7 @@ public class NumassControlUtils { throw new RuntimeException(e); } } else { + logger.warn("Configuration file not found"); return Optional.empty(); } } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/CM32Device.java b/numass-control/vac/src/main/java/inr/numass/readvac/CM32Device.java index 6b122b21..95c940f0 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/CM32Device.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/CM32Device.java @@ -54,29 +54,6 @@ public class CM32Device extends PortSensor { return meta().getString("type", "Leibold CM32"); } - // @Override -// protected int timeout() { -// return meta().getInt("timeout", 1000); -// } - @Override - protected Object computeState(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; -// } - } - private class CMVacMeasurement extends SimpleMeasurement { private static final String CM32_QUERY = "MES R PM 1\r\n"; @@ -88,19 +65,19 @@ public class CM32Device extends PortSensor { if (answer.isEmpty()) { this.progressUpdate("No signal"); - updateState(CONNECTION_STATE, false); + updateState(CONNECTED_STATE, false); return null; } else if (answer.indexOf("PM1:mbar") < -1) { this.progressUpdate("Wrong answer: " + answer); - updateState(CONNECTION_STATE, false); + updateState(CONNECTED_STATE, false); return null; } else if (answer.substring(14, 17).equals("OFF")) { this.progressUpdate("Off"); - updateState(CONNECTION_STATE, true); + updateState(CONNECTED_STATE, true); return null; } else { this.progressUpdate("OK"); - updateState(CONNECTION_STATE, true); + updateState(CONNECTED_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/MKSBaratronDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/MKSBaratronDevice.java index 39110336..3d865949 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/MKSBaratronDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/MKSBaratronDevice.java @@ -57,11 +57,11 @@ public class MKSBaratronDevice extends PortSensor { String answer = getHandler().sendAndWait("AV" + getChannel() + "\r", timeout()); if (answer == null || answer.isEmpty()) { // invalidateState("connection"); - updateState(CONNECTION_STATE, false); + updateState(CONNECTED_STATE, false); this.progressUpdate("No connection"); return null; } else { - updateState(CONNECTION_STATE, true); + updateState(CONNECTED_STATE, true); } double res = Double.parseDouble(answer); if (res <= 0) { diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/MKSVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/MKSVacDevice.java index 1640c9e8..efa8c6ca 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/MKSVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/MKSVacDevice.java @@ -68,18 +68,11 @@ public class MKSVacDevice extends PortSensor { @Override protected Object computeState(String stateName) throws ControlException { - if (getHandler() == null) { - notifyError("No port connection", null); - return null; - } switch (stateName) { - case CONNECTION_STATE: - return !talk("T?").isEmpty(); case "power": return talk("FP?").equals("ON"); default: - notifyError("State not found: " + stateName, null); - return null; + return super.computeState(stateName); } } @@ -97,16 +90,18 @@ public class MKSVacDevice extends PortSensor { @Override public void shutdown() throws ControlException { - setPowerOn(false); + if (isConnected()) { + setPowerOn(false); + } super.shutdown(); } - public boolean isPowerOn() { + private boolean isPowerOn() { return getState("power").booleanValue(); } - public void setPowerOn(boolean powerOn) throws ControlException { + private void setPowerOn(boolean powerOn) throws ControlException { if (powerOn != isPowerOn()) { if (powerOn) { // String ans = talkMKS(p1Port, "@253ENC!OFF;FF"); @@ -155,7 +150,7 @@ public class MKSVacDevice extends PortSensor { // if (getState("power").booleanValue()) { String answer = talk("PR" + getChannel() + "?"); if (answer == null || answer.isEmpty()) { - invalidateState(CONNECTION_STATE); + updateState(CONNECTED_STATE, false); this.progressUpdate("No connection"); return null; } @@ -168,9 +163,6 @@ public class MKSVacDevice extends PortSensor { this.progressUpdate("OK"); return res; } -// } else { -// return null; -// } } } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java index 0b76afba..a2b087de 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/MeradatVacDevice.java @@ -70,7 +70,7 @@ public class MeradatVacDevice extends PortSensor { if (answer.isEmpty()) { this.progressUpdate("No signal"); - updateState("connection", false); + updateState(CONNECTED_STATE, false); return null; } else { Matcher match = response.matcher(answer); @@ -84,12 +84,12 @@ public class MeradatVacDevice extends PortSensor { BigDecimal res = BigDecimal.valueOf(base * Math.pow(10, exp)); res = res.setScale(4, RoundingMode.CEILING); this.progressUpdate("OK"); - updateState("connection", true); + updateState(CONNECTED_STATE, true); return res.doubleValue(); } else { this.progressUpdate("Wrong answer: " + answer); - updateState("connection", false); + updateState(CONNECTED_STATE, false); return null; } } diff --git a/numass-control/vac/src/main/java/inr/numass/readvac/VacCollectorDevice.java b/numass-control/vac/src/main/java/inr/numass/readvac/VacCollectorDevice.java index 49601d69..7cf11213 100644 --- a/numass-control/vac/src/main/java/inr/numass/readvac/VacCollectorDevice.java +++ b/numass-control/vac/src/main/java/inr/numass/readvac/VacCollectorDevice.java @@ -39,15 +39,18 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import static hep.dataforge.control.devices.PortSensor.CONNECTED_STATE; + /** * @author Alexander Nozik */ -@RoleDef(name = Roles.STORAGE_ROLE, objectType = PointListener.class, info = "Storage for acquired points") +@RoleDef(name = Roles.STORAGE_ROLE, objectType = StorageConnection.class, info = "Storage for acquired points") @StateDef(name = "storing", writable = true, info = "Define if this device is currently writes to storage") public class VacCollectorDevice extends Sensor { private Map> sensorMap = new LinkedHashMap<>(); private int delay = 5000; + private StorageHelper helper = new StorageHelper(VacCollectorDevice.this, this::buildLoader); public VacCollectorDevice() { } @@ -78,11 +81,7 @@ public class VacCollectorDevice extends Sensor { } } - @Override - protected Object computeState(String stateName) throws ControlException { - //TODO add dot path notation for states - return Value.NULL; - } + //TODO add dot path notation for states @Override protected Measurement createMeasurement() { @@ -96,7 +95,7 @@ public class VacCollectorDevice extends Sensor { } public void setDelay(int delay) throws MeasurementException { - this.delay = 5000; + this.delay = delay; if (isMeasuring()) { getMeasurement().stop(false); getMeasurement().start(); @@ -111,6 +110,21 @@ public class VacCollectorDevice extends Sensor { } } + private PointLoader buildLoader(StorageConnection connection) { + TableFormatBuilder format = new TableFormatBuilder().setType("timestamp", ValueType.TIME); + getSensors().forEach((s) -> { + format.setType(s.getName(), ValueType.NUMBER); + }); + + return LoaderFactory.buildPointLoder(connection.getStorage(), "vactms", "", "timestamp", format.build()); + } + + @Override + public void onMeasurementResult(Measurement measurement, DataPoint result, Instant time) { + super.onMeasurementResult(measurement, result, time); + helper.push(result); + } + public Collection> getSensors() { return sensorMap.values(); } @@ -120,21 +134,19 @@ public class VacCollectorDevice extends Sensor { private final ValueCollector collector = new PointCollector(this::result, sensorMap.keySet()); private ScheduledExecutorService executor; private ScheduledFuture currentTask; - private StorageHelper helper; @Override public void start() { - helper = new StorageHelper(VacCollectorDevice.this, this::buildLoader); executor = Executors .newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "VacuumMeasurement thread")); currentTask = executor.scheduleWithFixedDelay(() -> { sensorMap.values().forEach((sensor) -> { try { Object value; - if (sensor.optBooleanState("disabled").orElse(false)) { - value = null; - } else { + if (sensor.optBooleanState(CONNECTED_STATE).orElse(false)) { value = sensor.read(); + } else { + value = null; } collector.put(sensor.getName(), value); } catch (Exception ex) { @@ -144,6 +156,7 @@ public class VacCollectorDevice extends Sensor { }, 0, delay, TimeUnit.MILLISECONDS); } + @Override protected synchronized void result(DataPoint result, Instant time) { super.result(result, time); @@ -152,15 +165,6 @@ public class VacCollectorDevice extends Sensor { }); } - private PointLoader buildLoader(StorageConnection connection) { - TableFormatBuilder format = new TableFormatBuilder().setType("timestamp", ValueType.TIME); - getSensors().forEach((s) -> { - format.setType(s.getName(), ValueType.NUMBER); - }); - - return LoaderFactory.buildPointLoder(connection.getStorage(), "vactms", "", "timestamp", format.build()); - } - private DataPoint terminator() { MapPoint.Builder p = new MapPoint.Builder(); p.putValue("timestamp", DateTimeUtils.now()); 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 db656c63..a3d3da29 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 @@ -32,6 +32,8 @@ import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ResourceBundle; +import static hep.dataforge.control.devices.PortSensor.CONNECTED_STATE; + /** * @author Alexander Nozik */ @@ -40,14 +42,14 @@ public class VacuumeterView extends DeviceViewConnection> impleme private static final DecimalFormat FORMAT = new DecimalFormat("0.###E0"); private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_TIME; - protected Node node; + Node node; @FXML private BorderPane root; @FXML - protected Label deviceNameLabel; + Label deviceNameLabel; @FXML - protected Label unitLabel; + Label unitLabel; @FXML private Label valueLabel; @FXML @@ -59,7 +61,7 @@ public class VacuumeterView extends DeviceViewConnection> impleme @SuppressWarnings("unchecked") public void accept(Device device, String measurementName, Measurement measurement) { measurement.addListener(this); - getDevice().meta().optValue("color").ifPresent(colorValue ->valueLabel.setTextFill(Color.valueOf(colorValue.stringValue()))); + getDevice().meta().optValue("color").ifPresent(colorValue -> valueLabel.setTextFill(Color.valueOf(colorValue.stringValue()))); } @@ -86,9 +88,9 @@ public class VacuumeterView extends DeviceViewConnection> impleme Platform.runLater(() -> { unitLabel.setText(getDevice().meta().getString("units", "mbar")); deviceNameLabel.setText(getDevice().getName()); - disableButton.setSelected(!getDevice().optBooleanState("disabled").orElse(false)); + disableButton.setSelected(getDevice().optBooleanState(CONNECTED_STATE).orElse(false)); disableButton.selectedProperty().addListener((ObservableValue observable, Boolean oldValue, Boolean newValue) -> { - getDevice().setState("disabled",!newValue); + getDevice().setState(CONNECTED_STATE, newValue); if (!newValue) { valueLabel.setText("---"); } @@ -133,7 +135,7 @@ public class VacuumeterView extends DeviceViewConnection> impleme } - public String getTitle() { + String getTitle() { return getDevice().meta().getString("title", getDevice().getName()); } diff --git a/numass-control/vac/src/main/resources/config/devices.xml b/numass-control/vac/src/main/resources/config/devices.xml index 9f50f237..6363e0c0 100644 --- a/numass-control/vac/src/main/resources/config/devices.xml +++ b/numass-control/vac/src/main/resources/config/devices.xml @@ -1,12 +1,12 @@ + - - + diff --git a/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml b/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml index 8e4d1382..296ee9da 100644 --- a/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml +++ b/numass-control/vac/src/main/resources/fxml/PoweredVacBox.fxml @@ -1,22 +1,11 @@ - - - + - - - - - - - - - - - + + @@ -26,7 +15,7 @@