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 (package private) org.bidib.jbidibc.messages.Node
debugSavedNode
(package private) BiDiBSystemConnectionMemo
mMemo
Reference to the system connection memo.protected org.bidib.jbidibc.messages.enums.CommandStationState
mSavedMode
protected java.util.TreeMap<java.lang.Long,org.bidib.jbidibc.messages.Node>
nodes
-
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 void
accessoryState(org.bidib.jbidibc.messages.Node node)
void
addMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)
Add a message Listener to the connectionvoid
addRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)
Add a raw message Listener to the connectionvoid
allAccessoryState()
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.void
allPortConfigX()
Request CONFIGX from all ports on all nodes of this connection.void
allPortLcStat()
Request LC_STAT from all ports on all nodes of this connection.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.org.bidib.jbidibc.messages.helpers.Context
connnectPort(PortAdapter p)
Opens the BiDiB connection in the jbidibc library, add listeners and initialize BiDiB.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.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.org.bidib.jbidibc.messages.Feature
findNodeFeature(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.BidibInterface
getBidib()
Get Bidib Interfaceorg.bidib.jbidibc.messages.Node
getCurrentGlobalProgrammerNode()
Get the cached global programmer node.org.bidib.jbidibc.messages.Node
getFirstBoosterNode()
Get the first booster node.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.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).org.bidib.jbidibc.messages.Node
getFirstOutputNode()
Get the first output node - a node that can control LC ports.org.bidib.jbidibc.messages.Node
getNodeByAddr(byte[] addr)
Get node by node address from nodelistorg.bidib.jbidibc.messages.Node
getNodeByUniqueID(long uniqueId)
Get node by unique id from nodelistorg.bidib.jbidibc.messages.Node
getNodeByUserName(java.lang.String userName)
Get node by node username from nodelistint
getNodeFeature(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.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.org.bidib.jbidibc.messages.enums.PortModelEnum
getPortModel(org.bidib.jbidibc.messages.Node node)
org.bidib.jbidibc.messages.Node
getRootNode()
Get root node from nodelistBiDiBSystemConnectionMemo
getSystemConnectionMemo()
Get access to the system connection memo associated with this traffic controller.java.lang.String
getSystemPrefix()
java.lang.String
getUserName()
boolean
hasAccessoryNode()
Check if we have at least one node capable of Accessory functionsboolean
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 boostervoid
portLcStat(org.bidib.jbidibc.messages.Node node, int typemask)
Request LC_STAT from all ports on a given node.void
removeMessageListener(org.bidib.jbidibc.core.MessageListener messageListener)
Remove a message Listener from the connectionvoid
removeRawMessageListener(org.bidib.jbidibc.messages.base.RawMessageListener rawMessageListener)
Remove a raw message Listener from the connectionvoid
sendBiDiBMessage(org.bidib.jbidibc.messages.message.BidibCommandMessage m, org.bidib.jbidibc.messages.Node node)
Forward a preformatted BiDiBMessage to the actual interface.boolean
sendPacket(byte[] packet, int repeats)
Send a specific packet to the rails.boolean
setCurrentGlobalProgrammerNode(org.bidib.jbidibc.messages.Node node)
Set the global programmer node to use.void
setSystemConnectionMemo(BiDiBSystemConnectionMemo m)
Set the system connection memo associated with this traffic controller.void
setWatchdogTimer(boolean state)
protected void
terminate()
void
TEST(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
-
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
-
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:
getSystemPrefix
in interfaceCommandStation
-
getUserName
public java.lang.String getUserName()
- Specified by:
getUserName
in 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:
sendPacket
in interfaceCommandStation
- Parameters:
packet
- Byte array representing the packet, including the error-correction byte.repeats
- Number of times to repeat the transmission.- Returns:
true
if the operation succeeds,false
otherwise.
-
terminate
protected void terminate()
-
-