Skip to content

Commit

Permalink
Add Reconnection Attempts and Logging for Wi-Fi Checker (Issue #347) (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Badis213 authored Nov 27, 2024
1 parent 4f12088 commit 8347231
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
89 changes: 72 additions & 17 deletions Auto WiFi Check/wifi_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,99 @@
import subprocess
import sys
import time
from datetime import datetime
import logging
import schedule as sc

# Create the log file if it doesn't already exist
LOG_FILE = "wifi_status_log.txt"
PING_HOST = "www.google.com"

try:
with open(LOG_FILE, 'x') as file:
file.write("Logs:\n")
print(f"File '{LOG_FILE}' created successfully.")
except FileExistsError:
print(f"File '{LOG_FILE}' already exists.")

# Set up logging to log to a file with timestamps
logging.basicConfig(filename=LOG_FILE,
level=logging.INFO,
format='%(asctime)s - %(message)s',
filemode='a') # Append mode

def enable():
subprocess.call("netsh interface set interface Wi-Fi enabled")
print("Turning On the laptop WiFi")
try:
subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True)
print("Turning On the laptop WiFi")
logging.info("WiFi enabled")
except Exception as e:
print(f"Failed to enable WiFi: {e}")
logging.error(f"Failed to enable WiFi: {e}")

def disable():
subprocess.call("netsh interface set interface Wi-Fi disabled")
print("Turning Off the laptop WiFi")

try:
subprocess.call("netsh interface set interface Wi-Fi disabled", shell=True)
print("Turning Off the laptop WiFi")
logging.info("WiFi disabled")
except Exception as e:
print(f"Failed to disable WiFi: {e}")
logging.error(f"Failed to disable WiFi: {e}")


def job():
if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0:
try:
subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True)
print("WiFi is enabled and connected to internet")
hostname = "www.google.com"
response = subprocess.call("ping -n 1 " + hostname)
logging.info("WiFi is enabled and connected to the internet.")

response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True)

if response == 1:
print("Your Connection is not working")
disable()
time.sleep(1)
enable()
logging.warning("WiFi connection not working, ping failed.")

attempt_counter = 0
max_attempts = 3

while attempt_counter < max_attempts:
print(f"Attempt {attempt_counter + 1} to reconnect...")
logging.info(f"Attempt {attempt_counter + 1} to reconnect...")

disable()
time.sleep(1)
enable()

time.sleep(5)

response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True)
if response == 0:
print("Reconnection successful!")
logging.info("Reconnection successful!")
break
else:
print(f"Reconnection attempt {attempt_counter + 1} failed.")
logging.warning(f"Reconnection attempt {attempt_counter + 1} failed.")

attempt_counter += 1

if attempt_counter == max_attempts and response != 0:
print(f"Failed to reconnect after {max_attempts} attempts.")
logging.error(f"Failed to reconnect after {max_attempts} attempts.")
except Exception as e:
print(f"Error during WiFi check: {e}")
logging.error(f"Error during WiFi check: {e}")

def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
except Exception as e:
logging.error(f"Admin check failed: {e}")
return False

if is_admin():
# job()
sc.every(50).seconds.do(job)
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)


while True:
sc.run_pending()
time.sleep(1)
time.sleep(1)
1 change: 1 addition & 0 deletions Auto WiFi Check/wifi_status_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 8347231

Please sign in to comment.