001package jmri.jmrit.withrottle;
002
003import jmri.LocoAddress;
004import jmri.DccLocoAddress;
005import jmri.DccThrottle;
006import jmri.InstanceManager;
007import jmri.ThrottleListener;
008import jmri.jmrit.roster.RosterEntry;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 *
014 * @author Brett Hoffman Copyright (C) 2010, 2011
015 */
016public class ConsistFunctionController implements ThrottleListener {
017
018    private DccThrottle throttle;
019    private RosterEntry rosterLoco = null;
020    private ThrottleController throttleController;
021
022    public ConsistFunctionController(ThrottleController tc) {
023        throttleController = tc;
024    }
025
026    public ConsistFunctionController(ThrottleController tc, RosterEntry re) {
027        throttleController = tc;
028        rosterLoco = re;
029    }
030
031    @Override
032    public void notifyThrottleFound(DccThrottle t) {
033        if (log.isDebugEnabled()) {
034            log.debug("Lead Loco throttle found: {}, for consist: {}", t, throttleController.getCurrentAddressString());
035        }
036        throttle = t;
037
038        if (rosterLoco == null) {
039            rosterLoco = throttleController.findRosterEntry(throttle);
040        }
041
042        throttleController.syncThrottleFunctions(throttle, rosterLoco);
043        throttleController.setFunctionThrottle(t);
044        throttleController.sendFunctionLabels(rosterLoco);
045        throttleController.sendAllFunctionStates(throttle);
046    }
047
048    @Override
049    public void notifyFailedThrottleRequest(LocoAddress address, String reason) {
050        log.error("Throttle request failed for {} because {}", address, reason);
051    }
052
053    /**
054     * No steal or share decisions made locally
055     */
056    @Override
057    public void notifyDecisionRequired(jmri.LocoAddress address, DecisionType question) {
058    }
059
060    public void dispose() {
061        jmri.InstanceManager.throttleManagerInstance().releaseThrottle(throttle, this);
062    }
063
064    public DccThrottle getThrottle() {
065        return throttle;
066    }
067
068    boolean requestThrottle(DccLocoAddress loco) {
069        return jmri.InstanceManager.throttleManagerInstance().requestThrottle(loco, this, true);
070    }
071
072    private final static Logger log = LoggerFactory.getLogger(ConsistFunctionController.class);
073
074}