Skip to content

Trying to fix the high CPU uses#8

Merged
arghyabi merged 1 commit into
masterfrom
fix_high_CPU_use
Sep 26, 2025
Merged

Trying to fix the high CPU uses#8
arghyabi merged 1 commit into
masterfrom
fix_high_CPU_use

Conversation

@arghyabi

Copy link
Copy Markdown
Owner

No description provided.

Signed-off-by: Arghya Biswas <arghyabiswas05@gmail.com>
@arghyabi
arghyabi requested a review from Copilot September 26, 2025 06:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes CPU usage in a water level monitoring system by implementing more efficient polling intervals and database change detection mechanisms.

  • Reduced frequent database polling by implementing file modification time tracking
  • Increased sleep intervals from 0.1s to 1s during normal operation and from 1s to 5s for distance checks
  • Added distance validation to filter out erroneous sensor readings

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
config.yaml Version bump from 1.2.2.1005 to 1.2.3.1006
Scripts/Utility.py Added distanceIsValid function to validate ultrasonic sensor readings
Scripts/Main.py Implemented efficient database polling with file modification tracking and increased sleep intervals
Scripts/Common.py Updated check intervals from 1s to 5s and added database check interval constant

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread Scripts/Main.py
Comment on lines +144 to +145
# Only check for scheduled valve operations once per minute to reduce processing
if now.second == 0: # Only check at the beginning of each minute

Copilot AI Sep 26, 2025

Copy link

Choose a reason for hiding this comment

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

This condition may cause valve operations to be missed if the loop iteration doesn't align with second == 0. Consider using a minute-based timestamp comparison instead of relying on the exact second alignment.

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Main.py
Comment on lines +252 to +258
# Smart sleep: shorter sleep when config updates are expected
if configUpdateAvailable:
# If we just processed a config update, check again sooner
time.sleep(0.5) # Quick follow-up check
else:
# Normal operation - longer sleep for CPU efficiency
time.sleep(1)

Copilot AI Sep 26, 2025

Copy link

Choose a reason for hiding this comment

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

The magic numbers 0.5 and 1 for sleep intervals should be defined as named constants in Common.py for better maintainability and consistency with other timing constants.

Copilot uses AI. Check for mistakes.
Comment thread Scripts/Utility.py
return False
if lastDistance > 0:
change = abs(distance - lastDistance)
if change / lastDistance > 0.1: # More than 10% change

Copilot AI Sep 26, 2025

Copy link

Choose a reason for hiding this comment

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

The magic number 0.1 (10% threshold) should be defined as a named constant to make it configurable and improve code readability.

Copilot uses AI. Check for mistakes.
@arghyabi
arghyabi merged commit 6cf0d65 into master Sep 26, 2025
1 check passed
@arghyabi
arghyabi deleted the fix_high_CPU_use branch September 26, 2025 06:07
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