Class AbstractSensor
- java.lang.Object
-
- jmri.implementation.AbstractNamedBean
-
- jmri.implementation.AbstractSensor
-
- All Implemented Interfaces:
java.lang.Comparable<NamedBean>
,PropertyChangeProvider
,DigitalIO
,NamedBean
,Sensor
- Direct Known Subclasses:
AcelaSensor
,BiDiBSensor
,CbusSensor
,Dcc4PcSensor
,DCCppSensor
,EcosSensor
,IpocsSensor
,JMRIClientSensor
,LnSensor
,MarklinSensor
,MqttSensor
,NceSensor
,OlcbSensor
,RaspberryPiSensor
,RfidSensor
,RpsSensor
,SerialSensor
,SerialSensor
,SerialSensor
,SerialSensor
,SerialSensor
,SerialSensor
,SRCPSensor
,TamsSensor
,XBeeSensor
,XNetSensor
,Z21CanSensor
,Z21RMBusSensor
public abstract class AbstractSensor extends AbstractNamedBean implements Sensor
Abstract class providing the basic logic of the Sensor interface.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface jmri.NamedBean
NamedBean.BadNameException, NamedBean.BadSystemNameException, NamedBean.BadUserNameException, NamedBean.DisplayOptions, NamedBean.DuplicateSystemNameException
-
Nested classes/interfaces inherited from interface jmri.Sensor
Sensor.PullResistance
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_inverted
protected int
_knownState
protected int
_rawState
protected java.lang.Runnable
r
(package private) Reporter
reporter
(package private) int
restartcount
protected long
sensorDebounceGoingActive
protected long
sensorDebounceGoingInActive
protected java.lang.Thread
thr
protected boolean
useDefaultTimerSettings
-
Fields inherited from class jmri.implementation.AbstractNamedBean
listenerRefs, mSystemName, register
-
Fields inherited from interface jmri.NamedBean
DISPLAY_NAME_FORMAT, INCONSISTENT, PROPERTY_STATE, QUOTED_NAME_FORMAT, UNKNOWN
-
Fields inherited from interface jmri.Sensor
ACTIVE, INACTIVE, MAX_DEBOUNCE
-
-
Constructor Summary
Constructors Constructor Description AbstractSensor(java.lang.String systemName)
AbstractSensor(java.lang.String systemName, java.lang.String userName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canInvert()
By default, all implementations based on this can invertjava.lang.String
describeState(int state)
Provide human-readable, localized version of state value.void
dispose()
Deactivate this object, so that it releases as many resources as possible and no longer effects others.java.lang.String
getBeanType()
For instances in the code where we are dealing with just a bean and a message needs to be passed to the user or in a log.boolean
getInverted()
Get the inverted state.int
getKnownState()
Query the known state.Sensor.PullResistance
getPullResistance()
Get the pull resistance.int
getRawState()
Used to return the Raw state of a sensor prior to the known state of a sensor being set.Reporter
getReporter()
Retrieve the reporter associated with this sensor if there is one.long
getSensorDebounceGoingActiveTimer()
Get the active debounce delay.long
getSensorDebounceGoingInActiveTimer()
Get the inactive debounce delay.int
getState()
Implement a shorter name for getKnownState.boolean
getUseDefaultTimerSettings()
Does this sensor use the default timers values?protected void
sensorDebounce()
Before going active or inactive or checking that we can go active, we will wait for sensorDebounceGoing(In)Active for things to settle down to help prevent a race condition.void
setInverted(boolean inverted)
Control whether the actual sensor input is considered to be inverted, e.g. the normal electrical signal that results in an ACTIVE state now results in an INACTIVE state.void
setKnownState(int newState)
Perform setKnownState(int) for implementations that can't actually do it on the layout.void
setOwnState(int s)
Set our internal state information, and notify bean listeners.void
setPullResistance(Sensor.PullResistance r)
Set the pull resistancevoid
setReporter(Reporter er)
Some sensor boards also serve the function of being able to report back train identities via such methods as RailCom.void
setSensorDebounceGoingActiveTimer(long time)
Set the active debounce delay.void
setSensorDebounceGoingInActiveTimer(long time)
Set the inactive debounce delay.void
setState(int s)
Implement a shorter name for setKnownState.void
setUseDefaultTimerSettings(boolean boo)
Use the timers specified in theSensorManager
for the debounce delay.protected boolean
stateChangeCheck(int newState)
Preprocess a Sensor state change request for specific implementations ofsetKnownState(int)
-
Methods inherited from class jmri.implementation.AbstractNamedBean
addPropertyChangeListener, addPropertyChangeListener, addPropertyChangeListener, addPropertyChangeListener, compareSystemNameSuffix, equals, firePropertyChange, getComment, getDisplayName, getDisplayName, getListenerRef, getListenerRefs, getNumPropertyChangeListeners, getProperty, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeListenersByReference, getPropertyKeys, getSystemName, getUserName, hashCode, removeProperty, removePropertyChangeListener, removePropertyChangeListener, setComment, setProperty, setUserName, toString, toStringSuffix, updateListenerRef, vetoableChange
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jmri.DigitalIO
requestUpdateFromLayout
-
Methods inherited from interface jmri.NamedBean
addPropertyChangeListener, addPropertyChangeListener, compareSystemNameSuffix, compareTo, getComment, getDisplayName, getDisplayName, getListenerRef, getListenerRefs, getNumPropertyChangeListeners, getProperty, getPropertyChangeListenersByReference, getPropertyKeys, getSystemName, getUsageReport, getUserName, removeProperty, setComment, setProperty, setUserName, toString, updateListenerRef, vetoableChange
-
Methods inherited from interface jmri.beans.PropertyChangeProvider
addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
Methods inherited from interface jmri.Sensor
getCommandedState, isConsistentState, setCommandedState
-
-
-
-
Field Detail
-
sensorDebounceGoingActive
protected long sensorDebounceGoingActive
-
sensorDebounceGoingInActive
protected long sensorDebounceGoingInActive
-
useDefaultTimerSettings
protected boolean useDefaultTimerSettings
-
thr
protected java.lang.Thread thr
-
r
protected java.lang.Runnable r
-
restartcount
int restartcount
-
_inverted
protected boolean _inverted
-
_knownState
protected int _knownState
-
_rawState
protected int _rawState
-
-
Constructor Detail
-
AbstractSensor
public AbstractSensor(java.lang.String systemName)
-
AbstractSensor
public AbstractSensor(java.lang.String systemName, java.lang.String userName)
-
-
Method Detail
-
getBeanType
@Nonnull public java.lang.String getBeanType()
Description copied from interface:NamedBean
For instances in the code where we are dealing with just a bean and a message needs to be passed to the user or in a log.- Specified by:
getBeanType
in interfaceNamedBean
- Returns:
- a string of the bean type, eg Turnout, Sensor etc
-
getKnownState
public int getKnownState()
Description copied from interface:DigitalIO
Query the known state. This is a bound parameter, so you can also register a listener to be informed of changes. A result is always returned; if no other feedback method is available, the commanded state will be used.- Specified by:
getKnownState
in interfaceDigitalIO
- Returns:
- the known state
-
setSensorDebounceGoingActiveTimer
public void setSensorDebounceGoingActiveTimer(long time)
Set the active debounce delay.- Specified by:
setSensorDebounceGoingActiveTimer
in interfaceSensor
- Parameters:
time
- delay in milliseconds; set to zero to de-activate debounce
-
getSensorDebounceGoingActiveTimer
public long getSensorDebounceGoingActiveTimer()
Get the active debounce delay.- Specified by:
getSensorDebounceGoingActiveTimer
in interfaceSensor
- Returns:
- delay in milliseconds
-
setSensorDebounceGoingInActiveTimer
public void setSensorDebounceGoingInActiveTimer(long time)
Set the inactive debounce delay.- Specified by:
setSensorDebounceGoingInActiveTimer
in interfaceSensor
- Parameters:
time
- delay in milliseconds; set to zero to de-activate debounce
-
getSensorDebounceGoingInActiveTimer
public long getSensorDebounceGoingInActiveTimer()
Get the inactive debounce delay.- Specified by:
getSensorDebounceGoingInActiveTimer
in interfaceSensor
- Returns:
- delay in milliseconds
-
setUseDefaultTimerSettings
public void setUseDefaultTimerSettings(boolean boo)
Description copied from interface:Sensor
Use the timers specified in theSensorManager
for the debounce delay.- Specified by:
setUseDefaultTimerSettings
in interfaceSensor
- Parameters:
boo
- true to set to current defaults if not previously true
-
getUseDefaultTimerSettings
public boolean getUseDefaultTimerSettings()
Description copied from interface:Sensor
Does this sensor use the default timers values? (A remarkably unfortunate name given the one above)- Specified by:
getUseDefaultTimerSettings
in interfaceSensor
- Returns:
- true if using default debounce values from the
SensorManager
-
sensorDebounce
protected void sensorDebounce()
Before going active or inactive or checking that we can go active, we will wait for sensorDebounceGoing(In)Active for things to settle down to help prevent a race condition.
-
describeState
@Nonnull @CheckReturnValue public java.lang.String describeState(int state)
Description copied from class:AbstractNamedBean
Provide human-readable, localized version of state value.This method is intended for use when presenting to a human operator.
- Specified by:
describeState
in interfaceNamedBean
- Overrides:
describeState
in classAbstractNamedBean
- Parameters:
state
- the state to describe- Returns:
- the state in localized form
-
setKnownState
public void setKnownState(int newState) throws JmriException
Perform setKnownState(int) for implementations that can't actually do it on the layout. Not intended for use by implementations that can.- Specified by:
setKnownState
in interfaceSensor
- Parameters:
newState
- the state to set- Throws:
JmriException
- if unable to set the state
-
stateChangeCheck
protected boolean stateChangeCheck(int newState) throws java.lang.IllegalArgumentException
Preprocess a Sensor state change request for specific implementations ofsetKnownState(int)
- Parameters:
newState
- the Sensor state command value passed- Returns:
- true if a Sensor.ACTIVE was requested and Sensor is not set to _inverted
- Throws:
java.lang.IllegalArgumentException
-
setOwnState
public void setOwnState(int s)
Set our internal state information, and notify bean listeners.- Parameters:
s
- the new state
-
getRawState
public int getRawState()
Description copied from interface:Sensor
Used to return the Raw state of a sensor prior to the known state of a sensor being set. The raw state value can be different from the known state when the sensor debounce option is used.- Specified by:
getRawState
in interfaceSensor
- Returns:
- raw state value
-
setState
public void setState(int s) throws JmriException
Implement a shorter name for setKnownState.This generally shouldn't be used by Java code; use setKnownState instead. The is provided to make Jython script access easier to read.
- Specified by:
setState
in interfaceNamedBean
- Parameters:
s
- the state- Throws:
JmriException
- general error when setting the state fails
-
getState
public int getState()
Implement a shorter name for getKnownState.This generally shouldn't be used by Java code; use getKnownState instead. The is provided to make Jython script access easier to read.
-
setInverted
public void setInverted(boolean inverted)
Control whether the actual sensor input is considered to be inverted, e.g. the normal electrical signal that results in an ACTIVE state now results in an INACTIVE state.- Specified by:
setInverted
in interfaceSensor
- Parameters:
inverted
- true if the sensor should be inverted; false otherwise
-
getInverted
public final boolean getInverted()
Get the inverted state. If true, the electrical signal that results in an ACTIVE state now results in an INACTIVE state.Used in polling loops in system-specific code, so made final to allow optimization.
- Specified by:
getInverted
in interfaceSensor
- Returns:
- true if the electrical signal that normally results in an ACTIVE state now results in an INACTIVE state; false otherwise
-
canInvert
public boolean canInvert()
By default, all implementations based on this can invert
-
setReporter
public void setReporter(Reporter er)
Some sensor boards also serve the function of being able to report back train identities via such methods as RailCom. The setting and creation of the reporter against the sensor should be done when the sensor is created. This information is not saved.- Specified by:
setReporter
in interfaceSensor
- Parameters:
er
- the reporter to set
-
getReporter
public Reporter getReporter()
Description copied from interface:Sensor
Retrieve the reporter associated with this sensor if there is one.- Specified by:
getReporter
in interfaceSensor
- Returns:
- the reporter or null if there is no associated reporter
-
setPullResistance
public void setPullResistance(Sensor.PullResistance r)
Set the pull resistanceIn this default implementation, the input value is ignored.
- Specified by:
setPullResistance
in interfaceSensor
- Parameters:
r
- PullResistance value to use.
-
getPullResistance
public Sensor.PullResistance getPullResistance()
Get the pull resistance.- Specified by:
getPullResistance
in interfaceSensor
- Returns:
- the currently set PullResistance value. In this default implementation, PullResistance.PULL_OFF is always returned.
-
dispose
public void dispose()
Description copied from class:AbstractNamedBean
Deactivate this object, so that it releases as many resources as possible and no longer effects others.For example, if this object has listeners, after a call to this method it should no longer notify those listeners. Any native or system-wide resources it maintains should be released, including threads, files, etc.
It is an error to invoke any other methods on this object once dispose() has been called. Note, however, that there is no guarantee about behavior in that case.
Afterwards, references to this object may still exist elsewhere, preventing its garbage collection. But it's formally dead, and shouldn't be keeping any other objects alive. Therefore, this method should null out any references to other objects that this NamedBean contained.
-
-