001package jmri.jmrit.ctc; 002 003import java.util.ArrayList; 004import jmri.Sensor; 005 006/** 007 * 008 * @author Gregory J. Bedlek Copyright (C) 2018, 2019 009 * 010 * The purpose of this object is to just see if the passed indicators are all 011 * lit (Sensor.ACTIVE), meaning that the specified route is "selected". 012 * Any unspecified indicators are ignored. 013 */ 014public class SwitchIndicatorsRoute { 015 private final NBHSensor _mSwitchIndicator1; 016 private final NBHSensor _mSwitchIndicator2; 017 private final NBHSensor _mSwitchIndicator3; 018 private final NBHSensor _mSwitchIndicator4; 019 private final NBHSensor _mSwitchIndicator5; 020 private final NBHSensor _mSwitchIndicator6; 021 022 public SwitchIndicatorsRoute( String module, String userIdentifier, String parameter, 023 String switchIndicator1, 024 String switchIndicator2, 025 String switchIndicator3, 026 String switchIndicator4, 027 String switchIndicator5, 028 String switchIndicator6 ) { 029 _mSwitchIndicator1 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator1", switchIndicator1, true); // NOI18N 030 _mSwitchIndicator2 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator2", switchIndicator2, true); // NOI18N 031 _mSwitchIndicator3 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator3", switchIndicator3, true); // NOI18N 032 _mSwitchIndicator4 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator4", switchIndicator4, true); // NOI18N 033 _mSwitchIndicator5 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator5", switchIndicator5, true); // NOI18N 034 _mSwitchIndicator6 = new NBHSensor(module, userIdentifier, parameter + " switchIndicator6", switchIndicator6, true); // NOI18N 035 } 036 037 public SwitchIndicatorsRoute(NBHSensor switchIndicator1, NBHSensor switchIndicator2, NBHSensor switchIndicator3, NBHSensor switchIndicator4, NBHSensor switchIndicator5, NBHSensor switchIndicator6) { 038 _mSwitchIndicator1 = switchIndicator1; 039 _mSwitchIndicator2 = switchIndicator2; 040 _mSwitchIndicator3 = switchIndicator3; 041 _mSwitchIndicator4 = switchIndicator4; 042 _mSwitchIndicator5 = switchIndicator5; 043 _mSwitchIndicator6 = switchIndicator6; 044 } 045 046 public SwitchIndicatorsRoute(ArrayList<NBHSensor> switchIndicators) { 047 int rows = switchIndicators.size(); 048 int i = 0; 049 if (i < rows) _mSwitchIndicator1 = switchIndicators.get(i++); else _mSwitchIndicator1 = null; 050 if (i < rows) _mSwitchIndicator2 = switchIndicators.get(i++); else _mSwitchIndicator2 = null; 051 if (i < rows) _mSwitchIndicator3 = switchIndicators.get(i++); else _mSwitchIndicator3 = null; 052 if (i < rows) _mSwitchIndicator4 = switchIndicators.get(i++); else _mSwitchIndicator4 = null; 053 if (i < rows) _mSwitchIndicator5 = switchIndicators.get(i++); else _mSwitchIndicator5 = null; 054 if (i < rows) _mSwitchIndicator6 = switchIndicators.get(i++); else _mSwitchIndicator6 = null; 055 } 056 057 public boolean isRouteSelected() { 058 if (!isSwitchIndicatorLit(_mSwitchIndicator1)) return false; 059 if (!isSwitchIndicatorLit(_mSwitchIndicator2)) return false; 060 if (!isSwitchIndicatorLit(_mSwitchIndicator3)) return false; 061 if (!isSwitchIndicatorLit(_mSwitchIndicator4)) return false; 062 if (!isSwitchIndicatorLit(_mSwitchIndicator5)) return false; 063 if (!isSwitchIndicatorLit(_mSwitchIndicator6)) return false; 064 return true; 065 } 066 067// Quick and Dirty Routine: If it doesn't exist, it's considered lit. If it exists, ACTIVE = lit. 068 private boolean isSwitchIndicatorLit(NBHSensor sensor) { 069 if (sensor == null) return true; 070 if (sensor.valid()) return sensor.getKnownState() == Sensor.ACTIVE; 071 return true; 072 } 073}