Retrying setting puck barcode reader

This commit is contained in:
gac-S_Changer
2018-12-17 15:20:08 +01:00
parent 8902314d59
commit 8d972ea853
2 changed files with 99 additions and 53 deletions

View File

@@ -24,6 +24,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptException;
@@ -150,10 +152,10 @@ public class Controller {
public void selectSample(Sample sample) {
getMainFrame().basePlatePanel.selectSample(sample);
if ((puckPanel != null) && puckPanel.isShowing()){
puckPanel.selectSample(sample);
if ((puckPanel != null) && puckPanel.isShowing()) {
puckPanel.selectSample(sample);
}
}
//public Panel getMainFrame() {
@@ -218,7 +220,7 @@ public class Controller {
} else if (state == State.Ready) {
refreshSamplesTable();
try {
currentMountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')");
currentMountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')");
Sample sample = basePlate.getSampleByName(currentMountedSample);
basePlate.resetLoadedSample();
sample.setLoaded(true);
@@ -227,13 +229,13 @@ public class Controller {
basePlate.resetLoadedSample();
}
getMainFrame().refresh();
}
}
void onTimer() {
try {
setPuckLoading(isPuckLoading());
setPuckLoading(getPuckLoading());
} catch (Exception ex) {
setPuckLoading(false);
Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex);
@@ -253,23 +255,21 @@ public class Controller {
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) { //Keep last value
String valStr = value.toString().trim();
if (puckLoading && isBarcodeReaderScanPucks()) {
if (isBarcodeReaderScanPucks()) {
onPuckBarcode(valStr);
} else {
getMainFrame().setSampleDatamatrix(valStr);
onSampleBarcode(valStr);
}
}
}
};
final DeviceListener barcodeReaderPuckListener = new DeviceAdapter() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) { //Keep last value
String valStr = value.toString().trim();
if (puckLoading) {
onPuckBarcode(valStr);
}
onPuckBarcode(valStr);
}
}
};
@@ -329,10 +329,10 @@ public class Controller {
public Puck getPuck(String name) {
return basePlate.getPuckByName(name);
}
public Sample getSample(String name) {
return basePlate.getSampleByName(name);
}
}
public BasePlate getBasePlate() {
return basePlate;
@@ -419,9 +419,9 @@ public class Controller {
} catch (Exception ex) {
return null;
}
}
}
public Boolean isPuckLoading() {
public Boolean getPuckLoading() {
try {
return getMainFrame().eval("is_puck_loading()", true).equals(true);
} catch (Exception ex) {
@@ -564,6 +564,10 @@ public class Controller {
getMainFrame().execute(statement, background, showReturn);
}
void execute(String statement, boolean background, boolean showReturn, boolean showException) {
getMainFrame().execute(statement, background, showReturn, showException);
}
////////////////////// Sample Info /////////////////////////////////
String samplesTableJson;
@@ -572,7 +576,7 @@ public class Controller {
//synchronized(samplesTableLock){
SwingUtilities.invokeLater(() -> {
try {
if (Context.getInstance().getState().isInitialized()){
if (Context.getInstance().getState().isInitialized()) {
String json = (String) Context.getInstance().evalLineBackground("get_samples_info()");
if (!json.equals(samplesTableJson)) {
samplesTableJson = json;
@@ -590,7 +594,7 @@ public class Controller {
} catch (Exception ex) {
clearSamplesTable();
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
//}
}
@@ -603,42 +607,72 @@ public class Controller {
////////////////////// Puck Loading /////////////////////////////////
Boolean puckLoading;
Puck.Detection[] currentDetection;
boolean puckReaderOk = true;
void setPuckLoading(Boolean value) {
if (value == null) {
value = false;
}
if (value != puckLoading) {
if ((dialogAskPuckDatamatrix!=null) && (dialogAskPuckDatamatrix.isShowing())){
if ((dialogAskPuckDatamatrix != null) && (dialogAskPuckDatamatrix.isShowing())) {
dialogAskPuckDatamatrix.setVisible(false);
}
}
puckLoading = value;
getMainFrame().setPuckDatamatrix(null);
Device reader = getPuckBarcodeReader();
if (reader!=null) {
String name = reader.getName();
if (puckLoading) {
execute(name + ".enable()", true);
execute(name + ".polling = 100", true);
currentDetection = basePlate.getDetection();
} else if (getState().isInitialized()) {
execute(name + ".polling = 0", true);
execute(name + ".disable()", true);
getMainFrame().setPuckDatamatrix(null);
if (reader != null) {
final String name = reader.getName();
BiFunction errorHandler =(BiFunction) (ret, ex) -> {
if (ex != null) {
if (puckReaderOk){
Logger.getLogger(Controller.class.getName()).warning("Communication failure: " + name);
}
puckLoading = null;
puckReaderOk = false;
} else {
if (!puckReaderOk){
Logger.getLogger(Controller.class.getName()).warning("Communication resumed: " + name);
}
puckReaderOk = true;
}
return ret;
};
try {
if (puckLoading) {
getMainFrame().evalAsync(name + ".enable(); " + name + ".polling = 100" , true).handle(errorHandler);
currentDetection = basePlate.getDetection();
} else if (getState().isInitialized()) {
getMainFrame().evalAsync(name + ".polling = 0; " + name + ".disable()", true).handle(errorHandler);
getMainFrame().setPuckDatamatrix(null);
}
} catch (Exception ex) {
errorHandler.apply(null, ex);
}
}
}
}
public boolean isPuckLoading() {
if (puckLoading == null) {
return false;
}
return puckLoading;
}
void onPuckBarcode(String datamatrix) {
if (puckLoading) {
if (isPuckLoading()) {
getMainFrame().setPuckDatamatrix(datamatrix);
System.out.println(datamatrix);
System.out.println("Detected Puck: " + datamatrix);
}
}
void onSampleBarcode(String datamatrix) {
getMainFrame().setSampleDatamatrix(datamatrix);
System.out.println("Detected Sample: " + datamatrix);
}
void onPuckDetectionChanged() {
if (puckLoading) {
if (isPuckLoading()) {
String datamatrix = getMainFrame().getPuckDatamatrix();
Puck.Detection[] detection = basePlate.getDetection();
for (int i = 0; i < Controller.NUMBER_OF_PUCKS; i++) {
@@ -707,10 +741,8 @@ public class Controller {
}
}
public void askPuckDatamatrix(Puck puck) {
if ((dialogAskPuckDatamatrix!=null) && (dialogAskPuckDatamatrix.isShowing())){
if ((dialogAskPuckDatamatrix != null) && (dialogAskPuckDatamatrix.isShowing())) {
dialogAskPuckDatamatrix.setVisible(false);
}
if (puck == null) {
@@ -740,10 +772,10 @@ public class Controller {
table.setFont(table.getFont().deriveFont(28.0f));
table.setRowHeight(40);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
scrollPane.setViewportView(table);
JPanel panel = new JPanel();
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JButton cancel = new JButton("Cancel");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(cancel, BorderLayout.WEST);
@@ -751,24 +783,24 @@ public class Controller {
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.add(buttonPanel, BorderLayout.SOUTH);
ok.setPreferredSize(new Dimension(200, ok.getPreferredSize().height));
cancel.setPreferredSize(new Dimension(200, ok.getPreferredSize().height));
dialogAskPuckDatamatrix = SwingUtils.showDialog(getMainFrame().getTopLevel(), "Puck Loading", new Dimension(400,600), panel);
cancel.addActionListener((ev)->{
dialogAskPuckDatamatrix = SwingUtils.showDialog(getMainFrame().getTopLevel(), "Puck Loading", new Dimension(400, 600), panel);
cancel.addActionListener((ev) -> {
dialogAskPuckDatamatrix.setVisible(false);
});
ok.addActionListener((ev)->{
});
ok.addActionListener((ev) -> {
int row = table.getSelectedRow();
String dm = (row>=0) ? String.valueOf(table.getValueAt(row, 0)) : null;
String dm = (row >= 0) ? String.valueOf(table.getValueAt(row, 0)) : null;
dialogAskPuckDatamatrix.setVisible(false);
if (dm!=null){
if (dm != null) {
linkPuckDatamatrix(puck, dm);
}
});
}
}
}