Skip to content

Commit

Permalink
feat(commission): Implement power control
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKevinWeiss committed Feb 20, 2024
1 parent 22140af commit c454da9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 27 deletions.
81 changes: 57 additions & 24 deletions src/inet_nm/cli_commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import inet_nm.commissioner as cmr
import inet_nm.config as cfg
from inet_nm.data_types import NmNode
from inet_nm.power_control import DEFAULT_MAX_ALLOWED_NODES, PowerControl
from inet_nm.usb_ctrl import TtyNotPresent, get_devices_from_tty


Expand Down Expand Up @@ -36,33 +38,64 @@ def _main():
bi_cfg.check_file(writable=False)

saved_nodes = nodes_cfg.load()
nm_nodes = (
get_devices_from_tty() if args.no_cache else get_devices_from_tty(saved_nodes)
)
print(f"Found {len(saved_nodes)} saved nodes in {args.config}")
if args.mock_dev:
nm_nodes.append(cmr.mock_device())
nm_nodes = []
with PowerControl(
locations=cfg.LocationConfig(args.config).load(),
nodes=saved_nodes,
max_powered_devices=DEFAULT_MAX_ALLOWED_NODES,
) as pc:
while not pc.power_on_complete:
pc.power_on_chunk()
nm_nodes.extend(
get_devices_from_tty()
if args.no_cache
else get_devices_from_tty(saved_nodes)
)
pc.power_off_unused()

try:
selected_node = cmr.select_available_node(nm_nodes)
except ValueError:
print("No available nodes found")
sys.exit(1)
except TtyNotPresent as exc:
# filter out duplicate nodes
nm_node: NmNode
found_ids = set()
filtered_nodes = []
for nm_node in nm_nodes:
if nm_node.uid in found_ids:
continue
found_ids.add(nm_node.uid)
filtered_nodes.append(nm_node)
nm_nodes = filtered_nodes

print(f"Found {len(saved_nodes)} saved nodes in {args.config}")
if args.mock_dev:
selected_node = nm_nodes[-1]
nm_nodes.append(cmr.mock_device())

try:
selected_node = cmr.select_available_node(nm_nodes)
except ValueError:
print("No available nodes found")
sys.exit(1)
except TtyNotPresent as exc:
if args.mock_dev:
selected_node = nm_nodes[-1]
else:
raise exc
if args.ignore:
selected_node.ignore = True
else:
raise exc
if args.ignore:
selected_node.ignore = True
else:
binfo = bi_cfg.load()
selected_node.board = args.board or cmr.select_board(
list(binfo.keys()), selected_node
)
if selected_node.board in binfo:
selected_node.features_provided = binfo[selected_node.board]
cmr.check_and_set_uninitialized_sn(selected_node)
binfo = bi_cfg.load()
selected_node.board = args.board or cmr.select_board(
list(binfo.keys()), selected_node
)
if selected_node.board in binfo:
selected_node.features_provided = binfo[selected_node.board]
if cmr.is_uninitialized_sn(selected_node):
uid = selected_node.uid
# Normally we would not need to power this as it would only get powered
# down if it was already in the nodes list... If the no-cache is used
# then it may be powered down and we need to power it up.
# Slow but safe.
pc.power_on_uid(uid)
cmr.set_uninitialized_sn(selected_node)
pc.power_off_uid(uid)
nodes = cmr.add_node_to_nodes(saved_nodes, selected_node)
nodes_cfg.save(nodes)
print(f"Updated {nodes_cfg.file_path}")
Expand Down
22 changes: 19 additions & 3 deletions src/inet_nm/commissioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,36 @@
from inet_nm.usb_ctrl import get_ttys_from_nm_node


def check_and_set_uninitialized_sn(node: NmNode, sns: List = None):
def is_uninitialized_sn(node: NmNode, sns: List = None):
"""
Check if a given NmNode has an uninitialized serial number and prompt the user
to set it.
Args:
node: An NmNode object.
sns: List of serial numbers to check against.
"""
"""
# We cannot do anything with the sns if we don't have the cp210x module
if cp210x is None:
return
return False
sns = sns or ["0001"]

if node.serial not in sns:
return False
return True


def set_uninitialized_sn(node: NmNode):
"""
Set the serial number of a given NmNode.
Args:
node: An NmNode object.
"""

if cp210x is None:
return

pid_vid_sn = {
Expand Down

0 comments on commit c454da9

Please sign in to comment.