MainPanel prototype

This commit is contained in:
gac-S_Changer
2018-03-16 15:00:32 +01:00
parent 983520f388
commit 3ccf49f01d
3 changed files with 46 additions and 36 deletions
@@ -38,7 +38,7 @@ public class EseraDetection implements AutoCloseable{
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){ for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
try{ try{
List det = (List) l.get(i); List det = (List) l.get(i);
puckState[i].set(det.get(0).equals(0), det.get(1).equals(0)); puckState[i].set((det.get(0).equals(0)) ? 1 : 0, (det.get(1).equals(0)) ? 1 : 0);
} catch (Exception ex){ } catch (Exception ex){
puckState[i].clear(); puckState[i].clear();
} }
+39 -30
View File
@@ -1,12 +1,16 @@
package ch.psi.mxsc; package ch.psi.mxsc;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.DeviceBase; import ch.psi.pshell.device.DeviceBase;
import ch.psi.utils.Arr; import ch.psi.utils.Arr;
import ch.psi.utils.Chrono; import ch.psi.utils.Chrono;
import ch.psi.utils.State; import ch.psi.utils.State;
import ch.psi.utils.Str;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@@ -44,7 +48,9 @@ public class PuckDetection extends DeviceBase{
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(3000)){ if (chrono.isTimeout(3000)){
setState(State.Offline); setState(State.Offline);
Controller.getInstance().clearPuckStates(); if (Controller.getInstance()!=null){
Controller.getInstance().clearPuckStates();
}
} }
Thread.sleep(1000); Thread.sleep(1000);
} }
@@ -63,14 +69,16 @@ public class PuckDetection extends DeviceBase{
org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1); org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1);
org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB); org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect(server); subscriber.connect(server);
subscriber.subscribe("Status".getBytes()); //subscriber.subscribe("Status".getBytes());
subscriber.subscribe("".getBytes());
try{ try{
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
String type = subscriber.recvStr(); //String type = subscriber.recvStr();
//System.out.println(type); //System.out.println(type);
String contents = subscriber.recvStr(); String contents = subscriber.recvStr();
//System.out.println(contents); //System.out.println(contents);
processMessage(contents); processMessage(contents);
if (Controller.getInstance()!=null){ if (Controller.getInstance()!=null){
Controller.getInstance().getMainFrame().repaint(); Controller.getInstance().getMainFrame().repaint();
} }
@@ -78,7 +86,9 @@ public class PuckDetection extends DeviceBase{
chrono = new Chrono(); chrono = new Chrono();
} }
} finally{ } finally{
Controller.getInstance().clearPuckStates(); if (Controller.getInstance()!=null){
Controller.getInstance().clearPuckStates();
}
subscriber.close(); subscriber.close();
context.term(); context.term();
} }
@@ -88,33 +98,25 @@ public class PuckDetection extends DeviceBase{
setState(State.Offline); setState(State.Offline);
} }
void processMessage(String msg){ void processMessage(String str){
ArrayList<Integer> present = new ArrayList<>(); try{
for (String line:msg.split("\t")){ //System.out.println(str);
try{ List<List> detection = (List) JsonSerializer.decode (str, List.class);
line = line.trim();
String[] tokens = line.split(" "); int id=1;
int id = Integer.valueOf(tokens[0].substring(1)); for (List<List> bus : detection){
present.add(id); for (List<Integer> sensor : bus){
PuckState puck = Controller.getInstance().getPuckState(id); Integer indDetector = sensor.get(0);
if (tokens.length<3){ Integer mecDetector = sensor.get(0);
puck.clear(); PuckState puck = Controller.getInstance().getPuckState(id);
} else { puck.set(mecDetector, indDetector);
puck.set(tokens[1].trim().equals("1"),tokens[2].trim().equals("1"));
} id++;
} catch (Exception ex){ }
getLogger().log(Level.INFO, null, ex);
} }
} } catch (Exception ex){
for (int i=1; i<= Controller.NUMBER_OF_PUCKS; i++){ getLogger().log(Level.INFO, null, ex);
if (!Arr.containsEqual(present.toArray(), i)){ }
try{
Controller.getInstance().getPuckState(i).clear();
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
}
}
} }
@Override @Override
@@ -128,4 +130,11 @@ public class PuckDetection extends DeviceBase{
thread = null; thread = null;
} }
} }
public static void main(String[] args) throws IOException, InterruptedException {
PuckDetection pd = new PuckDetection("PD","129.129.110.83:5556");
pd.initialize();
Thread.sleep(100000);
}
} }
+6 -5
View File
@@ -34,15 +34,16 @@ public class PuckState {
} }
} }
void set(boolean mecSwitch, boolean indSwitch) { void set(int mecSwitch, int indSwitch) {
online = true; online = true;
this.mecSwitch = mecSwitch; //TODO: Hanfle -1 value: error
this.indSwitch = indSwitch; this.mecSwitch = mecSwitch ==1;
this.indSwitch = mecSwitch ==1;
BasePlate basePlate = getBasePlate(); BasePlate basePlate = getBasePlate();
if (basePlate != null) { if (basePlate != null) {
if (mecSwitch != indSwitch) { if (this.mecSwitch != this.indSwitch) {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Error; basePlate.getPucks()[id - 1].detection = Puck.Detection.Error;
} else if (mecSwitch) { } else if (this.mecSwitch) {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Present; basePlate.getPucks()[id - 1].detection = Puck.Detection.Present;
} else { } else {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty; basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty;