Skip to content

Commit 8347231

Browse files
authored
Add Reconnection Attempts and Logging for Wi-Fi Checker (Issue #347) (#376)
* Add reconnection attempts for Wi-Fi * Delete Auto WiFi Check/wifi_status_log.txt * Added Wi-Fi reconnection attempts with logging * Create wifi_status_log.txt * Auto creation of log txt file
1 parent 4f12088 commit 8347231

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

Auto WiFi Check/wifi_checker.py

+72-17
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,99 @@
22
import subprocess
33
import sys
44
import time
5-
from datetime import datetime
5+
import logging
66
import schedule as sc
77

8+
# Create the log file if it doesn't already exist
9+
LOG_FILE = "wifi_status_log.txt"
10+
PING_HOST = "www.google.com"
11+
12+
try:
13+
with open(LOG_FILE, 'x') as file:
14+
file.write("Logs:\n")
15+
print(f"File '{LOG_FILE}' created successfully.")
16+
except FileExistsError:
17+
print(f"File '{LOG_FILE}' already exists.")
18+
19+
# Set up logging to log to a file with timestamps
20+
logging.basicConfig(filename=LOG_FILE,
21+
level=logging.INFO,
22+
format='%(asctime)s - %(message)s',
23+
filemode='a') # Append mode
824

925
def enable():
10-
subprocess.call("netsh interface set interface Wi-Fi enabled")
11-
print("Turning On the laptop WiFi")
26+
try:
27+
subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True)
28+
print("Turning On the laptop WiFi")
29+
logging.info("WiFi enabled")
30+
except Exception as e:
31+
print(f"Failed to enable WiFi: {e}")
32+
logging.error(f"Failed to enable WiFi: {e}")
1233

1334
def disable():
14-
subprocess.call("netsh interface set interface Wi-Fi disabled")
15-
print("Turning Off the laptop WiFi")
16-
35+
try:
36+
subprocess.call("netsh interface set interface Wi-Fi disabled", shell=True)
37+
print("Turning Off the laptop WiFi")
38+
logging.info("WiFi disabled")
39+
except Exception as e:
40+
print(f"Failed to disable WiFi: {e}")
41+
logging.error(f"Failed to disable WiFi: {e}")
1742

18-
1943
def job():
20-
if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0:
44+
try:
45+
subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True)
2146
print("WiFi is enabled and connected to internet")
22-
hostname = "www.google.com"
23-
response = subprocess.call("ping -n 1 " + hostname)
47+
logging.info("WiFi is enabled and connected to the internet.")
48+
49+
response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True)
50+
2451
if response == 1:
2552
print("Your Connection is not working")
26-
disable()
27-
time.sleep(1)
28-
enable()
53+
logging.warning("WiFi connection not working, ping failed.")
54+
55+
attempt_counter = 0
56+
max_attempts = 3
57+
58+
while attempt_counter < max_attempts:
59+
print(f"Attempt {attempt_counter + 1} to reconnect...")
60+
logging.info(f"Attempt {attempt_counter + 1} to reconnect...")
61+
62+
disable()
63+
time.sleep(1)
64+
enable()
65+
66+
time.sleep(5)
67+
68+
response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True)
69+
if response == 0:
70+
print("Reconnection successful!")
71+
logging.info("Reconnection successful!")
72+
break
73+
else:
74+
print(f"Reconnection attempt {attempt_counter + 1} failed.")
75+
logging.warning(f"Reconnection attempt {attempt_counter + 1} failed.")
76+
77+
attempt_counter += 1
78+
79+
if attempt_counter == max_attempts and response != 0:
80+
print(f"Failed to reconnect after {max_attempts} attempts.")
81+
logging.error(f"Failed to reconnect after {max_attempts} attempts.")
82+
except Exception as e:
83+
print(f"Error during WiFi check: {e}")
84+
logging.error(f"Error during WiFi check: {e}")
2985

3086
def is_admin():
3187
try:
3288
return ctypes.windll.shell32.IsUserAnAdmin()
33-
except:
89+
except Exception as e:
90+
logging.error(f"Admin check failed: {e}")
3491
return False
3592

3693
if is_admin():
37-
# job()
3894
sc.every(50).seconds.do(job)
3995
else:
4096
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
4197

42-
4398
while True:
4499
sc.run_pending()
45-
time.sleep(1)
100+
time.sleep(1)

Auto WiFi Check/wifi_status_log.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)