Updated battery monitor.
This commit is contained in:
@@ -12,7 +12,6 @@ import os
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
import configparser
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
@@ -29,43 +28,16 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class BatteryMonitor:
|
||||
def __init__(self):
|
||||
self.config_dir = Path.home() / '.config' / 'stormux'
|
||||
self.config_file = self.config_dir / 'battery_monitor.conf'
|
||||
# Hardcoded settings - no config file needed
|
||||
self.enabled = True
|
||||
self.warning_10_percent = True
|
||||
self.warning_5_percent = True
|
||||
self.shutdown_3_percent = True
|
||||
self.check_interval = 30
|
||||
self.speech_enabled = True
|
||||
|
||||
self.warned_10 = False
|
||||
self.warned_5 = False
|
||||
self.load_config()
|
||||
|
||||
def load_config(self):
|
||||
"""Load configuration with defaults"""
|
||||
self.config = configparser.ConfigParser()
|
||||
|
||||
# Default configuration
|
||||
defaults = {
|
||||
'enabled': 'true',
|
||||
'warning_10_percent': 'true',
|
||||
'warning_5_percent': 'true',
|
||||
'shutdown_3_percent': 'true',
|
||||
'check_interval': '30',
|
||||
'speech_enabled': 'true'
|
||||
}
|
||||
|
||||
self.config['DEFAULT'] = defaults
|
||||
|
||||
if self.config_file.exists():
|
||||
try:
|
||||
self.config.read(self.config_file)
|
||||
logger.info(f"Loaded config from {self.config_file}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Error reading config: {e}, using defaults")
|
||||
else:
|
||||
self.save_config()
|
||||
|
||||
def save_config(self):
|
||||
"""Save current configuration"""
|
||||
self.config_dir.mkdir(parents=True, exist_ok=True)
|
||||
with open(self.config_file, 'w') as f:
|
||||
self.config.write(f)
|
||||
logger.info(f"Saved config to {self.config_file}")
|
||||
|
||||
def has_battery(self):
|
||||
"""Check if system has a real battery"""
|
||||
@@ -125,7 +97,7 @@ class BatteryMonitor:
|
||||
|
||||
def speak(self, message):
|
||||
"""Use speech-dispatcher to speak a message"""
|
||||
if not self.config.getboolean('DEFAULT', 'speech_enabled'):
|
||||
if not self.speech_enabled:
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -149,7 +121,7 @@ class BatteryMonitor:
|
||||
def handle_low_battery(self, level):
|
||||
"""Handle low battery warnings and actions"""
|
||||
if level <= 3:
|
||||
if self.config.getboolean('DEFAULT', 'shutdown_3_percent'):
|
||||
if self.shutdown_3_percent:
|
||||
logger.critical(f"Battery at {level}% - initiating shutdown")
|
||||
self.speak("Critical battery level. System shutting down now.")
|
||||
time.sleep(2) # Give speech time to complete
|
||||
@@ -157,14 +129,14 @@ class BatteryMonitor:
|
||||
return
|
||||
|
||||
elif level <= 5 and not self.warned_5:
|
||||
if self.config.getboolean('DEFAULT', 'warning_5_percent'):
|
||||
if self.warning_5_percent:
|
||||
logger.warning(f"Battery at {level}% - urgent warning")
|
||||
self.play_urgent_sound()
|
||||
self.speak("Extremely low battery. Computer will shut down soon.")
|
||||
self.warned_5 = True
|
||||
|
||||
elif level <= 10 and not self.warned_10:
|
||||
if self.config.getboolean('DEFAULT', 'warning_10_percent'):
|
||||
if self.warning_10_percent:
|
||||
logger.warning(f"Battery at {level}% - first warning")
|
||||
self.speak("Low battery warning. Please connect power adapter.")
|
||||
self.warned_10 = True
|
||||
@@ -179,8 +151,8 @@ class BatteryMonitor:
|
||||
|
||||
def monitor(self):
|
||||
"""Main monitoring loop"""
|
||||
if not self.config.getboolean('DEFAULT', 'enabled'):
|
||||
logger.info("Battery monitoring disabled in config")
|
||||
if not self.enabled:
|
||||
logger.info("Battery monitoring disabled")
|
||||
return
|
||||
|
||||
if not self.has_battery():
|
||||
@@ -188,7 +160,6 @@ class BatteryMonitor:
|
||||
return
|
||||
|
||||
logger.info("Starting battery monitoring")
|
||||
check_interval = self.config.getint('DEFAULT', 'check_interval')
|
||||
|
||||
while True:
|
||||
try:
|
||||
@@ -200,14 +171,14 @@ class BatteryMonitor:
|
||||
else:
|
||||
logger.warning("Could not read battery level")
|
||||
|
||||
time.sleep(check_interval)
|
||||
time.sleep(self.check_interval)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logger.info("Battery monitoring stopped by user")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"Monitoring error: {e}")
|
||||
time.sleep(check_interval)
|
||||
time.sleep(self.check_interval)
|
||||
|
||||
def main():
|
||||
if os.geteuid() != 0:
|
||||
|
||||
Reference in New Issue
Block a user