|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjmri.jmrit.automat.AbstractAutomaton
public class AbstractAutomaton
Abstract base for user automaton classes, which provide individual bits of automation.
Each individual automaton runs in a separate thread, so they can operate independently. This class handles thread creation and scheduling, and provides a number of services for the user code.
Subclasses provide a "handle()" function, which does the needed work, and optionally a "init()" function. These can use any JMRI resources for input and output. It should not spin on a condition without explicit wait requests; it is more efficient to use the explicit wait services when waiting for some specific condition.
handle() is executed repeatedly until either the Automate object is halted(), or it returns "false". Returning "true" will just cause handle() to be invoked again, so you can cleanly restart the Automaton by returning from multiple points in the function.
Since handle() executes outside the GUI thread, it is important that access to GUI (AWT, Swing) objects be scheduled through the various service routines.
Services are provided by public member functions, described below. They must only be invoked from the init and handle methods, as they must be used in a delayable thread. If invoked from the GUI thread, for example, the program will appear to hang. To help ensure this, a warning will be logged if they are used before the thread starts.
For general use, e.g. in scripts, the most useful functions are:
waitMsec(int)
waitSensorActive(jmri.Sensor)
This is also available
in a form that will wait for any of a group of sensors to be active.
waitSensorInactive(jmri.Sensor)
This is also available
in a form that will wait for any of a group of sensors to be inactive.
waitSensorState(jmri.Sensor, int)
waitSensorChange(int, jmri.Sensor)
setTurnouts(jmri.Turnout[], jmri.Turnout[])
waitTurnoutConsistent(jmri.Turnout[])
waitChange(jmri.NamedBean[])
getThrottle(int, boolean)
readServiceModeCV(int)
writeServiceModeCV(int, int)
writeOpsModeCV(int, int, boolean, int)
Although this is named an "Abstract" class, it's actually concrete so that Jython code can easily use some of the methods.
| Nested Class Summary | |
|---|---|
class |
AbstractAutomaton.MsgFrame
Internal class to show a Frame |
| Field Summary | |
|---|---|
(package private) Thread |
currentThread
|
(package private) JFrame |
debugWaitFrame
|
(package private) static org.slf4j.Logger |
log
|
(package private) String |
message
|
(package private) JFrame |
messageFrame
|
protected boolean |
promptOnWait
Control optional debugging prompt. |
(package private) AutomatSummary |
summary
|
| Constructor Summary | |
|---|---|
AbstractAutomaton()
|
|
AbstractAutomaton(String name)
|
|
| Method Summary | |
|---|---|
(package private) void |
defaultName()
|
(package private) void |
done()
Part of the internal implementation; not for general use. |
int |
getCount()
Returns the number of times the handle routine has executed. |
String |
getName()
Gives access to the thread name. |
DccThrottle |
getThrottle(BasicRosterEntry re)
Obtains a DCC throttle, including waiting for the command station response. |
DccThrottle |
getThrottle(int address,
boolean longAddress)
Obtains a DCC throttle, including waiting for the command station response. |
protected boolean |
handle()
User-provided main routine. |
protected void |
init()
User-provided initialization routine. |
int |
readServiceModeCV(int CV)
Read a CV on the service track, including waiting for completion. |
void |
run()
Part of the implementation; not for general use. |
void |
setName(String name)
Update the name of this object. |
void |
setTurnouts(Turnout[] closed,
Turnout[] thrown)
Convenience function to set a bunch of turnouts and wait until they are all in a consistent state |
void |
start()
Start this automat processing. |
void |
stop()
Stop the thread immediately. |
protected void |
wait(int milliseconds)
Part of the intenal implementation, not intended for users. |
void |
waitChange(NamedBean[] mInputs)
Wait for one of a list of NamedBeans (sensors, signal heads and/or turnouts) to change. |
void |
waitMsec(int milliseconds)
Wait for a specified number of milliseconds, and then return control. |
void |
waitSensorActive(Sensor mSensor)
Wait for a sensor to be active. |
void |
waitSensorActive(Sensor[] mSensors)
Wait for one of a list of sensors to be be active. |
int |
waitSensorChange(int mState,
Sensor mSensor)
Wait for a sensor to change state. |
void |
waitSensorChange(Sensor[] mSensors)
Wait for one of an array of sensors to change. |
void |
waitSensorInactive(Sensor mSensor)
Wait for a sensor to be inactive. |
void |
waitSensorInactive(Sensor[] mSensors)
Wait for one of a list of sensors to be be inactive. |
void |
waitSensorState(Sensor[] mSensors,
int state)
Wait for one of a list of sensors to be be in a selected state. |
void |
waitSensorState(Sensor mSensor,
int state)
Internal service routine to wait for one sensor to be in (or become in) a specific state. |
void |
waitTurnoutConsistent(Turnout[] mTurnouts)
Wait for a list of turnouts to all be in a consistent state This works by registering a listener, which is likely to run in another thread. |
boolean |
writeOpsModeCV(int CV,
int value,
boolean longAddress,
int loco)
Write a CV in ops mode, including waiting for completion. |
boolean |
writeServiceModeCV(int CV,
int value)
Write a CV on the service track, including waiting for completion. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
AutomatSummary summary
Thread currentThread
protected boolean promptOnWait
JFrame messageFrame
String message
JFrame debugWaitFrame
static org.slf4j.Logger log
| Constructor Detail |
|---|
public AbstractAutomaton()
public AbstractAutomaton(String name)
| Method Detail |
|---|
public void start()
public void run()
This is invoked on currentThread.
run in interface Runnablepublic void stop()
void done()
public int getCount()
jmri.jmrit.automat.monitor to monitor progress
public String getName()
jmri.jmrit.automat.monitor
public void setName(String name)
getName()void defaultName()
protected void init()
protected boolean handle()
public void waitMsec(int milliseconds)
protected void wait(int milliseconds)
This handles exceptions internally, so they needn't clutter up the code. Note that the current implementation doesn't guarantee the time, either high or low.
Because of the way Jython access handles synchronization, this is explicitly synchronized internally.
milliseconds -
public int waitSensorChange(int mState,
Sensor mSensor)
The current (OK) state of the Sensor is passed to avoid a possible race condition. The new state is returned for a similar reason.
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
mState - Current state of the sensormSensor - Sensor to watch
public void waitSensorActive(Sensor mSensor)
mSensor - Sensor to watchpublic void waitSensorInactive(Sensor mSensor)
mSensor - Sensor to watch
public void waitSensorState(Sensor mSensor,
int state)
Used by waitSensorActive and waitSensorInactive
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
public void waitSensorInactive(Sensor[] mSensors)
public void waitSensorActive(Sensor[] mSensors)
public void waitSensorState(Sensor[] mSensors,
int state)
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
mSensors - Array of sensors to watchstate - State to check (static value from jmri.Sensors)public void waitTurnoutConsistent(Turnout[] mTurnouts)
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
mTurnouts - list of turnouts to watch
public void setTurnouts(Turnout[] closed,
Turnout[] thrown)
closed - turnouts to set to closed statethrown - turnouts to set to thrown statepublic void waitChange(NamedBean[] mInputs)
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
mInputs - Array of NamedBeans to watchpublic void waitSensorChange(Sensor[] mSensors)
This is an older method, now superceded by waitChange, which can wait for any NamedBean.
mSensors - Array of sensors to watch
public DccThrottle getThrottle(int address,
boolean longAddress)
address - longAddress - true if this is a long address, false for a short address
public DccThrottle getThrottle(BasicRosterEntry re)
re - specifies the desired locomotive
public boolean writeServiceModeCV(int CV,
int value)
CV - Number 1 through 512value -
public int readServiceModeCV(int CV)
CV - Number 1 through 512
public boolean writeOpsModeCV(int CV,
int value,
boolean longAddress,
int loco)
CV - Number 1 through 512value - loco - Locomotive decoder addresslongAddress - true is the locomotive is using a long address
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||