001package jmri.jmrix.openlcb;
002
003import jmri.DccLocoAddress;
004import jmri.DccThrottle;
005import jmri.LocoAddress;
006import jmri.jmrix.AbstractThrottleManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Implementation of a ThrottleManager for OpenLCB
012 *
013 * @author Bob Jacobsen Copyright (C) 2003, 2005, 2012
014 */
015public class OlcbThrottleManager extends AbstractThrottleManager {
016
017    @Deprecated (since="5.13.3", forRemoval=true)
018    public OlcbThrottleManager() {
019        this(jmri.InstanceManager.getDefault(jmri.jmrix.can.CanSystemConnectionMemo.class));
020    }
021
022    /**
023     * Constructor.
024     * @param memo system connection memo
025     */
026    public OlcbThrottleManager(jmri.SystemConnectionMemo memo) {
027        super(memo);
028    }
029
030    @Override
031    public void requestThrottleSetup(LocoAddress a, boolean control) {
032        // Immediately trigger the callback.
033        if (!(a instanceof DccLocoAddress)){
034            failedThrottleRequest(a, "Not a DccLocoAddress");
035            return;
036        }
037        DccLocoAddress address = (DccLocoAddress) a;
038        log.debug("new debug throttle for {}", address);
039        notifyThrottleKnown(new OlcbThrottle(address, adapterMemo), a);
040    }
041
042    /**
043     * Address 1 and above can be a long address
044     *
045     */
046    @Override
047    public boolean canBeLongAddress(int address) {
048        return (address >= 1);
049    }
050
051    /**
052     * Address 127 and below can be a short address
053     *
054     */
055    @Override
056    public boolean canBeShortAddress(int address) {
057        return (address <= 127);
058    }
059
060    /**
061     * Are there any ambiguous addresses (short vs long) on this system?
062     */
063    @Override
064    public boolean addressTypeUnique() {
065        return false;
066    }
067
068    @Override
069    public LocoAddress getAddress(String value, LocoAddress.Protocol protocol) {
070        // if OpenLCB handle here
071        if (protocol == LocoAddress.Protocol.OPENLCB) {
072            org.openlcb.NodeID node = new org.openlcb.NodeID(value);
073            return new OpenLcbLocoAddress(node);
074        } else {
075            // otherwise defer up to DCC
076            return super.getAddress(value, protocol);
077        }
078    }
079
080    @Override
081    public String[] getAddressTypes() {
082        return new String[]{LocoAddress.Protocol.DCC_SHORT.getPeopleName(),
083            LocoAddress.Protocol.DCC_LONG.getPeopleName(),
084            LocoAddress.Protocol.OPENLCB.getPeopleName()};
085    }
086
087    @Override
088    public LocoAddress.Protocol[] getAddressProtocolTypes() {
089        return new LocoAddress.Protocol[]{LocoAddress.Protocol.DCC_SHORT,
090            LocoAddress.Protocol.DCC_LONG,
091            LocoAddress.Protocol.OPENLCB};
092    }
093
094    @Override
095    public boolean disposeThrottle(DccThrottle t, jmri.ThrottleListener l) {
096        log.debug("disposeThrottle called for {}", t);
097        if (super.disposeThrottle(t, l)) {
098            if (t instanceof OlcbThrottle) {
099                OlcbThrottle lnt = (OlcbThrottle) t;
100                lnt.throttleDispose();
101                return true;
102            }
103        }
104        return false;
105    }
106
107    private final static Logger log = LoggerFactory.getLogger(OlcbThrottleManager.class);
108
109}