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