001package jmri.jmrix.powerline;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.*;
006
007/**
008 * Manage the system-specific Sensor implementation.
009 * <p>
010 * System names are: Powerline - "PSann", where a is the house code and nn is
011 * the unit number without padding.
012 *
013 * @author Bob Jacobsen Copyright (C) 2003, 2006, 2007, 2008
014 * @author Ken Cameron, (C) 2009, sensors from poll replies. Converted to
015 * multiple connection support
016 * @author kcameron Copyright (C) 2011
017 */
018abstract public class SerialSensorManager extends jmri.managers.AbstractSensorManager implements SerialListener {
019
020    private SerialTrafficController tc = null;
021
022    public SerialSensorManager(SerialTrafficController tc) {
023        super(tc.getAdapterMemo());
024        this.tc = tc;
025        tc.addSerialListener(this);
026    }
027
028    /**
029     * {@inheritDoc}
030     */
031    @Override
032    @Nonnull
033    public SerialSystemConnectionMemo getMemo() {
034        return (SerialSystemConnectionMemo) memo;
035    }
036
037    /**
038     * {@inheritDoc}
039     * <p>
040     * System name is normalized to ensure uniqueness.
041     * @throws IllegalArgumentException when SystemName can't be converted
042     */
043    @Override
044    @Nonnull
045    protected Sensor createNewSensor(@Nonnull String systemName, String userName) throws IllegalArgumentException {
046        Sensor s;
047        // validate the system name, and normalize it
048        String sName = tc.getAdapterMemo().getSerialAddress().normalizeSystemName(systemName);
049        if (sName.isEmpty()) {
050            // system name is not valid
051            throw new IllegalArgumentException("Invalid Powerline Sensor system name - " +  // NOI18N
052                    systemName);
053        }
054        // does this Sensor already exist?
055        s = getBySystemName(sName);
056        if (s != null) {
057            throw new IllegalArgumentException("Powerline Sensor with this name already exists - " +  // NOI18N
058                    systemName);
059        }
060        // Sensor system name is valid and Sensor doesn't exist, make a new one
061        if (userName == null) {
062            s = new SerialSensor(sName, tc);
063        } else {
064            s = new SerialSensor(sName, tc, userName);
065        }
066        return s;
067    }
068
069    /**
070     * Dummy routine
071     */
072    @Override
073    public void message(SerialMessage r) {
074        // this happens during some polls from sensor messages
075        //log.warn("unexpected message");
076    }
077
078    /**
079     * Process a reply to a poll of Sensors of one node
080     */
081    @Override
082    abstract public void reply(SerialReply r);
083
084    @Override
085    public boolean allowMultipleAdditions(@Nonnull String systemName) {
086        return false;
087    }
088
089    @Override
090    @javax.annotation.Nonnull
091    @javax.annotation.CheckReturnValue
092    public String getNextValidSystemName(@Nonnull NamedBean currentBean) throws JmriException {
093        throw new jmri.JmriException("getNextValidSystemName should not have been called");
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    @Nonnull
101    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
102        return tc.getAdapterMemo().getSerialAddress().validateSystemNameFormat(name, typeLetter(), locale);
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
110        return tc.getAdapterMemo().getSerialAddress().validSystemNameFormat(systemName, typeLetter());
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public String getEntryToolTip() {
118        return Bundle.getMessage("AddInputEntryToolTip");
119    }
120
121    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialSensorManager.class);
122
123}