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 new file mode 100644 index 00000000..d51ecb36 --- /dev/null +++ b/numass-control/vac/src/main/java/inr/numass/readvac/app/ReadVac.java @@ -0,0 +1,76 @@ +/* + * 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.app; + +import hep.dataforge.control.measurements.Sensor; +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 javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +/** + * + * @author Alexander Nozik + */ +public class ReadVac extends Application { + + VacCollectorController controller; + + @Override + public void start(Stage primaryStage) throws Exception { + Sensor p1 = new MKSVacDevice("com::/dev/ttyUSB0"); + p1.setName("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"); + + 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(); + controller.setDevice(collector); + + Scene scene = new Scene(loader.getRoot(), 800, 600); + + primaryStage.setTitle("Numass vacuum measurements"); + primaryStage.setScene(scene); + primaryStage.show(); + controller.startMeasurement(); + } + + @Override + public void stop() throws Exception { + if (controller != null) { + controller.stopMeasurement(); + controller.getDevice().shutdown(); + } + super.stop(); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + launch(args); + } + +} 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 72aa09c4..e48fa372 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 @@ -20,30 +20,18 @@ import hep.dataforge.exceptions.ControlException; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class CM32Device extends Sensor { +public class CM32Device extends NumassVacDevice { - private PortHandler handler; - -// public CM32Device(String name, Context context, Meta meta) { -// super(name, context, meta); -// } - - public void setHandler(PortHandler handler){ - this.handler = handler; + public CM32Device(String portName) { + super(portName); } - - /** - * @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("T--"); - handler.open(); - } - return handler; + + @Override + protected PortHandler buildHandler(String portName) throws ControlException { + String port = meta().getString("port", portName); + PortHandler newHandler = new ComPortHandler(port, 2400, 8, 1, 0); + newHandler.setDelimeter("T--"); + return newHandler; } @Override @@ -75,14 +63,7 @@ public class CM32Device extends Sensor { // } } - public boolean isConnected() { - return getState("connection").booleanValue(); - } - private int timeout() { - return meta().getInt("timeout", 400); - } - private class CMVacMeasurement extends SimpleMeasurement { private static final String CM32_QUERY = "MES R PM 1\r\n"; @@ -90,7 +71,7 @@ public class CM32Device extends Sensor { @Override protected synchronized Double doMeasure() throws Exception { - String answer = handler.sendAndWait(CM32_QUERY, timeout()); + String answer = getHandler().sendAndWait(CM32_QUERY, timeout()); if (answer.isEmpty()) { this.onProgressUpdate("No signal"); 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 43c8e6e8..861d93a6 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 @@ -24,16 +24,14 @@ import java.util.regex.Pattern; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class MKSBaratronDevice extends Sensor { +public class MKSBaratronDevice extends NumassVacDevice { - private PortHandler handler; - - public void setHandler(PortHandler handler) { - this.handler = handler; + public MKSBaratronDevice(String portName) { + super(portName); } private String talk(String request) throws ControlException { - String answer = getHandler().sendAndWait(String.format("@%s%s\r", getDeviceAddress(), request), timeout()); + String answer = getHandler().sendAndWait(String.format("%s\r", request), timeout()); Matcher match = Pattern.compile("(.*)\r").matcher(answer); if (match.matches()) { @@ -43,45 +41,23 @@ public class MKSBaratronDevice extends Sensor { } } - private String getDeviceAddress() { - //PENDING cache this? - return meta().getString("address", "253"); - } - - private int timeout() { - return meta().getInt("timeout", 400); - } - @Override protected Measurement createMeasurement() { return new BaratronMeasurement(); } - - public boolean isConnected() { - return getState("connection").booleanValue(); - } - @Override public String type() { return meta().getString("type", "MKS baratron"); } - /** - * @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 = PortFactory.buildPort(port); - handler = new ComPortHandler(port); - handler.setDelimeter("\r"); - handler.open(); - } + @Override + protected PortHandler buildHandler(String portName) throws ControlException { + PortHandler handler = super.buildHandler(portName); + handler.setDelimeter("\r"); return handler; } - + private int getChannel() { return meta().getInt("channel", 2); } 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 49041c74..74bc3cc2 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 @@ -6,7 +6,6 @@ package inr.numass.readvac.devices; 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; @@ -27,12 +26,10 @@ import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class MKSVacDevice extends Sensor { +public class MKSVacDevice extends NumassVacDevice { - private PortHandler handler; - - public void setHandler(PortHandler handler) { - this.handler = handler; + public MKSVacDevice(String portName) { + super(portName); } private String talk(String requestContent) throws ControlException { @@ -46,15 +43,18 @@ public class MKSVacDevice extends Sensor { } } + @Override + protected PortHandler buildHandler(String portName) throws ControlException { + PortHandler handler = super.buildHandler(portName); + handler.setDelimeter(";FF"); + return handler; + } + private String getDeviceAddress() { //PENDING cache this? return meta().getString("address", "253"); } - private int timeout() { - return meta().getInt("timeout", 400); - } - @Override protected Measurement createMeasurement() { return new MKSVacMeasurement(); @@ -89,28 +89,6 @@ public class MKSVacDevice extends Sensor { } } -// private Double readPressure(int channel) { -// try { -// String answer = talk("PR" + channel + "?"); -// if (answer == null || answer.isEmpty()) { -// invalidateState("connection"); -// return null; -// } -// double res = Double.parseDouble(answer); -// if (res <= 0) { -// return null; -// } else { -// return res; -// } -// } catch (ControlException ex) { -// invalidateState("connection"); -// return null; -// } -// } - public boolean isConnected() { - return getState("connection").booleanValue(); - } - public boolean isPowerOn() { return getState("power").booleanValue(); } @@ -153,21 +131,6 @@ public class MKSVacDevice extends Sensor { return meta().getString("type", "MKS vacuumeter"); } - /** - * @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 = PortFactory.buildPort(port); - handler = new ComPortHandler(port); - handler.setDelimeter(";FF"); - handler.open(); - } - return handler; - } - private int getChannel() { return meta().getInt("channel", 5); } 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 new file mode 100644 index 00000000..3461f094 --- /dev/null +++ b/numass-control/vac/src/main/java/inr/numass/readvac/devices/NumassVacDevice.java @@ -0,0 +1,71 @@ +/* + * 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 f961669a..74dfb17d 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 @@ -24,30 +24,17 @@ import java.util.regex.Pattern; @ValueDef(name = "port") @ValueDef(name = "delay") @ValueDef(name = "timeout") -public class VITVacDevice extends Sensor { +public class VITVacDevice extends NumassVacDevice { - private PortHandler handler; - -// public VITVacDevice(String name, Context context, Meta meta) { -// super(name, context, meta); -// } - - public void setHandler(PortHandler handler) { - this.handler = handler; + public VITVacDevice(String portName) { + super(portName); } - /** - * @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 PortHandler buildHandler(String portName) throws ControlException { + PortHandler newHandler = super.buildHandler(portName); + newHandler.setDelimeter("\r\n"); + return newHandler; } @Override @@ -79,13 +66,6 @@ public class VITVacDevice extends Sensor { // } } - public boolean isConnected() { - return getState("connection").booleanValue(); - } - - private int timeout() { - return meta().getInt("timeout", 400); - } private class CMVacMeasurement extends SimpleMeasurement { @@ -94,7 +74,7 @@ public class VITVacDevice extends Sensor { @Override protected synchronized Double doMeasure() throws Exception { - String answer = handler.sendAndWait(VIT_QUERY, timeout()); + String answer = getHandler().sendAndWait(VIT_QUERY, timeout()); if (answer.isEmpty()) { this.onProgressUpdate("No signal"); 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 1fa4669a..7af7333d 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 @@ -145,7 +145,7 @@ public class VacCollectorController implements Initializable, DeviceListener, Me this.device = device; device.getSensors().stream().map((sensor) -> { VacuumeterView controller; - if (sensor.hasState("power")) { + if (sensor.meta().getBoolean("powerButton", false)) { controller = new PoweredVacuumeterView(); } else { controller = new VacuumeterView();