From 109e81aecbe14d87a7406ed224d3b0fdce36ee4a Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Thu, 19 Apr 2018 17:35:54 +0200 Subject: [PATCH] --- src/main/java/ch/psi/mxsc/BasePlate.java | 30 +++- src/main/java/ch/psi/mxsc/BasePlatePanel.java | 15 +- src/main/java/ch/psi/mxsc/Controller.java | 43 ++++- src/main/java/ch/psi/mxsc/EseraDetection.java | 2 +- src/main/java/ch/psi/mxsc/HexiposiPanel.form | 21 ++- src/main/java/ch/psi/mxsc/HexiposiPanel.java | 83 ++++++--- src/main/java/ch/psi/mxsc/MainPanel.form | 6 +- src/main/java/ch/psi/mxsc/MainPanel.java | 12 +- src/main/java/ch/psi/mxsc/Puck.java | 105 ++++++++--- src/main/java/ch/psi/mxsc/PuckDetection.java | 167 ++++++++++-------- src/main/java/ch/psi/mxsc/PuckState.java | 4 +- src/main/java/ch/psi/mxsc/Sample.java | 8 +- 12 files changed, 315 insertions(+), 181 deletions(-) diff --git a/src/main/java/ch/psi/mxsc/BasePlate.java b/src/main/java/ch/psi/mxsc/BasePlate.java index 291e648..52fbde2 100644 --- a/src/main/java/ch/psi/mxsc/BasePlate.java +++ b/src/main/java/ch/psi/mxsc/BasePlate.java @@ -65,7 +65,7 @@ public class BasePlate extends DeviceBase { for (int i = 0; i < numberOfPucks; i++) { new Puck(this, i); } - getPucks()[0].setSelected(true); + getPucks()[0].setSelected(true); } @Override @@ -78,7 +78,15 @@ public class BasePlate extends DeviceBase { return (BasePlateConfig) super.getConfig(); } + boolean selectable; + public boolean isSelectable() { + return selectable; + } + public void setSelectable(boolean selectable) { + this.selectable = selectable; + } + public Puck[] getPucks() { ArrayList ret = new ArrayList<>(); for (Device d : getChildren()) { @@ -89,20 +97,24 @@ public class BasePlate extends DeviceBase { Puck getSelectedPuck(){ - for (Puck p:getPucks()){ - if (p.isSelected()){ - return p; + if (selectable){ + for (Puck p:getPucks()){ + if (p.isSelected()){ + return p; + } } } return null; } Sample getSelectedSample(){ - Puck puck = getSelectedPuck(); - if (puck != null){ - for (Sample s: puck.getSamples()){ - if (s.isSelected()){ - return s; + if (selectable){ + Puck puck = getSelectedPuck(); + if (puck != null){ + for (Sample s: puck.getSamples()){ + if (s.isSelected()){ + return s; + } } } } diff --git a/src/main/java/ch/psi/mxsc/BasePlatePanel.java b/src/main/java/ch/psi/mxsc/BasePlatePanel.java index 7cbffc7..1fd6838 100644 --- a/src/main/java/ch/psi/mxsc/BasePlatePanel.java +++ b/src/main/java/ch/psi/mxsc/BasePlatePanel.java @@ -83,15 +83,6 @@ public class BasePlatePanel extends DevicePanel { repaint(); } - boolean selectable; - public boolean getSelectable() { - return selectable; - } - - public void setSelectable(boolean selectable) { - this.selectable = selectable; - } - /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT @@ -140,7 +131,7 @@ public class BasePlatePanel extends DevicePanel { case single: platePlotRect = plotRect; puckPlotRect = null; - getDevice().draw(g2d, platePlotRect, true, false, true, DrawMode.center, img); + getDevice().draw(g2d, platePlotRect, (img==null), false, true, DrawMode.center, img); break; case horizontal: platePlotRect = new Rectangle(plotRect.x, plotRect.y, plotRect.width / 2, plotRect.height); @@ -211,7 +202,7 @@ public class BasePlatePanel extends DevicePanel { } private void checkMouseEvent(MouseEvent e, boolean pressed) { - if (isEnabled() && selectable) { + if (isEnabled() && (getDevice()!=null) && getDevice().isSelectable()) { try { Sample sample = getSample(e.getX(), e.getY()); Puck puck = getPuck(e.getX(), e.getY()); @@ -252,7 +243,7 @@ public class BasePlatePanel extends DevicePanel { @Override public void mouseClicked(MouseEvent e) { - if (isEnabled()) { + if (isEnabled()&& (getDevice()!=null)) { if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) { Sample sample = getSample(e.getX(), e.getY()); Puck puck = getPuck(e.getX(), e.getY()); diff --git a/src/main/java/ch/psi/mxsc/Controller.java b/src/main/java/ch/psi/mxsc/Controller.java index 08a00fe..c1a712c 100644 --- a/src/main/java/ch/psi/mxsc/Controller.java +++ b/src/main/java/ch/psi/mxsc/Controller.java @@ -6,8 +6,11 @@ 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.DeviceListener; import ch.psi.pshell.device.GenericDevice; import ch.psi.pshell.ui.Panel; +import ch.psi.utils.State; /** * @@ -16,6 +19,7 @@ public class Controller { static Controller instance; final BasePlate basePlate; final Panel mainFrame; + Device hexaposi; public static Controller getInstance(){ return instance; @@ -26,9 +30,9 @@ public class Controller { } enum PuckSensorAccess{ - RaspberryPi, - Esera; -} + RaspberryPi, + Esera; + } final PuckSensorAccess puckSensorAccess = PuckSensorAccess.RaspberryPi; @@ -36,6 +40,7 @@ public class Controller { static String PUCK_ESERA_DEVICE = "onewire"; public static final int NUMBER_OF_PUCKS = 30; + private Controller(Panel mainFrame){ basePlate = new BasePlate(); @@ -50,6 +55,10 @@ public class Controller { public Panel getMainFrame(){ return mainFrame; } + + public void updateView(){ + getMainFrame().repaint(); + } void onInitialize(int runCount) { GenericDevice former = getMainFrame().getDevice("BasePlate"); @@ -83,7 +92,13 @@ public class Controller { detection = new EseraDetection((Device) getMainFrame().getDevice(PUCK_ESERA_DEVICE)); } } - + hexaposi = (Device) getMainFrame().getDevice("hexiposi"); + hexaposi.addListener(new DeviceAdapter() { + @Override + public void onValueChanged(Device device, Object value, Object former) { + updateView(); + } + }); } final PuckState[] puckState; @@ -100,11 +115,27 @@ public class Controller { return getPuckStates()[id-1]; } + public int getPuckIndex(int address) throws Exception{ + if ((address<=0) || (address>NUMBER_OF_PUCKS)){ + throw new Exception("invalid puck address: "+ address); + } + for (int i=0; i - + - - - + - + + @@ -89,9 +88,10 @@ - + + @@ -124,9 +124,10 @@ - + + @@ -139,9 +140,10 @@ - + + @@ -154,9 +156,10 @@ - + + diff --git a/src/main/java/ch/psi/mxsc/HexiposiPanel.java b/src/main/java/ch/psi/mxsc/HexiposiPanel.java index 6f3f324..a687f0b 100644 --- a/src/main/java/ch/psi/mxsc/HexiposiPanel.java +++ b/src/main/java/ch/psi/mxsc/HexiposiPanel.java @@ -1,18 +1,40 @@ package ch.psi.mxsc; +import ch.psi.pshell.device.DiscretePositionerBase; import ch.psi.pshell.swing.DevicePanel; +import java.awt.Color; /** * */ public class HexiposiPanel extends DevicePanel { - /** - * Creates new form RoomTempPuckPanel - */ + final Color COLOR_ON = new Color(51,102,255); + final Color COLOR_OFF = Color.BLACK; + public HexiposiPanel() { initComponents(); + setDeviceName("hexiposi"); + } + + @Override + public DiscretePositionerBase getDevice(){ + return (DiscretePositionerBase) super.getDevice(); + } + + @Override + protected void onDeviceReadbackChanged(Object value) { + ledA.setColor("A".equals(value) ? COLOR_ON: COLOR_OFF); + ledB.setColor("B".equals(value) ? COLOR_ON: COLOR_OFF); + ledC.setColor("C".equals(value) ? COLOR_ON: COLOR_OFF); + ledD.setColor("D".equals(value) ? COLOR_ON: COLOR_OFF); + ledE.setColor("E".equals(value) ? COLOR_ON: COLOR_OFF); + ledF.setColor("F".equals(value) ? COLOR_ON: COLOR_OFF); + repaint(); + } + + /** * This method is called from within the constructor to initialize the form. @@ -26,49 +48,54 @@ public class HexiposiPanel extends DevicePanel { filler9 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); circlePanel2 = new ch.psi.mxsc.CirclePanel(); filler8 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); - led1 = new ch.psi.pshell.swing.Led(); + ledD = new ch.psi.pshell.swing.Led(); filler7 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0)); - led2 = new ch.psi.pshell.swing.Led(); + ledC = new ch.psi.pshell.swing.Led(); filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); - led3 = new ch.psi.pshell.swing.Led(); + ledE = new ch.psi.pshell.swing.Led(); filler10 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); filler5 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); - led5 = new ch.psi.pshell.swing.Led(); + ledB = new ch.psi.pshell.swing.Led(); filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); - led6 = new ch.psi.pshell.swing.Led(); + ledF = new ch.psi.pshell.swing.Led(); filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); - led7 = new ch.psi.pshell.swing.Led(); + ledA = new ch.psi.pshell.swing.Led(); filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767)); circlePanel2.setLayout(new java.awt.GridLayout(5, 3)); circlePanel2.add(filler8); - led1.setForeground(new java.awt.Color(51, 102, 255)); - led1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led1); + ledD.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledD.setLedSize(18); + circlePanel2.add(ledD); circlePanel2.add(filler7); - led2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led2); + ledC.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledC.setLedSize(18); + circlePanel2.add(ledC); circlePanel2.add(filler6); - led3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led3); + ledE.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledE.setLedSize(18); + circlePanel2.add(ledE); circlePanel2.add(filler10); circlePanel2.add(filler5); circlePanel2.add(filler4); - led5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led5); + ledB.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledB.setLedSize(18); + circlePanel2.add(ledB); circlePanel2.add(filler3); - led6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led6); + ledF.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledF.setLedSize(18); + circlePanel2.add(ledF); circlePanel2.add(filler2); - led7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - circlePanel2.add(led7); + ledA.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + ledA.setLedSize(18); + circlePanel2.add(ledA); circlePanel2.add(filler1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); @@ -102,11 +129,11 @@ public class HexiposiPanel extends DevicePanel { private javax.swing.Box.Filler filler7; private javax.swing.Box.Filler filler8; private javax.swing.Box.Filler filler9; - private ch.psi.pshell.swing.Led led1; - private ch.psi.pshell.swing.Led led2; - private ch.psi.pshell.swing.Led led3; - private ch.psi.pshell.swing.Led led5; - private ch.psi.pshell.swing.Led led6; - private ch.psi.pshell.swing.Led led7; + private ch.psi.pshell.swing.Led ledA; + private ch.psi.pshell.swing.Led ledB; + private ch.psi.pshell.swing.Led ledC; + private ch.psi.pshell.swing.Led ledD; + private ch.psi.pshell.swing.Led ledE; + private ch.psi.pshell.swing.Led ledF; // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/ch/psi/mxsc/MainPanel.form b/src/main/java/ch/psi/mxsc/MainPanel.form index 3d35a8f..4fd8fa4 100644 --- a/src/main/java/ch/psi/mxsc/MainPanel.form +++ b/src/main/java/ch/psi/mxsc/MainPanel.form @@ -405,7 +405,7 @@ - + @@ -428,7 +428,7 @@ - + @@ -455,7 +455,7 @@ - + diff --git a/src/main/java/ch/psi/mxsc/MainPanel.java b/src/main/java/ch/psi/mxsc/MainPanel.java index 58beefd..a759ec4 100644 --- a/src/main/java/ch/psi/mxsc/MainPanel.java +++ b/src/main/java/ch/psi/mxsc/MainPanel.java @@ -29,11 +29,11 @@ public class MainPanel extends Panel { public void onInitialize(int runCount) { Controller.getInstance().onInitialize(runCount); basePlatePanel.setDevice((Device) getDevice("BasePlate")); - basePlatePanel.setSelectable(false); - + basePlatePanel.getDevice().setSelectable(false); } + /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is @@ -70,7 +70,7 @@ public class MainPanel extends Panel { led4 = new ch.psi.pshell.swing.Led(); jLabel7 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); - hexiposiPanel1 = new ch.psi.mxsc.HexiposiPanel(); + hexiposiPanel = new ch.psi.mxsc.HexiposiPanel(); jPanel7 = new javax.swing.JPanel(); led5 = new ch.psi.pshell.swing.Led(); led6 = new ch.psi.pshell.swing.Led(); @@ -298,7 +298,7 @@ public class MainPanel extends Panel { .addComponent(jLabel7)) .addComponent(jButton1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) - .addComponent(hexiposiPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(hexiposiPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jPanel6Layout.setVerticalGroup( @@ -316,7 +316,7 @@ public class MainPanel extends Panel { .addComponent(jLabel7)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1)) - .addComponent(hexiposiPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(hexiposiPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); @@ -530,7 +530,7 @@ public class MainPanel extends Panel { // Variables declaration - do not modify//GEN-BEGIN:variables private ch.psi.mxsc.BasePlatePanel basePlatePanel; private javax.swing.JToggleButton buttonCamera; - private ch.psi.mxsc.HexiposiPanel hexiposiPanel1; + private ch.psi.mxsc.HexiposiPanel hexiposiPanel; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; diff --git a/src/main/java/ch/psi/mxsc/Puck.java b/src/main/java/ch/psi/mxsc/Puck.java index aefafc0..b0e938a 100644 --- a/src/main/java/ch/psi/mxsc/Puck.java +++ b/src/main/java/ch/psi/mxsc/Puck.java @@ -9,6 +9,7 @@ import ch.psi.pshell.imaging.DimensionDouble; import ch.psi.pshell.imaging.PointDouble; import ch.psi.utils.swing.MainFrame; import ch.psi.utils.swing.SwingUtils; +import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; @@ -41,6 +42,28 @@ public class Puck extends DeviceBase { new PointDouble(-28.39445573, 44.18263554) }; + static final Character[] SEGMENTS = new Character[]{ 'A', 'A', + 'F', 'F','F','F','F', + 'E', 'E','E','E','E', + 'D', 'D','D','D','D', + 'C', 'C','C','C','C', + 'B', 'B','B','B','B', + 'A', 'A', 'A'}; + static final int[] NUMBERS = new int[] { 1, 2, + 5, 4, 3, 1, 2, + 5, 4, 3, 1, 2, + 5, 4, 3, 1, 2, + 5, 4, 3, 1, 2, + 5, 4, 3, 1, 2, + 5, 4, 3}; + static final int[] ADDRESSES = new int[] { 6, 7, + 5, 4, 3, 1, 2, + 20, 19, 18, 16, 17, + 15, 14, 13, 11, 12, + 30, 29, 28, 26, 27, + 25, 24, 23, 21, 22, + 10, 9, 8}; + final static PointDouble referencePosition = new PointDouble(0.0, -66.9 - 0.2); final static Double referenceSize = 6.2 + 0.2; @@ -48,11 +71,19 @@ public class Puck extends DeviceBase { final int index; + + Character segment; + final int address; + final int number; Puck(BasePlate basePlate, int index) { - super(String.valueOf(index+1)); + //super(String.valueOf(index+1)); + super(SEGMENTS[index] + "" + NUMBERS[index]); this.setParent(basePlate); this.index = index; + this.segment = SEGMENTS[index]; + this.number = NUMBERS[index]; + this.address = ADDRESSES[index]; for (int i =0; i< numberOfSamples; i++){ new Sample(this, i); } @@ -80,8 +111,13 @@ public class Puck extends DeviceBase { return samplesPosition[sample.index]; } - + public boolean isSegmentSelected(){ + return ("" + getSegment()).equalsIgnoreCase(Controller.getInstance().getHexaposiPosition()); + } + public boolean isHighlithted(){ + return isSelected() || isSegmentSelected(); + } public int getIndex() { return index; @@ -93,10 +129,22 @@ public class Puck extends DeviceBase { return id; } - public void seId(String value) { + public void setId(String value) { id = value; } + + public char getSegment(){ + return segment; + } + + public int getNumber() { + return number; + } + public int getAddress() { + return address; + } + boolean enabled; public boolean isEnabled() { @@ -124,10 +172,10 @@ public class Puck extends DeviceBase { return detectionError; } - boolean selected; + private boolean selected; public boolean isSelected() { - return selected; + return selected && getBasePlate().isSelectable(); } public void setSelected(boolean value) { @@ -154,22 +202,18 @@ public class Puck extends DeviceBase { Color ret = Color.LIGHT_GRAY; switch (detection){ case Empty: - ret = Color.LIGHT_GRAY; + ret = isHighlithted() ? new Color(224, 224, 224) : Color.LIGHT_GRAY; break; case Present: - ret = new Color(0, 92, 92); + ret = isHighlithted() ? new Color(0, 140, 140) : new Color(128, 192, 192); break; case Error: - ret = new Color(128, 0, 0); + ret = isHighlithted() ? new Color(192, 10, 10) : new Color(192, 128, 128); break; case Offline: - ret = new Color(92, 92, 0); - break; - } - - boolean selected = isSelected(); - if (isSelected()) { - ret = ret.brighter(); + ret = isHighlithted() ? new Color(250, 255, 48) : new Color(253, 194, 41); + //ret = isHighlithted() ? new Color(230, 142, 40) : new Color(216, 159, 93); + break; } return ret; @@ -186,7 +230,7 @@ public class Puck extends DeviceBase { (int)((getSize().getWidth() / getBasePlate().getSize().getWidth()) * plotRect.width), (int)((getSize().getHeight() / getBasePlate().getSize().getHeight()) * plotRect.height) ); - if (isSelected()) { + if (isHighlithted()) { ret += 2; } return ret; @@ -207,7 +251,8 @@ public class Puck extends DeviceBase { } Color getLabelColor(boolean drawBackground) { - return drawBackground ? Color.BLACK : new Color (0,196,0); + return drawBackground ? (isHighlithted() ? Color.BLACK : new Color(92, 92, 92)) : (isHighlithted() ? new Color (0,255,0) : new Color (0,162,0)); + } Font getLabelFont() { @@ -232,16 +277,21 @@ public class Puck extends DeviceBase { Color getBorderColor(boolean drawBackground) { if (drawBackground){ - if (!isEnabled()){ + /*if (!isEnabled()){ return Color.GRAY; - } else if (isSelected()){ + } else */ + if (isHighlithted()){ return new Color(32,32,32); } return Color.GRAY; } - return isSelected() ? new Color(0,32,0) : new Color(0,128,0); + return isHighlithted() ? new Color(0,208,0) : new Color(0,128,0); } + int getBorderWidth(boolean drawBackground) { + return drawBackground ? isHighlithted() ? 2 : 1: isHighlithted() ? 2 : 1 ; + } + int getReferenceDrawSize() { return (int)((referenceSize / getSize().getWidth()) * getDrawSize()); } @@ -267,8 +317,9 @@ public class Puck extends DeviceBase { g.fillOval(position.x - size / 2, position.y - size / 2, size, size); } g.setColor(getBorderColor(drawBackground)); + g.setStroke(new BasicStroke(getBorderWidth(drawBackground))); g.drawOval(position.x - size / 2, position.y - size / 2, size, size); - + g.setStroke(new BasicStroke(1f)); if (drawSamples){ //Draw samples for (Sample sample: getSamples()) { @@ -285,12 +336,16 @@ public class Puck extends DeviceBase { g.setColor(refColor); position = getReferenceDrawPosition(); size = getReferenceDrawSize(); - g.fillArc(position.x - size / 2, position.y - size / 2, size, size, 180, 180); - + if (drawBackground){ + g.fillArc(position.x - size / 2, position.y - size / 2, size, size, 180, 180); + } + //Draw text - String text = String.valueOf(getIndex() + 1); + String text = getName(); //String.valueOf(getIndex() + 1); Point labelPosition = getLabelPosition(text, g); g.setColor(getLabelColor(drawBackground)); + g.setFont(getLabelFont()); + g.drawString(text, labelPosition.x, labelPosition.y); if (drawId){ String id = getId(); if (id!=null) { @@ -300,7 +355,5 @@ public class Puck extends DeviceBase { g.drawString(id, getDrawPosition().x - textSize.width/2 , getDrawPosition().y + 10 ); } } - g.setFont(getLabelFont()); - g.drawString(text, labelPosition.x, labelPosition.y); } } diff --git a/src/main/java/ch/psi/mxsc/PuckDetection.java b/src/main/java/ch/psi/mxsc/PuckDetection.java index 838ff8e..4ffe506 100644 --- a/src/main/java/ch/psi/mxsc/PuckDetection.java +++ b/src/main/java/ch/psi/mxsc/PuckDetection.java @@ -1,6 +1,5 @@ package ch.psi.mxsc; - import ch.psi.pshell.core.JsonSerializer; import ch.psi.pshell.device.DeviceBase; import ch.psi.utils.Arr; @@ -13,33 +12,33 @@ import java.util.List; import java.util.Map; import java.util.logging.Level; +public class PuckDetection extends DeviceBase { -public class PuckDetection extends DeviceBase{ final String server; public volatile Chrono chrono; boolean debug; - - public PuckDetection(String name, String server){ + + public PuckDetection(String name, String server) { super(name); this.server = server.startsWith("tcp://") ? server : "tcp://" + server; - } - - public boolean isDebug(){ + } + + public boolean isDebug() { return debug; } - public void setDebug(boolean value){ + public void setDebug(boolean value) { debug = value; } Thread thread; Thread watchDog; - + @Override - protected void doInitialize() throws IOException, InterruptedException{ + protected void doInitialize() throws IOException, InterruptedException { doClose(); - super.doInitialize(); - + super.doInitialize(); + chrono = new Chrono(); thread = new Thread(new Runnable() { @Override @@ -49,111 +48,129 @@ public class PuckDetection extends DeviceBase{ }); thread.setDaemon(true); thread.start(); - + watchDog = new Thread(new Runnable() { @Override public void run() { - try{ + try { while (!Thread.currentThread().isInterrupted()) { - if (chrono.isTimeout(3000)){ + if (chrono.isTimeout(3000)) { setState(State.Offline); - if (Controller.getInstance()!=null){ + if (Controller.getInstance() != null) { Controller.getInstance().clearPuckStates(); } - } + } Thread.sleep(1000); } - } catch (Exception ex){ + } catch (Exception ex) { getLogger().log(Level.INFO, null, ex); } } }); watchDog.setDaemon(true); - watchDog.start(); + watchDog.start(); } - - void subscriberTask(){ - try{ - setState(State.Ready); - org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1); - org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB); - subscriber.connect(server); - //subscriber.subscribe("Status".getBytes()); - subscriber.subscribe("".getBytes()); - try{ - if (debug){ - System.out.println("Start listening"); - } + + void subscriberTask() { + try { + setState(State.Ready); + if (isSimulated()) { while (!Thread.currentThread().isInterrupted()) { - //String type = subscriber.recvStr(); - //System.out.println(type); - String contents = subscriber.recvStr(); - if (debug){ - System.out.println(contents); + for (int address = 1; address <= Controller.NUMBER_OF_PUCKS; address++) { + Integer indDetector = ((address <= 6) || (address==30)) ? 1 : 0; + Integer mecDetector = ((address <= 6) || (address==29)) ? 1 : 0; + int index = Controller.getInstance().getPuckIndex(address); + PuckState puck = Controller.getInstance().getPuckState(index); + puck.set(mecDetector, indDetector); } - processMessage(contents); - - if (Controller.getInstance()!=null){ - Controller.getInstance().getMainFrame().repaint(); - } - setState(State.Ready); + if (Controller.getInstance() != null) { + Controller.getInstance().updateView(); + } chrono = new Chrono(); + Thread.sleep(2000); } - } finally{ - if (debug){ - System.out.println("Stop listening"); + } else { + org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1); + org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB); + subscriber.connect(server); + //subscriber.subscribe("Status".getBytes()); + subscriber.subscribe("".getBytes()); + try { + if (debug) { + System.out.println("Start listening"); + } + while (!Thread.currentThread().isInterrupted()) { + //String type = subscriber.recvStr(); + //System.out.println(type); + String contents = subscriber.recvStr(); + if (debug) { + System.out.println(contents); + } + processMessage(contents); + + if (Controller.getInstance() != null) { + Controller.getInstance().updateView(); + } + setState(State.Ready); + chrono = new Chrono(); + } + } finally { + if (debug) { + System.out.println("Stop listening"); + } + + if (Controller.getInstance() != null) { + Controller.getInstance().clearPuckStates(); + } + subscriber.close(); + context.term(); } - - if (Controller.getInstance()!=null){ - Controller.getInstance().clearPuckStates(); - } - subscriber.close(); - context.term(); } - } catch (Exception ex){ + } catch (Exception ex) { getLogger().log(Level.INFO, null, ex); } setState(State.Offline); } - - void processMessage(String str){ - try{ + + void processMessage(String str) { + try { //System.out.println(str); - List detection = (List) JsonSerializer.decode (str, List.class); - - int id=1; - for (List bus : detection){ - for (List sensor : bus){ + List detection = (List) JsonSerializer.decode(str, List.class); + + int address = 1; + for (List bus : detection) { + for (List sensor : bus) { Integer indDetector = sensor.get(0); - Integer mecDetector = sensor.get(1); - PuckState puck = Controller.getInstance().getPuckState(id); + Integer mecDetector = sensor.get(1); + int index = Controller.getInstance().getPuckIndex(address); + PuckState puck = Controller.getInstance().getPuckState(index); puck.set(mecDetector, indDetector); - - id++; - } + + address++; + } } - } catch (Exception ex){ + } catch (Exception ex) { getLogger().log(Level.INFO, null, ex); - } + } } - + @Override - protected void doClose(){ - if (watchDog!=null){ + protected void doClose() { + if (watchDog != null) { watchDog.interrupt(); watchDog = null; } - if (thread!=null){ + if (thread != null) { thread.interrupt(); thread = null; } } - + public static void main(String[] args) throws IOException, InterruptedException { - PuckDetection pd = new PuckDetection("PD","129.129.110.99:5556"); + PuckDetection pd = new PuckDetection("PD", "129.129.110.99:5556"); //PuckDetection pd = new PuckDetection("PD","raspberrypi:5556"); pd.setDebug(true); - pd.initialize(); + pd.initialize(); Thread.sleep(100000); } } diff --git a/src/main/java/ch/psi/mxsc/PuckState.java b/src/main/java/ch/psi/mxsc/PuckState.java index 6145875..96940d0 100644 --- a/src/main/java/ch/psi/mxsc/PuckState.java +++ b/src/main/java/ch/psi/mxsc/PuckState.java @@ -36,9 +36,9 @@ public class PuckState { void set(int mecSwitch, int indSwitch) { online = true; - //TODO: Hanfle -1 value: error + //TODO: Handle -1 value: error this.mecSwitch = mecSwitch ==1; - this.indSwitch = mecSwitch ==1; + this.indSwitch = indSwitch ==1; BasePlate basePlate = getBasePlate(); if (basePlate != null) { if (this.mecSwitch != this.indSwitch) { diff --git a/src/main/java/ch/psi/mxsc/Sample.java b/src/main/java/ch/psi/mxsc/Sample.java index eea1949..d2553e3 100644 --- a/src/main/java/ch/psi/mxsc/Sample.java +++ b/src/main/java/ch/psi/mxsc/Sample.java @@ -23,7 +23,7 @@ public class Sample extends DeviceBase { final int index; public Sample(Puck puck, int index){ - super((puck.getIndex()+1) + ":" + (index+1)); + super(puck.getName() + ":" + (index+1)); setParent(puck); this.index = index; } @@ -55,7 +55,7 @@ public class Sample extends DeviceBase { boolean selected; public boolean isSelected() { - return selected; + return selected && getPuck().getBasePlate().isSelectable(); } public void setSelected(boolean value) { @@ -112,7 +112,7 @@ public class Sample extends DeviceBase { ret = Color.CYAN.darker().darker(); } - if (selected) { + if (isSelected()) { ret = ret.brighter(); } return ret; @@ -141,7 +141,7 @@ public class Sample extends DeviceBase { Color getLabelColor(boolean drawBackground) { - return drawBackground ? Color.DARK_GRAY : new Color (0,128,0); + return drawBackground ? Color.DARK_GRAY : new Color (0,96,0); } Font getLabelFont() {