Files
MXSC/src/main/java/ch/psi/mxsc/DevicesPanel.java
2024-11-05 14:52:40 +01:00

449 lines
23 KiB
Java

package ch.psi.mxsc;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceAdapter;
import ch.psi.pshell.device.GenericDevice;
import ch.psi.pshell.swing.DevicePanel;
import ch.psi.pshell.swing.Led;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Color;
import java.awt.Window;
/**
*
*/
public class DevicesPanel extends javax.swing.JPanel {
GenericDevice robot;
GenericDevice barcode_reader;
GenericDevice barcode_reader_puck;
GenericDevice wago;
GenericDevice laser;
GenericDevice puck_detection;
GenericDevice sm;
GenericDevice img;
GenericDevice monitor_cam;
boolean imagingEnabled;
/**
* Creates new form DevicesPanel
*/
public DevicesPanel() {
initComponents();
}
GenericDevice[] getDevices(){
return new GenericDevice[]{robot, barcode_reader, barcode_reader_puck, wago, laser, puck_detection, sm};
}
final DeviceAdapter deviceListener = new DeviceAdapter() {
@Override
public void onStateChanged(Device device, State state, State former) {
update();
}
};
void initialize(){
Controller.getInstance().getDevicePool().addListener(new DevicePoolListener() {
@Override
public void onDeviceAdded(GenericDevice dev) {
updateDevices();
}
@Override
public void onDeviceRemoved(GenericDevice dev) {
}
});
imagingEnabled = Controller.getInstance().isImagingEnabled();
updateDevices();
}
void updateDevices(){
for (GenericDevice dev : getDevices()){
if (dev!=null){
dev.removeListener(deviceListener);
}
}
robot = Controller.getInstance().getDevice("robot");
barcode_reader = Controller.getInstance().getDevice("barcode_reader");
barcode_reader_puck = Controller.getInstance().getDevice("barcode_reader_puck");
wago = Controller.getInstance().getDevice("wago");
laser = Controller.getInstance().getDevice("ue");
puck_detection = Controller.getInstance().getDevice("puck_detection");
img = imagingEnabled ? Controller.getInstance().getDevice("img") : null;
monitor_cam = Controller.getInstance().getDevice("monitoring_cam");
sm = Controller.getInstance().getDevice("smart_magnet");
update();
for (GenericDevice dev : getDevices()){
if (dev!=null){
dev.addListener(deviceListener);
}
}
}
void update(){
setLedState(ledBarcodeReader, barcode_reader);
if ((barcode_reader_puck!=null) && (ledBarcodeReader.getColor()==Color.green)){
setLedState(ledBarcodeReader, barcode_reader_puck);
}
setLedState(ledCamera, img);
setLedState(ledMonitorCamera, monitor_cam);
setLedState(ledLaser, laser);
setLedState(ledPuckDetection, puck_detection);
setLedState(ledRobot, robot);
setLedState(ledWago, wago);
setLedState(ledSmartMagnet, sm);
}
void setLedState(Led led, GenericDevice dev){
if ((dev==null) || (dev.getState()==null)){
led.setColor(Color.black);
} else {
switch (dev.getState()){
case Ready:
led.setColor(Color.green);
break;
case Initializing:
case Paused:
case Busy:
led.setColor(Color.orange);
break;
case Disabled:
led.setColor(dev.getName().startsWith("barcode_reader") ? Color.green : Color.orange);
break;
case Invalid:
case Closing:
case Fault:
case Offline:
led.setColor(Color.red);
break;
default:
led.setColor(Color.darkGray);
break;
}
}
}
DevicePanel showDevicePanel(String device){
if (isActive()){
try{
DevicePanel ret = Controller.getInstance().getMainFrame().showDevicePanel(device);
Controller.getInstance().getMainFrame().onExpertCommand(SwingUtils.getWindow(ret));
return ret;
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}
return null;
}
void showRenderer(String device){
if (isActive()){
try{
Controller.getInstance().getMainFrame().showRenderer(device);
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}
}
boolean active = true;
public void setActive(boolean value){
active = value;
labelCamera.setActive(value);
labelMonitorCamera.setActive(value);
labelLaser.setActive(value);
labelPuckDetection.setActive(value);
labelReader.setActive(value);
labelRobot.setActive(value);
labelSmartMagnet.setActive(value);
labelWago.setActive(value);
}
public boolean isActive(){
return active;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
labelRobot = new ch.psi.mxsc.HyperlinkLabel();
ledRobot = new ch.psi.pshell.swing.Led();
ledWago = new ch.psi.pshell.swing.Led();
labelWago = new ch.psi.mxsc.HyperlinkLabel();
labelLaser = new ch.psi.mxsc.HyperlinkLabel();
ledLaser = new ch.psi.pshell.swing.Led();
labelCamera = new ch.psi.mxsc.HyperlinkLabel();
ledCamera = new ch.psi.pshell.swing.Led();
labelReader = new ch.psi.mxsc.HyperlinkLabel();
ledBarcodeReader = new ch.psi.pshell.swing.Led();
labelPuckDetection = new ch.psi.mxsc.HyperlinkLabel();
ledPuckDetection = new ch.psi.pshell.swing.Led();
ledSmartMagnet = new ch.psi.pshell.swing.Led();
labelSmartMagnet = new ch.psi.mxsc.HyperlinkLabel();
ledMonitorCamera = new ch.psi.pshell.swing.Led();
labelMonitorCamera = new ch.psi.mxsc.HyperlinkLabel();
labelRobot.setText("Robot");
labelRobot.setActive(true);
labelRobot.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledRobotMouseClicked(evt);
}
});
ledRobot.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledRobotMouseClicked(evt);
}
});
ledWago.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelWagoMouseClicked(evt);
}
});
labelWago.setText("Wago");
labelWago.setActive(true);
labelWago.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelWagoMouseClicked(evt);
}
});
labelLaser.setText("Laser");
labelLaser.setActive(true);
labelLaser.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledLaserMouseClicked(evt);
}
});
ledLaser.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledLaserMouseClicked(evt);
}
});
labelCamera.setText("Dewar Camera");
labelCamera.setActive(true);
labelCamera.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledCameraMouseClicked(evt);
}
});
ledCamera.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledCameraMouseClicked(evt);
}
});
labelReader.setText("Barcode Reader");
labelReader.setActive(true);
labelReader.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelReaderMouseClicked(evt);
}
});
ledBarcodeReader.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelReaderMouseClicked(evt);
}
});
labelPuckDetection.setText("Puck Detection");
labelPuckDetection.setActive(true);
labelPuckDetection.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledPuckDetectionMouseClicked(evt);
}
});
ledPuckDetection.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ledPuckDetectionMouseClicked(evt);
}
});
ledSmartMagnet.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelSmartMagnetMouseClicked(evt);
}
});
labelSmartMagnet.setText("Smart Magnet");
labelSmartMagnet.setActive(true);
labelSmartMagnet.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelSmartMagnetMouseClicked(evt);
}
});
ledMonitorCamera.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelMonitorCameraMouseClicked(evt);
}
});
labelMonitorCamera.setText("Monitoring Camera");
labelMonitorCamera.setActive(true);
labelMonitorCamera.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelMonitorCameraMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(ledRobot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelRobot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledWago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelWago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledLaser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelLaser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledBarcodeReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledPuckDetection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelPuckDetection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledSmartMagnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelSmartMagnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(ledMonitorCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelMonitorCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledRobot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelRobot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledWago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelWago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledLaser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelLaser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledBarcodeReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledPuckDetection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelPuckDetection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledSmartMagnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelSmartMagnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ledMonitorCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(labelMonitorCamera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void ledRobotMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ledRobotMouseClicked
showDevicePanel("robot");
}//GEN-LAST:event_ledRobotMouseClicked
private void labelSmartMagnetMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelSmartMagnetMouseClicked
showDevicePanel("smart_magnet");
}//GEN-LAST:event_labelSmartMagnetMouseClicked
private void ledCameraMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ledCameraMouseClicked
showDevicePanel("cam");
}//GEN-LAST:event_ledCameraMouseClicked
private void ledLaserMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ledLaserMouseClicked
showDevicePanel("ue");
}//GEN-LAST:event_ledLaserMouseClicked
private void labelWagoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelWagoMouseClicked
showDevicePanel("wago");
}//GEN-LAST:event_labelWagoMouseClicked
private void labelReaderMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelReaderMouseClicked
DevicePanel dp1 = showDevicePanel("barcode_reader");
if (Controller.getInstance().getDevicePool().getByName("barcode_reader_puck")!=null){
DevicePanel dp2 = showDevicePanel("barcode_reader_puck");
if ((dp1!=null)&& (dp2!=null)){
Window parent = ((Window )dp2.getTopLevelAncestor());
parent.setLocation(parent.getX()+80, parent.getY()+40);
}
}
}//GEN-LAST:event_labelReaderMouseClicked
private void ledPuckDetectionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ledPuckDetectionMouseClicked
showDevicePanel("puck_detection");
}//GEN-LAST:event_ledPuckDetectionMouseClicked
private void labelMonitorCameraMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelMonitorCameraMouseClicked
showRenderer("monitoring_cam");
}//GEN-LAST:event_labelMonitorCameraMouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private ch.psi.mxsc.HyperlinkLabel labelCamera;
private ch.psi.mxsc.HyperlinkLabel labelLaser;
private ch.psi.mxsc.HyperlinkLabel labelMonitorCamera;
private ch.psi.mxsc.HyperlinkLabel labelPuckDetection;
private ch.psi.mxsc.HyperlinkLabel labelReader;
private ch.psi.mxsc.HyperlinkLabel labelRobot;
private ch.psi.mxsc.HyperlinkLabel labelSmartMagnet;
private ch.psi.mxsc.HyperlinkLabel labelWago;
private ch.psi.pshell.swing.Led ledBarcodeReader;
private ch.psi.pshell.swing.Led ledCamera;
private ch.psi.pshell.swing.Led ledLaser;
private ch.psi.pshell.swing.Led ledMonitorCamera;
private ch.psi.pshell.swing.Led ledPuckDetection;
private ch.psi.pshell.swing.Led ledRobot;
private ch.psi.pshell.swing.Led ledSmartMagnet;
private ch.psi.pshell.swing.Led ledWago;
// End of variables declaration//GEN-END:variables
}