Skip to content

Commit

Permalink
Refactor connect
Browse files Browse the repository at this point in the history
  • Loading branch information
jcapona committed Sep 9, 2024
1 parent c1b1b34 commit b13ad5d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pi_top_usb_setup/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,18 @@ def name(self) -> str:
# A connection file is created in /etc/NetworkManager/system-connections/ with the name of the connection
return "PT-USB-SETUP-" + self.ssid.replace(" ", "_").replace('"', "")

def connect(self):
cmds = []
if any(
def should_connect_with_raspi_config(self):
return any(
[
isinstance(self.authentication, WpaPersonal),
isinstance(self.authentication, OWE),
isinstance(self.authentication, Open),
]
):
)

def connect(self):
cmds = []
if self.should_connect_with_raspi_config():
# On simple networks, we'll connect using raspi-config
# https://www.raspberrypi.com/documentation/computers/configuration.html#system-options36
plain = 0
Expand All @@ -463,7 +466,7 @@ def connect(self):
)
elif get_linux_distro() == "bookworm":
if self.exists():
# delete existing connection
# disconnect and delete existing connection
cmds.append(f'nmcli connection down "{self.name}"')
cmds.append(f'nmcli connection delete "{self.name}"')

Expand All @@ -480,7 +483,10 @@ def connect(self):
logger.info("--> Connecting to network")
for cmd in cmds:
logger.info(f"--> Executing: {cmd}")
run_command(cmd, timeout=30, check=True)
try:
run_command(cmd, timeout=30, check=True)
except Exception as e:
logger.error(f"Error connecting: {e}")

def exists(self):
if get_linux_distro() == "bookworm":
Expand Down

0 comments on commit b13ad5d

Please sign in to comment.