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     * {@inheritDoc }
040     */
041    @Override
042    public void requestThrottleSetup(LocoAddress address, boolean control) {
043        if (currentThrottle != null) {
044            log.error("DCC Direct cannot handle more than one throttle {}",address);
045            failedThrottleRequest(address, "DCC direct cannot handle more than one throttle "+ address);
046            return;
047        }
048        if (address instanceof DccLocoAddress) {
049            currentThrottle = new Throttle(((DccLocoAddress) address), tc, adapterMemo); // uses address object
050            notifyThrottleKnown(currentThrottle, currentThrottle.getLocoAddress());
051        }
052        else {
053            log.error("LocoAddress {} is not a DccLocoAddress",address);
054            failedThrottleRequest(address, "LocoAddress is not a DccLocoAddress " +address);
055        }
056    }
057
058    @Override
059    public boolean addressTypeUnique() {
060        return false;
061    }
062
063    @Override
064    public boolean canBeShortAddress(int a) {
065        return a < 128;
066    }
067
068    @Override
069    public boolean canBeLongAddress(int a) {
070        return a > 0;
071    }
072
073    /**
074     * Invoked when a throttle is released, this updates the local data
075     * structures.
076     */
077    @Override
078    public boolean disposeThrottle(jmri.DccThrottle t, jmri.ThrottleListener l) {
079        if (super.disposeThrottle(t, l)) {
080            currentThrottle = null;
081            return true;
082        }
083        return false;
084    }
085
086    private final static Logger log = LoggerFactory.getLogger(ThrottleManager.class);
087
088}