001package jmri.jmrix.marklin;
002
003import java.util.EnumSet;
004import jmri.LocoAddress;
005import jmri.SpeedStepMode;
006import jmri.jmrix.AbstractThrottleManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * MarklinDCC implementation of a ThrottleManager.
012 * <p>
013 * Based on early NCE code and on work by Bob Jacobsen.
014 *
015 * @author Kevin Dickerson Copyright (C) 2012
016 */
017public class MarklinThrottleManager extends AbstractThrottleManager implements MarklinListener {
018
019    /**
020     * Constructor.
021     * @param memo system connection.
022     */
023    public MarklinThrottleManager(MarklinSystemConnectionMemo memo) {
024        super(memo);
025    }
026
027    @Override
028    public void reply(MarklinReply m) {
029        //We are not sending commands from here yet!
030    }
031
032    @Override
033    public void message(MarklinMessage m) {
034        // messages are ignored
035    }
036
037    @Override
038    public void requestThrottleSetup(LocoAddress address, boolean control) {
039        /* Here we do not set notifythrottle, we simply create a new Marklin throttle.
040         The Marklin throttle in turn will notify the throttle manager of a successful or
041         unsuccessful throttle connection. */
042        log.debug("new MarklinThrottle for {}", address);
043        notifyThrottleKnown(new MarklinThrottle((MarklinSystemConnectionMemo) adapterMemo, address), address);
044    }
045
046    @Override
047    public boolean hasDispatchFunction() {
048        return false;
049    }
050
051    /**
052     * Address 100 and above is a long address
053     *
054     */
055    @Override
056    public boolean canBeLongAddress(int address) {
057        return isLongAddress(address);
058    }
059
060    /**
061     * Address 99 and below is a short address
062     *
063     */
064    @Override
065    public boolean canBeShortAddress(int address) {
066        return !isLongAddress(address);
067    }
068
069    /**
070     * Are there any ambiguous addresses (short vs long) on this system?
071     */
072    @Override
073    public boolean addressTypeUnique() {
074        return false;
075    }
076
077    /**
078     * Returns false
079     * <p>
080     * {@inheritDoc}
081     */
082    @Override
083    protected boolean singleUse() {
084        return false;
085    }
086
087    @Override
088    public String[] getAddressTypes() {
089        return new String[]{
090            LocoAddress.Protocol.DCC.getPeopleName(),
091            LocoAddress.Protocol.MFX.getPeopleName(),
092            LocoAddress.Protocol.MOTOROLA.getPeopleName()};
093    }
094
095    @Override
096    public LocoAddress.Protocol[] getAddressProtocolTypes() {
097        return new LocoAddress.Protocol[]{
098            LocoAddress.Protocol.DCC,
099            LocoAddress.Protocol.MFX,
100            LocoAddress.Protocol.MOTOROLA};
101    }
102
103    /*
104     * Local method for deciding short/long address
105     */
106    static boolean isLongAddress(int num) {
107        return (num >= 100);
108    }
109
110    @Override
111    public EnumSet<SpeedStepMode> supportedSpeedModes() {
112        return EnumSet.of(SpeedStepMode.NMRA_DCC_128, SpeedStepMode.NMRA_DCC_28);
113    }
114
115    @Override
116    public boolean disposeThrottle(jmri.DccThrottle t, jmri.ThrottleListener l) {
117        if (super.disposeThrottle(t, l)) {
118            if (t instanceof MarklinThrottle) {
119                MarklinThrottle lnt = (MarklinThrottle) t;
120                lnt.throttleDispose();
121                return true;
122            }
123        }
124        return false;
125    }
126
127    private final static Logger log = LoggerFactory.getLogger(MarklinThrottleManager.class);
128
129}