Skip to content

Commit 8f0ad74

Browse files
committed
Checking WiFi status in Linux
1 parent 394be9d commit 8f0ad74

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

ezshare_resmed.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def connect_to_wifi_linux(self):
257257
capture_output=True, text=True,
258258
check=True)
259259
except subprocess.CalledProcessError as e:
260+
self.connection_id = self.ssid
260261
raise RuntimeError(f'Error connecting to {self.ssid}. Return code: {e.returncode}, error: {e.stderr}') from e
261262

262263
# Regular expression pattern to match the string after "activated with"
@@ -311,9 +312,15 @@ def connect_to_wifi_windows(self):
311312
self.connection_id = connection_id
312313

313314
def wifi_connected(self):
315+
"""
316+
Checks if WiFi is connected
317+
318+
Returns:
319+
bool: True if connected to specified WiFi network, False if not
320+
"""
314321
if self.connected:
315322
return True
316-
elif self.platform_system == 'Windows':
323+
if self.platform_system == 'Windows':
317324
existing_profile_cmd = 'netsh wlan show interfaces'
318325
try:
319326
current_profile_result = subprocess.run(existing_profile_cmd,
@@ -326,10 +333,10 @@ def wifi_connected(self):
326333
if line.strip().startswith('Profile'):
327334
if line.split(':')[1].strip() == self.connection_id:
328335
self.connected = True
329-
return self.connected
336+
return True
330337
else:
331338
return False
332-
339+
333340
def has_network_manager(self):
334341
"""
335342
Checks if nmcli is present on the system and if it can manage the WiFi network
@@ -395,10 +402,10 @@ def run(self):
395402
self.connect_to_wifi()
396403
except RuntimeError as e:
397404
logger.warning('Failed to connect to %s. Error: %s', self.ssid, str(e))
398-
self.print('Waiting a few seconds for connection to establish...')
399-
time.sleep(self.connection_delay)
405+
else:
406+
self.print('Waiting a few seconds for connection to establish...')
407+
time.sleep(self.connection_delay)
400408

401-
402409
if not self.wifi_connected():
403410
if sys.__stdin__.isatty():
404411
response = input('Unable to connect automatically, please connect manually and press "C" to continue or any other key to cancel: ')
@@ -628,12 +635,14 @@ def disconnect_from_wifi(self):
628635
shell=True, check=True)
629636

630637
elif self.platform_system == 'Linux' and self.interface_name is not None:
631-
if self.connection_id:
638+
if self.connected:
632639
self.print(f'Disconnecting from {self.ssid}')
633640
disconnect_cmd = f'nmcli connection down {self.connection_id}'
634641
subprocess.run(disconnect_cmd, shell=True,
635642
capture_output=True, text=True, check=True)
636-
delete_cmd = f'nmcli connection delete {self.connection_id}'
643+
if self.connection_id:
644+
self.print(f'Removing profile for {self.connection_id}...')
645+
delete_cmd = f'nmcli connection delete "{self.connection_id}"'
637646
subprocess.run(delete_cmd, shell=True,
638647
capture_output=True, text=True, check=True)
639648

@@ -790,7 +799,7 @@ def main():
790799

791800
try:
792801
ezshare.run()
793-
except Exception or SystemExit as e:
802+
except BaseException as e:
794803
raise e
795804
finally:
796805
ezshare.disconnect_from_wifi()

0 commit comments

Comments
 (0)