From 7a31a994fbe2869f0f36ccfa0705941500a9d261 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 24 Jan 2018 16:12:31 +1000 Subject: [PATCH] dbus: return the drawings as array of timestamps As we're planning to cache the data locally, the timestamps are a unique-enough way that makes it possible to access a specific drawing. And this way we can also delete some drawings without all other indices shifting around. Fixes #16 --- README.md | 8 ++++---- tools/tuhi-kete.py | 18 +++++++++++------- tuhi/dbusserver.py | 19 ++++++++++++------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5442b66..07d4a39 100644 --- a/README.md +++ b/README.md @@ -137,9 +137,9 @@ org.freedesktop.tuhi1.Device The physical dimensions (width, height) in µm Read-only - Property: DrawingsAvailable (u) - An integer indicating the number of drawings available. Drawings are - zero-indexed, see GetJSONData(). + Property: DrawingsAvailable (at) + An array of timestamps of the available drawings. The timestamp of + each drawing can be used as index to GetJSONData(). Read-only Property: Listening (b) @@ -202,7 +202,7 @@ org.freedesktop.tuhi1.Device signal arriving, the property DrawingsAvailable may still be updated and it's the responsibility of the client to fetch the JSON data. - Method: GetJSONData(index: u) -> (s) + Method: GetJSONData(index: t) -> (s) Returns a JSON file with the drawings specified by the index argument. Drawings are zero-indexed and the requested index must be less than the DrawingsAvailable property value. See section JSON FILE FORMAT for diff --git a/tools/tuhi-kete.py b/tools/tuhi-kete.py index 6be3299..0bb7f7e 100755 --- a/tools/tuhi-kete.py +++ b/tools/tuhi-kete.py @@ -325,8 +325,8 @@ class Listener(GObject.Object): if self.device is None: return - if self.device.drawings_available > 0: - logger.info('{}: drawings available: {}'.format(self.device, self.device.drawings_available)) + if self.device.drawings_available: + self._log_drawings_available(self.device) if self.device.listening: logger.info("{}: device already listening".format(self.device)) @@ -352,7 +352,11 @@ class Listener(GObject.Object): self.manager.quit() def _on_drawings_available(self, device, pspec): - logger.info('{}: drawings available: {}'.format(device, device.drawings_available)) + self._log_drawings_available(device) + + def _log_drawings_available(self, device): + s = ", ".join(["{}".format(t) for t in device.drawings_available]) + logger.info('{}: drawings available: {}'.format(device, s)) class Fetcher(GObject.Object): @@ -371,17 +375,17 @@ class Fetcher(GObject.Object): logger.error("{}: device not found".format(address)) return - ndrawings = self.device.drawings_available if index != 'all': try: - self.indices = [int(index)] - if index >= ndrawings: + index = int(index) + if index not in self.device.drawings_available: raise ValueError() + self.indices = [index] except ValueError: logger.error("Invalid index {}".format(index)) return else: - self.indices = list(range(ndrawings)) + self.indices = self.device.drawings_available def run(self): if self.device is None or self.indices is None: diff --git a/tuhi/dbusserver.py b/tuhi/dbusserver.py index d3d665d..30b1fc1 100755 --- a/tuhi/dbusserver.py +++ b/tuhi/dbusserver.py @@ -53,7 +53,7 @@ INTROSPECTION_XML = """ - + @@ -141,7 +141,7 @@ class TuhiDBusDevice(_TuhiDBus): self.name = device.name self.btaddr = device.address self.width, self.height = 0, 0 - self.drawings = [] + self.drawings = {} self.paired = device.paired self._listening = False self._listening_client = None @@ -215,7 +215,10 @@ class TuhiDBusDevice(_TuhiDBus): h = GLib.Variant.new_uint32(self.height) return GLib.Variant.new_tuple(w, h) elif propname == 'DrawingsAvailable': - return GLib.Variant.new_uint32(len(self.drawings)) + ts = GLib.Variant.new_array(GLib.VariantType('t'), + [GLib.Variant.new_uint64(t) + for t in self.drawings.keys()]) + return ts elif propname == 'Listening': return GLib.Variant.new_boolean(self.listening) @@ -291,15 +294,17 @@ class TuhiDBusDevice(_TuhiDBus): index = args[0] try: drawing = self.drawings[index] - except IndexError: + except KeyError: return '' else: return drawing.to_json() def add_drawing(self, drawing): - self.drawings.append(drawing) - self.properties_changed({'DrawingsAvailable': - GLib.Variant.new_uint32(len(self.drawings))}) + self.drawings[drawing.timestamp] = drawing + ts = GLib.Variant.new_array(GLib.VariantType('t'), + [GLib.Variant.new_uint64(t) + for t in self.drawings.keys()]) + self.properties_changed({'DrawingsAvailable': ts}) def notify_button_press_required(self): logger.debug("Sending ButtonPressRequired signal")