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