This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package ch.psi.mxsc;
|
||||
|
||||
|
||||
import ch.psi.pshell.core.JsonSerializer;
|
||||
import ch.psi.pshell.device.DeviceBase;
|
||||
import ch.psi.utils.Arr;
|
||||
@@ -13,33 +12,33 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class PuckDetection extends DeviceBase {
|
||||
|
||||
public class PuckDetection extends DeviceBase{
|
||||
final String server;
|
||||
public volatile Chrono chrono;
|
||||
boolean debug;
|
||||
|
||||
public PuckDetection(String name, String server){
|
||||
|
||||
public PuckDetection(String name, String server) {
|
||||
super(name);
|
||||
this.server = server.startsWith("tcp://") ? server : "tcp://" + server;
|
||||
}
|
||||
|
||||
public boolean isDebug(){
|
||||
}
|
||||
|
||||
public boolean isDebug() {
|
||||
return debug;
|
||||
}
|
||||
|
||||
public void setDebug(boolean value){
|
||||
public void setDebug(boolean value) {
|
||||
debug = value;
|
||||
}
|
||||
|
||||
Thread thread;
|
||||
Thread watchDog;
|
||||
|
||||
|
||||
@Override
|
||||
protected void doInitialize() throws IOException, InterruptedException{
|
||||
protected void doInitialize() throws IOException, InterruptedException {
|
||||
doClose();
|
||||
super.doInitialize();
|
||||
|
||||
super.doInitialize();
|
||||
|
||||
chrono = new Chrono();
|
||||
thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
@@ -49,111 +48,129 @@ public class PuckDetection extends DeviceBase{
|
||||
});
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
|
||||
|
||||
watchDog = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
try {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
if (chrono.isTimeout(3000)){
|
||||
if (chrono.isTimeout(3000)) {
|
||||
setState(State.Offline);
|
||||
if (Controller.getInstance()!=null){
|
||||
if (Controller.getInstance() != null) {
|
||||
Controller.getInstance().clearPuckStates();
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception ex){
|
||||
} catch (Exception ex) {
|
||||
getLogger().log(Level.INFO, null, ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
watchDog.setDaemon(true);
|
||||
watchDog.start();
|
||||
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());
|
||||
subscriber.subscribe("".getBytes());
|
||||
try{
|
||||
if (debug){
|
||||
System.out.println("Start listening");
|
||||
}
|
||||
|
||||
void subscriberTask() {
|
||||
try {
|
||||
setState(State.Ready);
|
||||
if (isSimulated()) {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
//String type = subscriber.recvStr();
|
||||
//System.out.println(type);
|
||||
String contents = subscriber.recvStr();
|
||||
if (debug){
|
||||
System.out.println(contents);
|
||||
for (int address = 1; address <= Controller.NUMBER_OF_PUCKS; address++) {
|
||||
Integer indDetector = ((address <= 6) || (address==30)) ? 1 : 0;
|
||||
Integer mecDetector = ((address <= 6) || (address==29)) ? 1 : 0;
|
||||
int index = Controller.getInstance().getPuckIndex(address);
|
||||
PuckState puck = Controller.getInstance().getPuckState(index);
|
||||
puck.set(mecDetector, indDetector);
|
||||
}
|
||||
processMessage(contents);
|
||||
|
||||
if (Controller.getInstance()!=null){
|
||||
Controller.getInstance().getMainFrame().repaint();
|
||||
}
|
||||
setState(State.Ready);
|
||||
if (Controller.getInstance() != null) {
|
||||
Controller.getInstance().updateView();
|
||||
}
|
||||
chrono = new Chrono();
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
} finally{
|
||||
if (debug){
|
||||
System.out.println("Stop listening");
|
||||
} else {
|
||||
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("".getBytes());
|
||||
try {
|
||||
if (debug) {
|
||||
System.out.println("Start listening");
|
||||
}
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
//String type = subscriber.recvStr();
|
||||
//System.out.println(type);
|
||||
String contents = subscriber.recvStr();
|
||||
if (debug) {
|
||||
System.out.println(contents);
|
||||
}
|
||||
processMessage(contents);
|
||||
|
||||
if (Controller.getInstance() != null) {
|
||||
Controller.getInstance().updateView();
|
||||
}
|
||||
setState(State.Ready);
|
||||
chrono = new Chrono();
|
||||
}
|
||||
} finally {
|
||||
if (debug) {
|
||||
System.out.println("Stop listening");
|
||||
}
|
||||
|
||||
if (Controller.getInstance() != null) {
|
||||
Controller.getInstance().clearPuckStates();
|
||||
}
|
||||
subscriber.close();
|
||||
context.term();
|
||||
}
|
||||
|
||||
if (Controller.getInstance()!=null){
|
||||
Controller.getInstance().clearPuckStates();
|
||||
}
|
||||
subscriber.close();
|
||||
context.term();
|
||||
}
|
||||
} catch (Exception ex){
|
||||
} catch (Exception ex) {
|
||||
getLogger().log(Level.INFO, null, ex);
|
||||
}
|
||||
setState(State.Offline);
|
||||
}
|
||||
|
||||
void processMessage(String str){
|
||||
try{
|
||||
|
||||
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){
|
||||
List<List> detection = (List) JsonSerializer.decode(str, List.class);
|
||||
|
||||
int address = 1;
|
||||
for (List<List> bus : detection) {
|
||||
for (List<Integer> sensor : bus) {
|
||||
Integer indDetector = sensor.get(0);
|
||||
Integer mecDetector = sensor.get(1);
|
||||
PuckState puck = Controller.getInstance().getPuckState(id);
|
||||
Integer mecDetector = sensor.get(1);
|
||||
int index = Controller.getInstance().getPuckIndex(address);
|
||||
PuckState puck = Controller.getInstance().getPuckState(index);
|
||||
puck.set(mecDetector, indDetector);
|
||||
|
||||
id++;
|
||||
}
|
||||
|
||||
address++;
|
||||
}
|
||||
}
|
||||
} catch (Exception ex){
|
||||
} catch (Exception ex) {
|
||||
getLogger().log(Level.INFO, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doClose(){
|
||||
if (watchDog!=null){
|
||||
protected void doClose() {
|
||||
if (watchDog != null) {
|
||||
watchDog.interrupt();
|
||||
watchDog = null;
|
||||
}
|
||||
if (thread!=null){
|
||||
if (thread != null) {
|
||||
thread.interrupt();
|
||||
thread = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
PuckDetection pd = new PuckDetection("PD","129.129.110.99:5556");
|
||||
PuckDetection pd = new PuckDetection("PD", "129.129.110.99:5556");
|
||||
//PuckDetection pd = new PuckDetection("PD","raspberrypi:5556");
|
||||
pd.setDebug(true);
|
||||
pd.initialize();
|
||||
pd.initialize();
|
||||
Thread.sleep(100000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user