001package jmri.jmrit.ussctc; 002 003import java.util.List; 004import jmri.Conditional; 005import jmri.ConditionalAction; 006import jmri.ConditionalVariable; 007import jmri.InstanceManager; 008import jmri.JmriException; 009import jmri.Logix; 010import jmri.Turnout; 011import jmri.implementation.DefaultConditionalAction; 012 013/** 014 * Provide bean-like access to the collection of Logix, Routes, Memories, etc 015 * that make up a OsIndicator. 016 * <p> 017 * An OS Indicator drives the lamp on the panel for a particular OS. Honors a 018 * separate lock/unlocked indication by showing occupied if the associated 019 * turnout has been unlocked. 020 * 021 * @see jmri.jmrit.ussctc.OsIndicatorFrame 022 * @see jmri.jmrit.ussctc.OsIndicatorPanel 023 * @see jmri.jmrit.ussctc.OsIndicatorAction 024 * @author Bob Jacobsen Copyright (C) 2007 025 */ 026public class OsIndicator implements Constants { 027 028 static String logixPrefix = InstanceManager.getDefault(jmri.LogixManager.class).getSystemNamePrefix(); 029 final static String namePrefix = logixPrefix + ":" + commonNamePrefix + "OsIndicator" + commonNameSuffix; // NOI18N 030 031 /** 032 * Nobody can build anonymous object 033 */ 034 //private OsIndicator() {} 035 /** 036 * Create one from scratch 037 * 038 * @param output User- or System name of output turnout to be driven 039 * @param osSensor User- or System name of Sensor determining OS occupancy 040 * @param lock Name of NamedBean used for Locking (type to be decided) 041 */ 042 public OsIndicator(String output, String osSensor, String lock) { 043 this.lock = lock; 044 this.osSensor = osSensor; 045 this.output = output; 046 } 047 048 /** 049 * Create the underlying objects that implement this 050 */ 051 public void instantiate() { 052 // find/create Logix 053 String nameP = namePrefix + output; 054 Logix l = InstanceManager.getDefault(jmri.LogixManager.class). 055 getLogix(nameP); 056 if (l == null) { 057 l = InstanceManager.getDefault(jmri.LogixManager.class). 058 createNewLogix(nameP, ""); // NOI18N 059 } 060 l.deActivateLogix(); 061 // Find/create conditional and add 062 Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class) 063 .getConditional(l, nameP + "C1"); // NOI18N 064 if (c == null) { 065 c = InstanceManager.getDefault(jmri.ConditionalManager.class) 066 .createNewConditional(nameP + "C1", ""); // NOI18N 067 l.addConditional(nameP + "C1", -1); // NOI18N 068 } 069 070 // Load variable into the Conditional 071 List<ConditionalVariable> variableList = c.getCopyOfStateVariables(); 072 variableList.add(new ConditionalVariable(false, Conditional.Operator.NONE, 073 Conditional.Type.SENSOR_INACTIVE, 074 osSensor, true)); 075 if (!lock.isEmpty()) { 076 variableList.add(new ConditionalVariable(false, Conditional.Operator.AND, 077 Conditional.Type.SENSOR_INACTIVE, 078 lock, true)); 079 } 080 c.setStateVariables(variableList); 081 082 List<ConditionalAction> actionList = c.getCopyOfActions(); 083 actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, 084 Conditional.Action.SET_TURNOUT, output, 085 Turnout.CLOSED, " ")); // NOI18N 086 actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE, 087 Conditional.Action.SET_TURNOUT, output, 088 Turnout.THROWN, " ")); // NOI18N 089 c.setAction(actionList); // string data 090 091 // and put it back in operation 092 l.activateLogix(); 093 094 } 095 096 /** 097 * Create an object to represent an existing OsIndicator. 098 * 099 * @param outputName name of output Turnout that drives the indicator 100 * @throws JmriException if no such OsIndicator exists, or some problem 101 * found 102 */ 103 public OsIndicator(String outputName) throws jmri.JmriException { 104 this.output = outputName; 105 106 // findLogix 107 String nameP = namePrefix + output; 108 Logix l = InstanceManager.getDefault(jmri.LogixManager.class). 109 getLogix(nameP); 110 if (l == null) { 111 throw new jmri.JmriException("Logix does not exist"); // NOI18N 112 } 113 114 // Find/create conditional and add 115 Conditional c = InstanceManager.getDefault(jmri.ConditionalManager.class) 116 .getConditional(l, nameP + "C1"); // NOI18N 117 if (c == null) { 118 throw new jmri.JmriException("Conditional does not exist"); // NOI18N 119 } 120 121 // Load variables from the Conditional 122 List<ConditionalVariable> variableList = c.getCopyOfStateVariables(); 123 ConditionalVariable variable = variableList.get(0); 124 osSensor = variable.getName(); 125 if (variableList.size() > 0) { 126 variable = variableList.get(1); 127 lock = variable.getName(); 128 } 129 } 130 131 public String getOutputName() { 132 return output; 133 } 134 135 public String getOsSensorName() { 136 return osSensor; 137 } 138 139 public String getLockName() { 140 return lock; 141 } 142 143 String output; 144 String osSensor; 145 String lock; 146 147}