Closedown

This commit is contained in:
gac-S_Changer
2017-02-10 11:39:31 +01:00
parent 4317184b9f
commit c8aa2524d5

View File

@@ -1,6 +1,7 @@
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;
@@ -10,6 +11,7 @@ import java.util.logging.Level;
public class PuckDetection extends DeviceBase{
public static final int PUCKS_NUMBER = 30;
final String server;
Chrono chrono;
public PuckDetection(String name, String server){
super(name);
@@ -55,6 +57,7 @@ public class PuckDetection extends DeviceBase{
Thread thread;
Thread watchDog;
@Override
protected void doInitialize() throws IOException, InterruptedException{
@@ -64,6 +67,7 @@ public class PuckDetection extends DeviceBase{
for (int i=0; i<PUCKS_NUMBER; i++ ){
pucks[i] = new PuckState();
}
chrono = new Chrono();
thread = new Thread(new Runnable() {
@Override
public void run() {
@@ -72,6 +76,20 @@ public class PuckDetection extends DeviceBase{
});
thread.setDaemon(true);
thread.start();
watchDog = new Thread(new Runnable() {
@Override
public void run() {
if (chrono.isTimeout(3000)){
setState(State.Offline);
for (PuckState puck:pucks){
puck.clear();
}
}
}
});
watchDog.setDaemon(true);
watchDog.start();
}
@@ -89,6 +107,8 @@ public class PuckDetection extends DeviceBase{
String contents = subscriber.recvStr();
System.out.println(contents);
processMessage(contents);
setState(State.Ready);
chrono = new Chrono();
}
} finally{
for (PuckState puck:pucks){
@@ -130,6 +150,10 @@ public class PuckDetection extends DeviceBase{
@Override
protected void doClose(){
if (watchDog!=null){
watchDog.interrupt();
watchDog = null;
}
if (thread!=null){
thread.interrupt();
thread = null;