001package jmri.jmrit.ussctc;
002
003import jmri.*;
004
005
006/**
007 * Implements a traffic lock.
008 * <p>
009 * A signal can't be set if a route (set of turnout settings) is present
010 * and the far-end signal is set against. Each lock object handles one route.
011 *
012 * @author Bob Jacobsen Copyright (C) 2007, 2017
013 */
014public class TrafficLock implements Lock {
015
016    /**
017     * @param signal SignalHeadSection at far end of this route
018     * @param direction Setting that, if present in the far SignalHeadSection, means to lock
019     */
020    public TrafficLock(SignalHeadSection signal, CodeGroupThreeBits direction) {
021        this.farSignal = signal;
022        this.direction = direction;
023        beans = null;
024    }
025
026    /**
027     * @param signal SignalHeadSection at far end of this route
028     * @param direction Setting that, if present in the far SignalHeadSection, means to lock
029     * @param beans bean setting array.
030     */
031    public TrafficLock(SignalHeadSection signal, CodeGroupThreeBits direction, BeanSetting[] beans) {
032        this.farSignal = signal;
033        this.direction = direction;
034        this.beans = beans;
035    }
036
037    SignalHeadSection farSignal;
038    CodeGroupThreeBits direction;
039    BeanSetting[] beans;
040
041    /**
042     * Test the lock conditions
043     * @return True if lock is clear and operation permitted
044     */
045    @Override
046    public boolean isLockClear(LockLogger lockLogger) {
047        if (beans != null) {
048            // if route doesn't match, permitted
049            for (BeanSetting bean : beans) {
050                if ( ! bean.check()) {
051                    lockLogger.setStatus(this, "");
052                    return true;
053                }
054            }
055        }
056
057        if (farSignal.getLastIndication() == direction || farSignal.isRunningTime() ) {
058                lockLogger.setStatus(this, "Traffic locked to signal \""+farSignal.getName()+"\"");
059                return false;
060        }
061        lockLogger.setStatus(this, "");
062        return true;
063    }
064
065    @Override
066    public String toString() {
067        String retval = isLockClear(debugLockLogger) ? "clear " : "locked";
068        retval = retval+debugLockLogger.memory.getValue();
069        return retval;
070    }
071
072}