001package jmri.jmrix.sprog;
002
003import jmri.Turnout;
004import jmri.implementation.AbstractTurnout;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Sprog implementation of the Turnout interface.
010 * <p>
011 * This object doesn't listen to the Sprog communications. This is because it
012 * should be the only object that is sending messages for this turnout; more
013 * than one Turnout object pointing to a single device is not allowed.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2005
016 * @author J.M. (Mark) Knox Copyright (C) 2005
017 */
018public class SprogCSTurnout extends AbstractTurnout {
019
020    private SprogSystemConnectionMemo _memo = null;
021
022    /**
023     * Create SPROG Turnout object.
024     * <p>
025     * Sprog turnouts use the NMRA number (0-511) as their numerical
026     * identification.
027     *
028     * @param number address for turnout
029     * @param memo connection to get prefix from
030     */
031    public SprogCSTurnout(int number, SprogSystemConnectionMemo memo) {
032        super(memo.getSystemPrefix() + "T" + number);
033        _number = number;
034        _memo = memo;
035        commandStation = _memo.getCommandStation();
036    }
037
038    public int getNumber() {
039        return _number;
040    }
041
042    /**
043     * {@inheritDoc}
044     * Sends a command via the commandStation.
045     */
046    @Override
047    protected void forwardCommandChangeToLayout(int newState) {
048        // sort out states
049        if ((newState & Turnout.CLOSED) != 0) {
050            if (statesOk(newState)) {
051                // send a CLOSED command
052                commandStation.forwardCommandChangeToLayout(_number, true ^ getInverted());
053            }
054        } else {
055            // send a THROWN command
056            commandStation.forwardCommandChangeToLayout(_number, false ^ getInverted());
057        }
058    }
059
060    private SprogCommandStation commandStation;
061
062    public void setCommandStation(SprogCommandStation command) {
063        commandStation = command;
064    }
065
066    @Override
067    protected void turnoutPushbuttonLockout(boolean _pushButtonLockout) {
068        if (log.isDebugEnabled()) {
069            log.debug("Send command to {} Pushbutton {}T{}",
070                    (_pushButtonLockout ? "Lock" : "Unlock"),
071                    _memo.getSystemPrefix(),
072                    _number);
073        }
074    }
075
076    @Override
077    public boolean canInvert() {
078        return true;
079    }
080
081    int _number; // turnout number
082
083    private final static Logger log = LoggerFactory.getLogger(SprogCSTurnout.class);
084
085}