mirror of
https://github.com/nicolargo/glances.git
synced 2024-12-23 01:01:31 +03:00
Merge branch 'sensors_battery' into develop
This commit is contained in:
commit
34e5c4c699
@ -19,14 +19,26 @@
|
||||
|
||||
"""Battery plugin."""
|
||||
|
||||
import psutil
|
||||
|
||||
from glances.logger import logger
|
||||
from glances.plugins.glances_plugin import GlancesPlugin
|
||||
|
||||
# Batinfo library (optional; Linux-only)
|
||||
batinfo_tag = True
|
||||
try:
|
||||
import batinfo
|
||||
except ImportError:
|
||||
logger.debug("Batinfo library not found. Glances cannot grab battery info.")
|
||||
logger.debug("batpercent plugin - Batinfo library not found. Trying fallback to PsUtil.")
|
||||
batinfo_tag = False
|
||||
|
||||
# PsUtil library 5.2.0 or higher (optional; Linux-only)
|
||||
psutil_tag = True
|
||||
try:
|
||||
psutil.sensors_battery()
|
||||
except AttributeError:
|
||||
logger.debug("batpercent plugin - PsUtil 5.2.0 or higher is needed to grab battery stats.")
|
||||
psutil_tag = False
|
||||
|
||||
|
||||
class Plugin(GlancesPlugin):
|
||||
@ -80,24 +92,34 @@ class GlancesGrabBat(object):
|
||||
|
||||
def __init__(self):
|
||||
"""Init batteries stats."""
|
||||
try:
|
||||
self.bat = batinfo.batteries()
|
||||
self.initok = True
|
||||
self.bat_list = []
|
||||
self.update()
|
||||
except Exception as e:
|
||||
self.initok = False
|
||||
logger.debug("Cannot init GlancesGrabBat class (%s)" % e)
|
||||
|
||||
if batinfo_tag:
|
||||
self.bat = batinfo.batteries()
|
||||
elif psutil_tag:
|
||||
self.bat = psutil
|
||||
else:
|
||||
self.bat = None
|
||||
|
||||
def update(self):
|
||||
"""Update the stats."""
|
||||
if self.initok:
|
||||
if batinfo_tag:
|
||||
# Use the batinfo lib to grab the stats
|
||||
# Compatible with multiple batteries
|
||||
self.bat.update()
|
||||
self.bat_list = [{
|
||||
'label': 'Battery',
|
||||
'value': self.battery_percent,
|
||||
'unit': '%'}]
|
||||
elif psutil_tag:
|
||||
# Use the PSUtil 5.2.0 or higher lib to grab the stats
|
||||
# Give directly the battery percent
|
||||
self.bat_list = [{
|
||||
'label': 'Battery',
|
||||
'value': int(self.bat.sensors_battery().percent),
|
||||
'unit': '%'}]
|
||||
else:
|
||||
# No stats...
|
||||
self.bat_list = []
|
||||
|
||||
def get(self):
|
||||
@ -107,7 +129,7 @@ class GlancesGrabBat(object):
|
||||
@property
|
||||
def battery_percent(self):
|
||||
"""Get batteries capacity percent."""
|
||||
if not self.initok or not self.bat.stat:
|
||||
if not batinfo_tag or not self.bat.stat:
|
||||
return []
|
||||
|
||||
# Init the bsum (sum of percent)
|
||||
|
Loading…
Reference in New Issue
Block a user