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
pull/34/head
Peter Hutterer 2018-01-24 16:12:31 +10:00
parent 80d214c78f
commit 7a31a994fb
3 changed files with 27 additions and 18 deletions

View File

@ -137,9 +137,9 @@ org.freedesktop.tuhi1.Device
The physical dimensions (width, height) in µm The physical dimensions (width, height) in µm
Read-only Read-only
Property: DrawingsAvailable (u) Property: DrawingsAvailable (at)
An integer indicating the number of drawings available. Drawings are An array of timestamps of the available drawings. The timestamp of
zero-indexed, see GetJSONData(). each drawing can be used as index to GetJSONData().
Read-only Read-only
Property: Listening (b) Property: Listening (b)
@ -202,7 +202,7 @@ org.freedesktop.tuhi1.Device
signal arriving, the property DrawingsAvailable may still be updated signal arriving, the property DrawingsAvailable may still be updated
and it's the responsibility of the client to fetch the JSON data. 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. Returns a JSON file with the drawings specified by the index argument.
Drawings are zero-indexed and the requested index must be less than Drawings are zero-indexed and the requested index must be less than
the DrawingsAvailable property value. See section JSON FILE FORMAT for the DrawingsAvailable property value. See section JSON FILE FORMAT for

View File

@ -325,8 +325,8 @@ class Listener(GObject.Object):
if self.device is None: if self.device is None:
return return
if self.device.drawings_available > 0: if self.device.drawings_available:
logger.info('{}: drawings available: {}'.format(self.device, self.device.drawings_available)) self._log_drawings_available(self.device)
if self.device.listening: if self.device.listening:
logger.info("{}: device already listening".format(self.device)) logger.info("{}: device already listening".format(self.device))
@ -352,7 +352,11 @@ class Listener(GObject.Object):
self.manager.quit() self.manager.quit()
def _on_drawings_available(self, device, pspec): 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): class Fetcher(GObject.Object):
@ -371,17 +375,17 @@ class Fetcher(GObject.Object):
logger.error("{}: device not found".format(address)) logger.error("{}: device not found".format(address))
return return
ndrawings = self.device.drawings_available
if index != 'all': if index != 'all':
try: try:
self.indices = [int(index)] index = int(index)
if index >= ndrawings: if index not in self.device.drawings_available:
raise ValueError() raise ValueError()
self.indices = [index]
except ValueError: except ValueError:
logger.error("Invalid index {}".format(index)) logger.error("Invalid index {}".format(index))
return return
else: else:
self.indices = list(range(ndrawings)) self.indices = self.device.drawings_available
def run(self): def run(self):
if self.device is None or self.indices is None: if self.device is None or self.indices is None:

View File

@ -53,7 +53,7 @@ INTROSPECTION_XML = """
<property type='b' name='Listening' access='read'> <property type='b' name='Listening' access='read'>
<annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/> <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/>
</property> </property>
<property type='u' name='DrawingsAvailable' access='read'> <property type='at' name='DrawingsAvailable' access='read'>
<annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/> <annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/>
</property> </property>
@ -141,7 +141,7 @@ class TuhiDBusDevice(_TuhiDBus):
self.name = device.name self.name = device.name
self.btaddr = device.address self.btaddr = device.address
self.width, self.height = 0, 0 self.width, self.height = 0, 0
self.drawings = [] self.drawings = {}
self.paired = device.paired self.paired = device.paired
self._listening = False self._listening = False
self._listening_client = None self._listening_client = None
@ -215,7 +215,10 @@ class TuhiDBusDevice(_TuhiDBus):
h = GLib.Variant.new_uint32(self.height) h = GLib.Variant.new_uint32(self.height)
return GLib.Variant.new_tuple(w, h) return GLib.Variant.new_tuple(w, h)
elif propname == 'DrawingsAvailable': 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': elif propname == 'Listening':
return GLib.Variant.new_boolean(self.listening) return GLib.Variant.new_boolean(self.listening)
@ -291,15 +294,17 @@ class TuhiDBusDevice(_TuhiDBus):
index = args[0] index = args[0]
try: try:
drawing = self.drawings[index] drawing = self.drawings[index]
except IndexError: except KeyError:
return '' return ''
else: else:
return drawing.to_json() return drawing.to_json()
def add_drawing(self, drawing): def add_drawing(self, drawing):
self.drawings.append(drawing) self.drawings[drawing.timestamp] = drawing
self.properties_changed({'DrawingsAvailable': ts = GLib.Variant.new_array(GLib.VariantType('t'),
GLib.Variant.new_uint32(len(self.drawings))}) [GLib.Variant.new_uint64(t)
for t in self.drawings.keys()])
self.properties_changed({'DrawingsAvailable': ts})
def notify_button_press_required(self): def notify_button_press_required(self):
logger.debug("Sending ButtonPressRequired signal") logger.debug("Sending ButtonPressRequired signal")