diff --git a/mopidy_autoplay/__init__.py b/mopidy_autoplay/__init__.py index cbafcd4..85c0459 100644 --- a/mopidy_autoplay/__init__.py +++ b/mopidy_autoplay/__init__.py @@ -92,6 +92,11 @@ def get_config_schema(self): # mute = True|False|"auto") schema['mixer.mute'] = AutoValue(config.Boolean) + # Save state on events + schema['autosave_events'] = AutoValue(config.List,optional=True) + schema['autosave_min_interval'] = AutoValue( + config.Integer, minimum=10) + return schema def setup(self, registry): diff --git a/mopidy_autoplay/ext.conf b/mopidy_autoplay/ext.conf index 222d577..de13aa5 100644 --- a/mopidy_autoplay/ext.conf +++ b/mopidy_autoplay/ext.conf @@ -21,3 +21,8 @@ playback.time_position = auto # Mixer (volume = [0..100]; mute = on|off|true|false) mixer.volume = auto mixer.mute = auto + +# Auto safe on events. See https://docs.mopidy.com/en/latest/api/core/#core-events for possible events +# autosave_events = track_playback_started +autosave_events = +autosave_min_interval = 60 diff --git a/mopidy_autoplay/frontend.py b/mopidy_autoplay/frontend.py index fdf2e2c..80714e2 100755 --- a/mopidy_autoplay/frontend.py +++ b/mopidy_autoplay/frontend.py @@ -9,7 +9,7 @@ import glob import json import pykka - +import time from mopidy import core from . import Extension, Recollection @@ -36,7 +36,7 @@ def __init__(self, config, core): logger.debug( "Use '%s' as statefile.", self.statefile) - + self._last_autosave = time.time() # The frontend implementation def on_start(self): # noqa: D401 @@ -44,6 +44,7 @@ def on_start(self): # noqa: D401 logger.debug("on_start()") state = self.read_state(self.statefile) + if state: self.restore_state(state) @@ -54,8 +55,18 @@ def on_stop(self): # noqa: D401 state = self.store_state() self.write_state(state, self.statefile) - # Helper functions - + def on_event(self, event, **kwargs): + logger.debug("Event %s args: %s",event,str(kwargs)) + if event in self.config['autosave_events']: + now = time.time() + if (now - self._last_autosave) < self.config['autosave_min_interval']: + logger.info("Skip autosave") + else: + logger.info("Autosave ({0})".format(event)) + state = self.store_state() + self.write_state(state, self.statefile) + self._last_autosave = now + def _get_config(self, state, controller, option): """ Return the configuration from `config` and `state`.