Make the battery device use HAL
This commit is contained in:
parent
cc2b8884c0
commit
ab370bd04e
@ -15,20 +15,31 @@
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import gobject
|
||||
import dbus
|
||||
|
||||
from model.devices import device
|
||||
|
||||
_LEVEL_PROP = 'battery.charge_level.percentage'
|
||||
|
||||
class Device(device.Device):
|
||||
__gproperties__ = {
|
||||
'level' : (int, None, None, 0, 100, 0,
|
||||
gobject.PARAM_READABLE)
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
device.Device.__init__(self)
|
||||
def __init__(self, udi):
|
||||
device.Device.__init__(self, udi)
|
||||
|
||||
self._level = 0
|
||||
self._timeout_id = gobject.timeout_add(2000, self._check_battery_level)
|
||||
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
|
||||
proxy = bus.get_object('org.freedesktop.Hal', udi)
|
||||
self._battery = dbus.Interface(proxy, 'org.freedesktop.Hal.Device')
|
||||
bus.add_signal_receiver(self._battery_changed,
|
||||
'PropertyModified',
|
||||
'org.freedesktop.Hal.Device',
|
||||
'org.freedesktop.Hal',
|
||||
udi)
|
||||
|
||||
self._level = self._battery.GetProperty(_LEVEL_PROP)
|
||||
|
||||
def do_get_property(self, pspec):
|
||||
if pspec.name == 'level':
|
||||
@ -37,29 +48,9 @@ class Device(device.Device):
|
||||
def get_type(self):
|
||||
return 'battery'
|
||||
|
||||
def _check_battery_level(self):
|
||||
new_level = self._get_battery_level()
|
||||
|
||||
if new_level != self._level:
|
||||
self._level = new_level
|
||||
self.notify('level')
|
||||
|
||||
return True
|
||||
|
||||
def _get_battery_level(self):
|
||||
battery_class_path = '/sys/class/battery/psu_0/'
|
||||
|
||||
capacity_path = battery_class_path + 'capacity_percentage'
|
||||
try:
|
||||
f = open(capacity_path, 'r')
|
||||
val = f.read().split('\n')
|
||||
level = int(val[0])
|
||||
f.close()
|
||||
except:
|
||||
level = 0
|
||||
|
||||
return level
|
||||
|
||||
def __del__(self):
|
||||
gobject.source_remove(self._timeout_id)
|
||||
self._timeout_id = 0
|
||||
def _battery_changed(self, num_changes, changes_list):
|
||||
for change in changes_list:
|
||||
if change[0] == 'battery.charge_level.percentage':
|
||||
self._level = self._battery.GetProperty(_LEVEL_PROP)
|
||||
print self._level
|
||||
self.notify('level')
|
||||
|
@ -31,9 +31,12 @@ _nm_state_to_state = {
|
||||
}
|
||||
|
||||
class Device(gobject.GObject):
|
||||
def __init__(self):
|
||||
def __init__(self, device_id=None):
|
||||
gobject.GObject.__init__(self)
|
||||
self._id = util.unique_id()
|
||||
if device_id:
|
||||
self._id = device_id
|
||||
else:
|
||||
self._id = util.unique_id()
|
||||
|
||||
def get_type(self):
|
||||
return 'unknown'
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
import logging
|
||||
import gobject
|
||||
import dbus
|
||||
|
||||
from model.devices import device
|
||||
from model.devices.network import wired
|
||||
@ -41,10 +42,19 @@ class DevicesModel(gobject.GObject):
|
||||
|
||||
self._devices = {}
|
||||
self._sigids = {}
|
||||
self.add_device(battery.Device())
|
||||
|
||||
self._observe_hal_manager()
|
||||
self._observe_network_manager()
|
||||
|
||||
def _observe_hal_manager(self):
|
||||
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
|
||||
proxy = bus.get_object('org.freedesktop.Hal',
|
||||
'/org/freedesktop/Hal/Manager')
|
||||
hal_manager = dbus.Interface(proxy, 'org.freedesktop.Hal.Manager')
|
||||
|
||||
for udi in hal_manager.FindDeviceByCapability('battery'):
|
||||
self.add_device(battery.Device(udi))
|
||||
|
||||
def _observe_network_manager(self):
|
||||
network_manager = hardwaremanager.get_network_manager()
|
||||
if not network_manager:
|
||||
|
Loading…
Reference in New Issue
Block a user