001package jmri.jmrix.direct;
002
003import jmri.CommandStation;
004import jmri.DccLocoAddress;
005import jmri.LocoAddress;
006import jmri.jmrix.AbstractThrottleManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Direct DCC implementation of a ThrottleManager.
012 * <p>
013 * When the traffic manager doesn't have anything else to do, it comes here to
014 * get a command to send.
015 * <p>
016 * This is a partial implementation, which can only handle one Throttle at a
017 * time. It also is missing logic to alternate sending speed and function
018 * commands; right now it only sends the first group of function packets.
019 *
020 * @author Bob Jacobsen Copyright (C) 2004
021 */
022public class ThrottleManager extends AbstractThrottleManager {
023
024    private CommandStation tc;
025    /**
026     * Constructor for a Direct ThrottleManager.
027     * @param memo system connection.
028     */
029    public ThrottleManager(DirectSystemConnectionMemo memo) {
030        super(memo);
031        tc = memo.getTrafficController();
032        jmri.InstanceManager.setDefault(jmri.jmrix.direct.ThrottleManager.class, this);
033    }
034
035    Throttle currentThrottle = null;
036
037    /**
038     * Create throttle data structures.
039     */
040    @Override
041    public void requestThrottleSetup(LocoAddress address, boolean control) {
042        if (currentThrottle != null) {
043            log.error("DCC Direct cannot handle more than one throttle {}",address);
044            failedThrottleRequest(address, "DCC direct cannot handle more than one throttle "+ address);
045            return;
046        }
047        if (address instanceof DccLocoAddress) {
048            currentThrottle = new Throttle(((DccLocoAddress) address), tc); // uses address object
049            notifyThrottleKnown(currentThrottle, currentThrottle.getLocoAddress());
050        }
051        else {
052            log.error("LocoAddress {} is not a DccLocoAddress",address);
053            failedThrottleRequest(address, "LocoAddress is not a DccLocoAddress " +address);
054        }
055    }
056
057    @Override
058    public boolean addressTypeUnique() {
059        return false;
060    }
061
062    @Override
063    public boolean canBeShortAddress(int a) {
064        return a < 128;
065    }
066
067    @Override
068    public boolean canBeLongAddress(int a) {
069        return a > 0;
070    }
071
072    /**
073     * Invoked when a throttle is released, this updates the local data
074     * structures.
075     */
076    @Override
077    public boolean disposeThrottle(jmri.DccThrottle t, jmri.ThrottleListener l) {
078        if (super.disposeThrottle(t, l)) {
079            currentThrottle = null;
080            return true;
081        }
082        return false;
083    }
084
085    private final static Logger log = LoggerFactory.getLogger(ThrottleManager.class);
086
087}