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."""
|
"""Battery plugin."""
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
from glances.plugins.glances_plugin import GlancesPlugin
|
from glances.plugins.glances_plugin import GlancesPlugin
|
||||||
|
|
||||||
# Batinfo library (optional; Linux-only)
|
# Batinfo library (optional; Linux-only)
|
||||||
|
batinfo_tag = True
|
||||||
try:
|
try:
|
||||||
import batinfo
|
import batinfo
|
||||||
except ImportError:
|
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):
|
class Plugin(GlancesPlugin):
|
||||||
@ -80,24 +92,34 @@ class GlancesGrabBat(object):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Init batteries stats."""
|
"""Init batteries stats."""
|
||||||
try:
|
self.bat_list = []
|
||||||
|
|
||||||
|
if batinfo_tag:
|
||||||
self.bat = batinfo.batteries()
|
self.bat = batinfo.batteries()
|
||||||
self.initok = True
|
elif psutil_tag:
|
||||||
self.bat_list = []
|
self.bat = psutil
|
||||||
self.update()
|
else:
|
||||||
except Exception as e:
|
self.bat = None
|
||||||
self.initok = False
|
|
||||||
logger.debug("Cannot init GlancesGrabBat class (%s)" % e)
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the stats."""
|
"""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.update()
|
||||||
self.bat_list = [{
|
self.bat_list = [{
|
||||||
'label': 'Battery',
|
'label': 'Battery',
|
||||||
'value': self.battery_percent,
|
'value': self.battery_percent,
|
||||||
'unit': '%'}]
|
'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:
|
else:
|
||||||
|
# No stats...
|
||||||
self.bat_list = []
|
self.bat_list = []
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
@ -107,7 +129,7 @@ class GlancesGrabBat(object):
|
|||||||
@property
|
@property
|
||||||
def battery_percent(self):
|
def battery_percent(self):
|
||||||
"""Get batteries capacity percent."""
|
"""Get batteries capacity percent."""
|
||||||
if not self.initok or not self.bat.stat:
|
if not batinfo_tag or not self.bat.stat:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Init the bsum (sum of percent)
|
# Init the bsum (sum of percent)
|
||||||
|
Loading…
Reference in New Issue
Block a user