001package jmri.jmrix.rfid.merg.concentrator;
002
003import javax.annotation.Nonnull;
004import jmri.IdTag;
005import jmri.IdTagManager;
006import jmri.InstanceManager;
007import jmri.Reporter;
008import jmri.implementation.decorators.TimeoutReporter;
009import jmri.jmrix.rfid.*;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Rfid implementation of a ReporterManager.
015 * <p>
016 * System names are "FnRpppp", where Fn is the connection prefix, ppp is a representation of the RFID reader.
017 *
018 * @author Bob Jacobsen Copyright (C) 2008
019 * @author Matthew Harris Copyright (C) 2011
020 * @since 2.11.4
021 */
022public class ConcentratorReporterManager extends RfidReporterManager {
023
024    private final RfidTrafficController tc;
025
026    public ConcentratorReporterManager(RfidSystemConnectionMemo memo) {
027        super(memo);
028        this.tc = memo.getTrafficController();
029        attach();
030    }
031
032    private void attach() {
033        tc.addRfidListener(this);
034    }
035
036    @Override
037    @Nonnull
038    protected Reporter createNewReporter(@Nonnull String systemName, String userName) throws IllegalArgumentException {
039        log.debug("Create new Reporter: {}", systemName);
040        if (!systemName.matches(getSystemNamePrefix() + "[" + tc.getRange() + "]")) {
041            log.warn("Invalid Reporter name: {}} - out of supported range {}", systemName, tc.getRange());
042            throw new IllegalArgumentException("Invalid Reporter name: " + systemName + " - out of supported range " + tc.getRange());
043        }
044        Reporter r = new TimeoutReporter( new RfidReporter(systemName, userName));
045        r.addPropertyChangeListener(this);
046        return r;
047    }
048
049    @Override
050    public void message(RfidMessage m) {
051        if (m.toString().equals(new ConcentratorMessage(tc.getAdapterMemo().getProtocol().initString(), 0).toString())) {
052            log.info("Sent init string: {}", m);
053        } else {
054            super.message(m);
055        }
056    }
057
058    @Override
059    public synchronized void reply(RfidReply r) {
060        if (r instanceof ConcentratorReply) {
061            processReply((ConcentratorReply) r);
062        }
063    }
064
065    private void processReply(ConcentratorReply r) {
066        if (!tc.getAdapterMemo().getProtocol().isValid(r)) {
067            log.warn("Invalid message - skipping {}", r);
068            return;
069        }
070        if (!r.isInRange()) {
071            log.warn("Invalid concentrator reader range - skipping {}", r);
072            return;
073        }
074        IdTag idTag = InstanceManager.getDefault(IdTagManager.class).provideIdTag(tc.getAdapterMemo().getProtocol().getTag(r));
075        TimeoutReporter report = (TimeoutReporter) provideReporter(getSystemNamePrefix() + r.getReaderPort());
076        report.notify(idTag);
077    }
078
079    private static final Logger log = LoggerFactory.getLogger(ConcentratorReporterManager.class);
080
081}