From 5b45c42f771f4e6056c85d7b13b7d9ac6ec78a87 Mon Sep 17 00:00:00 2001 From: gac-S_Changer Date: Fri, 23 Jun 2017 15:03:27 +0200 Subject: [PATCH] Startup --- plugins/PuckDetection.java | 169 +++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 plugins/PuckDetection.java diff --git a/plugins/PuckDetection.java b/plugins/PuckDetection.java new file mode 100644 index 0000000..8edf011 --- /dev/null +++ b/plugins/PuckDetection.java @@ -0,0 +1,169 @@ + +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; + + +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; + } + + + public static class PuckState{ + public boolean online; + public boolean mecSwitch; + public boolean indSwitch; + + void clear(){ + online = false; + mecSwitch = false; + indSwitch = false; + } + void set(boolean mecSwitch, boolean indSwitch){ + online = true; + this.mecSwitch = mecSwitch; + this.indSwitch = indSwitch; + } + + @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 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; + } + } +}