001package jmri.jmrit.ussctc;
002
003import jmri.*;
004
005/**
006 * Models a traffic relay.
007 * <p>
008 * A traffic relay has three states, representing a section of track is
009 * allocated to traffic in one direction, the other, or neither.
010 *
011 * @author Bob Jacobsen Copyright (C) 2007, 2017
012 */
013public class TrafficRelay implements Lock {
014
015    enum State {
016        Left,
017        Right,
018        Neither }
019
020    /**
021     * @param signal SignalHeadSection at far end of this route
022     * @param direction Setting that, if present in the far SignalHeadSection, means to lock
023     */
024    public TrafficRelay(SignalHeadSection signal, CodeGroupThreeBits direction) {
025        this.farSignal = signal;
026        this.direction = direction;
027        beans = null;
028    }
029
030    /**
031     * @param signal SignalHeadSection at far end of this route
032     * @param direction Setting that, if present in the far SignalHeadSection, means to lock
033     * @param beans bean setting array.
034     */
035    public TrafficRelay(SignalHeadSection signal, CodeGroupThreeBits direction, BeanSetting[] beans) {
036        this.farSignal = signal;
037        this.direction = direction;
038        this.beans = beans;
039    }
040
041    SignalHeadSection farSignal;
042    CodeGroupThreeBits direction;
043    BeanSetting[] beans;
044
045    /**
046     * Test for new condition
047     * @return True if lock is clear and operation permitted
048     */
049    @Override
050    public boolean isLockClear(LockLogger lockLogger) {
051        if (beans != null) {
052            // if route doesn't match, permitted
053            for (BeanSetting bean : beans) {
054                if ( ! bean.check()) {
055                    lockLogger.setStatus(this, "");
056                    return true;
057                }
058            }
059        }
060
061        if (farSignal.getLastIndication() == direction || farSignal.isRunningTime() ) {
062                lockLogger.setStatus(this, "Traffic locked to "+farSignal.getName());
063                return false;
064        }
065        lockLogger.setStatus(this, "");
066        return true;
067    }
068
069}