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
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

View File

@ -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:

View File

@ -53,7 +53,7 @@ INTROSPECTION_XML = """
<property type='b' name='Listening' access='read'>
<annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/>
</property>
<property type='u' name='DrawingsAvailable' access='read'>
<property type='at' name='DrawingsAvailable' access='read'>
<annotation name='org.freedesktop.DBus.Property.EmitsChangedSignal' value='true'/>
</property>
@ -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")