Skip to content

Commit

Permalink
feat(commission): Allow multiple tty boards
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKevinWeiss committed Aug 22, 2023
1 parent 585a0b9 commit aa5c889
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
35 changes: 31 additions & 4 deletions src/inet_nm/commissioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ def get_tty_from_nm_node(nm_node: NmNode) -> str:
"""
Retrieve the TTY device node string for a given NmNode.
Args:
nm_node: An NmNode object.
Returns:
The TTY device node string.
Raises:
TtyNotPresent: If a TTY device could not be found for the given NmNode.
"""
ttys = get_ttys_from_nm_node(nm_node)
if ttys:
return ttys[0]

raise TtyNotPresent(f"Could not find tty device for {nm_node}")


def get_ttys_from_nm_node(nm_node: NmNode) -> List[str]:
"""
Retrieve the TTY device node string for a given NmNode.
Args:
nm_node: An NmNode object.
Expand All @@ -65,6 +85,8 @@ def get_tty_from_nm_node(nm_node: NmNode) -> str:
model_id = nm_node.product_id
serial_short = nm_node.serial

ttys = []

for device in context.list_devices(subsystem="tty"):
parent = device.find_parent("usb", "usb_device")
if parent is None:
Expand All @@ -74,9 +96,8 @@ def get_tty_from_nm_node(nm_node: NmNode) -> str:
and parent.get("ID_MODEL_ID") == model_id
and parent.get("ID_SERIAL_SHORT") == serial_short
):
return device.device_node

raise TtyNotPresent(f"Could not find tty device for {nm_node}")
ttys.append(device.device_node)
return ttys


def select_available_node(nodes: List[NmNode]) -> NmNode:
Expand All @@ -90,11 +111,17 @@ def select_available_node(nodes: List[NmNode]) -> NmNode:
The selected NmNode.
"""

uids = list()

def _nice_node(node: NmNode):
msg = f"{get_tty_from_nm_node(node)} {node.vendor}"
nonlocal uids
ttys = get_ttys_from_nm_node(node)
tty = ttys[uids.count(node.uid)]
msg = f"{tty} {node.vendor}"
if node.model:
msg += f" {node.model}"
msg += f" {node.serial}"
uids.append(node.uid)
return msg

return nm_prompt_choice("Select the node", nodes, _nice_node)
Expand Down
13 changes: 9 additions & 4 deletions src/inet_nm/runner_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import inet_nm.locking as lk
from inet_nm._helpers import nm_print
from inet_nm.commissioner import TtyNotPresent, get_tty_from_nm_node
from inet_nm.commissioner import get_ttys_from_nm_node
from inet_nm.data_types import EnvConfigFormat, NmNode, NodeEnv
from inet_nm.filelock import FileLock

Expand Down Expand Up @@ -124,9 +124,10 @@ def run(self):

self.threads = []
for idx, node in enumerate(self.nodes):
try:
nm_port = get_tty_from_nm_node(node)
except TtyNotPresent:
ttys = get_ttys_from_nm_node(node)
if ttys:
nm_port = ttys[0]
else:
nm_port = "Unknown"
node_env = NodeEnv(
NM_IDX=idx,
Expand All @@ -135,6 +136,10 @@ def run(self):
NM_BOARD=node.board,
NM_PORT=nm_port,
).to_dict()

# Inject multiple ttys values if available
for i, tty in enumerate(ttys):
node_env[f"NM_PORT_{i}"] = tty
node_env.update(self.extra_env.shared)
node_env.update(self.extra_env.nodes.get(node.uid, {}))

Expand Down

0 comments on commit aa5c889

Please sign in to comment.