glances_batpercent.py 3.4 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
N
Nicolas Hennion 已提交
19
"""
A
Alessio Sergi 已提交
20
Batinfo (battery) plugin
N
Nicolas Hennion 已提交
21
"""
A
Alessio Sergi 已提交
22

A
Alessio Sergi 已提交
23
# Batinfo library (optional; Linux-only)
A
Alessio Sergi 已提交
24 25
try:
    import batinfo
N
Nicolas Hennion 已提交
26
except ImportError:
A
Alessio Sergi 已提交
27 28
    pass

N
Nicolas Hennion 已提交
29 30
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
31 32 33 34


class Plugin(GlancesPlugin):
    """
A
Alessio Sergi 已提交
35
    Glances battery capacity plugin
A
Alessio Sergi 已提交
36 37 38 39

    stats is a list
    """

40 41
    def __init__(self, args=None):
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
42 43

        # Init the sensor class
A
Alessio Sergi 已提交
44
        self.glancesgrabbat = GlancesGrabBat()
A
Alessio Sergi 已提交
45

N
Nicolargo 已提交
46 47 48 49
        # We do not want to display the stat in a dedicated area
        # The HDD temp is displayed within the sensors plugin
        self.display_curse = False

50
        # Init stats
A
Alessio Sergi 已提交
51
        self.reset()
52 53 54 55 56 57 58

    def reset(self):
        """
        Reset/init the stats
        """
        self.stats = []

59
    def update(self):
60

A
Alessio Sergi 已提交
61
        """
A
Alessio Sergi 已提交
62
        Update battery capacity stats using the input method
A
Alessio Sergi 已提交
63 64
        """

65 66 67
        # Reset stats
        self.reset()

68
        if self.get_input() == 'local':
N
Nicolargo 已提交
69 70
            # Update stats
            self.glancesgrabbat.update()
N
Nicolargo 已提交
71
            self.stats = self.glancesgrabbat.get()
72

73
        elif self.get_input() == 'snmp':
74 75 76
            # Update stats using SNMP
            # Not avalaible
            pass
A
Alessio Sergi 已提交
77

78
        return self.stats
A
Alessio Sergi 已提交
79

A
Alessio Sergi 已提交
80

A
Alessio Sergi 已提交
81
class GlancesGrabBat(object):
A
Alessio Sergi 已提交
82
    """
A
Alessio Sergi 已提交
83
    Get batteries stats using the Batinfo library
A
Alessio Sergi 已提交
84 85 86 87 88 89 90 91 92
    """

    def __init__(self):
        """
        Init batteries stats
        """
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
93
            self.bat_list = []
94
            self.update()
A
Alessio Sergi 已提交
95 96
        except Exception:
            # print(_("Warning: Cannot grab battery sensor. Missing BatInfo library."))
A
Alessio Sergi 已提交
97 98
            self.initok = False

99
    def update(self):
A
Alessio Sergi 已提交
100 101 102 103
        """
        Update the stats
        """
        if self.initok:
104 105
            reply = self.bat.update()
            if reply is not None:
N
Nicolargo 已提交
106
                self.bat_list = []
A
Alessio Sergi 已提交
107 108 109
                new_item = {'label': _("Battery (%)"),
                            'value': self.getcapacitypercent()}
                self.bat_list.append(new_item)
A
Alessio Sergi 已提交
110 111 112 113 114 115 116 117
        else:
            self.bat_list = []

    def get(self):
        # Update the stats
        return self.bat_list

    def getcapacitypercent(self):
N
Nicolargo 已提交
118
        if not self.initok or self.bat.stat == []:
A
Alessio Sergi 已提交
119
            return []
N
Nicolargo 已提交
120

A
Alessio Sergi 已提交
121 122 123
        # Init the bsum (sum of percent) and bcpt (number of batteries)
        # and Loop over batteries (yes a computer could have more than 1 battery)
        bsum = 0
N
Nicolargo 已提交
124
        for bcpt in range(len(self.bat.stat)):
A
Alessio Sergi 已提交
125
            try:
N
Nicolargo 已提交
126
                bsum = bsum + int(self.bat.stat[bcpt].capacity)
A
Alessio Sergi 已提交
127 128
            except ValueError:
                return []
N
Nicolas Hennion 已提交
129
            bcpt = bcpt + 1
N
Nicolargo 已提交
130

A
Alessio Sergi 已提交
131 132
        # Return the global percent
        return int(bsum / bcpt)