Oops, some previously added files not added to svn (I hate it when that
[rox-lithium.git] / pmu.py
blob898891c702a4c5e36414e1dbad7ea650d9e6072f
1 ##############################################################################
2 ##
3 ## $Id: pmu.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
4 ##
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
10 ## are met:
12 ## 1. Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## 2. Redistributions in binary form must reproduce the above copyright
15 ## notice, this list of conditions and the following disclaimer in the
16 ## documentation and/or other materials provided with the distribution.
17 ## 3. The name of the author may not be used to endorse or promote products
18 ## derived from this software without specific prior written permission.
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ###############################################################################
35 import sys
38 #enums
39 OFFLINE = 0
40 ONLINE = 1
41 CHARGING = 2 #implies ONLINE
42 NOBATTERY = 3
44 UNKNOWN = -1
46 class Pmu:
47 """Interface class for PMU"""
49 def __init__(self):
50 res = sys.platform
51 if res.find("linux2") > -1:
52 self.pmu = PmuLinux()
54 else:
55 self.pmu = None #throw exception (os unknown)
56 raise NotImplemented
59 def update(self):
60 """Updates the PMU state"""
61 self.pmu.update()
64 def percent(self):
65 """Returns percentage capacity of all batteries"""
66 return self.pmu.percent()
69 def time(self):
70 """Returns time of all batteries (in minutes)"""
71 return self.pmu.time()
74 def charging_state(self):
75 """Returns ac state (off-/online/charging)"""
76 return self.pmu.charging_state()
82 #base class for os dependent pmu classes
84 class PmuBase:
85 def __init__(self):
86 self.ac_line_state = OFFLINE
88 self.life_percent = 0
89 self.life_time = 0 #0 seconds
91 #initial reading of pmu info
92 self.update()
94 def percent(self):
95 #returns percentage capacity of all batteries
96 return self.life_percent
99 def time(self):
100 #returns time of all batteries (in minutes)
101 return self.life_time
104 def charging_state(self):
105 return self.ac_line_state
110 #implementation for Linux
111 #if we want support more than battery info we need a low level module implemented using C
113 class PmuLinux(PmuBase):
114 def update(self):
115 #read /proc/pmu and extract needed infos
117 try:
118 pmu_proc = open("/proc/pmu/info")
119 except IOError:
120 raise EnvironmentError
122 data = {}
123 lines = pmu_proc.readlines()
124 for line in lines:
125 token = line.split(':')
126 data[token[0].strip()] = token[1].strip()
128 try:
129 pmu_battery = open("/proc/pmu/battery_0")
130 except:
131 self.ac_line_state = ONLINE
132 return
134 lines = pmu_battery.readlines()
135 for line in lines:
136 try:
137 token = line.split(':')
138 data[token[0].strip()] = token[1].strip()
139 except:
140 pass
142 if data['Battery count'] != "0" and data['AC Power'] == "1" and \
143 (int(data['charge']) < int(data['max_charge'])):
144 self.ac_line_state = CHARGING
145 elif data['Battery count'] == "0" or data['AC Power'] == "1":
146 self.ac_line_state = ONLINE
147 else:
148 self.ac_line_state = OFFLINE
150 self.life_percent = int(100 * int(data['charge']) / int(data['max_charge']))
152 self.life_time = int(int(data['time rem.']) / 60)
155 pmu_proc.close()
156 pmu_battery.close()