glances_batpercent.py 3.5 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 35 36 37 38 39


class Plugin(GlancesPlugin):
    """
    Glances's batterie capacity Plugin

    stats is a list
    """

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

        # Init the sensor class
        self.glancesgrabbat = glancesGrabBat()

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
        """
62
        Update batterie 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 80 81 82 83 84 85 86 87 88 89 90 91

class glancesGrabBat:
    """
    Get batteries stats using the Batinfo librairie
    """

    def __init__(self):
        """
        Init batteries stats
        """
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
92
            self.bat_list = []
93
            self.update()
N
Nicolargo 已提交
94
        except Exception as e:
95
            # print(_("Warning: Can not grab batterie sensor. Missing BatInfo lib (%s).") % e)
A
Alessio Sergi 已提交
96 97
            self.initok = False

N
Nicolargo 已提交
98

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

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

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

A
Alessio Sergi 已提交
124 125 126
        # 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 已提交
127
        for bcpt in range(len(self.bat.stat)):
A
Alessio Sergi 已提交
128
            try:
N
Nicolargo 已提交
129
                bsum = bsum + int(self.bat.stat[bcpt].capacity)
A
Alessio Sergi 已提交
130 131
            except ValueError:
                return []
N
Nicolas Hennion 已提交
132
            bcpt = bcpt + 1
N
Nicolargo 已提交
133

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