Node
The Node is the building block of an OpenLCB/LCC network. Each Node can communicate with any other Node on the network by sending events or datagrams over the common bus. Each Node object can be attached to an Interface (or multiple) to allow for complex network architectures. Each Message should originate from one Node.
- class pyolcb.Node(address: Address, interfaces: Interface | list[Interface], snip: SimpleNodeInfo = None, protocols: Protocol = None, allocate_alias: bool = True)
Implementation of an OpenLCB/LCC
Node.On an interface that is connected, the node claims an alias (Check ID, 200 ms listen, Reserve ID, Alias Map Definition) and announces itself with Initialization Complete before the constructor returns. On a GridConnect interface that connects later, or reconnects, it claims a fresh alias each time the link comes up.
Once permitted, the node answers Verify Node ID, Alias Mapping Enquiry, Protocol Support Inquiry, Simple Node Information requests (when
snipis given) and Identify Events, and rejects addressed requests it does not implement. It also acts as a client:simple_node_info(),protocol_support_inquiry()anddatagram_exchange()send a request and wait for the answer, andpyolcb.memory_config.MemoryConfigurationbuilds on them.- Parameters:
address (Address) – The address (full and optionally a preferred alias) to be associated with the
Node.interfaces (Interface | list[Interface]) – An
Interfaceor list thereof to connect theNodeto.snip (SimpleNodeInfo, optional) – Identity strings to answer SNIP requests with.
protocols (protocols.Protocol, optional) – Protocols to report in Protocol Support Reply.
allocate_alias (bool) – Claim the alias with the standard handshake (default).
Falsekeeps the old behaviour of announcing with a preset alias immediately.
- add_consumed_event(event_id: int, state: EventState = EventState.UNKNOWN)
Declare an event this node consumes, so Identify Events and Identify Consumer are answered.
- add_consumer(event: Event | int, function: callable)
Register a function to be run on receipt of a specific
Event.- Parameters:
event (int | Event) – The ID or
Eventto consume. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.function (callable) – The function to be called upon receipt of the specified
Event. Must be able to take no parameters.
- add_frame_listener(function: callable)
Call
function(frame, outgoing)for every frame received (outgoingFalse) and every frame this node sends (True). Useful for traffic monitors.
- add_produced_event(event_id: int, state: EventState = EventState.UNKNOWN)
Declare an event this node produces, so Identify Events and Identify Producer are answered.
- add_state_listener(function: callable)
Call
function(True)each time the node becomes permitted (after every successful alias claim) andfunction(False)when every interface has lost its connection.
- addressed_request(destination: int, mti: int, payload: bytes, reply_mti: int, timeout: float = 2.0) bytes
Send an addressed message to
destination(an alias) and return the reassembled payload of thereply_mtimessage it answers with.Raises
InteractionRejectedif the node rejects the request andReplyTimeoutif it does not answer.
- alias_of(node_id: int) int | None
The alias last seen for
node_id, orNone.
- claim_alias(attempts: int = 16) int
Claim an alias with the CID/RID/AMD handshake and announce the node. Blocks for at least 200 ms. Returns the alias.
- consume(event: Event | int)
Run the consumer for a specific
Event.- Parameters:
event (int | Event) – The ID or
Eventto consume. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.- Returns:
Returns what the registered consumer function returns.
- Return type:
any
- datagram_exchange(destination: Address | int, data: bytes, timeout: float = 3.0, expect_reply: bool = True, reply_filter: callable = None, retries: int = 3) bytes | None
Send a datagram and wait for it to be acknowledged, then (with
expect_reply) for the reply datagram, which is acknowledged with Datagram Received OK.- Parameters:
destination (Address | int) – The node, as an
Addressor 48-bit node ID.data (bytes) – The datagram content, 1 to 72 bytes.
timeout (float) – Seconds to wait for each answer. A Datagram Received OK that announces a longer reply time extends it.
expect_reply (bool) – Wait for a reply datagram after the acknowledgement.
reply_filter (callable, optional) –
reply_filter(data) -> boolpicks the reply among datagrams from the destination.retries (int) – How often to resend after a temporary rejection.
- Returns:
The reply datagram,
b""when no reply was wanted, orNoneif the node acknowledged without announcing a reply.- Return type:
bytes | None
- get_alias() int
Get the
Node’s alias.- Returns:
Returns the
Node’s alias as anint.- Return type:
int
- get_consumer(event: Event | int)
Get the consumer for a specific
Event.- Parameters:
event (int | Event) – The ID or
Eventto consume. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.- Returns:
Returns the registered consumer function.
- Return type:
callable
- identify_consumer(event_id: int)
Ask the consumers of
event_idto identify themselves and their state.
- identify_events(destination: int = None)
Ask one node (by alias) or, with no destination, every node to identify its events.
- identify_producer(event_id: int)
Ask the producers of
event_idto identify themselves and their state.
- node_id_of(alias: int) int | None
The node ID last seen for
alias, orNone.
- property permitted: bool
True once the node has claimed an alias and may send messages.
- process_message(message)
Handle one received frame (a
Frameorcan.Message). Interfaces call this for every frame they receive.
- produce(event: int | Event)
Produce an
Eventand send the resulting message on all interfaces.- Parameters:
event (int | Event) – The ID or
Eventto produce and send. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.
- produce_event(event_id: int)
Send a Producer/Consumer Event Report for the full 64-bit
event_id.
- protocol_support_inquiry(destination: Address | int, timeout: float = 2.0) Protocol
Ask a node which protocols it supports.
- protocol_support_reply(destination: int)
Send this node’s Protocol Support Reply to
destination(an alias).
- remove_consumer(event: Event | int)
Deregister the function to be run on receipt of a specific
Event.- Parameters:
event (int | Event) – The ID or
Eventto consume. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.
- replace_consumer(event: Event | int, function: callable)
Replace a function that is run on receipt of a specific
Event.- Parameters:
event (int | Event) – The ID or
Eventto consume. By default, if anintis provided for this parameter, and the (unsigned) value fits within two bytes, theEventwill be tagged with the address of theNode. This behavior can be overridden by passing anEventobject with no source address.function (callable) – The function to be called upon receipt of the specified
Event. Must be able to take no parameters.
- resolve_alias(node: Address | int, timeout: float = 1.0) int
Find the alias of a node given as an
Addressor a 48-bit node ID, asking the network with an Alias Mapping Enquiry if it is not yet known.
- send(messages: Message | list[Message])
Send a
Message(or sequence thereof) from thisNodeon all registered interfaces.
- send_addressed(mti: int, destination: int, payload: bytes = b'')
Send an addressed message to
destination(an alias), split into frames as needed.
- send_datagram(destination: int, data: bytes)
Send a datagram to
destination(an alias) without waiting for the acknowledgement.
- send_global(mti: int, payload: bytes = b'')
Send a global message (12-bit
mti) with up to 8 bytes of payload.
- set_datagram_handler(datagram_handler: callable)
Register a function to be run on receipt of a
Datagram.
- set_unknown_message_processor(function: callable)
Register a function to be run on receipt of a message of unknown type.
- Parameters:
function (callable) – The function to be called upon receipt of an unknown message. Must take a
Messageas the first parameter.
- simple_node_info(destination: Address | int, timeout: float = 3.0) SimpleNodeInfo
Ask a node for its Simple Node Information (manufacturer, model, versions, user name).
- verify_node_id(address: Address | int = None)
Send a request to verify aliases on an OpenLCB/LCC network.
- wait_permitted(timeout: float = None) bool
Block until the node holds an alias. Returns
Falseon timeout.