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    @Override
046    public Turnout createNewTurnout(@Nonnull String systemName, String userName) {
047        Turnout t;
048        int addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1));
049        t = new EasyDccTurnout(getSystemPrefix(), addr, getMemo());
050        t.setUserName(userName);
051
052        return t;
053    }
054
055    /**
056     * Listeners for messages from the command station.
057     */
058    @Override
059    public void message(EasyDccMessage m) {
060        log.debug("message received unexpectedly: {}", m.toString());
061    }
062
063    // Listen for status changes from EasyDcc system
064    @Override
065    public void reply(EasyDccReply r) {
066        // There isn't anything meaningful coming back at this time.
067        log.debug("reply received unexpectedly: {}", r.toString());
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
075        return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID;
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    @Nonnull
083    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
084        return validateIntegerSystemNameFormat(systemName, 1, MAX_ACC_DECODER_ADDRESS, locale);
085    }
086
087    /**
088     * Get the bit address from the system name.
089     * @param systemName a valid LocoNet-based Turnout System Name
090     * @return the turnout number extracted from the system name
091     */
092    public int getBitFromSystemName(String systemName) {
093        try {
094            validateSystemNameFormat(systemName, Locale.getDefault());
095        } catch (IllegalArgumentException ex) {
096            return 0;
097        }
098        return Integer.parseInt(systemName.substring(getSystemNamePrefix().length()));
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public String getEntryToolTip() {
106        return Bundle.getMessage("AddOutputEntryToolTip");
107    }
108
109    private final static Logger log = LoggerFactory.getLogger(EasyDccTurnoutManager.class);
110
111}