[no commit message]

This commit is contained in:
Alexander Nozik 2016-02-01 12:32:53 +03:00
parent fbd6cb6844
commit af7cda92bd
2 changed files with 191 additions and 4 deletions

View File

@ -0,0 +1,121 @@
/*
* 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.measurements.RegularMeasurement;
import hep.dataforge.control.measurements.SingleMeasurementDevice;
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;
/**
*
* @author Alexander Nozik
*/
@ValueDef(name = "port")
@ValueDef(name = "delay")
@ValueDef(name = "timeout")
public class CM32Device extends SingleMeasurementDevice<RegularMeasurement<Double>> {
private PortHandler handler;
public CM32Device(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("T--");
handler.open();
}
return handler;
}
@Override
protected RegularMeasurement<Double> createMeasurement() {
return new CMVacMeasurement();
}
@Override
public String type() {
return meta().getString("type", "Leibold CM32");
}
@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 RegularMeasurement<Double> {
private static final String CM32_QUERY = "MES R PM 1\r\n";
@Override
protected Double doMeasurement() throws Exception {
String answer = handler.sendAndWait(CM32_QUERY, timeout());
if (answer.isEmpty()) {
this.progressUpdate("No signal");
updateState("connection", false);
return null;
} else if (answer.indexOf("PM1:mbar") < -1) {
this.progressUpdate("Wrong answer: " + answer);
updateState("connection", false);
return null;
} else if (answer.substring(14, 17).equals("OFF")) {
this.progressUpdate("Off");
updateState("connection", true);
return null;
} else {
this.progressUpdate("OK");
updateState("connection", true);
return Double.parseDouble(answer.substring(14, 17) + answer.substring(19, 23));
}
}
@Override
protected Duration getDelay() {
return Duration.of(meta().getInt("delay", 5000), ChronoUnit.MILLIS);
}
}
}

View File

@ -3,19 +3,23 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inr.numass.readvac;
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.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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder;
/**
*
@ -25,6 +29,7 @@ import java.util.regex.Pattern;
@ValueDef(name = "channel")
@ValueDef(name = "port")
@ValueDef(name = "delay")
@ValueDef(name = "timeout")
public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Double>> {
// private static final String DELIMETER = ";FF";
@ -35,7 +40,7 @@ public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Dou
}
private String talk(String requestContent) throws ControlException {
String answer = handler.sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout());
String answer = getHandler().sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout());
Matcher match = Pattern.compile("@253ACK(.*);FF").matcher(answer);
if (match.matches()) {
@ -61,7 +66,7 @@ public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Dou
@Override
protected Object calculateState(String stateName) throws ControlException {
if (handler == null) {
if (getHandler() == null) {
notifyError("No port connection", null);
return null;
}
@ -76,6 +81,18 @@ public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Dou
}
}
@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);
}
}
private Double readPressure(int channel) {
try {
String answer = talk("PR" + channel + "?");
@ -103,11 +120,58 @@ public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Dou
return getState("power").booleanValue();
}
public void setPowerOn(boolean powerOn) throws ControlException {
if (powerOn != isPowerOn()) {
if (powerOn) {
// String ans = talkMKS(p1Port, "@253ENC!OFF;FF");
// if (!ans.equals("OFF")) {
// LoggerFactory.getLogger(getClass()).warn("The @253ENC!OFF;FF command is not working");
// }
String ans = talk("FP!ON");
if (ans.equals("ON")) {
setState("power", true);
} else {
this.notifyError("Failed to set power state", null);
}
} else {
String ans = talk("FP!OFF");
if (ans.equals("OFF")) {
setState("power", false);
} else {
this.notifyError("Failed to set power state", null);
}
}
}
}
public BooleanProperty powerOnProperty() {
try {
return new JavaBeanBooleanPropertyBuilder().bean(this)
.name("powerOn").getter("isPowerOn").setter("setPowerOn").build();
} catch (NoSuchMethodException ex) {
throw new Error(ex);
}
}
@Override
public String type() {
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.open();
}
return handler;
}
private class MKSVacMeasurement extends RegularMeasurement<Double> {
@Override
@ -121,8 +185,10 @@ public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Dou
double res = Double.parseDouble(answer);
if (res <= 0) {
this.progressUpdate("Non positive");
invalidateState("power");
return null;
} else {
this.progressUpdate("OK");
return res;
}
}