Package jmri.jmrix.bidib
Class BiDiBTrafficController
- java.lang.Object
-
- jmri.jmrix.bidib.BiDiBTrafficController
-
- All Implemented Interfaces:
CommandStation
public class BiDiBTrafficController extends java.lang.Object implements CommandStation
The BiDiB Traffic Controller provides the interface for JMRI to the BiDiB Library (jbidibc) - it does not handle any protocol functions itself. Therefor it does not extend AbstractMRTrafficController. Instead, it delegates BiDiB handling to a BiDiB controller instance (serial, simulation, etc.) using BiDiBInterface.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringASYNCCONNECTIONINIT(package private) org.bidib.jbidibc.messages.NodedebugSavedNodestatic java.lang.StringISNETBIDIB(package private) BiDiBSystemConnectionMemomMemoReference to the system connection memo.protected org.bidib.jbidibc.messages.enums.CommandStationStatemSavedModeprotected java.util.TreeMap<java.lang.Long,org.bidib.jbidibc.messages.Node>nodesstatic java.lang.StringUSELOCALPING
-
Constructor Summary
Constructors Constructor Description BiDiBTrafficController(org.bidib.jbidibc.core.BidibInterface b)Create a new BiDiBTrafficController instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccessoryState(org.bidib.jbidibc.messages.Node node)voidaddConnectionChangedListener(java.awt.event.ActionListener l)Add/Remove an ActionListener to be called when the connection has changed.voidaddMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)Add a message Listener to the connectionvoidaddRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)Add a raw message Listener to the connectionvoidallAccessoryState()voidallFeedback()Request Feedback Status (called BM status in BiDiB - BM (Belegtmelder) is german for "feedback") from all ports on all nodes of this connection.voidallPortConfigX()Request CONFIGX from all ports on all nodes of this connection.voidallPortLcStat()Request LC_STAT from all ports on all nodes of this connection.intcheckProgMode(boolean needProgMode, org.bidib.jbidibc.messages.Node node)Check if the command station is in the requested state (Normal, PT) If the command station is not in the requested state, a message is sent to BiDiB to switch to the requested state.voidconnectionLost()Set the connection to lost state.org.bidib.jbidibc.messages.helpers.ContextconnnectPort(PortAdapter p)Opens the BiDiB connection in the jbidibc library, add listeners and initialize BiDiB.org.bidib.jbidibc.messages.LcConfigXconvertConfig2ConfigX(org.bidib.jbidibc.messages.Node node, org.bidib.jbidibc.messages.LcConfig lcConfig)Convert a CONFIG object to a CONFIGX object.voidfeedback(org.bidib.jbidibc.messages.Node node)Request Feedback Status (called BM status in BiDiB - BM (Belegtmelder) is german for "feedback") from all ports on a given node.org.bidib.jbidibc.messages.FeaturefindNodeFeature(org.bidib.jbidibc.messages.Node node, int requestedFeatureId)Find a feature for given node.java.util.List<org.bidib.jbidibc.messages.LcConfigX>getAllPortConfigX(org.bidib.jbidibc.messages.Node node, org.bidib.jbidibc.messages.enums.LcOutputType type)Request CONFIGX from all ports on a given node and possibly only for a given type.org.bidib.jbidibc.core.BidibInterfacegetBidib()Get Bidib Interfaceorg.bidib.jbidibc.messages.NodegetCurrentGlobalProgrammerNode()Get the cached global programmer node.org.bidib.jbidibc.messages.NodegetFirstBoosterNode()Get the first booster node.org.bidib.jbidibc.messages.NodegetFirstCommandStationNode()Get the first and most probably only command station node (also used for Programming on the Main - POM) A cached value is returned here for performance reasons since the function is called very often.org.bidib.jbidibc.messages.NodegetFirstGlobalProgrammerNode()Get the most probably only global programmer node (aka service mode node, used for prgramming track - PT, not for POM) If there are more than one suitable node, get the last one (reverse nodes list search).org.bidib.jbidibc.messages.NodegetFirstOutputNode()Get the first output node - a node that can control LC ports.org.bidib.jbidibc.messages.NodegetNodeByAddr(byte[] addr)Get node by node address from nodelistorg.bidib.jbidibc.messages.NodegetNodeByUniqueID(long uniqueId)Get node by unique id from nodelistorg.bidib.jbidibc.messages.NodegetNodeByUserName(java.lang.String userName)Get node by node username from nodelistintgetNodeFeature(org.bidib.jbidibc.messages.Node node, int requestedFeatureId)Get the feature value of a node.java.util.Map<java.lang.Long,org.bidib.jbidibc.messages.Node>getNodeList()Get the list of nodes foundorg.bidib.jbidibc.messages.LcConfigXgetPortConfigX(org.bidib.jbidibc.messages.Node node, int portAddr, org.bidib.jbidibc.messages.enums.LcOutputType type)Request CONFIGX if a given port on a given node and possibly only for a given type.org.bidib.jbidibc.messages.enums.PortModelEnumgetPortModel(org.bidib.jbidibc.messages.Node node)org.bidib.jbidibc.messages.NodegetRootNode()Get root node from nodelistBiDiBSystemConnectionMemogetSystemConnectionMemo()Get access to the system connection memo associated with this traffic controller.java.lang.StringgetSystemPrefix()java.lang.StringgetUserName()booleanhasAccessoryNode()Check if we have at least one node capable of Accessory functionsbooleanisConnectionReady()Check if the connection is ready to communicate.booleanisDetached()Check if the connection is detached i.e. it is opened, paired but the logon has been rejected.booleanisGlobalProgrammerNode(org.bidib.jbidibc.messages.Node node)A node suitable as a global programmer must be - a command station, - must support service mode programming and - must be a boosterbooleanisNetBiDiB()Check of the connection is netBiDiB.voidportLcStat(org.bidib.jbidibc.messages.Node node, int typemask)Request LC_STAT from all ports on a given node.voidremoveConnectionChangedListener(java.awt.event.ActionListener l)voidremoveMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)Remove a message Listener from the connectionvoidremoveRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)Remove a raw message Listener from the connectionvoidsendBiDiBMessage(org.bidib.jbidibc.messages.message.BidibCommandMessage m, org.bidib.jbidibc.messages.Node node)Forward a preformatted BiDiBMessage to the actual interface.booleansendPacket(byte[] packet, int repeats)Send a specific packet to the rails.booleansetCurrentGlobalProgrammerNode(org.bidib.jbidibc.messages.Node node)Set the global programmer node to use.voidsetLogon(boolean logon)Set or remove the detached state.voidsetSystemConnectionMemo(BiDiBSystemConnectionMemo m)Set the system connection memo associated with this traffic controller.voidsetWatchdogTimer(boolean state)voidstartLocalPing()Start jbidibc built-in local ping as a watchdog of the connection.protected voidterminate()voidTEST(boolean a)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.CommandStation
sendAccSignalDecoderPkt, sendAltAccSignalDecoderPkt
-
-
-
-
Field Detail
-
ASYNCCONNECTIONINIT
public static final java.lang.String ASYNCCONNECTIONINIT
- See Also:
- Constant Field Values
-
ISNETBIDIB
public static final java.lang.String ISNETBIDIB
- See Also:
- Constant Field Values
-
USELOCALPING
public static final java.lang.String USELOCALPING
- See Also:
- Constant Field Values
-
nodes
protected final java.util.TreeMap<java.lang.Long,org.bidib.jbidibc.messages.Node> nodes
-
mSavedMode
protected volatile org.bidib.jbidibc.messages.enums.CommandStationState mSavedMode
-
debugSavedNode
org.bidib.jbidibc.messages.Node debugSavedNode
-
mMemo
BiDiBSystemConnectionMemo mMemo
Reference to the system connection memo.
-
-
Constructor Detail
-
BiDiBTrafficController
public BiDiBTrafficController(org.bidib.jbidibc.core.BidibInterface b)
Create a new BiDiBTrafficController instance. Must provide a BidibInterface reference at creation time.- Parameters:
b- reference to associated jbidibc object, preserved for later.
-
-
Method Detail
-
connnectPort
public org.bidib.jbidibc.messages.helpers.Context connnectPort(PortAdapter p)
Opens the BiDiB connection in the jbidibc library, add listeners and initialize BiDiB.- Parameters:
p- BiDiB port adapter (serial or simulation)- Returns:
- a jbidibc context
-
isConnectionReady
public boolean isConnectionReady()
Check if the connection is ready to communicate. For netBiDiB this is the case only if the pairing was successful and the server has logged in. For all other connections the connection is ready if it has been successfully opened.- Returns:
- true if the connection is ready
-
isNetBiDiB
public boolean isNetBiDiB()
Check of the connection is netBiDiB.- Returns:
- true if this connection is netBiDiB
-
connectionLost
public void connectionLost()
Set the connection to lost state. All nodes will be removed and the components will be invalidated
-
isDetached
public boolean isDetached()
Check if the connection is detached i.e. it is opened, paired but the logon has been rejected.- Returns:
- true if detached
-
setLogon
public void setLogon(boolean logon)
Set or remove the detached state. If logoff is requested (detach), a logon reject is sent to the device and all nodes will be notified that they are no more reachable (lost node). The connection remains active, but other clients may request a logon from the connected device. If a logon is requested (attach), the connected device is asked for a new logon. When the logon is received, the nodes will be re-initialized by the traffic controller.- Parameters:
logon- - true for logon (attach), false for logoff (detach)
-
addConnectionChangedListener
public void addConnectionChangedListener(java.awt.event.ActionListener l)
Add/Remove an ActionListener to be called when the connection has changed.- Parameters:
l- - an Object implementing the ActionListener interface
-
removeConnectionChangedListener
public void removeConnectionChangedListener(java.awt.event.ActionListener l)
-
startLocalPing
public void startLocalPing()
Start jbidibc built-in local ping as a watchdog of the connection. If there is no response from the device, the connection will be closed.
-
TEST
public void TEST(boolean a)
-
getBidib
public org.bidib.jbidibc.core.BidibInterface getBidib()
Get Bidib Interface- Returns:
- Bidib Interface
-
getNodeList
public java.util.Map<java.lang.Long,org.bidib.jbidibc.messages.Node> getNodeList()
Get the list of nodes found- Returns:
- list of nodes
-
getNodeByUniqueID
public org.bidib.jbidibc.messages.Node getNodeByUniqueID(long uniqueId)
Get node by unique id from nodelist- Parameters:
uniqueId- search for this- Returns:
- node
-
getNodeByAddr
public org.bidib.jbidibc.messages.Node getNodeByAddr(byte[] addr)
Get node by node address from nodelist- Parameters:
addr- input to search- Returns:
- node
-
getNodeByUserName
public org.bidib.jbidibc.messages.Node getNodeByUserName(java.lang.String userName)
Get node by node username from nodelist- Parameters:
userName- input to search- Returns:
- node
-
getRootNode
public org.bidib.jbidibc.messages.Node getRootNode()
Get root node from nodelist- Returns:
- node
-
isGlobalProgrammerNode
public boolean isGlobalProgrammerNode(org.bidib.jbidibc.messages.Node node)
A node suitable as a global programmer must be - a command station, - must support service mode programming and - must be a booster. - for other nodes than the global command station the local DCC generator must be switched on (MSG_BOOST_STAT returns this as "control")- Parameters:
node- to check- Returns:
- true if the node is suitable as a global progreammer
-
getFirstGlobalProgrammerNode
public org.bidib.jbidibc.messages.Node getFirstGlobalProgrammerNode()
Get the most probably only global programmer node (aka service mode node, used for prgramming track - PT, not for POM) If there are more than one suitable node, get the last one (reverse nodes list search). In this case the command station (probably the root node and first entry in the list) should probably not be used as a global programmer and the other have been added just for that purpose. TODO: the user should select the global programmer node if there multiple nodes suitable as a global programmer.- Returns:
- programmer node or null if none available
-
setCurrentGlobalProgrammerNode
public boolean setCurrentGlobalProgrammerNode(org.bidib.jbidibc.messages.Node node)
Set the global programmer node to use.- Parameters:
node- to be used as global programmer node or null to remove the currentGlobalProgrammerNode- Returns:
- true if node is a suitable global programmer node, currentGlobalProgrammerNode is set to that node. false if it is not.
-
getCurrentGlobalProgrammerNode
public org.bidib.jbidibc.messages.Node getCurrentGlobalProgrammerNode()
Get the cached global programmer node. If there is no, try to find a suitable node. Note that the global programmer node may dynamically change by user settings. Be sure to update or invalidate currentGlobalProgrammerNode.- Returns:
- the current global programmer node or null if none available.
-
getFirstCommandStationNode
public org.bidib.jbidibc.messages.Node getFirstCommandStationNode()
Get the first and most probably only command station node (also used for Programming on the Main - POM) A cached value is returned here for performance reasons since the function is called very often. We don't expect the command station to change.- Returns:
- command station node
-
getFirstBoosterNode
public org.bidib.jbidibc.messages.Node getFirstBoosterNode()
Get the first booster node. There may be more booster nodes, so prefer the command station and then try the others- Returns:
- booster node
-
getFirstOutputNode
public org.bidib.jbidibc.messages.Node getFirstOutputNode()
Get the first output node - a node that can control LC ports. TODO: the method does not make much sense and its only purpose is to check if we have an output node at all. Therefor it should be converted to "hasOutputNode" or similar.- Returns:
- output node
-
hasAccessoryNode
public boolean hasAccessoryNode()
Check if we have at least one node capable of Accessory functions- Returns:
- true or false
-
findNodeFeature
public org.bidib.jbidibc.messages.Feature findNodeFeature(org.bidib.jbidibc.messages.Node node, int requestedFeatureId)
Find a feature for given node.- Parameters:
node- selected noderequestedFeatureId- as integer- Returns:
- a Feature object or null if the node does not have the feature at all
-
getNodeFeature
public int getNodeFeature(org.bidib.jbidibc.messages.Node node, int requestedFeatureId)
Get the feature value of a node.- Parameters:
node- selected noderequestedFeatureId- feature to get- Returns:
- the feature value as integer or 0 if the node does not have the feature
-
getPortModel
public org.bidib.jbidibc.messages.enums.PortModelEnum getPortModel(org.bidib.jbidibc.messages.Node node)
-
addMessageListener
public void addMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)
Add a message Listener to the connection- Parameters:
messageListener- to be added
-
removeMessageListener
public void removeMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)
Remove a message Listener from the connection- Parameters:
messageListener- to be removed
-
addRawMessageListener
public void addRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)
Add a raw message Listener to the connection- Parameters:
rawMessageListener- to be added
-
removeRawMessageListener
public void removeRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)
Remove a raw message Listener from the connection- Parameters:
rawMessageListener- to be removed
-
allPortConfigX
public void allPortConfigX()
Request CONFIGX from all ports on all nodes of this connection. Returns after all data has been received. Received data is delivered to registered Message Listeners.
-
getAllPortConfigX
public java.util.List<org.bidib.jbidibc.messages.LcConfigX> getAllPortConfigX(org.bidib.jbidibc.messages.Node node, org.bidib.jbidibc.messages.enums.LcOutputType type)
Request CONFIGX from all ports on a given node and possibly only for a given type.- Parameters:
node- requested nodetype- - if null, request all port types- Returns:
- Note: always returns null, since data is not collected synchroneously but delivered to registered Message Listeners.
-
getPortConfigX
public org.bidib.jbidibc.messages.LcConfigX getPortConfigX(org.bidib.jbidibc.messages.Node node, int portAddr, org.bidib.jbidibc.messages.enums.LcOutputType type)
Request CONFIGX if a given port on a given node and possibly only for a given type.- Parameters:
node- requested nodeportAddr- as an integertype- - if null, request all port types- Returns:
- Note: always returns null, since data is not collected synchroneously but delivered to registered Message Listeners.
-
convertConfig2ConfigX
public org.bidib.jbidibc.messages.LcConfigX convertConfig2ConfigX(org.bidib.jbidibc.messages.Node node, org.bidib.jbidibc.messages.LcConfig lcConfig)
Convert a CONFIG object to a CONFIGX object. This is a convenience method so the JMRI components need only to handle the CONFIGX format- Parameters:
node- context nodelcConfig- the LcConfig object- Returns:
- a new LcConfigX object
-
allPortLcStat
public void allPortLcStat()
Request LC_STAT from all ports on all nodes of this connection. Returns immediately. Received data is delivered to registered Message Listeners.
-
portLcStat
public void portLcStat(org.bidib.jbidibc.messages.Node node, int typemask)
Request LC_STAT from all ports on a given node. Returns immediately. Received data is delivered to registered Message Listeners. The differences for the addressing model an the old LC_STAT handling are hidden to the caller.- Parameters:
node- selected nodetypemask- a 16 bit type mask where each bit represents a type, Bit0 is SWITCHPORT, Bit1 is LIGHTPORT and so on. Bit 15 is INPUTPORT. Return LC_STAT only for ports which are selected on the type mask (the correspondend bit is set).
-
allAccessoryState
public void allAccessoryState()
-
accessoryState
public void accessoryState(org.bidib.jbidibc.messages.Node node)
-
allFeedback
public void allFeedback()
Request Feedback Status (called BM status in BiDiB - BM (Belegtmelder) is german for "feedback") from all ports on all nodes of this connection. Returns immediately. Received data is delivered to registered Message Listeners.
-
feedback
public void feedback(org.bidib.jbidibc.messages.Node node)
Request Feedback Status (called BM status in BiDiB - BM (Belegtmelder) is german for "feedback") from all ports on a given node. Returns immediately. Received data is delivered to registered Message Listeners.- Parameters:
node- selected node
-
sendBiDiBMessage
public void sendBiDiBMessage(org.bidib.jbidibc.messages.message.BidibCommandMessage m, org.bidib.jbidibc.messages.Node node)
Forward a preformatted BiDiBMessage to the actual interface.- Parameters:
m- Message to send;node- BiDiB node to send the message to
-
checkProgMode
public int checkProgMode(boolean needProgMode, org.bidib.jbidibc.messages.Node node)
Check if the command station is in the requested state (Normal, PT) If the command station is not in the requested state, a message is sent to BiDiB to switch to the requested state.- Parameters:
needProgMode- true if we request the command station to be in programming state, false if normal state is requestednode- selected node- Returns:
- 0 if nothing to do, 1 if state has been changed, -1 on error
-
setWatchdogTimer
public final void setWatchdogTimer(boolean state)
-
getSystemConnectionMemo
public BiDiBSystemConnectionMemo getSystemConnectionMemo()
Get access to the system connection memo associated with this traffic controller.- Returns:
- associated systemConnectionMemo object
-
setSystemConnectionMemo
public void setSystemConnectionMemo(BiDiBSystemConnectionMemo m)
Set the system connection memo associated with this traffic controller.- Parameters:
m- associated systemConnectionMemo object
-
getSystemPrefix
public java.lang.String getSystemPrefix()
- Specified by:
getSystemPrefixin interfaceCommandStation
-
getUserName
public java.lang.String getUserName()
- Specified by:
getUserNamein interfaceCommandStation
-
sendPacket
public boolean sendPacket(byte[] packet, int repeats)
Send a specific packet to the rails. Not supported! We probably don't need the command station interface at all... ... besides perhaps consist control or DCC Signal Mast / Head ??- Specified by:
sendPacketin interfaceCommandStation- Parameters:
packet- Byte array representing the packet, including the error-correction byte.repeats- Number of times to repeat the transmission.- Returns:
trueif the operation succeeds,falseotherwise.
-
terminate
protected void terminate()
-
-