001package jmri.jmrix.rfid.merg.concentrator;
002
003import javax.annotation.Nonnull;
004import jmri.IdTag;
005import jmri.IdTagManager;
006import jmri.InstanceManager;
007import jmri.Sensor;
008import jmri.jmrix.rfid.RfidMessage;
009import jmri.jmrix.rfid.RfidReply;
010import jmri.jmrix.rfid.RfidSensorManager;
011import jmri.jmrix.rfid.RfidSystemConnectionMemo;
012import jmri.jmrix.rfid.RfidTrafficController;
013import jmri.jmrix.rfid.TimeoutRfidSensor;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Manage the Rfid-specific Sensor implementation.
019 * <p>
020 * System names are "FSpppp", where ppp is a representation of the RFID reader.
021 *
022 * @author Bob Jacobsen Copyright (C) 2007
023 * @author Matthew Harris Copyright (C) 2011
024 * @since 2.11.4
025 */
026public class ConcentratorSensorManager extends RfidSensorManager {
027
028    private final RfidTrafficController tc;
029
030    public ConcentratorSensorManager(RfidSystemConnectionMemo memo) {
031        super(memo);
032        this.tc = memo.getTrafficController();
033        attach();
034    }
035
036    private void attach() {
037        tc.addRfidListener(this);
038    }
039
040    /**
041     * {@inheritDoc}
042     */
043    @Override
044    @Nonnull
045    protected Sensor createNewSensor(@Nonnull String systemName, String userName) throws IllegalArgumentException {
046        log.debug("Create new Sensor");
047        TimeoutRfidSensor s = new TimeoutRfidSensor(systemName, userName);
048        s.addPropertyChangeListener(this);
049        return s;
050    }
051
052    @Override
053    public void message(RfidMessage m) {
054        if (m.toString().equals(new ConcentratorMessage(tc.getAdapterMemo().getProtocol().initString(), 0).toString())) {
055            log.info("Sent init string: {}", m);
056        } else {
057            super.message(m);
058        }
059    }
060
061    @Override
062    public synchronized void reply(RfidReply r) {
063        if (r instanceof ConcentratorReply) {
064            processReply((ConcentratorReply) r);
065        }
066    }
067
068    private void processReply(ConcentratorReply r) {
069        if (!tc.getAdapterMemo().getProtocol().isValid(r)) {
070            log.warn("Invalid message - skipping {}", r);
071            return;
072        }
073        if (!r.isInRange()) {
074            log.warn("Invalid concentrator reader range - skipping {}", r);
075            return;
076        }
077        IdTag idTag = InstanceManager.getDefault(IdTagManager.class).provideIdTag(tc.getAdapterMemo().getProtocol().getTag(r));
078        TimeoutRfidSensor sensor = (TimeoutRfidSensor) provideSensor(getSystemNamePrefix() + r.getReaderPort());
079        sensor.notify(idTag);
080    }
081
082    // to free resources when no longer used
083    @Override
084    public void dispose() {
085        tc.removeRfidListener(this);
086        super.dispose();
087    }
088
089    /**
090     * Validates to validateTrimmedSystemNameFormat.
091     * {@inheritDoc}
092     */
093    @Override
094    @Nonnull
095    public String validateSystemNameFormat(@Nonnull String name, @Nonnull java.util.Locale locale) throws jmri.NamedBean.BadSystemNameException {
096        return validateTrimmedSystemNameFormat(name, locale);
097    }
098    
099    private static final Logger log = LoggerFactory.getLogger(ConcentratorSensorManager.class);
100
101}