base: update the battery state to unknown after 5 minutes

If we don't get battery updates for too long, we don't really know anymore
what the battery is like.

Fixes #47
pull/53/merge
Peter Hutterer 2018-01-31 11:42:03 +10:00 committed by Benjamin Tissoires
parent 1a25163733
commit 2a3b32a4fe
2 changed files with 25 additions and 1 deletions

View File

@ -157,7 +157,11 @@ org.freedesktop.tuhi1.Device
Property: BatteryPercent (u)
The last known battery charge level in percent. This charge level is
only valid when the BatteryState is other than Unknown.
only accurate when the BatteryState is other than Unknown.
When the BatteryState is Unknown and BatteryPercent is nonzero, the
value is the last known percentage value.
Read-only
Property: BatteryState (u)

View File

@ -15,6 +15,7 @@ import argparse
import enum
import logging
import sys
import time
from gi.repository import GObject
from tuhi.dbusserver import TuhiDBusServer
@ -48,6 +49,8 @@ class TuhiDevice(GObject.Object):
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
}
BATTERY_UPDATE_MIN_INTERVAL = 300
def __init__(self, bluez_device, config, uuid=None, paired=True):
GObject.Object.__init__(self)
self.config = config
@ -58,6 +61,8 @@ class TuhiDevice(GObject.Object):
self._uuid = uuid
self._battery_state = TuhiDevice.BatteryState.UNKNOWN
self._battery_percent = 0
self._last_battery_update_time = 0
self._battery_timer_source = None
bluez_device.connect('connected', self._on_bluez_device_connected)
bluez_device.connect('disconnected', self._on_bluez_device_disconnected)
@ -171,6 +176,21 @@ class TuhiDevice(GObject.Object):
self.battery_state = TuhiDevice.BatteryState.DISCHARGING
self.battery_percent = percent
# If we don't get battery updates for a while, switch the state
# to unknown
if self._battery_timer_source is not None:
GObject.source_remove(self._battery_timer_source)
self._battery_timer_source = \
GObject.timeout_add_seconds(self.BATTERY_UPDATE_MIN_INTERVAL,
self._on_battery_timeout)
self._last_battery_update_time = time.time()
def _on_battery_timeout(self):
if self._last_battery_update_time < time.time() - self.BATTERY_UPDATE_MIN_INTERVAL:
self.battery_state = TuhiDevice.BatteryState.UNKNOWN
self._battery_timer_source = None # gets auto-destroyed
return False
class Tuhi(GObject.Object):
__gsignals__ = {