Aded puck detection using Esera
This commit is contained in:
58
src/main/java/ch/psi/mxsc/EseraDetection.java
Normal file
58
src/main/java/ch/psi/mxsc/EseraDetection.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package ch.psi.mxsc;
|
||||
|
||||
import static ch.psi.mxsc.MainPanel.NUMBER_OF_PUCKS;
|
||||
import ch.psi.pshell.device.Device;
|
||||
import ch.psi.pshell.device.DeviceAdapter;
|
||||
import ch.psi.utils.State;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class EseraDetection implements AutoCloseable{
|
||||
final Device device;
|
||||
|
||||
public EseraDetection(Device device){
|
||||
this.device = device;
|
||||
if (device!=null){
|
||||
device.addListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DeviceAdapter listener = new DeviceAdapter(){
|
||||
@Override
|
||||
public void onStateChanged(Device device, State state, State former) {
|
||||
if (!state.isInitialized()){
|
||||
MainPanel.getInstance().clearPuckStates();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onValueChanged(Device device, Object value, Object former) {
|
||||
if ((value == null) || !(value instanceof List)){
|
||||
MainPanel.getInstance().clearPuckStates();
|
||||
} else {
|
||||
List l = (List)value;
|
||||
PuckState[] puckState = MainPanel.getInstance().getPuckStates();
|
||||
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
|
||||
try{
|
||||
List det = (List) l.get(i);
|
||||
puckState[i].set(det.get(0).equals(0), det.get(1).equals(0));
|
||||
} catch (Exception ex){
|
||||
puckState[i].clear();
|
||||
}
|
||||
}
|
||||
MainPanel.getInstance().repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (device!=null){
|
||||
device.removeListener(listener);
|
||||
}
|
||||
MainPanel.getInstance().clearPuckStates();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
package ch.psi.mxsc;
|
||||
|
||||
import ch.psi.pshell.core.DevicePoolListener;
|
||||
import ch.psi.pshell.device.Device;
|
||||
import ch.psi.pshell.device.GenericDevice;
|
||||
import ch.psi.pshell.ui.Panel;
|
||||
import ch.psi.utils.swing.SwingUtils;
|
||||
@@ -16,7 +18,32 @@ public class MainPanel extends Panel {
|
||||
BasePlate basePlate;
|
||||
/** Creates new form Panel */
|
||||
static MainPanel instance;
|
||||
static String PUCK_DETECTION_DEVICE = "onewire";
|
||||
|
||||
public static final int NUMBER_OF_PUCKS = 30;
|
||||
|
||||
final PuckState[] puckState;
|
||||
public PuckState[] getPuckStates(){
|
||||
return puckState;
|
||||
}
|
||||
EseraDetection detection;
|
||||
|
||||
//From 1 to PUCKS_NUMBER
|
||||
public PuckState getPuckState(int id) throws Exception{
|
||||
if ((id<=0) || (id>NUMBER_OF_PUCKS)){
|
||||
throw new Exception("invalid puck id: "+ id);
|
||||
}
|
||||
return getPuckStates()[id-1];
|
||||
}
|
||||
|
||||
public void clearPuckStates(){
|
||||
for (PuckState puck:getPuckStates()){
|
||||
puck.clear();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
public static MainPanel getInstance(){
|
||||
return instance;
|
||||
}
|
||||
@@ -26,20 +53,46 @@ public class MainPanel extends Panel {
|
||||
basePlate = new BasePlate();
|
||||
SwingUtils.setEnumCombo(comboMode, BasePlatePanel.Mode.class);
|
||||
comboMode.setSelectedItem(basePlatePanel.getMode());
|
||||
this.setPersistedComponents(new Component[]{comboMode});
|
||||
this.setPersistedComponents(new Component[]{comboMode});
|
||||
puckState = new PuckState[NUMBER_OF_PUCKS];
|
||||
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
|
||||
puckState[i] = new PuckState(i+1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onInitialize(int runCount) {
|
||||
basePlatePanel.setDevice(basePlate);
|
||||
//basePlatePanel.setShowSamples(true);
|
||||
//puckPanel.setDevice(basePlate.getPucks()[0]);
|
||||
GenericDevice former = getDevice("BasePlate");
|
||||
if (former!=null){
|
||||
removeDevice(former);
|
||||
}
|
||||
addDevice(basePlate);
|
||||
addDevice(basePlate);
|
||||
clearPuckStates();
|
||||
|
||||
getContext().getDevicePool().addListener(new DevicePoolListener() {
|
||||
@Override
|
||||
public void onDeviceAdded(GenericDevice dev) {
|
||||
if (dev.getName().equals(PUCK_DETECTION_DEVICE)){
|
||||
detection = new EseraDetection((Device) dev);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onDeviceRemoved(GenericDevice dev) {
|
||||
if (dev.getName().equals(PUCK_DETECTION_DEVICE)){
|
||||
detection.close();
|
||||
detection = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (detection != null){
|
||||
detection.close();
|
||||
detection = null;
|
||||
}
|
||||
if ((Device) getDevice(PUCK_DETECTION_DEVICE) != null){
|
||||
detection = new EseraDetection((Device) getDevice(PUCK_DETECTION_DEVICE));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package ch.psi.mxsc;
|
||||
|
||||
|
||||
import ch.psi.mxsc.Puck.Detection;
|
||||
import ch.psi.pshell.device.DeviceBase;
|
||||
import ch.psi.utils.Arr;
|
||||
import ch.psi.utils.Chrono;
|
||||
@@ -9,92 +8,16 @@ import ch.psi.utils.State;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Level;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
|
||||
public class PuckDetection extends DeviceBase{
|
||||
public static final int PUCKS_NUMBER = 30;
|
||||
public class PuckDetection extends DeviceBase{
|
||||
final String server;
|
||||
public volatile Chrono chrono;
|
||||
|
||||
public PuckDetection(String name, String server){
|
||||
super(name);
|
||||
this.server = server.startsWith("tcp://") ? server : "tcp://" + server;
|
||||
}
|
||||
|
||||
void updateGraphics(){
|
||||
if (MainPanel.getInstance()!=null){
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
MainPanel.getInstance().repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static class PuckState{
|
||||
public final int id;
|
||||
public boolean online;
|
||||
public boolean mecSwitch;
|
||||
public boolean indSwitch;
|
||||
|
||||
PuckState(int id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
BasePlate getBasePlate(){
|
||||
if (MainPanel.getInstance()==null){
|
||||
return null;
|
||||
}
|
||||
return MainPanel.getInstance().basePlate;
|
||||
}
|
||||
|
||||
void clear(){
|
||||
online = false;
|
||||
mecSwitch = false;
|
||||
indSwitch = false;
|
||||
BasePlate basePlate = getBasePlate();
|
||||
if (basePlate!=null){
|
||||
basePlate.getPucks()[id-1].detection = Detection.Offline;
|
||||
}
|
||||
}
|
||||
void set(boolean mecSwitch, boolean indSwitch){
|
||||
online = true;
|
||||
this.mecSwitch = mecSwitch;
|
||||
this.indSwitch = indSwitch;
|
||||
BasePlate basePlate = getBasePlate();
|
||||
if (basePlate!=null){
|
||||
if (mecSwitch!=indSwitch){
|
||||
basePlate.getPucks()[id-1].detection = Detection.Error;
|
||||
} else if (mecSwitch){
|
||||
basePlate.getPucks()[id-1].detection = Detection.Present;
|
||||
} else {
|
||||
basePlate.getPucks()[id-1].detection = Detection.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "Online = " + online + "\ns1 = " + mecSwitch+ "\ns2 = " + indSwitch;
|
||||
}
|
||||
}
|
||||
|
||||
PuckState[] pucks;
|
||||
public PuckState[] getPucks(){
|
||||
return pucks;
|
||||
}
|
||||
|
||||
//From 1 to PUCKS_NUMBER
|
||||
public PuckState getPuck(int id) throws Exception{
|
||||
assertInitialized();
|
||||
if ((id<=0) || (id>PUCKS_NUMBER)){
|
||||
throw new Exception("invalid puck id: "+ id);
|
||||
}
|
||||
return pucks[id-1];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Thread thread;
|
||||
Thread watchDog;
|
||||
@@ -104,10 +27,6 @@ public class PuckDetection extends DeviceBase{
|
||||
doClose();
|
||||
super.doInitialize();
|
||||
|
||||
pucks = new PuckState[PUCKS_NUMBER];
|
||||
for (int i=0; i<PUCKS_NUMBER; i++ ){
|
||||
pucks[i] = new PuckState(i+1);
|
||||
}
|
||||
chrono = new Chrono();
|
||||
thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
@@ -125,11 +44,8 @@ public class PuckDetection extends DeviceBase{
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
if (chrono.isTimeout(3000)){
|
||||
setState(State.Offline);
|
||||
for (PuckState puck:pucks){
|
||||
puck.clear();
|
||||
}
|
||||
}
|
||||
updateGraphics();
|
||||
MainPanel.getInstance().clearPuckStates();
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception ex){
|
||||
@@ -156,15 +72,14 @@ public class PuckDetection extends DeviceBase{
|
||||
String contents = subscriber.recvStr();
|
||||
System.out.println(contents);
|
||||
processMessage(contents);
|
||||
updateGraphics();
|
||||
if (MainPanel.getInstance()!=null){
|
||||
MainPanel.getInstance().repaint();
|
||||
}
|
||||
setState(State.Ready);
|
||||
chrono = new Chrono();
|
||||
}
|
||||
} finally{
|
||||
for (PuckState puck:pucks){
|
||||
puck.clear();
|
||||
}
|
||||
updateGraphics();
|
||||
MainPanel.getInstance().clearPuckStates();
|
||||
subscriber.close();
|
||||
context.term();
|
||||
}
|
||||
@@ -182,7 +97,7 @@ public class PuckDetection extends DeviceBase{
|
||||
String[] tokens = line.split(" ");
|
||||
int id = Integer.valueOf(tokens[0].substring(1));
|
||||
present.add(id);
|
||||
PuckState puck = getPuck(id);
|
||||
PuckState puck = MainPanel.getInstance().getPuckState(id);
|
||||
if (tokens.length<3){
|
||||
puck.clear();
|
||||
} else {
|
||||
@@ -192,9 +107,13 @@ public class PuckDetection extends DeviceBase{
|
||||
getLogger().log(Level.INFO, null, ex);
|
||||
}
|
||||
}
|
||||
for (int i=1; i<= PUCKS_NUMBER; i++){
|
||||
for (int i=1; i<= MainPanel.NUMBER_OF_PUCKS; i++){
|
||||
if (!Arr.containsEqual(present.toArray(), i)){
|
||||
pucks[i-1].clear();
|
||||
try{
|
||||
MainPanel.getInstance().getPuckState(i).clear();
|
||||
} catch (Exception ex){
|
||||
getLogger().log(Level.INFO, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
58
src/main/java/ch/psi/mxsc/PuckState.java
Normal file
58
src/main/java/ch/psi/mxsc/PuckState.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
|
||||
*/
|
||||
package ch.psi.mxsc;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PuckState {
|
||||
|
||||
public final int id;
|
||||
public boolean online;
|
||||
public boolean mecSwitch;
|
||||
public boolean indSwitch;
|
||||
|
||||
PuckState(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
BasePlate getBasePlate() {
|
||||
if (MainPanel.getInstance() == null) {
|
||||
return null;
|
||||
}
|
||||
return MainPanel.getInstance().basePlate;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
online = false;
|
||||
mecSwitch = false;
|
||||
indSwitch = false;
|
||||
BasePlate basePlate = getBasePlate();
|
||||
if (basePlate != null) {
|
||||
basePlate.getPucks()[id - 1].detection = Puck.Detection.Offline;
|
||||
}
|
||||
}
|
||||
|
||||
void set(boolean mecSwitch, boolean indSwitch) {
|
||||
online = true;
|
||||
this.mecSwitch = mecSwitch;
|
||||
this.indSwitch = indSwitch;
|
||||
BasePlate basePlate = getBasePlate();
|
||||
if (basePlate != null) {
|
||||
if (mecSwitch != indSwitch) {
|
||||
basePlate.getPucks()[id - 1].detection = Puck.Detection.Error;
|
||||
} else if (mecSwitch) {
|
||||
basePlate.getPucks()[id - 1].detection = Puck.Detection.Present;
|
||||
} else {
|
||||
basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Online = " + online + "\ns1 = " + mecSwitch + "\ns2 = " + indSwitch;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user