Aded puck detection using Esera

This commit is contained in:
gac-S_Changer
2017-03-16 16:18:09 +01:00
parent 4d0b19d801
commit ff1bfbe15c
4 changed files with 188 additions and 100 deletions

View File

@@ -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);
}
}
}
}