001package jmri.jmrix.debugthrottle;
002
003import java.util.EnumSet;
004import jmri.DccLocoAddress;
005import jmri.DccThrottle;
006import jmri.LocoAddress;
007import jmri.SpeedStepMode;
008import jmri.jmrix.AbstractThrottleManager;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Implementation of a ThrottleManager for debugging.
014 *
015 * @author Bob Jacobsen Copyright (C) 2003, 2005
016 */
017public class DebugThrottleManager extends AbstractThrottleManager {
018
019    public DebugThrottleManager() {
020        super();
021    }
022
023    /**
024     * Constructor.
025     * @param memo system connection.
026     */
027    public DebugThrottleManager(jmri.SystemConnectionMemo memo) {
028        super(memo);
029    }
030
031    @Override
032    public void requestThrottleSetup(LocoAddress a, boolean control) {
033        if (a instanceof DccLocoAddress) {
034            // Immediately trigger the callback.
035            DccLocoAddress address = (DccLocoAddress) a;
036            log.debug("new debug throttle for {}", address);
037            notifyThrottleKnown(new DebugThrottle(address, adapterMemo), a);
038        }
039        else {
040            log.error("LocoAddress {} is not a DccLocoAddress",a);
041        }
042    }
043
044    /**
045     * Address 1 and above can be a long address
046     *
047     */
048    @Override
049    public boolean canBeLongAddress(int address) {
050        return (address >= 1);
051    }
052
053    /**
054     * Address 127 and below can be a short address
055     *
056     */
057    @Override
058    public boolean canBeShortAddress(int address) {
059        return (address <= 127);
060    }
061
062    /**
063     * Are there any ambiguous addresses (short vs long) on this system?
064     */
065    @Override
066    public boolean addressTypeUnique() {
067        return false;
068    }
069
070    @Override
071    public boolean disposeThrottle(DccThrottle t, jmri.ThrottleListener l) {
072        log.debug("disposeThrottle called for {}", t);
073        if (super.disposeThrottle(t, l)) {
074            if (t instanceof DebugThrottle) {
075                DebugThrottle lnt = (DebugThrottle) t;
076                lnt.throttleDispose();
077                return true;
078            }
079            else {
080                log.error("DccThrottle {} is not a DebugThrottle",t);
081            }
082        }
083        return false;
084    }
085
086    /**
087     * What speed modes are supported by this system? value should be xor of
088     * possible modes specified by the DccThrottle interface
089     */
090    @Override
091    public EnumSet<SpeedStepMode> supportedSpeedModes() {
092        return EnumSet.allOf(SpeedStepMode.class);
093    }
094
095    private final static Logger log = LoggerFactory.getLogger(DebugThrottleManager.class);
096
097}