001package jmri.jmrix.rfid.generic.standalone;
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 StandaloneSensorManager extends RfidSensorManager {
027
028    private final RfidTrafficController tc;
029
030    public StandaloneSensorManager(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 StandaloneMessage(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 StandaloneReply) {
064            processReply((StandaloneReply) r);
065        }
066    }
067
068    private void processReply(StandaloneReply r) {
069        if (!tc.getAdapterMemo().getProtocol().isValid(r)) {
070            log.warn("Invalid message - skipping {}", r);
071            return;
072        }
073        IdTag idTag = InstanceManager.getDefault(IdTagManager.class).provideIdTag(tc.getAdapterMemo().getProtocol().getTag(r));
074        TimeoutRfidSensor sensor = (TimeoutRfidSensor) provideSensor(getSystemNamePrefix() + "1");
075        sensor.notify(idTag);
076    }
077
078    /**
079     * Validates to contain at least 1 number.
080     * {@inheritDoc}
081     */
082    @Override
083    @Nonnull
084    public String validateSystemNameFormat(@Nonnull String name, @Nonnull java.util.Locale locale) throws jmri.NamedBean.BadSystemNameException {
085        return validateTrimmedMin1NumberSystemNameFormat(name,locale);
086    }
087
088    private static final Logger log = LoggerFactory.getLogger(StandaloneSensorManager.class);
089
090}