001package jmri.jmrix.easydcc;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Turnout;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Implement turnout manager for EasyDcc systems.
011 * <p>
012 * System names are "ETnnn", where E is the user configurable system prefix,
013 * nnn is the turnout number without padding.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001
016 */
017public class EasyDccTurnoutManager extends jmri.managers.AbstractTurnoutManager implements EasyDccListener {
018
019    private EasyDccTrafficController trafficController = null;
020    public final static int MAX_ACC_DECODER_ADDRESS = 511;
021
022    /**
023     * Create an new EasyDCC TurnoutManager.
024     *
025     * @param memo the SystemConnectionMemo for this connection (contains the prefix string needed to parse names)
026     */
027    public EasyDccTurnoutManager(EasyDccSystemConnectionMemo memo) {
028        super(memo);
029        // connect to the TrafficManager
030        trafficController = memo.getTrafficController();
031        // listen for turnout creation
032        trafficController.addEasyDccListener(this);
033        log.debug("EasyDCC TurnoutManager prefix={}", getSystemPrefix());
034    }
035
036    /**
037     * {@inheritDoc}
038     */
039    @Override
040    @Nonnull
041    public EasyDccSystemConnectionMemo getMemo() {
042        return (EasyDccSystemConnectionMemo) memo;
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    @Nonnull
049    @Override
050    protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException {
051        int addr;
052        try {
053            addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1));
054        } catch (NumberFormatException e) {
055            throw new IllegalArgumentException("Could not create EasyDCC Turnout Systemname '"+systemName+"' .");
056        }
057        Turnout t = new EasyDccTurnout(getSystemPrefix(), addr, getMemo());
058        t.setUserName(userName);
059        return t;
060    }
061
062    /**
063     * Listeners for messages from the command station.
064     */
065    @Override
066    public void message(EasyDccMessage m) {
067        log.debug("message received unexpectedly: {}", m.toString());
068    }
069
070    // Listen for status changes from EasyDcc system
071    @Override
072    public void reply(EasyDccReply r) {
073        // There isn't anything meaningful coming back at this time.
074        log.debug("reply received unexpectedly: {}", r.toString());
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
082        return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID;
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    @Nonnull
090    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
091        return validateIntegerSystemNameFormat(systemName, 1, MAX_ACC_DECODER_ADDRESS, locale);
092    }
093
094    /**
095     * Get the bit address from the system name.
096     * @param systemName a valid LocoNet-based Turnout System Name
097     * @return the turnout number extracted from the system name
098     */
099    public int getBitFromSystemName(String systemName) {
100        try {
101            validateSystemNameFormat(systemName, Locale.getDefault());
102        } catch (IllegalArgumentException ex) {
103            return 0;
104        }
105        return Integer.parseInt(systemName.substring(getSystemNamePrefix().length()));
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public String getEntryToolTip() {
113        return Bundle.getMessage("AddOutputEntryToolTip");
114    }
115
116    private final static Logger log = LoggerFactory.getLogger(EasyDccTurnoutManager.class);
117
118}