package ch.psi.mxsc; import ch.psi.pshell.core.JsonSerializer; import ch.psi.pshell.device.DeviceBase; import ch.psi.utils.Chrono; import ch.psi.utils.State; import ch.psi.utils.Threading; import java.io.IOException; import java.util.List; import java.util.logging.Level; public class PuckDetection extends DeviceBase { final String server; public volatile Chrono chrono; boolean debug; public PuckDetection(String name, String server) { super(name); this.server = server.startsWith("tcp://") ? server : "tcp://" + server; } public boolean isDebug() { return debug; } public void setDebug(boolean value) { debug = value; } Thread thread; Thread watchDog; @Override protected void doInitialize() throws IOException, InterruptedException { doClose(); super.doInitialize(); chrono = new Chrono(); thread = new Thread(new Runnable() { @Override public void run() { subscriberTask(); } }); thread.setDaemon(true); thread.start(); watchDog = new Thread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { if (chrono.isTimeout(3000)) { if (!isSimulated()) { setState(State.Offline); if (Controller.getInstance() != null) { Controller.getInstance().clearPuckStates(); } } } Thread.sleep(1000); } } catch (InterruptedException ex) { getLogger().fine("Watch-dog thread interrupted"); } catch (Exception ex) { getLogger().log(Level.WARNING, null, ex); } } }); watchDog.setDaemon(true); watchDog.start(); } void subscriberTask() { try { setState(State.Ready); if (isSimulated()) { while (!Thread.currentThread().isInterrupted()) { 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); } if (Controller.getInstance() != null) { Controller.getInstance().updateView(); } chrono = new Chrono(); Thread.sleep(2000); } } 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(); } } } catch (Exception ex) { getLogger().log(Level.INFO, null, ex); } setState(State.Offline); } void processMessage(String str) { try { //System.out.println(str); List detection = (List) JsonSerializer.decode(str, List.class); int address = 1; for (List bus : detection) { for (List sensor : bus) { Integer indDetector = sensor.get(0); Integer mecDetector = sensor.get(1); int index = Controller.getInstance().getPuckIndex(address); PuckState puck = Controller.getInstance().getPuckState(index); puck.set(mecDetector, indDetector); address++; } } setCache(str); } catch (Exception ex) { getLogger().log(Level.INFO, null, ex); } } public PuckState getPuckState(int id) throws Exception { return Controller.getInstance().getPuckState(id); } public Puck getPuck(String name) throws Exception { return Controller.getInstance().getPuck(name); } @Override protected void doClose() { if (watchDog != null) { watchDog.interrupt(); watchDog = null; } if (thread != null) { try { Threading.stop(thread, true, 2000); } catch (InterruptedException ex) { //TODO: Filtering InterruptedException. But stop() should not throw InterruptedException; getLogger().log(Level.WARNING, null, ex); } 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","raspberrypi:5556"); pd.setDebug(true); pd.initialize(); Thread.sleep(100000); } }