fix battery usage
This commit is contained in:
72
bar/services/battery.py
Normal file
72
bar/services/battery.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import psutil
|
||||
from fabric.core.service import Service, Signal
|
||||
from fabric.utils import invoke_repeater
|
||||
|
||||
|
||||
class BatteryService(Service):
|
||||
@Signal
|
||||
def battery_changed(self, percent: float, charging: bool) -> None:
|
||||
"""Signal emitted when battery status changes"""
|
||||
pass
|
||||
|
||||
def __init__(self, update_interval=10000, **kwargs): # Check every 10 seconds
|
||||
super().__init__(**kwargs)
|
||||
self._percent = 0.0
|
||||
self._charging = False
|
||||
self._update_interval = update_interval
|
||||
self._timer_id = None
|
||||
|
||||
# Start periodic updates
|
||||
self.start_monitoring()
|
||||
|
||||
def start_monitoring(self):
|
||||
"""Start monitoring battery status"""
|
||||
if self._timer_id is None:
|
||||
# Get initial values
|
||||
self._update_battery()
|
||||
# Set up periodic updates
|
||||
self._timer_id = invoke_repeater(self._update_interval, self._update_battery)
|
||||
|
||||
def stop_monitoring(self):
|
||||
"""Stop monitoring battery status"""
|
||||
if self._timer_id is not None:
|
||||
from gi.repository import GLib
|
||||
GLib.source_remove(self._timer_id)
|
||||
self._timer_id = None
|
||||
|
||||
def _update_battery(self):
|
||||
"""Update battery status and emit signal if changed"""
|
||||
try:
|
||||
# Use the same pattern as the example
|
||||
bat_sen = psutil.sensors_battery()
|
||||
if not bat_sen:
|
||||
# No battery sensor available (desktop systems)
|
||||
new_percent = 100.0 # Assume plugged in
|
||||
new_charging = True
|
||||
else:
|
||||
new_percent = bat_sen.percent
|
||||
new_charging = bat_sen.power_plugged
|
||||
|
||||
# Only emit signal if values changed
|
||||
percent_changed = abs(new_percent - self._percent) > 0.5
|
||||
charging_changed = new_charging != self._charging
|
||||
|
||||
if percent_changed or charging_changed:
|
||||
self._percent = new_percent
|
||||
self._charging = new_charging
|
||||
self.battery_changed(new_percent, new_charging)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error updating battery status: {e}")
|
||||
|
||||
return True # Keep the timer running
|
||||
|
||||
@property
|
||||
def percent(self):
|
||||
"""Get current battery percentage"""
|
||||
return self._percent
|
||||
|
||||
@property
|
||||
def charging(self):
|
||||
"""Get current charging status"""
|
||||
return self._charging
|
||||
65
bar/services/system_stats.py
Normal file
65
bar/services/system_stats.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import psutil
|
||||
from fabric.core.service import Service, Signal
|
||||
from fabric.utils import invoke_repeater
|
||||
|
||||
|
||||
class SystemStatsService(Service):
|
||||
@Signal
|
||||
def stats_changed(self, cpu_percent: float, memory_percent: float) -> None:
|
||||
"""Signal emitted when system stats change"""
|
||||
pass
|
||||
|
||||
def __init__(self, update_interval=3000, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._cpu_percent = 0.0
|
||||
self._memory_percent = 0.0
|
||||
self._update_interval = update_interval
|
||||
self._timer_id = None
|
||||
|
||||
# Start periodic updates
|
||||
self.start_monitoring()
|
||||
|
||||
def start_monitoring(self):
|
||||
"""Start monitoring system stats"""
|
||||
if self._timer_id is None:
|
||||
# Get initial values
|
||||
self._update_stats()
|
||||
# Set up periodic updates
|
||||
self._timer_id = invoke_repeater(self._update_interval, self._update_stats)
|
||||
|
||||
def stop_monitoring(self):
|
||||
"""Stop monitoring system stats"""
|
||||
if self._timer_id is not None:
|
||||
from gi.repository import GLib
|
||||
GLib.source_remove(self._timer_id)
|
||||
self._timer_id = None
|
||||
|
||||
def _update_stats(self):
|
||||
"""Update system stats and emit signal if changed"""
|
||||
try:
|
||||
new_cpu = psutil.cpu_percent()
|
||||
new_memory = psutil.virtual_memory().percent
|
||||
|
||||
# Only emit signal if values changed significantly (reduce noise)
|
||||
cpu_changed = abs(new_cpu - self._cpu_percent) > 1.0
|
||||
memory_changed = abs(new_memory - self._memory_percent) > 1.0
|
||||
|
||||
if cpu_changed or memory_changed:
|
||||
self._cpu_percent = new_cpu
|
||||
self._memory_percent = new_memory
|
||||
self.stats_changed(new_cpu / 100, new_memory / 100)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error updating system stats: {e}")
|
||||
|
||||
return True # Keep the timer running
|
||||
|
||||
@property
|
||||
def cpu_percent(self):
|
||||
"""Get current CPU percentage"""
|
||||
return self._cpu_percent / 100
|
||||
|
||||
@property
|
||||
def memory_percent(self):
|
||||
"""Get current memory percentage"""
|
||||
return self._memory_percent / 100
|
||||
Reference in New Issue
Block a user