001package jmri.jmrit.ussctc;
002
003import jmri.*;
004
005
006/**
007 * Lock if a turnout isn't in the desired state.
008 * <p>
009 * Can be used to e.g. lock when a call-on (turnout) is set
010 *
011 * @author Bob Jacobsen Copyright (C) 2007, 2017
012 */
013public class TurnoutLock implements Lock {
014
015    /**
016     * @param name System or user name of turnout to monitor
017     * @param okValue If this value isn't present, the operation is locked out
018     */
019    public TurnoutLock(String name, int okValue) {
020        Turnout t = InstanceManager.getDefault(TurnoutManager.class).provideTurnout(name);
021        turnout = InstanceManager.getDefault(NamedBeanHandleManager.class).getNamedBeanHandle(name,t);
022        this.value = okValue;
023    }
024
025    NamedBeanHandle<Turnout> turnout;
026    int value;
027
028    /**
029     * Test the lock conditions
030     * @return True if lock is clear and operation permitted
031     */
032    @Override
033    public boolean isLockClear(LockLogger lockLogger) {
034        if (turnout.getBean().getKnownState() != value) {
035                lockLogger.setStatus(this, "Locked due to setting: "+turnout.getBean().getDisplayName());
036            return false;
037        }
038        lockLogger.setStatus(this, "");
039        return true;
040    }
041
042    @Override
043    public String toString() {
044        String retval = isLockClear(debugLockLogger) ? "clear " : "locked";
045        retval = retval+debugLockLogger.memory.getValue();
046        return retval;
047    }
048
049}