Fixed devices library and vacuum measurements

This commit is contained in:
Alexander Nozik 2016-02-14 22:05:13 +03:00
parent b74775efc4
commit c1ed9a9f37
5 changed files with 29 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -34,6 +35,7 @@ public class VacCollectorDevice extends Sensor<DataPoint> {
/** /**
* Sensors in reversed order * Sensors in reversed order
*
* @param name * @param name
* @param context * @param context
* @param meta * @param meta
@ -65,18 +67,20 @@ public class VacCollectorDevice extends Sensor<DataPoint> {
return "Numass vacuum"; return "Numass vacuum";
} }
public Collection<Sensor> getSensors(){ public Collection<Sensor> getSensors() {
return sensorMap.values(); return sensorMap.values();
} }
private class VacuumMeasurement extends AbstractMeasurement<DataPoint> { private class VacuumMeasurement extends AbstractMeasurement<DataPoint> {
private final ValueCollector collector = new PointCollector(this::onResult, sensorMap.keySet()); private final ValueCollector collector = new PointCollector(this::onResult, sensorMap.keySet());
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private ScheduledExecutorService executor;
private ScheduledFuture<?> currentTask; private ScheduledFuture<?> currentTask;
@Override @Override
public void start() { public void start() {
executor = Executors
.newSingleThreadScheduledExecutor((Runnable r) -> new Thread(r, "VacuumMeasurement thread"));
currentTask = executor.scheduleWithFixedDelay(() -> { currentTask = executor.scheduleWithFixedDelay(() -> {
sensorMap.entrySet().stream().parallel().forEach((entry) -> { sensorMap.entrySet().stream().parallel().forEach((entry) -> {
try { try {
@ -98,7 +102,9 @@ public class VacCollectorDevice extends Sensor<DataPoint> {
public boolean stop(boolean force) { public boolean stop(boolean force) {
boolean isRunning = currentTask != null; boolean isRunning = currentTask != null;
if (isRunning) { if (isRunning) {
getLogger().debug("Stoping vacuum collector measurement");
currentTask.cancel(force); currentTask.cancel(force);
executor.shutdown();
currentTask = null; currentTask = null;
onFinish(); onFinish();
} }

View File

@ -10,6 +10,7 @@ import hep.dataforge.control.devices.Device;
import hep.dataforge.control.devices.DeviceListener; import hep.dataforge.control.devices.DeviceListener;
import hep.dataforge.control.measurements.Measurement; import hep.dataforge.control.measurements.Measurement;
import hep.dataforge.control.measurements.MeasurementListener; import hep.dataforge.control.measurements.MeasurementListener;
import hep.dataforge.control.measurements.Sensor;
import hep.dataforge.data.DataPoint; import hep.dataforge.data.DataPoint;
import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.ControlException;
import hep.dataforge.exceptions.MeasurementException; import hep.dataforge.exceptions.MeasurementException;
@ -139,7 +140,10 @@ public class VacCollectorController implements Initializable, DeviceListener, Me
public void stopMeasurement() { public void stopMeasurement() {
try { try {
getDevice().stopMeasurement(true); getDevice().stopMeasurement(true);
} catch (MeasurementException ex) { for (Sensor sensor : getDevice().getSensors()) {
sensor.shutdown();
}
} catch (ControlException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }

View File

@ -24,6 +24,7 @@ import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import org.controlsfx.control.StatusBar; import org.controlsfx.control.StatusBar;
/** /**
@ -52,6 +53,9 @@ public class VacuumeterView extends DeviceViewController implements MeasurementL
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void accept(Device device, String measurementName, Measurement measurement) { public void accept(Device device, String measurementName, Measurement measurement) {
measurement.addListener(this); measurement.addListener(this);
if (device.meta().hasValue("color")) {
valueLabel.setTextFill(Color.valueOf(device.meta().getString("color")));
}
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import hep.dataforge.context.GlobalContext;
import hep.dataforge.control.measurements.Sensor; import hep.dataforge.control.measurements.Sensor;
import hep.dataforge.control.virtual.SensorFactory; import hep.dataforge.control.virtual.SensorFactory;
import hep.dataforge.control.virtual.Virtual; import hep.dataforge.control.virtual.Virtual;
import hep.dataforge.meta.MetaBuilder;
import inr.numass.readvac.devices.VacCollectorDevice; import inr.numass.readvac.devices.VacCollectorDevice;
import inr.numass.readvac.fx.VacCollectorController; import inr.numass.readvac.fx.VacCollectorController;
import java.time.Duration; import java.time.Duration;
@ -37,10 +38,15 @@ public class TestVac extends Application {
} else { } else {
return null; return null;
} }
}).addState("power").build(); })
.addState("power")
.setMeta(new MetaBuilder("device")
.setValue("color", "magenta")
.setValue("thickness", 3))
.build();
VacCollectorDevice collector = new VacCollectorDevice("collector", VacCollectorDevice collector = new VacCollectorDevice("collector",
GlobalContext.instance(), null, poweredSensor, sensor3, sensor2, sensor1); GlobalContext.instance(), null, sensor1, sensor2, sensor3, poweredSensor);
collector.init(); collector.init();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/VacCollector.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/VacCollector.fxml"));
@ -66,7 +72,6 @@ public class TestVac extends Application {
controller.getDevice().shutdown(); controller.getDevice().shutdown();
} }
super.stop(); super.stop();
System.exit(0);
} }
/** /**