001package jmri.jmrix.rfid.generic.standalone;
002
003import jmri.jmrix.AbstractMRListener;
004import jmri.jmrix.AbstractMRMessage;
005import jmri.jmrix.AbstractMRReply;
006import jmri.jmrix.rfid.RfidMessage;
007import jmri.jmrix.rfid.RfidSystemConnectionMemo;
008import jmri.jmrix.rfid.RfidTrafficController;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Converts Stream-based I/O to/from messages. The "SerialInterface" side
014 * sends/receives message objects.
015 * <p>
016 * The connection to a SerialPortController is via a pair of *Streams, which
017 * then carry sequences of characters for transmission. Note that this
018 * processing is handled in an independent thread.
019 * <p>
020 * This maintains a list of nodes, but doesn't currently do anything with it.
021 *
022 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2005, 2006, 2008
023 * @author Matthew Harris Copyright (c) 2011
024 * @since 2.11.4
025 */
026public class StandaloneTrafficController extends RfidTrafficController {
027
028    @Override
029    public void sendInitString() {
030        String init = adapterMemo.getProtocol().initString();
031        if (init.length() > 0) {
032            sendRfidMessage(new StandaloneMessage(init, 0), null);
033        }
034    }
035
036    public StandaloneTrafficController(RfidSystemConnectionMemo memo) {
037        super();
038        adapterMemo = memo;
039        logDebug = log.isDebugEnabled();
040
041        // not polled at all, so allow unexpected messages, and
042        // use poll delay just to spread out startup
043        setAllowUnexpectedReply(true);
044        mWaitBeforePoll = 1000;  // can take a long time to send
045
046    }
047
048    @Override
049    protected void forwardToPort(AbstractMRMessage m, AbstractMRListener reply) {
050        if (logDebug) {
051            log.debug("forward {}", m);
052        }
053        sendInterlock = ((RfidMessage) m).getInterlocked();
054        super.forwardToPort(m, reply);
055    }
056
057    @Override
058    public RfidMessage getRfidMessage(int length) {
059        return new StandaloneMessage(length);
060    }
061
062    @Override
063    protected AbstractMRReply newReply() {
064        StandaloneReply reply = new StandaloneReply(adapterMemo.getTrafficController());
065        return reply;
066    }
067
068    @Override
069    public String getRange() {
070        return "1";
071    }
072
073    @Override
074    protected boolean endOfMessage(AbstractMRReply msg) {
075        return adapterMemo.getProtocol().endOfMessage(msg);
076    }
077
078    boolean sendInterlock = false; // send the 00 interlock when CRC received
079
080    private static final Logger log = LoggerFactory.getLogger(StandaloneTrafficController.class);
081}