This commit is contained in:
gac-S_Changer
2017-02-10 16:34:00 +01:00
parent 4ccd5f6ca1
commit 69e745730d
13 changed files with 1382 additions and 1038 deletions

View File

@@ -0,0 +1,213 @@
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;
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;
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;
@Override
protected void doInitialize() throws IOException, InterruptedException{
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
public void run() {
subscriberTask();
}
});
thread.setDaemon(true);
thread.start();
watchDog = new Thread(new Runnable() {
@Override
public void run() {
try{
while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(3000)){
setState(State.Offline);
for (PuckState puck:pucks){
puck.clear();
}
}
updateGraphics();
Thread.sleep(1000);
}
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
}
});
watchDog.setDaemon(true);
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());
try{
while (!Thread.currentThread().isInterrupted()) {
String type = subscriber.recvStr();
System.out.println(type);
String contents = subscriber.recvStr();
System.out.println(contents);
processMessage(contents);
updateGraphics();
setState(State.Ready);
chrono = new Chrono();
}
} finally{
for (PuckState puck:pucks){
puck.clear();
}
updateGraphics();
subscriber.close();
context.term();
}
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
setState(State.Offline);
}
void processMessage(String msg){
ArrayList<Integer> present = new ArrayList<>();
for (String line:msg.split("\t")){
try{
line = line.trim();
String[] tokens = line.split(" ");
int id = Integer.valueOf(tokens[0].substring(1));
present.add(id);
PuckState puck = getPuck(id);
if (tokens.length<3){
puck.clear();
} else {
puck.set(tokens[1].trim().equals("1"),tokens[2].trim().equals("1"));
}
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
}
for (int i=1; i<= PUCKS_NUMBER; i++){
if (!Arr.containsEqual(present.toArray(), i)){
pucks[i-1].clear();
}
}
}
@Override
protected void doClose(){
if (watchDog!=null){
watchDog.interrupt();
watchDog = null;
}
if (thread!=null){
thread.interrupt();
thread = null;
}
}
}