1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2015 Nicolargo <[email protected]> |
||
6 | # |
||
7 | # Glances is free software; you can redistribute it and/or modify |
||
8 | # it under the terms of the GNU Lesser General Public License as published by |
||
9 | # the Free Software Foundation, either version 3 of the License, or |
||
10 | # (at your option) any later version. |
||
11 | # |
||
12 | # Glances is distributed in the hope that it will be useful, |
||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | # GNU Lesser General Public License for more details. |
||
16 | # |
||
17 | # You should have received a copy of the GNU Lesser General Public License |
||
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | |||
20 | """Battery plugin.""" |
||
21 | |||
22 | from glances.logger import logger |
||
23 | from glances.plugins.glances_plugin import GlancesPlugin |
||
24 | |||
25 | # Batinfo library (optional; Linux-only) |
||
26 | try: |
||
27 | import batinfo |
||
28 | except ImportError: |
||
29 | logger.debug("Batinfo library not found. Glances cannot grab battery info.") |
||
30 | |||
31 | |||
32 | View Code Duplication | class Plugin(GlancesPlugin): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
33 | |||
34 | """Glances battery capacity plugin. |
||
35 | |||
36 | stats is a list |
||
37 | """ |
||
38 | |||
39 | def __init__(self, args=None): |
||
40 | """Init the plugin.""" |
||
41 | super(Plugin, self).__init__(args=args) |
||
42 | |||
43 | # Init the sensor class |
||
44 | self.glancesgrabbat = GlancesGrabBat() |
||
45 | |||
46 | # We do not want to display the stat in a dedicated area |
||
47 | # The HDD temp is displayed within the sensors plugin |
||
48 | self.display_curse = False |
||
49 | |||
50 | # Init stats |
||
51 | self.reset() |
||
52 | |||
53 | def reset(self): |
||
54 | """Reset/init the stats.""" |
||
55 | self.stats = [] |
||
56 | |||
57 | @GlancesPlugin._log_result_decorator |
||
58 | def update(self): |
||
59 | """Update battery capacity stats using the input method.""" |
||
60 | # Reset stats |
||
61 | self.reset() |
||
62 | |||
63 | if self.input_method == 'local': |
||
64 | # Update stats |
||
65 | self.glancesgrabbat.update() |
||
66 | self.stats = self.glancesgrabbat.get() |
||
67 | |||
68 | elif self.input_method == 'snmp': |
||
69 | # Update stats using SNMP |
||
70 | # Not avalaible |
||
71 | pass |
||
72 | |||
73 | return self.stats |
||
74 | |||
75 | |||
76 | class GlancesGrabBat(object): |
||
77 | |||
78 | """Get batteries stats using the batinfo library.""" |
||
79 | |||
80 | def __init__(self): |
||
81 | """Init batteries stats.""" |
||
82 | try: |
||
83 | self.bat = batinfo.batteries() |
||
84 | self.initok = True |
||
85 | self.bat_list = [] |
||
86 | self.update() |
||
87 | except Exception as e: |
||
88 | self.initok = False |
||
89 | logger.debug("Cannot init GlancesGrabBat class (%s)" % e) |
||
90 | |||
91 | def update(self): |
||
92 | """Update the stats.""" |
||
93 | if self.initok: |
||
94 | self.bat.update() |
||
95 | self.bat_list = [{ |
||
96 | 'label': 'Battery', |
||
97 | 'value': self.battery_percent, |
||
98 | 'unit': '%'}] |
||
99 | else: |
||
100 | self.bat_list = [] |
||
101 | |||
102 | def get(self): |
||
103 | """Get the stats.""" |
||
104 | return self.bat_list |
||
105 | |||
106 | @property |
||
107 | def battery_percent(self): |
||
108 | """Get batteries capacity percent.""" |
||
109 | if not self.initok or not self.bat.stat: |
||
110 | return [] |
||
111 | |||
112 | # Init the bsum (sum of percent) |
||
113 | # and Loop over batteries (yes a computer could have more than 1 battery) |
||
114 | bsum = 0 |
||
115 | for b in self.bat.stat: |
||
116 | try: |
||
117 | bsum += int(b.capacity) |
||
118 | except ValueError: |
||
119 | return [] |
||
120 | |||
121 | # Return the global percent |
||
122 | return int(bsum / len(self.bat.stat)) |
||
123 |