BakeoutController-Basic  0.1
A Controller for the Omicron vacuum chamber
ResultController.java
1 package ui.controllers;
2 
4 import javafx.application.Platform;
5 import javafx.concurrent.Task;
6 import javafx.fxml.FXML;
7 import javafx.scene.chart.LineChart;
8 import javafx.scene.chart.XYChart;
9 import javafx.scene.text.Text;
10 import kernel.Kernel;
11 import kernel.views.variables.*;
12 import org.jetbrains.annotations.NotNull;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import ui.Controller;
17 
18 import java.text.SimpleDateFormat;
19 import java.time.DateTimeException;
20 import java.time.Duration;
21 import java.util.concurrent.Executor;
22 
26 @Controller
27 public class ResultController implements Executor {
28 
32  private static final Logger log = LoggerFactory.getLogger
33  (ResultController.class);
34 
39  private static final Duration pressurePollingInterval =
40  Duration.ofMillis(1000);
41 
45  private static final Duration voltagePollingInterval =
46  Duration.ofMillis(1000);
47 
48  private static final Integer maximumNumberOfDataPointsInCharts = 30;
49 
53  @Autowired private Kernel kernel;
54 
58  @FXML private volatile Text reportedPressure;
59 
63  @FXML private volatile Text reportedVoltage;
64 
68  @FXML private LineChart<String, Float> pressureChart;
69 
73  @FXML private LineChart<String, Float> voltageChart;
74 
78  private XYChart.Series<String, Float> pressureSeries;
79 
83  private XYChart.Series<String, Float> voltageSeries;
84 
89  @FXML public void initialize(){
90  configurePressureSeries();
91  if (kernel.getVariableProvidersView().hasPressureProvider()){
92  log.info("Found pressure provider. Reporting");
93  configurePressureProvider();
94  }
95 
96  configureVoltageSeries();
97  if (kernel.getVariableProvidersView().hasVoltageProvider()){
98  log.info("Found voltage provider. Reporting");
99  configureVoltageProvider();
100  }
101  }
102 
106  @FXML
108  if (kernel.getVariableProvidersView().hasPressureProvider()){
109  log.info("Found pressure provider. Using this for reports");
110  configurePressureProvider();
111  }
112  if (kernel.getVariableProvidersView().hasVoltageProvider()){
113  log.info("Found voltage provider. Using this for reports");
114  configureVoltageProvider();
115  }
116  }
117 
122  @Override
123  public void execute(@NotNull Runnable command){
124  Platform.runLater(command);
125  }
126 
130  private void configureVoltageProvider(){
131  VariableProvider<Voltage> provider = kernel.getVariableProvidersView()
132  .getVoltageProvider();
133 
134  try {
135  provider.setPollingInterval(voltagePollingInterval);
136  } catch (NegativeDurationException error){
137  error.printStackTrace();
138  }
139 
140  provider.addOnChangeListener(new VoltageChangeHandler(this));
141  }
142 
146  private void configurePressureProvider(){
147  VariableProvider<Pressure> provider = kernel
148  .getVariableProvidersView().getPressureProvider();
149 
150  try {
151  provider.setPollingInterval(pressurePollingInterval);
152  } catch (NegativeDurationException error){
153  error.printStackTrace();
154  }
155 
156  provider.addOnChangeListener(new PressureChangeHandler(this));
157  }
158 
162  private void configureVoltageSeries(){
163  voltageSeries = new XYChart.Series<>();
164  voltageSeries.setName("Voltage / V");
165 
166  voltageChart.getData().add(voltageSeries);
167  }
168 
172  private void configurePressureSeries(){
173  pressureSeries = new XYChart.Series<>();
174  pressureSeries.setName("Pressure / mBar");
175 
176  pressureChart.getData().add(pressureSeries);
177  }
178 
183  private class VoltageChangeHandler implements
184  VariableChangeEventListener<Voltage> {
185 
190  private Executor taskExecutor;
191 
196  public VoltageChangeHandler(Executor taskExecutor){
197  this.taskExecutor = taskExecutor;
198  }
199 
203  @Override
204  public void onChange(Voltage newVoltage){
205  if (voltageChart.isVisible() && reportedVoltage.isVisible()) {
206  Task<Void> task = new UpdateVoltageTask(newVoltage);
207  this.taskExecutor.execute(task);
208  }
209  }
210  }
211 
215  private class PressureChangeHandler implements
216  VariableChangeEventListener<Pressure> {
217 
221  private Executor taskExecutor;
222 
226  public PressureChangeHandler(Executor taskExecutor){
227  this.taskExecutor = taskExecutor;
228  }
229 
234  @Override
235  public void onChange(Pressure newPressure){
236  if (pressureChart.isVisible() && reportedPressure.isVisible()) {
237  Task<Void> task = new UpdatePressureTask(newPressure);
238  this.taskExecutor.execute(task);
239  }
240  }
241  }
242 
246  private class UpdatePressureTask extends Task<Void> {
250  private Pressure newPressure;
251 
255  public UpdatePressureTask(Pressure newPressure){
256  this.newPressure = newPressure;
257  }
258 
263  @Override
264  public Void call(){
265  updatePressureText();
266  updatePressureChart();
267  return null;
268  }
269 
273  private void updatePressureText(){
274  reportedPressure.setText(newPressure.getValue().toString());
275  }
276 
282  private void updatePressureChart() throws DateTimeException {
283  String xValue = getDateFromPressure();
284  Float yValue = newPressure.getValue();
285  XYChart.Data<String, Float> dataPoint = new XYChart.Data<>(
286  xValue, yValue);
287 
288  pressureSeries.getData().add(dataPoint);
289  ResultController.checkSeriesSizeCorrect(pressureSeries);
290  }
291 
297  @NotNull
298  private String getDateFromPressure() throws DateTimeException {
299  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
300  return formatter.format(newPressure.getDate());
301  }
302  }
303 
307  private class UpdateVoltageTask extends Task<Void> {
311  private Voltage newVoltage;
312 
316  public UpdateVoltageTask(Voltage newVoltage){
317  this.newVoltage = newVoltage;
318  }
319 
324  @Override
325  public Void call(){
326  updateVoltageText();
327  updateVoltageChart();
328  return null;
329  }
330 
334  private void updateVoltageText(){
335  reportedVoltage.setText(newVoltage.getValue().toString());
336  }
337 
341  private void updateVoltageChart(){
342  String xValue = getDateFromVoltage();
343  Float yValue = newVoltage.getValue().floatValue();
344 
345  XYChart.Data<String, Float> dataPoint = new XYChart.Data<>(
346  xValue, yValue
347  );
348 
349  voltageSeries.getData().add(dataPoint);
350  ResultController.checkSeriesSizeCorrect(voltageSeries);
351  }
352 
356  @NotNull
357  private String getDateFromVoltage(){
358  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
359  return formatter.format(newVoltage.getDate());
360  }
361  }
362 
369  private static void checkSeriesSizeCorrect(XYChart.Series series){
370  Integer dataPointNumber = series.getData().size();
371 
372  if (dataPointNumber > maximumNumberOfDataPointsInCharts){
373  series.getData().remove(0);
374  }
375  }
376 }
void setPollingInterval(Duration pollingInterval)
void addOnChangeListener(VariableChangeEventListener< T > listener)
void execute(@NotNull Runnable command)
Git Repo