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

View File

@@ -38,7 +38,7 @@ public class EseraDetection implements AutoCloseable{
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));
puckState[i].set((det.get(0).equals(0)) ? 1 : 0, (det.get(1).equals(0)) ? 1 : 0);
} catch (Exception ex){
puckState[i].clear();
}

View File

@@ -1,12 +1,16 @@
package ch.psi.mxsc;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.utils.Arr;
import ch.psi.utils.Chrono;
import ch.psi.utils.State;
import ch.psi.utils.Str;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@@ -44,7 +48,9 @@ public class PuckDetection extends DeviceBase{
while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(3000)){
setState(State.Offline);
Controller.getInstance().clearPuckStates();
if (Controller.getInstance()!=null){
Controller.getInstance().clearPuckStates();
}
}
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.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect(server);
subscriber.subscribe("Status".getBytes());
//subscriber.subscribe("Status".getBytes());
subscriber.subscribe("".getBytes());
try{
while (!Thread.currentThread().isInterrupted()) {
String type = subscriber.recvStr();
//String type = subscriber.recvStr();
//System.out.println(type);
String contents = subscriber.recvStr();
//System.out.println(contents);
processMessage(contents);
if (Controller.getInstance()!=null){
Controller.getInstance().getMainFrame().repaint();
}
@@ -78,7 +86,9 @@ public class PuckDetection extends DeviceBase{
chrono = new Chrono();
}
} finally{
Controller.getInstance().clearPuckStates();
if (Controller.getInstance()!=null){
Controller.getInstance().clearPuckStates();
}
subscriber.close();
context.term();
}
@@ -88,33 +98,25 @@ public class PuckDetection extends DeviceBase{
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 = Controller.getInstance().getPuckState(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);
void processMessage(String str){
try{
//System.out.println(str);
List<List> detection = (List) JsonSerializer.decode (str, List.class);
int id=1;
for (List<List> bus : detection){
for (List<Integer> sensor : bus){
Integer indDetector = sensor.get(0);
Integer mecDetector = sensor.get(0);
PuckState puck = Controller.getInstance().getPuckState(id);
puck.set(mecDetector, indDetector);
id++;
}
}
}
for (int i=1; i<= Controller.NUMBER_OF_PUCKS; i++){
if (!Arr.containsEqual(present.toArray(), i)){
try{
Controller.getInstance().getPuckState(i).clear();
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
}
}
} catch (Exception ex){
getLogger().log(Level.INFO, null, ex);
}
}
@Override
@@ -128,4 +130,11 @@ public class PuckDetection extends DeviceBase{
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);
}
}

View File

@@ -34,15 +34,16 @@ public class PuckState {
}
}
void set(boolean mecSwitch, boolean indSwitch) {
void set(int mecSwitch, int indSwitch) {
online = true;
this.mecSwitch = mecSwitch;
this.indSwitch = indSwitch;
//TODO: Hanfle -1 value: error
this.mecSwitch = mecSwitch ==1;
this.indSwitch = mecSwitch ==1;
BasePlate basePlate = getBasePlate();
if (basePlate != null) {
if (mecSwitch != indSwitch) {
if (this.mecSwitch != this.indSwitch) {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Error;
} else if (mecSwitch) {
} else if (this.mecSwitch) {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Present;
} else {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty;