diff --git a/src/main/java/ch/psi/mxsc/Controller.java b/src/main/java/ch/psi/mxsc/Controller.java index efbdc0e..fdfee91 100644 --- a/src/main/java/ch/psi/mxsc/Controller.java +++ b/src/main/java/ch/psi/mxsc/Controller.java @@ -16,7 +16,6 @@ import ch.psi.pshell.device.GenericDevice; import ch.psi.pshell.app.App; import ch.psi.pshell.framework.Panel; import ch.psi.pshell.framework.Setup; -import ch.psi.pshell.sequencer.Sequencer; import ch.psi.pshell.utils.Arr; import ch.psi.pshell.utils.Audio; import ch.psi.pshell.utils.Chrono; @@ -25,9 +24,7 @@ import ch.psi.pshell.swing.SwingUtils; import ch.psi.pshell.utils.State.StateException; import java.awt.BorderLayout; import java.awt.Dimension; -import java.awt.Frame; import java.awt.Point; -import java.awt.Rectangle; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; @@ -67,9 +64,7 @@ public class Controller { Device barcode_reader; Device barcode_reader_puck; PuckDetection puck_detection; - JDialog dialogAskPuckDatamatrix; - String currentMountedSample; - PuckLoadingDialog dialogPuckLoading; + String currentMountedSample; public static Controller getInstance() { return instance; @@ -132,6 +127,19 @@ public class Controller { } } + void setSinglePuckType(Puck.PuckType puckType) { + for (Puck p : basePlate.getPucks()) { + p.setPuckType(puckType); + } + } + + void resetPuckTypes() { + for (Puck p : basePlate.getPucks()) { + p.setPuckType(Puck.PuckType.Unknown); + } + } + + final PuckSensorAccess puckSensorAccess = PuckSensorAccess.RaspberryPi; static String PUCK_ESERA_DEVICE = "onewire"; @@ -251,27 +259,7 @@ public class Controller { clearSamplesTable(); refreshSamplesTable(); } - - public Sample getMountedSample() throws Exception{ - String mountedSample = (String) Context.getSequencer().evalLineBackground("get_setting('mounted_sample_position')"); - checkMountedSampleChange(currentMountedSample, mountedSample); - currentMountedSample = mountedSample; - Sample sample = basePlate.getSampleByName(currentMountedSample); - return sample; - } - - public void resetMountedSample() throws Exception{ - String mountedSample = (String) Context.getSequencer().evalLineBackground("get_setting('mounted_sample_position')"); - checkMountedSampleChange(currentMountedSample, null); - Context.getSequencer().evalLineBackground("set_setting('mounted_sample_position', None)"); - } - - void checkMountedSampleChange(String former, String current){ - if (((current == null) && (former!=null)) || ((current != null) && (!current.equals(former)))){ - logSampleMounted(former, current); - } - } - + public void onStateChange(State state, State former) { if (state == State.Initializing) { getMainFrame().removeDevice(basePlate); @@ -344,6 +332,7 @@ public class Controller { } } }; + void updateDevices() { barcode_reader = (Device) getDevice("barcode_reader"); @@ -366,7 +355,121 @@ public class Controller { } checkPuckDetectionEnabling(); } + + GenericDevice getDevice(String name) { + return getMainFrame().getDevice(name); + } + DevicePool getDevicePool() { + return Context.getDevicePool(); + } + + State getState() { + return getMainFrame().getState(); + } + + void execute(String statement) { + getMainFrame().execute(statement); + } + + void execute(String statement, boolean background) { + getMainFrame().execute(statement, background); + } + + void execute(String statement, boolean background, boolean showReturn) { + getMainFrame().execute(statement, background, showReturn); + } + + void execute(String statement, boolean background, boolean showReturn, boolean showException) { + getMainFrame().execute(statement, background, showReturn, showException); + } + + + ////////////////////// Puck image detection ///////////////////////////////// + + public void imageDetectPucks() throws StateException { + imageDetectPucks(null, null, null); + } + + public void imageDetectPucks(JComponent plot, JComponent renderer, JComponent text) throws StateException { + Map args = new HashMap(); + args.put("PLOT", plot); + args.put("RENDERER", renderer); + args.put("TEXT", text); + getMainFrame().runAsync("imgproc/LedDetectionProc", args).handle((ret, ex) -> { + if (ex == null) { + Map> map = (Map>) ret; + setImageDetection(map); + } else { + getMainFrame().showException((Exception) ex); + } + return ret; + }); + updateView(); + } + + public void clearImageDetection() throws StateException, ScriptException, IOException, InterruptedException { + Map> map = (Map>) getMainFrame().eval("clear_detection(None)"); + setImageDetection(map); + } + + void setImageDetection(Map> map) { + for (Puck.PuckType puckType : Puck.PuckType.values()) { + for (String name : map.get(puckType.toString())) { + Puck p = basePlate.getPuckByName(name); + if (p != null) { + p.setPuckType(puckType); + } + } + } + updateView(); + } + + + ////////////////////// BasePlatePanel callbacks ///////////////////////////////// + + BasePlatePanel puckPanel; + + void onPuckPressed(Puck puck) { + //JPanel panel = new SinglePuckPanel(puck); + //panel.setOpaque(false); + //getMainFrame().setDetail(panel); + if ((puckPanel == null) || (puckPanel != getMainFrame().getDetail())) { + puckPanel = new BasePlatePanel(); + puckPanel.setMode(BasePlatePanel.Mode.puck); + puckPanel.setSelectionMode(BasePlatePanel.SelectionMode.Samples); + puckPanel.setDevice((Device) getDevice("BasePlate")); + puckPanel.setEnabled(true); //TODO: Puck cannot be shared between two panels (device store single coordinates foe comparing to click) + getMainFrame().setDetail(puckPanel); + } + } + + void onPuckReleased(Puck puck) { + if (!puck.isSelected()) { + getMainFrame().setDefaultDetail(); + puckPanel = null; + } + } + + void onSamplePressed(Sample sample) { + onPuckPressed((sample==null)? null : sample.getPuck()); + } + + void onSampleReleased(Sample sample) { + onPuckReleased(sample.getPuck()); + } + + void onPlatePressed(BasePlate plate) { + if (plate.getSelectedPuck() == null) { + getMainFrame().setDefaultDetail(); + puckPanel = null; + } + } + + void onPlateReleased(BasePlate palte) { + } + + ////////////////////// Puck & Sample Info ///////////////////////////////// PuckState[] puckState; public PuckState[] getPuckStates() { @@ -434,292 +537,7 @@ public class Controller { return pucks.get(random.nextInt(pucks.size())); } - - public Boolean isLedRoomTemp() { - try { - if (getMainFrame().getState().isInitialized()){ - return getMainFrame().eval("is_led_room_temp()", true).equals(true); - } - } catch (Exception ex) { - } - return null; - } - - public Boolean isRoomTemp() { - try { - if (getMainFrame().getState().isInitialized()){ - return getMainFrame().eval("is_room_temp()", true).equals(true); - } - } catch (Exception ex) { - } - return null; - } - - public Boolean isServiceMode() { - try { - if (getMainFrame().getState().isInitialized()){ - return getMainFrame().eval("is_service_mode()", true).equals(true); - } - } catch (Exception ex) { - } - return null; - } - - //Non-blocking - public boolean isDoorOpen() { - try { - return doorsOpened; - //if (getMainFrame().getState().isInitialized()){ - // return getMainFrame().eval("is_door_closed()", true).equals(false); - // } - } catch (Exception ex) { - } - return false; - } - - //Non-blocking - public boolean isRobotParked() { - try { - if (getMainFrame().getState().isInitialized()){ - return getMainFrame().eval("'pPark' in robot.get_current_points_cached()", true).equals(true); - } - } catch (Exception ex) { - } - return false; - } - - Point fmm; - public void onCoverDetection(Point fmm ){ - this.fmm = fmm; - checkPuckLoading(); - } - - public boolean isCoverDetected() { - return fmm!=null; - } - - boolean doorsOpened; - boolean doorsHaveOpened; - public void onDoorsOpenChange(boolean value){ - doorsOpened = value; - doorsHaveOpened =value; - checkPuckLoading(); - } - - public void setServiceMode(boolean value){ - try{ - String state = value ? "True" : "False"; - getMainFrame().eval("set_service_mode(" + state + ")", true); - } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); - } - checkPuckLoading(); - } - - public boolean canSetPuckLoading(){ - if (isServiceMode()){ - return true; - } - return !isCoverDetected() && isDoorOpen() && isRobotParked() && getState().isInitialized(); - } - - private void checkPuckLoading(){ - try{ - if (isServiceMode()){ - //Open/close manually in service mode - } else { - if (!canSetPuckLoading()){ - setPuckLoading(false); - } else { - //Opens automatically once after door is oppen - if (doorsHaveOpened){ - doorsHaveOpened=false; - //setPuckLoading(true); - } - } - } - } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); - } - } - - public Boolean isBarcodeReaderScanPucks() { - try { - return getMainFrame().eval("is_barcode_reader_scan_pucks()", true).equals(true); - } catch (Exception ex) { - return false; - } - } - - public Device getPuckBarcodeReader() { - try { - return (Device) getMainFrame().eval("get_puck_barcode_reader()", true); - } catch (Exception ex) { - return null; - } - } - - public String getWorkingMode() { - try { - return String.valueOf(getMainFrame().eval("robot.working_mode", true)); - } catch (Exception ex) { - return "Unknown"; - } - } - - public boolean isManualMode() { - try { - return getMainFrame().eval("is_manual_mode()", true).equals(true); - } catch (Exception ex) { - return false; - } - } - - public Boolean isRoomTempEnabled() { - try { - return getMainFrame().eval("is_room_temperature_enabled()", true).equals(true); - } catch (Exception ex) { - return null; - } - } - - public Boolean isBeamlineStatusEnabled() { - try { - return getMainFrame().eval("is_beamline_status_enabled()", true).equals(true); - } catch (Exception ex) { - return null; - } - } - - public Boolean isImagingEnabled() { - try { - return getMainFrame().eval("is_imaging_enabled()", true).equals(true); - } catch (Exception ex) { - return null; - } - } - - public void imageDetectPucks() throws StateException { - imageDetectPucks(null, null, null); - } - - public void imageDetectPucks(JComponent plot, JComponent renderer, JComponent text) throws StateException { - Map args = new HashMap(); - args.put("PLOT", plot); - args.put("RENDERER", renderer); - args.put("TEXT", text); - getMainFrame().runAsync("imgproc/LedDetectionProc", args).handle((ret, ex) -> { - if (ex == null) { - Map> map = (Map>) ret; - setImageDetection(map); - } else { - getMainFrame().showException((Exception) ex); - } - return ret; - }); - updateView(); - } - - public void clearImageDetection() throws StateException, ScriptException, IOException, InterruptedException { - Map> map = (Map>) getMainFrame().eval("clear_detection(None)"); - setImageDetection(map); - } - - void setImageDetection(Map> map) { - for (Puck.PuckType puckType : Puck.PuckType.values()) { - for (String name : map.get(puckType.toString())) { - Puck p = basePlate.getPuckByName(name); - if (p != null) { - p.setPuckType(puckType); - } - } - } - updateView(); - } - - void setSinglePuckType(Puck.PuckType puckType) { - for (Puck p : basePlate.getPucks()) { - p.setPuckType(puckType); - } - } - - void resetPuckTypes() { - for (Puck p : basePlate.getPucks()) { - p.setPuckType(Puck.PuckType.Unknown); - } - } - - BasePlatePanel puckPanel; - - void onPuckPressed(Puck puck) { - //JPanel panel = new SinglePuckPanel(puck); - //panel.setOpaque(false); - //getMainFrame().setDetail(panel); - if ((puckPanel == null) || (puckPanel != getMainFrame().getDetail())) { - puckPanel = new BasePlatePanel(); - puckPanel.setMode(BasePlatePanel.Mode.puck); - puckPanel.setSelectionMode(BasePlatePanel.SelectionMode.Samples); - puckPanel.setDevice((Device) getDevice("BasePlate")); - puckPanel.setEnabled(true); //TODO: Puck cannot be shared between two panels (device store single coordinates foe comparing to click) - getMainFrame().setDetail(puckPanel); - } - } - - void onPuckReleased(Puck puck) { - if (!puck.isSelected()) { - getMainFrame().setDefaultDetail(); - puckPanel = null; - } - } - - void onSamplePressed(Sample sample) { - onPuckPressed((sample==null)? null : sample.getPuck()); - } - - void onSampleReleased(Sample sample) { - onPuckReleased(sample.getPuck()); - } - - void onPlatePressed(BasePlate plate) { - if (plate.getSelectedPuck() == null) { - getMainFrame().setDefaultDetail(); - puckPanel = null; - } - } - - void onPlateReleased(BasePlate palte) { - } - - GenericDevice getDevice(String name) { - return getMainFrame().getDevice(name); - } - - DevicePool getDevicePool() { - return Context.getDevicePool(); - } - - State getState() { - return getMainFrame().getState(); - } - - void execute(String statement) { - getMainFrame().execute(statement); - } - - void execute(String statement, boolean background) { - getMainFrame().execute(statement, background); - } - - void execute(String statement, boolean background, boolean showReturn) { - 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; //final Object samplesTableLock = new Object(); @@ -754,14 +572,116 @@ public class Controller { samplesTableJson = null; getMainFrame().setSamplesTable(new Object[][]{}); } + + + Map getPuckDatamatrix() { + try { + return (Map) Context.getSequencer().evalLineBackground("get_puck_datamatrix()"); + } catch (Exception ex) { + return null; + } + } + + Map getPuckDatamatrixInfo() { + try { + return (Map) Context.getSequencer().evalLineBackground("get_puck_datamatrix_info()"); + } catch (Exception ex) { + return null; + } + } + + String getPuckFromDatamatrix(String dm){ + try { + return (String) Context.getSequencer().evalLineBackground("get_puck_from_datamatrix('" + dm + "')"); + } catch (Exception ex) { + return null; + } + } + + public Sample getMountedSample() throws Exception{ + String mountedSample = (String) Context.getSequencer().evalLineBackground("get_setting('mounted_sample_position')"); + checkMountedSampleChange(currentMountedSample, mountedSample); + currentMountedSample = mountedSample; + Sample sample = basePlate.getSampleByName(currentMountedSample); + return sample; + } + + public void resetMountedSample() throws Exception{ + String mountedSample = (String) Context.getSequencer().evalLineBackground("get_setting('mounted_sample_position')"); + checkMountedSampleChange(currentMountedSample, null); + Context.getSequencer().evalLineBackground("set_setting('mounted_sample_position', None)"); + } + + void checkMountedSampleChange(String former, String current){ + if (((current == null) && (former!=null)) || ((current != null) && (!current.equals(former)))){ + logSampleMounted(former, current); + } + } + + + ////////////////////// Device callbacks ///////////////////////////////// + + Point fmm; + public void onCoverDetection(Point fmm ){ + this.fmm = fmm; + checkPuckLoading(); + } + + public boolean isCoverDetected() { + return fmm!=null; + } + + boolean doorsOpened; + public void onDoorsOpenChange(boolean value){ + doorsOpened = value; + doorsHaveOpened =value; + checkPuckLoading(); + } + + public void onSampleBarcode(String datamatrix) { + getMainFrame().setSampleDatamatrix(datamatrix); + System.out.println("Detected Sample: " + datamatrix); + } + + public void onPuckBarcode(String datamatrix) { + if (isPuckLoading()) { + playSound("scanned"); + System.out.println("Detected Puck: " + datamatrix); + onPuckScanned(datamatrix); + } + removedPuck = null; + } - ////////////////////// Puck Loading ///////////////////////////////// - boolean puckLoading; Puck.Detection[] currentDetection; + void onPuckDetectionChanged() { + if (isPuckLoading()) { + Puck.Detection[] detection = basePlate.getDetection(); + for (int i = 0; i < Controller.NUMBER_OF_PUCKS; i++) { + Puck puck = basePlate.getPucks()[i]; + + boolean detectedPuckInserted = (currentDetection[i] != Puck.Detection.Present) && (detection[i] == Puck.Detection.Present); + boolean detectedPuckRemoved = (currentDetection[i] != Puck.Detection.Empty) && (detection[i] == Puck.Detection.Empty); + if (detectedPuckInserted) { + onPuckMounted(puck); + } else if (detectedPuckRemoved) { + onPuckUnmounted(puck); + } + } + + currentDetection = detection; + } + } + + ////////////////////// Puck Loading ///////////////////////////////// + PuckLoadingDialog dialogPuckLoading; + JDialog dialogAskPuckDatamatrix; + boolean puckLoading; boolean puckReaderOk = true; String insertedPuckDatamatrix; Puck removedPuck; - + boolean doorsHaveOpened; + + //Enables/disables puck loading mode. Opens dialod if PuckMountMode is Dialog. void setPuckLoading(boolean load) { if (load != puckLoading) { if (load){ @@ -824,7 +744,40 @@ public class Controller { } } } + + //Returns if can switch to puck loading mode: no cover, doors open and robot parked. Or else, service mode. + public boolean canSetPuckLoading(){ + if (!getState().isInitialized()){ + return false; + } + if (isServiceMode()){ + return true; + } + return !isCoverDetected() && isDoorOpen() && isRobotParked(); + } + //Reset puck loading mode id any condition is false - closing the dialog + private void checkPuckLoading(){ + try{ + if (isServiceMode()){ + //Open/close manually in service mode + } else { + if (!canSetPuckLoading()){ + setPuckLoading(false); + } else { + //Opens automatically once after door is oppen + if (doorsHaveOpened){ + doorsHaveOpened=false; + //setPuckLoading(true); + } + } + } + } catch (Exception ex) { + Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); + } + } + + //Returns if puck loading mode is enabled. public boolean isPuckLoading() { try { if (isPuckDetectionLoading()){ @@ -840,76 +793,166 @@ public class Controller { return puckLoading; } - String laserPos; - public void setLaserPos(String pos){ - try { - laserPos = pos; - getMainFrame().evalAsync("set_laser_pos(" + ((pos==null) ? "" : ("'" + pos + "'") ) + ")" ,true); - getMainFrame().basePlatePanel.pointPuck((pos==null) ? null : basePlate.getPuckByName(pos)); - if ((dialogPuckLoading != null) && (dialogPuckLoading.isVisible()) && dialogPuckLoading.getBasePlatePanel()!=null) { - dialogPuckLoading.getBasePlatePanel().pointPuck((pos==null) ? null : basePlate.getPuckByName(pos)); - } - } catch (StateException ex) { - Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); + + //Returns if puck detection is enabled during puck loading only (puck_detection == "loading") + Boolean puckDetectionLoading; + public boolean isPuckDetectionLoading() throws IOException{ + if (puckDetectionLoading==null){ + puckDetectionLoading = "loading".equals(Context.getSetting("puck_detection")); } + return puckDetectionLoading; } - - public void setLaserPos(Puck puck){ - if (puck==null){ - setLaserPos((String)null); - } else { - setLaserPos(puck.getName()); - } - - } - - public String getLaserPos(){ - return laserPos; - } + //Manages puck detection enabling/disabling if isPuckDetectionLoading()==true + public void checkPuckDetectionEnabling(){ + boolean showingPuckLoadingDialog; + try{ + showingPuckLoadingDialog = (dialogPuckLoading != null) && (dialogPuckLoading.isShowing()); + if ( isPuckDetectionLoading()){ + if (showingPuckLoadingDialog){ + var werePresent = new ArrayList(); + Puck[] pucks = basePlate.getPucks(); + for (Puck p : pucks){ + if (p.getDetection() == Puck.Detection.Present){ + werePresent.add(p.getName()); + } + } + puck_detection.applyCache(); + - void playSound(String name) { - try { - Audio.playFile(new File(Setup.expandPath("{home}/sounds/" + name + ".wav")), false); + var present = new ArrayList(); + for (Puck p : pucks){ + if (p.getDetection() == Puck.Detection.Present){ + present.add(p.getName()); + } + } + var removed = new ArrayList<>(werePresent); + removed.removeAll(present); + + if (removed.size()>0){ + PuckDetectionErrorDialog dlg = new PuckDetectionErrorDialog(dialogPuckLoading, false); + dlg.initialize(removed); + dlg.setLocationRelativeTo(dialogPuckLoading); + dlg.setVisible(true); + for (String address: removed){ + Puck puck = basePlate.getPuckByName(address); + clearPuckMountedSample(puck); + Controller.getInstance().linkPuckDatamatrix(puck, null, false); + } + } + } + puck_detection.setEnabled(showingPuckLoadingDialog); + } } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.FINE, null, ex); - } + Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); + } } + - public void onPuckBarcode(String datamatrix) { - if (isPuckLoading()) { - playSound("scanned"); - System.out.println("Detected Puck: " + datamatrix); - onPuckScanned(datamatrix); + //Shows puck loading dialog + void showDialogPuckLoading() { + if ((dialogPuckLoading != null) && (dialogPuckLoading.isVisible())) { + return; } - removedPuck = null; - } - - public void onSampleBarcode(String datamatrix) { - getMainFrame().setSampleDatamatrix(datamatrix); - System.out.println("Detected Sample: " + datamatrix); - } + dialogPuckLoading = new PuckLoadingDialog(mainFrame.getTopLevel(), false, false, true); + dialogPuckLoading.setVisible(true); + checkPuckDetectionEnabling(); - void onPuckDetectionChanged() { - if (isPuckLoading()) { - Puck.Detection[] detection = basePlate.getDetection(); - for (int i = 0; i < Controller.NUMBER_OF_PUCKS; i++) { - Puck puck = basePlate.getPucks()[i]; - - boolean detectedPuckInserted = (currentDetection[i] != Puck.Detection.Present) && (detection[i] == Puck.Detection.Present); - boolean detectedPuckRemoved = (currentDetection[i] != Puck.Detection.Empty) && (detection[i] == Puck.Detection.Empty); - if (detectedPuckInserted) { - onPuckMounted(puck); - } else if (detectedPuckRemoved) { - onPuckUnmounted(puck); + dialogPuckLoading.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + try{ + setPuckLoading(false); + } catch (Exception ex) { + Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); + } } - } + @Override + public void windowClosed(WindowEvent e) { + checkPuckDetectionEnabling(); + } + + }); + } - currentDetection = detection; + //Hides puck loading dialog + void hideDialogPuckLoading() { + if (dialogPuckLoading != null) { + dialogPuckLoading.setVisible(false); + dialogPuckLoading.dispose(); + dialogPuckLoading = null; + + try { + List puckInfo = (List) getMainFrame().eval("get_puck_info()", true); + Context.getSequencer().sendEvent("DewarContentUpdate", puckInfo); + } catch (Exception ex) { + Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); + } } } - + + //Called when a puck is scanned + public void onPuckScanned(String datamatrix) { + Logger.getLogger(Controller.class.getName()).info("Scanned puck: " + datamatrix); + if (isPuckLoading()) { + getMainFrame().setPuckDatamatrix(datamatrix); + if ( hasLoadDialog()) { + showDialogPuckLoading(); + dialogPuckLoading.onPuckScanned(datamatrix); + } + } else { + getMainFrame().setPuckDatamatrix(null); + } + } + + //Called when a puck is detected inserted in the dewar + public void onPuckMounted(Puck puck) { + String datamatrix = getMainFrame().getPuckDatamatrix(); + Logger.getLogger(Controller.class.getName()).info("Mounted puck: " + puck.getName() + " - datamatrix:" + datamatrix); + playSound("mounted"); + logPuckDetectionChange(puck, true); + onPuckScanned(null); + if ((datamatrix==null) || (datamatrix.isBlank())){ + if (puck == removedPuck){ + datamatrix = insertedPuckDatamatrix; + } + } + + if ((datamatrix != null) && (!datamatrix.isEmpty())) { + Controller.getInstance().linkPuckDatamatrix(puck, datamatrix, !hasLoadDialog()); + } + + if (hasLoadDialog()) { + showDialogPuckLoading(); + dialogPuckLoading.onPuckMounted(puck, datamatrix); + } else { + if ((datamatrix == null) || (datamatrix.isEmpty())){ + askPuckDatamatrix(puck); + } + } + insertedPuckDatamatrix = datamatrix; + removedPuck = null; + } + + //Called when a puck is detected removed from the the dewar + public void onPuckUnmounted(Puck puck) { + Logger.getLogger(Controller.class.getName()).info("Unmounted puck: " + puck.getName()); + playSound("unmounted"); + clearPuckMountedSample(puck); + Controller.getInstance().linkPuckDatamatrix(puck, null, !hasLoadDialog()); + if ( hasLoadDialog()) { + showDialogPuckLoading(); + dialogPuckLoading.onPuckUnmounted(puck); + } + removedPuck = puck; + } + public boolean hasLoadDialog(){ + return(puckMountMode == PuckMountMode.Dialog); + } + + //Links a puck to a datamatrix, or removes the link is datamatrix=null. + //This is called by puck detectiokn events and also when puck is manually linked to a datamatrix. public void linkPuckDatamatrix(Puck puck, String datamatrix, boolean showMessage) { // if ( ((puck.getId()==null) && (datamatrix!=null)) || // (puck.getId()!=null) && (!puck.getId().equals(datamatrix))){ @@ -942,32 +985,8 @@ public class Controller { getMainFrame().refresh(); // } } - - Map getPuckDatamatrix() { - try { - return (Map) Context.getSequencer().evalLineBackground("get_puck_datamatrix()"); - } catch (Exception ex) { - return null; - } - } - - Map getPuckDatamatrixInfo() { - try { - return (Map) Context.getSequencer().evalLineBackground("get_puck_datamatrix_info()"); - } catch (Exception ex) { - return null; - } - } - String getPuckFromDatamatrix(String dm){ - try { - return (String) Context.getSequencer().evalLineBackground("get_puck_from_datamatrix('" + dm + "')"); - } catch (Exception ex) { - return null; - } - - } - + //Manual selection of a datamatrix for a detected puck if does not have a loading dialog void askPuckDatamatrix(Puck puck) { if ((dialogAskPuckDatamatrix != null) && (dialogAskPuckDatamatrix.isShowing())) { dialogAskPuckDatamatrix.setVisible(false); @@ -1028,160 +1047,9 @@ public class Controller { } - } + } - Boolean puckDetectionLoading; - public boolean isPuckDetectionLoading() throws IOException{ - if (puckDetectionLoading==null){ - puckDetectionLoading = "loading".equals(Context.getSetting("puck_detection")); - } - return puckDetectionLoading; - } - - - boolean showingPuckLoadingDialog; - public void checkPuckDetectionEnabling(){ - try{ - showingPuckLoadingDialog = (dialogPuckLoading != null) && (dialogPuckLoading.isShowing()); - if ( isPuckDetectionLoading()){ - if (showingPuckLoadingDialog){ - var werePresent = new ArrayList(); - Puck[] pucks = basePlate.getPucks(); - for (Puck p : pucks){ - if (p.getDetection() == Puck.Detection.Present){ - werePresent.add(p.getName()); - } - } - puck_detection.applyCache(); - - - var present = new ArrayList(); - for (Puck p : pucks){ - if (p.getDetection() == Puck.Detection.Present){ - present.add(p.getName()); - } - } - var removed = new ArrayList<>(werePresent); - removed.removeAll(present); - - if (removed.size()>0){ - PuckDetectionErrorDialog dlg = new PuckDetectionErrorDialog(dialogPuckLoading, false); - dlg.initialize(removed); - dlg.setLocationRelativeTo(dialogPuckLoading); - dlg.setVisible(true); - for (String address: removed){ - Puck puck = basePlate.getPuckByName(address); - clearPuckMountedSample(puck); - Controller.getInstance().linkPuckDatamatrix(puck, null, false); - } - } - } - puck_detection.setEnabled(showingPuckLoadingDialog); - } - } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); - } - } - - - void showDialogPuckLoading() { - if ((dialogPuckLoading != null) && (dialogPuckLoading.isVisible())) { - return; - } - dialogPuckLoading = new PuckLoadingDialog(mainFrame.getTopLevel(), false, false, true); - dialogPuckLoading.setVisible(true); - checkPuckDetectionEnabling(); - - dialogPuckLoading.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - try{ - setPuckLoading(false); - } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); - } - } - @Override - public void windowClosed(WindowEvent e) { - checkPuckDetectionEnabling(); - } - - }); - } - - void hideDialogPuckLoading() { - if (dialogPuckLoading != null) { - dialogPuckLoading.setVisible(false); - dialogPuckLoading.dispose(); - dialogPuckLoading = null; - - try { - List puckInfo = (List) getMainFrame().eval("get_puck_info()", true); - Context.getSequencer().sendEvent("DewarContentUpdate", puckInfo); - } catch (Exception ex) { - Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); - } - } - } - - - public void onPuckScanned(String datamatrix) { - Logger.getLogger(Controller.class.getName()).info("Scanned puck: " + datamatrix); - if (isPuckLoading()) { - getMainFrame().setPuckDatamatrix(datamatrix); - if ( hasLoadDialog()) { - showDialogPuckLoading(); - dialogPuckLoading.onPuckScanned(datamatrix); - } - } else { - getMainFrame().setPuckDatamatrix(null); - } - } - - public boolean hasLoadDialog(){ - return(puckMountMode == PuckMountMode.Dialog); - } - - public void onPuckMounted(Puck puck) { - String datamatrix = getMainFrame().getPuckDatamatrix(); - Logger.getLogger(Controller.class.getName()).info("Mounted puck: " + puck.getName() + " - datamatrix:" + datamatrix); - playSound("mounted"); - logPuckDetectionChange(puck, true); - onPuckScanned(null); - if ((datamatrix==null) || (datamatrix.isBlank())){ - if (puck == removedPuck){ - datamatrix = insertedPuckDatamatrix; - } - } - - if ((datamatrix != null) && (!datamatrix.isEmpty())) { - Controller.getInstance().linkPuckDatamatrix(puck, datamatrix, !hasLoadDialog()); - } - - if (hasLoadDialog()) { - showDialogPuckLoading(); - dialogPuckLoading.onPuckMounted(puck, datamatrix); - } else { - if ((datamatrix == null) || (datamatrix.isEmpty())){ - askPuckDatamatrix(puck); - } - } - insertedPuckDatamatrix = datamatrix; - removedPuck = null; - } - - public void onPuckUnmounted(Puck puck) { - Logger.getLogger(Controller.class.getName()).info("Unmounted puck: " + puck.getName()); - playSound("unmounted"); - clearPuckMountedSample(puck); - Controller.getInstance().linkPuckDatamatrix(puck, null, !hasLoadDialog()); - if ( hasLoadDialog()) { - showDialogPuckLoading(); - dialogPuckLoading.onPuckUnmounted(puck); - } - removedPuck = puck; - } - + //Clear mounted flag of samples of removed puck. void clearPuckMountedSample(Puck puck){ try{ Sample sample = getMountedSample(); @@ -1196,6 +1064,171 @@ public class Controller { Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex); } } + + + + ////////////////////// Laser Pointer ///////////////////////////////// + + String laserPos; + public void setLaserPos(String pos){ + try { + laserPos = pos; + getMainFrame().evalAsync("set_laser_pos(" + ((pos==null) ? "" : ("'" + pos + "'") ) + ")" ,true); + getMainFrame().basePlatePanel.pointPuck((pos==null) ? null : basePlate.getPuckByName(pos)); + if ((dialogPuckLoading != null) && (dialogPuckLoading.isVisible()) && dialogPuckLoading.getBasePlatePanel()!=null) { + dialogPuckLoading.getBasePlatePanel().pointPuck((pos==null) ? null : basePlate.getPuckByName(pos)); + } + } catch (StateException ex) { + Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); + } + } + + + public void setLaserPos(Puck puck){ + if (puck==null){ + setLaserPos((String)null); + } else { + setLaserPos(puck.getName()); + } + + } + + public String getLaserPos(){ + return laserPos; + } + + + ////////////////////// Utilities ///////////////////////////////// + + void playSound(String name) { + try { + Audio.playFile(new File(Setup.expandPath("{home}/sounds/" + name + ".wav")), false); + } catch (Exception ex) { + Logger.getLogger(Controller.class.getName()).log(Level.FINE, null, ex); + } + } + + public Boolean isLedRoomTemp() { + try { + if (getMainFrame().getState().isInitialized()){ + return getMainFrame().eval("is_led_room_temp()", true).equals(true); + } + } catch (Exception ex) { + } + return null; + } + + public Boolean isRoomTemp() { + try { + if (getMainFrame().getState().isInitialized()){ + return getMainFrame().eval("is_room_temp()", true).equals(true); + } + } catch (Exception ex) { + } + return null; + } + + public Boolean isServiceMode() { + try { + if (getMainFrame().getState().isInitialized()){ + return getMainFrame().eval("is_service_mode()", true).equals(true); + } + } catch (Exception ex) { + } + return null; + } + + + //Non-blocking + public boolean isDoorOpen() { + try { + return doorsOpened; + //if (getMainFrame().getState().isInitialized()){ + // return getMainFrame().eval("is_door_closed()", true).equals(false); + // } + } catch (Exception ex) { + } + return false; + } + + //Non-blocking + public boolean isRobotParked() { + try { + if (getMainFrame().getState().isInitialized()){ + return getMainFrame().eval("'pPark' in robot.get_current_points_cached()", true).equals(true); + } + } catch (Exception ex) { + } + return false; + } + + public void setServiceMode(boolean value){ + try{ + String state = value ? "True" : "False"; + getMainFrame().eval("set_service_mode(" + state + ")", true); + } catch (Exception ex) { + Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); + } + checkPuckLoading(); + } + + public Boolean isBarcodeReaderScanPucks() { + try { + return getMainFrame().eval("is_barcode_reader_scan_pucks()", true).equals(true); + } catch (Exception ex) { + return false; + } + } + + public Device getPuckBarcodeReader() { + try { + return (Device) getMainFrame().eval("get_puck_barcode_reader()", true); + } catch (Exception ex) { + return null; + } + } + + public String getWorkingMode() { + try { + return String.valueOf(getMainFrame().eval("robot.working_mode", true)); + } catch (Exception ex) { + return "Unknown"; + } + } + + public boolean isManualMode() { + try { + return getMainFrame().eval("is_manual_mode()", true).equals(true); + } catch (Exception ex) { + return false; + } + } + + public Boolean isRoomTempEnabled() { + try { + return getMainFrame().eval("is_room_temperature_enabled()", true).equals(true); + } catch (Exception ex) { + return null; + } + } + + public Boolean isBeamlineStatusEnabled() { + try { + return getMainFrame().eval("is_beamline_status_enabled()", true).equals(true); + } catch (Exception ex) { + return null; + } + } + + public Boolean isImagingEnabled() { + try { + return getMainFrame().eval("is_imaging_enabled()", true).equals(true); + } catch (Exception ex) { + return null; + } + } + + ////////////////////// Logs ///////////////////////////////// void logSampleMounted(String former, String current){ logEvent((current!=null) ? "Sample Mounted" : "Sample Unmounted", former, current);