From f88a5b2222838fb7da8308935d315067059bfc16 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Mon, 15 Jan 2018 15:17:43 +0100 Subject: [PATCH] wacom: disconnect when we have finished retrieving the data There is no point keeping the connection alive just to drain the battery. --- README.md | 9 +++++---- tuhi.py | 4 ++++ tuhi/ble.py | 18 ++++++++++++++++++ tuhi/wacom.py | 7 ++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 189cf52..8a5fb8c 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,11 @@ org.freedesktop.tuhi1.Device interactivity (e.g. the user may need to press the sync button). When the device connects, the daemon downloads all drawings from the - device. If successfull, the drawings are deleted from the device. The - data is held by the daemon in non-persistent storage until the daemon - is stopped or we run out of memory, whichever happens earlier. Use - GetJSONData() to retrieve the data from the daemon. + device and disconnects from the device. If successfull, the drawings + are deleted from the device. The data is held by the daemon in + non-persistent storage until the daemon is stopped or we run out of + memory, whichever happens earlier. Use GetJSONData() to retrieve the + data from the daemon. When drawings become available from the device, the DrawingsAvailable property updates to the number of available drawings. diff --git a/tuhi.py b/tuhi.py index 811ee91..fe2d5bd 100755 --- a/tuhi.py +++ b/tuhi.py @@ -78,6 +78,7 @@ class TuhiDevice(GObject.Object): self._tuhi_dbus_device = tuhi_dbus_device self._wacom_device = WacomDevice(bluez_device) self._wacom_device.connect('drawing', self._on_drawing_received) + self._wacom_device.connect('done', self._on_fetching_finished, bluez_device) self.drawings = [] bluez_device.connect('connected', self._on_bluez_device_connected) @@ -123,6 +124,9 @@ class TuhiDevice(GObject.Object): self._tuhi_dbus_device.add_drawing(d) + def _on_fetching_finished(self, device, bluez_device): + bluez_device.disconnect_device() + class Tuhi(GObject.Object): __gsignals__ = { diff --git a/tuhi/ble.py b/tuhi/ble.py index 5aeeb34..5d417dc 100755 --- a/tuhi/ble.py +++ b/tuhi/ble.py @@ -202,6 +202,24 @@ class BlueZDevice(GObject.Object): if isinstance(result, Exception): logger.error('Connection failed: {}'.format(result)) + def disconnect_device(self): + """ + Disconnect the bluetooth device via bluez. This function is + asynchronous and returns immediately. + """ + i = self.obj.get_interface(ORG_BLUEZ_DEVICE1) + if not i.get_cached_property('Connected').get_boolean(): + logger.info('{}: Device is already disconnected'.format(self.address)) + self.emit('disconnected') + return + + logger.info('{}: Disconnecting'.format(self.address)) + i.Disconnect(result_handler=self._on_disconnect_result) + + def _on_disconnect_result(self, obj, result, user_data): + if isinstance(result, Exception): + logger.error('Disconnection failed: {}'.format(result)) + def _on_properties_changed(self, obj, properties, invalidated_properties): properties = properties.unpack() diff --git a/tuhi/wacom.py b/tuhi/wacom.py index 97c1175..f39502d 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -121,6 +121,8 @@ class WacomDevice(GObject.Object): __gsignals__ = { "drawing": (GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)), + "done": + (GObject.SIGNAL_RUN_FIRST, None, ()), } def __init__(self, device): @@ -570,7 +572,10 @@ class WacomDevice(GObject.Object): def run(self): logger.debug('{}: starting'.format(self.device.address)) - self.retrieve_data() + try: + self.retrieve_data() + finally: + self.emit("done") def start(self): self.thread = threading.Thread(target=self.run)