persist and restore device settings
This commit is contained in:
parent
bde54aba3b
commit
04ea8293a8
|
@ -73,7 +73,6 @@ class DeviceUnreachable(_KwException):
|
|||
DEVICE_UNIFYING_RECEIVER = (0x046d, 0xc52b, 2, 'logitech-djreceiver')
|
||||
DEVICE_UNIFYING_RECEIVER_2 = (0x046d, 0xc532, 2, 'logitech-djreceiver')
|
||||
DEVICE_NANO_RECEIVER = (0x046d, 0xc52f, 1, 'hid-generic')
|
||||
# DEVICE_VXNANO_RECEIVER = (0x046d, 0xc526, 1, 'hid-generic')
|
||||
|
||||
|
||||
def receivers():
|
||||
|
|
|
@ -124,9 +124,15 @@ class DeviceStatus(dict):
|
|||
assert changed_callback
|
||||
self._changed_callback = changed_callback
|
||||
|
||||
# is the device active?
|
||||
self._active = None
|
||||
|
||||
# timestamp of when this status object was last updated
|
||||
self.updated = 0
|
||||
|
||||
# optional object able to persist device settings
|
||||
self.configuration = None
|
||||
|
||||
def __str__(self):
|
||||
def _item(name, format):
|
||||
value = self.get(name)
|
||||
|
@ -203,13 +209,18 @@ class DeviceStatus(dict):
|
|||
|
||||
def _changed(self, active=True, alert=ALERT.NONE, reason=None, timestamp=None):
|
||||
assert self._changed_callback
|
||||
assert self._device
|
||||
d = self._device
|
||||
was_active, self._active = self._active, active
|
||||
if active:
|
||||
if not was_active:
|
||||
# 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).
|
||||
self._device.enable_notifications()
|
||||
d.enable_notifications()
|
||||
d.settings, None
|
||||
if self.configuration:
|
||||
self.configuration.apply_to(d)
|
||||
else:
|
||||
if was_active:
|
||||
battery = self.get(BATTERY_LEVEL)
|
||||
|
@ -218,6 +229,8 @@ class DeviceStatus(dict):
|
|||
# to change much while the device is offline.
|
||||
if battery is not None:
|
||||
self[BATTERY_LEVEL] = battery
|
||||
if self.configuration:
|
||||
self.configuration.acquire_from(d)
|
||||
|
||||
if self.updated == 0 and active:
|
||||
# if the device is active on the very first status notification,
|
||||
|
@ -228,7 +241,7 @@ class DeviceStatus(dict):
|
|||
|
||||
# if _log.isEnabledFor(_DEBUG):
|
||||
# _log.debug("device %d changed: active=%s %s", self._device.number, self._active, dict(self))
|
||||
self._changed_callback(self._device, alert, reason)
|
||||
self._changed_callback(d, alert, reason)
|
||||
|
||||
def poll(self, timestamp):
|
||||
d = self._device
|
||||
|
|
|
@ -15,7 +15,7 @@ _file_path = _path.join(_path.join(_XDG_CONFIG_HOME, 'solaar'), 'config.json')
|
|||
|
||||
|
||||
from solaar import __version__
|
||||
_configuration = { '_version': __version__ }
|
||||
_configuration = {}
|
||||
|
||||
|
||||
def _load():
|
||||
|
@ -98,13 +98,14 @@ def get_device(device, key, default_value=None):
|
|||
|
||||
|
||||
def apply_to(device):
|
||||
"""Apply the last saved configuration to a device."""
|
||||
if not _configuration:
|
||||
_load()
|
||||
|
||||
if _log.isEnabledFor(_DEBUG):
|
||||
_log.debug("applying to %s", device)
|
||||
|
||||
entry = _device_entry(device)
|
||||
if _log.isEnabledFor(_DEBUG):
|
||||
_log.debug("applying %s to %s", entry, device)
|
||||
|
||||
for s in device.settings:
|
||||
value = s.read()
|
||||
if s.name in entry:
|
||||
|
@ -117,18 +118,19 @@ def apply_to(device):
|
|||
|
||||
|
||||
def acquire_from(device):
|
||||
"""Read the values of all the settings a device has, and save them."""
|
||||
if not _configuration:
|
||||
_load()
|
||||
|
||||
if _log.isEnabledFor(_DEBUG):
|
||||
_log.debug("acquiring from %s", device)
|
||||
|
||||
entry = _device_entry(device)
|
||||
for s in device.settings:
|
||||
value = s.read()
|
||||
if value is not None:
|
||||
entry[s.name] = value
|
||||
|
||||
if _log.isEnabledFor(_DEBUG):
|
||||
_log.debug("acquired %s from %s", entry, device)
|
||||
|
||||
save()
|
||||
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ class ReceiverListener(_listener.EventsListener):
|
|||
_log.exception("closing receiver %s" % r.path)
|
||||
self.status_changed_callback(r) #, _status.ALERT.NOTIFICATION)
|
||||
|
||||
# configuration.save()
|
||||
configuration.save()
|
||||
|
||||
def tick(self, timestamp):
|
||||
if not self.tick_period:
|
||||
|
@ -177,6 +177,7 @@ class ReceiverListener(_listener.EventsListener):
|
|||
# read these as soon as possible, they will be used everywhere
|
||||
dev.protocol, dev.codename
|
||||
dev.status = _status.DeviceStatus(dev, self._status_changed)
|
||||
dev.status.configuration = configuration
|
||||
# the receiver changed status as well
|
||||
self._status_changed(self.receiver)
|
||||
|
||||
|
|
Loading…
Reference in New Issue