cache notification flags when possible

avoids unnecessary reads from devices when the status hasn't changed
This commit is contained in:
Daniel Pavel 2013-06-19 17:03:01 +02:00
parent 150c43f41f
commit b4bca4670b
3 changed files with 20 additions and 20 deletions

View File

@ -183,14 +183,13 @@ class PairedDevice(object):
ok = _hidpp10.set_notification_flags(self, set_flag_bits)
flag_bits = _hidpp10.get_notification_flags(self)
if flag_bits is not None:
flag_bits = tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits))
flag_names = None if flag_bits is None else tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits))
if ok:
_log.info("%s: device notifications %s %s", self, 'enabled' if enable else 'disabled', flag_bits)
_log.info("%s: device notifications %s %s", self, 'enabled' if enable else 'disabled', flag_names)
else:
_log.warn("%s: failed to %s device notifications %s", self, 'enable' if enable else 'disable', flag_bits)
return ok
_log.warn("%s: failed to %s device notifications %s", self, 'enable' if enable else 'disable', flag_names)
return flag_bits if ok else None
def request(self, request_id, *params):
return _base.request(self.receiver.handle, self.number, request_id, *params)
@ -289,14 +288,13 @@ class Receiver(object):
ok = _hidpp10.set_notification_flags(self, set_flag_bits)
flag_bits = _hidpp10.get_notification_flags(self)
if flag_bits is not None:
flag_bits = tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits))
flag_names = None if flag_bits is None else tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits))
if ok:
_log.info("%s: receiver notifications %s => %s", self, 'enabled' if enable else 'disabled', flag_bits)
_log.info("%s: receiver notifications %s => %s", self, 'enabled' if enable else 'disabled', flag_names)
else:
_log.warn("%s: failed to %s receiver notifications %s", self, 'enable' if enable else 'disable', flag_bits)
return ok
_log.warn("%s: failed to %s receiver notifications %s", self, 'enable' if enable else 'disable', flag_names)
return flag_bits if ok else None
def notify_devices(self):
"""Scan all devices."""

View File

@ -37,6 +37,7 @@ BATTERY_STATUS='battery-status'
BATTERY_CHARGING='battery-charging'
LIGHT_LEVEL='light-level'
ERROR='error'
NOTIFICATIONS='notifications'
# If the battery charge is under this percentage, trigger an attention event
# (blink systray icon/notification/whatever).
@ -67,6 +68,7 @@ class ReceiverStatus(dict):
self.new_device = None
self[ERROR] = None
self[NOTIFICATIONS] = _hidpp10.get_notification_flags(receiver)
def __str__(self):
count = len(self._receiver)
@ -89,6 +91,9 @@ class ReceiverStatus(dict):
# make sure to read some stuff that may be read later by the UI
r.serial, r.firmware, None
# r.enable_notifications()
self[NOTIFICATIONS] = _hidpp10.get_notification_flags(r)
def process_notification(self, n):
if n.sub_id == 0x4A:
self.lock_open = bool(n.address & 0x01)
@ -217,7 +222,8 @@ class DeviceStatus(dict):
# Make sure to set notification flags on the device, they
# get cleared when the device is turned off (but not when the device
# goes idle, and we can't tell the difference right now).
d.enable_notifications()
self[NOTIFICATIONS] = d.enable_notifications()
if self.configuration:
self.configuration.attach_to(d)
else:

View File

@ -20,6 +20,7 @@ from logitech.unifying_receiver.status import (
BATTERY_CHARGING as _BATTERY_CHARGING,
LIGHT_LEVEL as _LIGHT_LEVEL,
ENCRYPTED as _ENCRYPTED,
NOTIFICATIONS as _NOTFITICATIONS,
)
from . import config_panel as _config_panel
from . import action as _action, icons as _icons
@ -453,15 +454,10 @@ def _update_details(button):
for fw in device.firmware:
yield (fw.kind, (fw.name + ' ' + fw.version).strip())
if device.kind is None or device.status:
# don't show notifications for offline devices
notification_flags = _hidpp10.get_notification_flags(device)
if notification_flags:
notification_flags = _hidpp10.NOTIFICATION_FLAG.flag_names(notification_flags)
else:
notification_flags = ('(none)',)
yield ('Notifications', ('\n%16s' % ' ').join(notification_flags))
flag_bits = device.status.get(_NOTFITICATIONS)
if flag_bits is not None:
flag_names = ('(none)',) if flag_bits == 0 else _hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits)
yield ('Notifications', ('\n%16s' % ' ').join(flag_names))
items = _details_items(device)
markup_text = '<small><tt>' + '\n'.join('%-14s: %s' % i for i in items if i) + '</tt></small>'