001package jmri.jmrix.rps;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.JmriException;
006import jmri.Sensor;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Manage the RPS-specific Sensor implementation.
012 * <p>
013 * System names are "RSpppp", where ppp is a CSV representation of the region.
014 *
015 * @author Bob Jacobsen Copyright (C) 2007, 2019
016 */
017public class RpsSensorManager extends jmri.managers.AbstractSensorManager {
018
019    public RpsSensorManager(RpsSystemConnectionMemo memo) {
020        super(memo);
021    }
022
023    /**
024     * {@inheritDoc}
025     */
026    @Override
027    @Nonnull
028    public RpsSystemConnectionMemo getMemo() {
029        return (RpsSystemConnectionMemo) memo;
030    }
031
032    /**
033     * {@inheritDoc}
034     * <p>
035     * System Name is normalized.
036     * Assumes calling method has checked that a Sensor with this system
037     * name does not already exist.
038     *
039     * @throws IllegalArgumentException if the system name is not in a valid format
040     */
041    @Override
042    @Nonnull
043    protected Sensor createNewSensor(@Nonnull String systemName, String userName) throws IllegalArgumentException {
044        try {
045           RpsSensor r = new RpsSensor(systemName, userName, getSystemPrefix());
046           Distributor.instance().addMeasurementListener(r);
047           return r;
048       } catch(java.lang.StringIndexOutOfBoundsException sioe){
049            throw new IllegalArgumentException("Invalid System Name: " + systemName);
050       }
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    @Nonnull
058    public String createSystemName(@Nonnull String curAddress, @Nonnull String prefix) throws JmriException {
059        if (!prefix.equals(getSystemPrefix())) {
060            log.warn("prefix does not match memo.prefix");
061            throw new JmriException("Unable to convert " + curAddress + ", Prefix does not match");
062        }
063        String sys = getSystemPrefix() + typeLetter() + curAddress;
064        // first, check validity
065        try {
066            validSystemNameFormat(sys);
067        } catch (IllegalArgumentException e) {
068            throw new JmriException(e.toString());
069        }
070        return sys;
071    }
072    
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    @Nonnull
078    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
079        return getMemo().validateSystemNameFormat(name, this, locale);
080    }
081    
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
087        return getMemo().validSystemNameFormat(systemName, typeLetter());
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public String getEntryToolTip() {
095        return Bundle.getMessage("AddInputEntryToolTip");
096    }
097
098    private final static Logger log = LoggerFactory.getLogger(RpsSensorManager.class);
099
100}