Add caching of OsaMixer volume

If volume is just managed through Mopidy it is always correct. If another
application changes the volume, Mopidy will be correct within 30 seconds.
This commit is contained in:
Stein Magnus Jodal 2010-03-09 11:45:42 +01:00
parent 38399c844b
commit 96365d3467

View File

@ -1,15 +1,32 @@
from subprocess import Popen, PIPE
import time
from mopidy.mixers import BaseMixer
CACHE_TTL = 30
class OsaMixer(BaseMixer):
_cache = None
_last_update = None
def _valid_cache(self):
return (self._cache is not None
and self._last_update is not None
and (int(time.time() - self._last_update) < CACHE_TTL))
def _get_volume(self):
try:
return int(Popen(
['osascript', '-e', 'output volume of (get volume settings)'],
stdout=PIPE).communicate()[0])
except ValueError:
return None
if not self._valid_cache():
try:
self._cache = int(Popen(
['osascript', '-e',
'output volume of (get volume settings)'],
stdout=PIPE).communicate()[0])
except ValueError:
self._cache = None
self._last_update = int(time.time())
return self._cache
def _set_volume(self, volume):
Popen(['osascript', '-e', 'set volume output volume %d' % volume])
self._cache = volume
self._last_update = int(time.time())