Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reconnection Attempts and Logging for Wi-Fi Checker (Issue #347) #376

Merged
merged 7 commits into from
Nov 27, 2024

Conversation

Badis213
Copy link
Contributor

PR Title

Add reconnection attempts for Wi-Fi with logging

Summary

This PR introduces a Wi-Fi reconnection mechanism with logging. It attempts to reconnect to Wi-Fi up to 3 times if the connection is not working. The process is logged with information on the Wi-Fi status and reconnection attempts.

Description

This change adds functionality for:

  • Wi-Fi Reconnection Attempts: The script checks the Wi-Fi status and tries to reconnect up to 3 times if the connection fails.
  • Logging: Logs are added to track the status of Wi-Fi (enabled/disabled), reconnection attempts, and results (success or failure). The log file wifi_status_log.txt records all actions.

The changes are as follows:

  • Integrated a retry mechanism in the job() function to attempt reconnection up to 3 times if no internet is detected.
  • Added logging to track Wi-Fi connection status and reconnection attempts.

Checks

in the repository

  • Made no changes that degrade the functioning of the repository
  • Gave each commit a meaningful title

Thank You,

Badis

@Badis213 Badis213 changed the title Add Reconnection Attempts and Logging for Wi-Fi Checker (#347) Add Reconnection Attempts and Logging for Wi-Fi Checker (Issue #347) Nov 10, 2024
@@ -0,0 +1 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the need for this file? and this line specifically? 

Copy link
Contributor Author

@Badis213 Badis213 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wifi_status_log.txt file is set up to capture logs from Wi-Fi connection events. Since it only starts filling when the code runs, it’s currently empty. Let me know if you’d prefer I remove it from the repository.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try to add the logic inside the script? This means that when the logging starts to capture, it should create the file automatically. That means there shouldn't be any file initially, but when the script runs and captures the log, it should create the file and then insert the data.

you can follow this code as reference:

file_name = "example.txt"

try:
    with open(file_name, 'x') as file:
        file.write("This is a new file created only if it didn't exist.")
    print(f"File '{file_name}' created successfully.")
except FileExistsError:
    print(f"File '{file_name}' already exists.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely can, I'll do that today.

Copy link
Contributor Author

@Badis213 Badis213 Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is done.
Here are the updates I made to the script:

Automatic log file creation:
The script now checks if the log file exists. If not, it automatically creates the file before writing any logs.

Improved error handling:
Added better error handling for netsh and ping commands to ensure the script behaves more reliably in different scenarios.

Constants for better configuration:
Extracted key values like the log file name and ping target into constants for easier customization.

I tested everything, and it works as expected. Below is the log file when the WiFi is manually disabled:

2024-11-18 19:15:02,500 - WiFi is enabled and connected to the internet.
2024-11-18 19:15:02,527 - WiFi connection not working, ping failed.
2024-11-18 19:15:02,527 - Attempt 1 to reconnect...
2024-11-18 19:15:07,146 - WiFi disabled
2024-11-18 19:15:11,814 - WiFi enabled
2024-11-18 19:15:16,864 - Reconnection attempt 1 failed.
2024-11-18 19:15:16,864 - Attempt 2 to reconnect...
2024-11-18 19:15:21,157 - WiFi disabled
2024-11-18 19:15:23,870 - WiFi enabled
2024-11-18 19:15:28,926 - Reconnection attempt 2 failed.
2024-11-18 19:15:28,936 - Attempt 3 to reconnect...
2024-11-18 19:15:33,427 - WiFi disabled
2024-11-18 19:15:35,974 - WiFi enabled
2024-11-18 19:15:41,098 - Reconnection successful!

This comment was marked as resolved.

Auto WiFi Check/wifi_checker.py Show resolved Hide resolved
@Badis213 Badis213 requested a review from hasan-py November 12, 2024 16:50
Copy link
Collaborator

@hasan-py hasan-py left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is possible to add the logic I mentioned in the comment, then you may do so. Otherwise, just request it again, and I will merge the changes. Thanks for your contributions 🥇

@Badis213 Badis213 requested a review from hasan-py November 24, 2024 14:49
Copy link
Collaborator

@hasan-py hasan-py left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -0,0 +1 @@

This comment was marked as resolved.

@hasan-py hasan-py merged commit 8347231 into DhanushNehru:master Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants